-
Notifications
You must be signed in to change notification settings - Fork 9
Add eval context #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add eval context #173
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #pragma once | ||
|
|
||
| #include <ATen/ATen.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <vector> | ||
|
|
||
| #include "wf/tensor_bundle.hpp" | ||
|
|
||
| namespace ams | ||
| { | ||
|
|
||
| class InferenceModel; // forward declaration | ||
| class LayoutTransform; // forward declaration | ||
|
|
||
| /// EvalContext is the shared state for all Actions executed during | ||
| /// an AMS evaluation pipeline. It contains user-provided tensors, | ||
| /// model references, layout handlers, and intermediate storage. | ||
| /// | ||
| /// This structure intentionally contains no behavior. All semantics | ||
| /// are implemented by Actions operating on EvalContext. | ||
| struct EvalContext { | ||
|
|
||
| // ------------------------------------------------------------------ | ||
| // User-provided data | ||
| // ------------------------------------------------------------------ | ||
| TensorBundle Inputs; ///< Pure inputs (not modified) | ||
| TensorBundle Inouts; ///< Tensors modified in-place by evaluation | ||
| TensorBundle Outputs; ///< Pure outputs written by the model or fallback | ||
|
|
||
| // ------------------------------------------------------------------ | ||
| // Model and control configuration | ||
| // ------------------------------------------------------------------ | ||
| const InferenceModel* Model = nullptr; ///< Surrogate model, may be null | ||
koparasy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| LayoutTransform* Layout = nullptr; ///< Layout transform handler | ||
| std::optional<float> Threshold; ///< Uncertainty threshold (if used) | ||
|
|
||
| // ------------------------------------------------------------------ | ||
| // Intermediate tensors | ||
| // ------------------------------------------------------------------ | ||
| at::Tensor ModelInput; ///< Model-side input tensor | ||
| at::Tensor ModelOutput; ///< Model-side output tensor | ||
| std::optional<at::Tensor> | ||
| Uncertainties; ///< Uncertainty predictions if the model produces them | ||
|
|
||
| // ------------------------------------------------------------------ | ||
| // Fallback control and indices | ||
| // ------------------------------------------------------------------ | ||
| std::vector<int64_t> FallbackIndices; ///< Samples requiring fallback | ||
|
|
||
| // ------------------------------------------------------------------ | ||
| // Constructors | ||
| // ------------------------------------------------------------------ | ||
| EvalContext() = default; | ||
|
|
||
| EvalContext(TensorBundle inputs, | ||
| TensorBundle inouts, | ||
| TensorBundle outputs, | ||
| const InferenceModel* model, | ||
koparasy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| LayoutTransform* layout, | ||
| std::optional<float> threshold) | ||
| : Inputs(std::move(inputs)), | ||
| Inouts(std::move(inouts)), | ||
| Outputs(std::move(outputs)), | ||
| Model(model), | ||
| Layout(layout), | ||
| Threshold(std::move(threshold)) | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| } // namespace ams | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| #include "wf/eval_context.hpp" | ||
|
|
||
| #include <ATen/ATen.h> | ||
|
|
||
| #include <catch2/catch_test_macros.hpp> | ||
| #include <catch2/matchers/catch_matchers_floating_point.hpp> | ||
|
|
||
| #include "wf/tensor_bundle.hpp" | ||
|
|
||
| CATCH_TEST_CASE("EvalContext default construction", "[evalcontext]") | ||
| { | ||
| ams::EvalContext ctx; | ||
|
|
||
| // Bundles should start empty | ||
| CATCH_REQUIRE(ctx.Inputs.empty()); | ||
| CATCH_REQUIRE(ctx.Inouts.empty()); | ||
| CATCH_REQUIRE(ctx.Outputs.empty()); | ||
|
|
||
| // Optional threshold should be disengaged | ||
| CATCH_REQUIRE_FALSE(ctx.Threshold.has_value()); | ||
|
|
||
| // Optional uncertainties should be disengaged | ||
| CATCH_REQUIRE_FALSE(ctx.Uncertainties.has_value()); | ||
|
|
||
| // Model and layout pointers should be nullptr | ||
| CATCH_REQUIRE(ctx.Model == nullptr); | ||
| CATCH_REQUIRE(ctx.Layout == nullptr); | ||
|
|
||
| // Intermediate tensors should be empty | ||
| CATCH_REQUIRE(ctx.ModelInput.numel() == 0); | ||
| CATCH_REQUIRE(ctx.ModelOutput.numel() == 0); | ||
|
|
||
| // No fallback indices yet | ||
| CATCH_REQUIRE(ctx.FallbackIndices.empty()); | ||
| } | ||
|
|
||
| CATCH_TEST_CASE("EvalContext parameterized construction", "[evalcontext]") | ||
| { | ||
| // Prepare bundles | ||
| ams::TensorBundle ins; | ||
| ams::TensorBundle ios; | ||
| ams::TensorBundle outs; | ||
|
|
||
| ins.add("a", at::ones({2})); | ||
| ios.add("b", at::zeros({3})); | ||
| outs.add("c", at::full({1}, 42)); | ||
|
|
||
| // Dummy pointers | ||
| ams::InferenceModel* modelPtr = nullptr; | ||
koparasy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ams::LayoutTransform* layoutPtr = nullptr; | ||
|
|
||
| // Construct context with threshold | ||
| ams::EvalContext ctx(std::move(ins), | ||
| std::move(ios), | ||
| std::move(outs), | ||
| modelPtr, | ||
| layoutPtr, | ||
| 0.75f); | ||
|
|
||
| // Bundles moved correctly | ||
| CATCH_REQUIRE(ctx.Inputs.size() == 1); | ||
| CATCH_REQUIRE(ctx.Inouts.size() == 1); | ||
| CATCH_REQUIRE(ctx.Outputs.size() == 1); | ||
|
|
||
| // Threshold exists and is correct | ||
| CATCH_REQUIRE(ctx.Threshold.has_value()); | ||
| CATCH_REQUIRE_THAT(ctx.Threshold.value(), | ||
| Catch::Matchers::WithinAbs(0.75f, 1e-6)); | ||
|
|
||
| // Model + layout pointers preserved | ||
| CATCH_REQUIRE(ctx.Model == modelPtr); | ||
| CATCH_REQUIRE(ctx.Layout == layoutPtr); | ||
|
|
||
| // Intermediate tensors must still be empty | ||
| CATCH_REQUIRE(ctx.ModelInput.numel() == 0); | ||
| CATCH_REQUIRE(ctx.ModelOutput.numel() == 0); | ||
|
|
||
| // Uncertainties should be disengaged on construction | ||
| CATCH_REQUIRE_FALSE(ctx.Uncertainties.has_value()); | ||
|
|
||
| // No fallback indices yet | ||
| CATCH_REQUIRE(ctx.FallbackIndices.empty()); | ||
| } | ||
|
|
||
| CATCH_TEST_CASE("EvalContext optional uncertainties usage", "[evalcontext]") | ||
| { | ||
| ams::EvalContext ctx; | ||
|
|
||
| // Initially no uncertainties | ||
| CATCH_REQUIRE_FALSE(ctx.Uncertainties.has_value()); | ||
|
|
||
| // Assign uncertainties tensor | ||
| ctx.Uncertainties = at::full({4}, 0.123f); | ||
|
|
||
| CATCH_REQUIRE(ctx.Uncertainties.has_value()); | ||
| CATCH_REQUIRE(ctx.Uncertainties->sizes() == at::IntArrayRef({4})); | ||
| CATCH_REQUIRE( | ||
| at::allclose(*ctx.Uncertainties, at::full({4}, 0.123f), 1e-6, 1e-6)); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.