summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXusong Wang <xusongw@google.com>2019-06-06 02:57:02 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2019-06-06 02:57:02 +0000
commitd58a358c01801670f5e30590800039c524a8d0ad (patch)
treef0ebb509d17826487a4ff8208a93533cfb609d91
parentc10792320e89b5ebf5f84e88b03cec133b7b3f11 (diff)
parent1b03cee05df3e41b32a506562b7e0ba87612e11a (diff)
downloadml-d58a358c01801670f5e30590800039c524a8d0ad.tar.gz
Merge changes I2350d3b1,I3f64f5b2 into qt-dev
* changes: Use relative bias and MSE on fp values. Use rounding in requantize.
-rw-r--r--nn/common/OperationsUtils.cpp2
-rw-r--r--nn/runtime/test/fuzzing/RandomGraphGenerator.cpp8
-rw-r--r--nn/runtime/test/fuzzing/RandomGraphGenerator.h6
3 files changed, 11 insertions, 5 deletions
diff --git a/nn/common/OperationsUtils.cpp b/nn/common/OperationsUtils.cpp
index 99c11780c..e128afbb7 100644
--- a/nn/common/OperationsUtils.cpp
+++ b/nn/common/OperationsUtils.cpp
@@ -340,7 +340,7 @@ uint8_t requantize(uint8_t value, const Shape& oldShape, const Shape& newShape)
double doubleRet = doubleValue / newShape.scale + newShape.offset;
if (doubleRet < 0) return 0;
if (doubleRet > 255) return 255;
- return static_cast<uint8_t>(doubleRet);
+ return static_cast<uint8_t>(std::round(doubleRet));
}
bool floorPrepare(const Shape& input, Shape* output) {
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