summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreeHugger Robot <treehugger-gerrit@google.com>2017-10-10 02:42:13 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2017-10-10 02:42:13 +0000
commit69b0aeab425ea1f8dcd050fb933bae2b77ab5b8a (patch)
treedd964c6360591162dbc5eeff92681eebe62097d2
parent32295105dc4dc0a8ec02b45415a054c9c9d89071 (diff)
parentc86cfc7e23d5a1ec0f81dbb049da9256718d56f0 (diff)
downloadml-69b0aeab425ea1f8dcd050fb933bae2b77ab5b8a.tar.gz
Merge "Refactor test harness" into oc-mr1-dev
-rw-r--r--nn/runtime/test/TestGenerated.cpp6
-rw-r--r--nn/tools/test_generator/include/TestHarness.h168
2 files changed, 102 insertions, 72 deletions
diff --git a/nn/runtime/test/TestGenerated.cpp b/nn/runtime/test/TestGenerated.cpp
index 460269904..27dc04a71 100644
--- a/nn/runtime/test/TestGenerated.cpp
+++ b/nn/runtime/test/TestGenerated.cpp
@@ -132,10 +132,8 @@ class Example {
Result r = execution.compute();
ASSERT_EQ(Result::NO_ERROR, r);
// Filter out don't cares
- MixedTyped filtered_golden;
- MixedTyped filtered_test;
- filter(golden, &filtered_golden, is_ignored);
- filter(test, &filtered_test, is_ignored);
+ MixedTyped filtered_golden = filter(golden, is_ignored);
+ MixedTyped filtered_test = filter(test, is_ignored);
// We want "close-enough" results for float
compare(filtered_golden, filtered_test);
}
diff --git a/nn/tools/test_generator/include/TestHarness.h b/nn/tools/test_generator/include/TestHarness.h
index 075ea0610..256a206e9 100644
--- a/nn/tools/test_generator/include/TestHarness.h
+++ b/nn/tools/test_generator/include/TestHarness.h
@@ -14,6 +14,9 @@
* limitations under the License.
*/
+/* Header-only library for various helpers of test harness
+ * See frameworks/ml/nn/runtime/test/TestGenerated.cpp for how this is used.
+ */
#ifndef ANDROID_ML_NN_TOOLS_TEST_GENERATOR_TEST_HARNESS_H
#define ANDROID_ML_NN_TOOLS_TEST_GENERATOR_TEST_HARNESS_H
@@ -32,67 +35,91 @@ typedef std::tuple<Float32Operands, // ANEURALNETWORKS_TENSOR_FLOAT32
Int32Operands, // ANEURALNETWORKS_TENSOR_INT32
Quant8Operands // ANEURALNETWORKS_TENSOR_QUANT8_ASYMM
>
- MixedTyped;
+ MixedTyped;
typedef std::pair<MixedTyped, MixedTyped> MixedTypedExampleType;
-// Helper template - go through a given type of input/output
-#define FOR_EACH_HELPER(constness, ty, index) \
- inline void for_each( \
- constness MixedTyped& idx_and_data, \
- std::function<void(int, constness std::vector<ty> &)> execute) { \
- for (auto &i : std::get<index>(idx_and_data)) { \
- execute(i.first, i.second); \
- } \
+template <typename T>
+struct MixedTypedIndex {};
+
+template <>
+struct MixedTypedIndex<float> {
+ static constexpr size_t index = 0;
+};
+template <>
+struct MixedTypedIndex<int32_t> {
+ static constexpr size_t index = 1;
+};
+template <>
+struct MixedTypedIndex<uint8_t> {
+ static constexpr size_t index = 2;
+};
+
+// Go through all index-value pairs of a given input type
+template <typename T>
+inline void for_each(const MixedTyped& idx_and_data,
+ std::function<void(int, const std::vector<T>&)> execute) {
+ for (auto& i : std::get<MixedTypedIndex<T>::index>(idx_and_data)) {
+ execute(i.first, i.second);
}
+}
-// Make sure the index matches the tuple member types in MixedTyped
-FOR_EACH_HELPER(, float, 0)
-FOR_EACH_HELPER(, int32_t, 1)
-FOR_EACH_HELPER(, uint8_t, 2)
+// non-const variant of for_each
+template <typename T>
+inline void for_each(MixedTyped& idx_and_data,
+ std::function<void(int, std::vector<T>&)> execute) {
+ for (auto& i : std::get<MixedTypedIndex<T>::index>(idx_and_data)) {
+ execute(i.first, i.second);
+ }
+}
-FOR_EACH_HELPER(const, float, 0)
-FOR_EACH_HELPER(const, int32_t, 1)
-FOR_EACH_HELPER(const, uint8_t, 2)
-#undef FOR_EACH_HELPER
+// internal helper for for_all
+template <typename T>
+inline void for_all_internal(
+ MixedTyped& idx_and_data,
+ std::function<void(int, void*, size_t)> execute_this) {
+ for_each<T>(idx_and_data, [&execute_this](int idx, std::vector<T>& m) {
+ execute_this(idx, static_cast<void*>(m.data()), m.size() * sizeof(T));
+ });
+}
-// Go through all index-value pairs
+// Go through all index-value pairs of all input types
// expects a functor that takes (int index, void *raw data, size_t sz)
inline void for_all(MixedTyped& idx_and_data,
- std::function<void(int, void *, size_t)> execute_this) {
-#define FOR_EACH_TYPE(ty) \
- for_each(idx_and_data, [&execute_this](int idx, std::vector<ty>& m) { \
- execute_this(idx, (void *)m.data(), m.size() * sizeof(ty)); \
+ std::function<void(int, void*, size_t)> execute_this) {
+ for_all_internal<float>(idx_and_data, execute_this);
+ for_all_internal<int32_t>(idx_and_data, execute_this);
+ for_all_internal<uint8_t>(idx_and_data, execute_this);
+}
+
+// Const variant of internal helper for for_all
+template <typename T>
+inline void for_all_internal(
+ const MixedTyped& idx_and_data,
+ std::function<void(int, const void*, size_t)> execute_this) {
+ for_each<T>(idx_and_data, [&execute_this](int idx, const std::vector<T>& m) {
+ execute_this(idx, static_cast<const void*>(m.data()), m.size() * sizeof(T));
});
- FOR_EACH_TYPE(float);
- FOR_EACH_TYPE(int32_t);
- FOR_EACH_TYPE(uint8_t);
-#undef FOR_EACH_TYPE
}
-// Const variants of the helper
-// Helper template - go through all index-value pairs
-// expects a functor that takes (int index, void *raw data, size_t sz)
+// Go through all index-value pairs (const variant)
+// expects a functor that takes (int index, const void *raw data, size_t sz)
inline void for_all(
- const MixedTyped& idx_and_data,
- std::function<void(int, const void *, size_t)> execute_this) {
-#define FOR_EACH_TYPE(ty) \
- for_each( \
- idx_and_data, [&execute_this](int idx, const std::vector<ty>& m) { \
- execute_this(idx, (const void *)m.data(), m.size() * sizeof(ty)); \
- });
- FOR_EACH_TYPE(float);
- FOR_EACH_TYPE(int32_t);
- FOR_EACH_TYPE(uint8_t);
-#undef FOR_EACH_TYPE
+ const MixedTyped& idx_and_data,
+ std::function<void(int, const void*, size_t)> execute_this) {
+ for_all_internal<float>(idx_and_data, execute_this);
+ for_all_internal<int32_t>(idx_and_data, execute_this);
+ for_all_internal<uint8_t>(idx_and_data, execute_this);
}
// Helper template - resize test output per golden
template <typename ty, size_t tuple_index>
void resize_accordingly_(const MixedTyped& golden, MixedTyped& test) {
- for_each(golden, [&test](int index, const std::vector<ty>& m) {
- auto& t = std::get<tuple_index>(test);
- t[index].resize(m.size());
- });
+ std::function<void(int, const std::vector<ty>&)> execute =
+ [&test](int index, const std::vector<ty>& m) {
+ auto& t = std::get<tuple_index>(test);
+ t[index].resize(m.size());
+ };
+ for_each<ty>(golden, execute);
}
inline void resize_accordingly(const MixedTyped& golden, MixedTyped& test) {
@@ -102,20 +129,22 @@ inline void resize_accordingly(const MixedTyped& golden, MixedTyped& test) {
}
template <typename ty, size_t tuple_index>
-void filter_(const MixedTyped& golden, MixedTyped *filtered,
- std::function<bool(int)> is_ignored) {
- for_each(golden,
- [filtered, &is_ignored](int index, const std::vector<ty>& m) {
- auto& g = std::get<tuple_index>(*filtered);
- if (!is_ignored(index)) g[index] = m;
- });
+void filter_internal(const MixedTyped& golden, MixedTyped* filtered,
+ std::function<bool(int)> is_ignored) {
+ for_each<ty>(golden,
+ [filtered, &is_ignored](int index, const std::vector<ty>& m) {
+ auto& g = std::get<tuple_index>(*filtered);
+ if (!is_ignored(index)) g[index] = m;
+ });
}
-inline void filter(const MixedTyped& golden, MixedTyped *filtered,
- std::function<bool(int)> is_ignored) {
- filter_<float, 0>(golden, filtered, is_ignored);
- filter_<int32_t, 1>(golden, filtered, is_ignored);
- filter_<uint8_t, 2>(golden, filtered, is_ignored);
+inline MixedTyped filter(const MixedTyped& golden,
+ std::function<bool(int)> is_ignored) {
+ MixedTyped filtered;
+ filter_internal<float, 0>(golden, &filtered, is_ignored);
+ filter_internal<int32_t, 1>(golden, &filtered, is_ignored);
+ filter_internal<uint8_t, 2>(golden, &filtered, is_ignored);
+ return filtered;
}
// Compare results
@@ -124,18 +153,21 @@ inline void filter(const MixedTyped& golden, MixedTyped *filtered,
#define VALUE_TYPE(x) VECTOR_TYPE(x)::value_type
template <size_t tuple_index>
void compare_(
- const MixedTyped& golden, const MixedTyped& test,
- std::function<void(VALUE_TYPE(tuple_index), VALUE_TYPE(tuple_index))> cmp) {
- for_each(golden, [&test, &cmp](int index,
- const VECTOR_TYPE(tuple_index)& m) {
- const auto& test_operands = std::get<tuple_index>(test);
- const auto& test_ty = test_operands.find(index);
- ASSERT_NE(test_ty, test_operands.end());
- for (unsigned int i = 0; i < m.size(); i++) {
- SCOPED_TRACE(testing::Message() << "When comparing element " << i);
- cmp(m[i], test_ty->second[i]);
- }
- });
+ const MixedTyped& golden, const MixedTyped& test,
+ std::function<void(VALUE_TYPE(tuple_index), VALUE_TYPE(tuple_index))>
+ cmp) {
+ for_each<VALUE_TYPE(tuple_index)>(
+ golden,
+ [&test, &cmp](int index, const VECTOR_TYPE(tuple_index) & m) {
+ const auto& test_operands = std::get<tuple_index>(test);
+ const auto& test_ty = test_operands.find(index);
+ ASSERT_NE(test_ty, test_operands.end());
+ for (unsigned int i = 0; i < m.size(); i++) {
+ SCOPED_TRACE(testing::Message()
+ << "When comparing element " << i);
+ cmp(m[i], test_ty->second[i]);
+ }
+ });
}
#undef VALUE_TYPE
#undef VECTOR_TYPE