Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f6235f8a authored by Xusong Wang's avatar Xusong Wang
Browse files

Fix logics for floating-point comparision in VTS test.

Set the acceptable error range based on both absolute tolerance and
relative tolerance.

Currently, absolute tolerance is set to 1e-5 for FP32 and 5 epsilon
(~5e-3) for FP16 relaxed computation. The relative tolerance is set to
5ULP of the corresponding precision. Add a TODO mark for potential
future adjustment on error limit based on testing.

Bug: 111768023

Test: none
Change-Id: Idedcec3e09fd7de9696811b93c81d0f180e896ef
parent 5bc97998
Loading
Loading
Loading
Loading
+12 −7
Original line number Diff line number Diff line
@@ -66,8 +66,8 @@ void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* sr
// Top level driver for models and examples generated by test_generator.py
// Test driver for those generated from ml/nn/runtime/test/spec
void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
                           const std::vector<MixedTypedExampleType>& examples,
                           float fpRange = 1e-5f) {
                           const std::vector<MixedTypedExampleType>& examples, float fpAtol = 1e-5f,
                           float fpRtol = 1e-5f) {
    const uint32_t INPUT = 0;
    const uint32_t OUTPUT = 1;

@@ -175,7 +175,7 @@ void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool
        MixedTyped filtered_test = filter(test, is_ignored);

        // We want "close-enough" results for float
        compare(filtered_golden, filtered_test, fpRange);
        compare(filtered_golden, filtered_test, fpAtol, fpRtol);
    }
}

@@ -220,7 +220,8 @@ void Execute(const sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> c
    EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
    ASSERT_NE(nullptr, preparedModel.get());

    EvaluatePreparedModel(preparedModel, is_ignored, examples);
    float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f;
    EvaluatePreparedModel(preparedModel, is_ignored, examples, fpAtol, fpRtol);
}

void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model,
@@ -265,9 +266,13 @@ void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> c
    EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
    ASSERT_NE(nullptr, preparedModel.get());

    // If in relaxed mode, set the error range to be 5ULP of FP16.
    float fpRange = !model.relaxComputationFloat32toFloat16 ? 1e-5f : 5.0f * 0.0009765625f;
    EvaluatePreparedModel(preparedModel, is_ignored, examples, fpRange);
    // TODO: Adjust the error limit based on testing.
    // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16.
    float fpAtol = !model.relaxComputationFloat32toFloat16 ? 1e-5f : 5.0f * 0.0009765625f;
    // Set the relative tolerance to be 5ULP of the corresponding FP precision.
    float fpRtol = !model.relaxComputationFloat32toFloat16 ? 5.0f * 1.1920928955078125e-7f
                                                           : 5.0f * 0.0009765625f;
    EvaluatePreparedModel(preparedModel, is_ignored, examples, fpAtol, fpRtol);
}

}  // namespace generated_tests