summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXusong Wang <xusongw@google.com>2019-05-31 10:23:56 -0700
committerXusong Wang <xusongw@google.com>2019-05-31 10:23:56 -0700
commit1b03cee05df3e41b32a506562b7e0ba87612e11a (patch)
tree1c9df13e2ccc511dac08d326c0e205aa1dde24f0
parent7fdc3e2d0c1bed514780c1c69e15db063eb64670 (diff)
downloadml-1b03cee05df3e41b32a506562b7e0ba87612e11a.tar.gz
Use relative bias and MSE on fp values.
Fixes: 134099258 Test: NeuralNetworksTest_static_fuzzing Change-Id: I2350d3b117836f8d6de9a3e816edbad61588107c
-rw-r--r--nn/runtime/test/fuzzing/RandomGraphGenerator.cpp8
-rw-r--r--nn/runtime/test/fuzzing/RandomGraphGenerator.h6
2 files changed, 10 insertions, 4 deletions
diff --git a/nn/runtime/test/fuzzing/RandomGraphGenerator.cpp b/nn/runtime/test/fuzzing/RandomGraphGenerator.cpp
index 18295e49f..2ad183b48 100644
--- a/nn/runtime/test/fuzzing/RandomGraphGenerator.cpp
+++ b/nn/runtime/test/fuzzing/RandomGraphGenerator.cpp
@@ -277,6 +277,7 @@ constexpr uint32_t kMaxNumberOfPrintedErrors = 5;
template <typename T>
void expectNear(const RandomOperand& op, const OperandBuffer& test,
const AccuracyCriterion& criterion) {
+ constexpr uint32_t kMinNumberOfElementsToTestBiasMSE = 10;
const T* actualBuffer = reinterpret_cast<const T*>(test.data());
const T* expectedBuffer = reinterpret_cast<const T*>(op.buffer.data());
uint32_t len = op.getNumberOfElements();
@@ -297,8 +298,11 @@ void expectNear(const RandomOperand& op, const OperandBuffer& test,
continue;
}
- // Accumulate bias and MSE.
+ // Accumulate bias and MSE. Use relative bias and MSE for floating point values.
double diff = actual - expected;
+ if constexpr (NN_IS_FLOAT(T)) {
+ diff /= std::max(1.0, std::abs(expected));
+ }
bias += diff;
mse += diff * diff;
@@ -309,7 +313,7 @@ void expectNear(const RandomOperand& op, const OperandBuffer& test,
EXPECT_EQ(numErrors, 0u);
// Test bias and MSE.
- if (len == numSkip) return;
+ if (len < numSkip + kMinNumberOfElementsToTestBiasMSE) return;
bias /= static_cast<double>(len - numSkip);
mse /= static_cast<double>(len - numSkip);
EXPECT_LE(std::fabs(bias), criterion.bias);
diff --git a/nn/runtime/test/fuzzing/RandomGraphGenerator.h b/nn/runtime/test/fuzzing/RandomGraphGenerator.h
index 5599f0810..a0c41968b 100644
--- a/nn/runtime/test/fuzzing/RandomGraphGenerator.h
+++ b/nn/runtime/test/fuzzing/RandomGraphGenerator.h
@@ -103,11 +103,13 @@ struct RandomOperation {
// TODO: Consider relative bias and mse on floating point data types?
struct AccuracyCriterion {
// We expect the driver results to be unbiased.
- // Formula: abs(sum_{i}(actual - expected)) <= bias
+ // Formula: abs(sum_{i}(diff)) <= bias, where
+ // * fixed point: diff = actual - expected
+ // * floating point: diff = (actual - expected) / max(1, abs(expected))
float bias = std::numeric_limits<float>::max();
// Set the threshold on Mean Square Error (MSE).
- // Formula: sum_{i}((actual - expected) ^ 2) / sum(1) <= mse
+ // Formula: sum_{i}(diff ^ 2) / sum(1) <= mse
float mse = std::numeric_limits<float>::max();
// We also set accuracy thresholds on each element to detect any particular edge cases that may