|
| 1 | +#include "wf/eval_context.hpp" |
| 2 | + |
| 3 | +#include <ATen/ATen.h> |
| 4 | + |
| 5 | +#include <catch2/catch_test_macros.hpp> |
| 6 | +#include <catch2/matchers/catch_matchers_floating_point.hpp> |
| 7 | + |
| 8 | +#include "wf/tensor_bundle.hpp" |
| 9 | + |
| 10 | +CATCH_TEST_CASE("EvalContext default construction", "[evalcontext]") |
| 11 | +{ |
| 12 | + ams::EvalContext ctx; |
| 13 | + |
| 14 | + // Bundles should start empty |
| 15 | + CATCH_REQUIRE(ctx.Inputs.empty()); |
| 16 | + CATCH_REQUIRE(ctx.Inouts.empty()); |
| 17 | + CATCH_REQUIRE(ctx.Outputs.empty()); |
| 18 | + |
| 19 | + // Optional threshold should be disengaged |
| 20 | + CATCH_REQUIRE_FALSE(ctx.Threshold.has_value()); |
| 21 | + |
| 22 | + // Optional uncertainties should be disengaged |
| 23 | + CATCH_REQUIRE_FALSE(ctx.Uncertainties.has_value()); |
| 24 | + |
| 25 | + // Model and layout pointers should be nullptr |
| 26 | + CATCH_REQUIRE(ctx.Model == nullptr); |
| 27 | + CATCH_REQUIRE(ctx.Layout == nullptr); |
| 28 | + |
| 29 | + // Intermediate tensors should be empty |
| 30 | + CATCH_REQUIRE(ctx.ModelInput.numel() == 0); |
| 31 | + CATCH_REQUIRE(ctx.ModelOutput.numel() == 0); |
| 32 | + |
| 33 | + // No fallback indices yet |
| 34 | + CATCH_REQUIRE(ctx.FallbackIndices.empty()); |
| 35 | +} |
| 36 | + |
| 37 | +CATCH_TEST_CASE("EvalContext parameterized construction", "[evalcontext]") |
| 38 | +{ |
| 39 | + // Prepare bundles |
| 40 | + ams::TensorBundle ins; |
| 41 | + ams::TensorBundle ios; |
| 42 | + ams::TensorBundle outs; |
| 43 | + |
| 44 | + ins.add("a", at::ones({2})); |
| 45 | + ios.add("b", at::zeros({3})); |
| 46 | + outs.add("c", at::full({1}, 42)); |
| 47 | + |
| 48 | + // Dummy pointers |
| 49 | + ams::ml::InferenceModel* modelPtr = nullptr; |
| 50 | + ams::LayoutTransform* layoutPtr = nullptr; |
| 51 | + |
| 52 | + // Construct context with threshold |
| 53 | + ams::EvalContext ctx(std::move(ins), |
| 54 | + std::move(ios), |
| 55 | + std::move(outs), |
| 56 | + modelPtr, |
| 57 | + layoutPtr, |
| 58 | + 0.75f); |
| 59 | + |
| 60 | + // Bundles moved correctly |
| 61 | + CATCH_REQUIRE(ctx.Inputs.size() == 1); |
| 62 | + CATCH_REQUIRE(ctx.Inouts.size() == 1); |
| 63 | + CATCH_REQUIRE(ctx.Outputs.size() == 1); |
| 64 | + |
| 65 | + // Threshold exists and is correct |
| 66 | + CATCH_REQUIRE(ctx.Threshold.has_value()); |
| 67 | + CATCH_REQUIRE_THAT(ctx.Threshold.value(), |
| 68 | + Catch::Matchers::WithinAbs(0.75f, 1e-6)); |
| 69 | + |
| 70 | + // Model + layout pointers preserved |
| 71 | + CATCH_REQUIRE(ctx.Model == modelPtr); |
| 72 | + CATCH_REQUIRE(ctx.Layout == layoutPtr); |
| 73 | + |
| 74 | + // Intermediate tensors must still be empty |
| 75 | + CATCH_REQUIRE(ctx.ModelInput.numel() == 0); |
| 76 | + CATCH_REQUIRE(ctx.ModelOutput.numel() == 0); |
| 77 | + |
| 78 | + // Uncertainties should be disengaged on construction |
| 79 | + CATCH_REQUIRE_FALSE(ctx.Uncertainties.has_value()); |
| 80 | + |
| 81 | + // No fallback indices yet |
| 82 | + CATCH_REQUIRE(ctx.FallbackIndices.empty()); |
| 83 | +} |
| 84 | + |
| 85 | +CATCH_TEST_CASE("EvalContext optional uncertainties usage", "[evalcontext]") |
| 86 | +{ |
| 87 | + ams::EvalContext ctx; |
| 88 | + |
| 89 | + // Initially no uncertainties |
| 90 | + CATCH_REQUIRE_FALSE(ctx.Uncertainties.has_value()); |
| 91 | + |
| 92 | + // Assign uncertainties tensor |
| 93 | + ctx.Uncertainties = at::full({4}, 0.123f); |
| 94 | + |
| 95 | + CATCH_REQUIRE(ctx.Uncertainties.has_value()); |
| 96 | + CATCH_REQUIRE(ctx.Uncertainties->sizes() == at::IntArrayRef({4})); |
| 97 | + CATCH_REQUIRE( |
| 98 | + at::allclose(*ctx.Uncertainties, at::full({4}, 0.123f), 1e-6, 1e-6)); |
| 99 | +} |
0 commit comments