/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * 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 #include #include #include #include #include #include namespace generated_tests { constexpr const size_t gMaximumNumberOfErrorMessages = 10; typedef std::map> Float32Operands; typedef std::map> Int32Operands; typedef std::map> Quant8Operands; typedef std::tuple MixedTyped; typedef std::pair MixedTypedExampleType; template struct MixedTypedIndex {}; template <> struct MixedTypedIndex { static constexpr size_t index = 0; }; template <> struct MixedTypedIndex { static constexpr size_t index = 1; }; template <> struct MixedTypedIndex { static constexpr size_t index = 2; }; // Go through all index-value pairs of a given input type template inline void for_each(const MixedTyped& idx_and_data, std::function&)> execute) { for (auto& i : std::get::index>(idx_and_data)) { execute(i.first, i.second); } } // non-const variant of for_each template inline void for_each(MixedTyped& idx_and_data, std::function&)> execute) { for (auto& i : std::get::index>(idx_and_data)) { execute(i.first, i.second); } } // internal helper for for_all template inline void for_all_internal( MixedTyped& idx_and_data, std::function execute_this) { for_each(idx_and_data, [&execute_this](int idx, std::vector& m) { execute_this(idx, static_cast(m.data()), m.size() * sizeof(T)); }); } // 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 execute_this) { for_all_internal(idx_and_data, execute_this); for_all_internal(idx_and_data, execute_this); for_all_internal(idx_and_data, execute_this); } // Const variant of internal helper for for_all template inline void for_all_internal( const MixedTyped& idx_and_data, std::function execute_this) { for_each(idx_and_data, [&execute_this](int idx, const std::vector& m) { execute_this(idx, static_cast(m.data()), m.size() * sizeof(T)); }); } // 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 execute_this) { for_all_internal(idx_and_data, execute_this); for_all_internal(idx_and_data, execute_this); for_all_internal(idx_and_data, execute_this); } // Helper template - resize test output per golden template void resize_accordingly_(const MixedTyped& golden, MixedTyped& test) { std::function&)> execute = [&test](int index, const std::vector& m) { auto& t = std::get(test); t[index].resize(m.size()); }; for_each(golden, execute); } inline void resize_accordingly(const MixedTyped& golden, MixedTyped& test) { resize_accordingly_(golden, test); resize_accordingly_(golden, test); resize_accordingly_(golden, test); } template void filter_internal(const MixedTyped& golden, MixedTyped* filtered, std::function is_ignored) { for_each(golden, [filtered, &is_ignored](int index, const std::vector& m) { auto& g = std::get(*filtered); if (!is_ignored(index)) g[index] = m; }); } inline MixedTyped filter(const MixedTyped& golden, std::function is_ignored) { MixedTyped filtered; filter_internal(golden, &filtered, is_ignored); filter_internal(golden, &filtered, is_ignored); filter_internal(golden, &filtered, is_ignored); return filtered; } // Compare results #define VECTOR_TYPE(x) \ typename std::tuple_element::type::mapped_type #define VALUE_TYPE(x) VECTOR_TYPE(x)::value_type template void compare_( const MixedTyped& golden, const MixedTyped& test, std::function cmp) { for_each( golden, [&test, &cmp](int index, const VECTOR_TYPE(tuple_index) & m) { const auto& test_operands = std::get(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 inline void compare(const MixedTyped& golden, const MixedTyped& test, float fpRange = 1e-5f) { size_t totalNumberOfErrors = 0; compare_<0>(golden, test, [&totalNumberOfErrors, fpRange](float g, float t) { if (totalNumberOfErrors < gMaximumNumberOfErrorMessages) { EXPECT_NEAR(g, t, fpRange); } if (std::abs(g - t) > fpRange) { totalNumberOfErrors++; } }); compare_<1>(golden, test, [&totalNumberOfErrors](int32_t g, int32_t t) { if (totalNumberOfErrors < gMaximumNumberOfErrorMessages) { EXPECT_EQ(g, t); } if (g != t) { totalNumberOfErrors++; } }); compare_<2>(golden, test, [&totalNumberOfErrors](uint8_t g, uint8_t t) { if (totalNumberOfErrors < gMaximumNumberOfErrorMessages) { EXPECT_NEAR(g, t, 1); } if (std::abs(g - t) > 1) { totalNumberOfErrors++; } }); EXPECT_EQ(size_t{0}, totalNumberOfErrors); } }; // namespace generated_tests #endif // ANDROID_ML_NN_TOOLS_TEST_GENERATOR_TEST_HARNESS_H