From 53ef79504b4c881c572735393c2eede5fa556c46 Mon Sep 17 00:00:00 2001 From: Jan Eilers Date: Wed, 2 Jun 2021 12:01:25 +0100 Subject: IVGCVSW-5826 Change weights layout for depthwise to [1,H,W,I*M] * This change is necessary because tflite uses a [1,H,W,I*M] format and uses the I*M dimension for per axis quantization. Our previous layout [M,I,H,W] can't handle the correlating quantization scales. * Updates Onnx-, TfLiteParser and TfliteDelegate * Updates the CpuRef, CpuAcc and GpuAcc backends * Adjusts unit tests * Adds test to ensure models with old layout can still be read and executed * Adds conversion function to previous layout [1,H,W,I*M] --> [M,I,H,W] which can be used by backend developers !android-nn-driver:5553 Signed-off-by: Jan Eilers Change-Id: Ifef23368b8c3702cf315a5838d214f7dc13c0152 --- src/backends/reference/test/CMakeLists.txt | 2 + .../reference/test/RefPerAxisIteratorTests.cpp | 252 +++++++++++++++++++++ .../reference/test/RefPerChannelDecoderTests.cpp | 156 +++++++++++++ 3 files changed, 410 insertions(+) create mode 100644 src/backends/reference/test/RefPerAxisIteratorTests.cpp create mode 100644 src/backends/reference/test/RefPerChannelDecoderTests.cpp (limited to 'src/backends/reference/test') diff --git a/src/backends/reference/test/CMakeLists.txt b/src/backends/reference/test/CMakeLists.txt index 76541cfdaa..d7c5da896a 100644 --- a/src/backends/reference/test/CMakeLists.txt +++ b/src/backends/reference/test/CMakeLists.txt @@ -13,6 +13,8 @@ list(APPEND armnnRefBackendUnitTests_sources RefLayerTests.cpp RefMemoryManagerTests.cpp RefOptimizedNetworkTests.cpp + RefPerAxisIteratorTests.cpp + RefPerChannelDecoderTests.cpp RefRuntimeTests.cpp RefTensorHandleTests.cpp RefWorkloadFactoryHelper.hpp diff --git a/src/backends/reference/test/RefPerAxisIteratorTests.cpp b/src/backends/reference/test/RefPerAxisIteratorTests.cpp new file mode 100644 index 0000000000..7da4c0fb0f --- /dev/null +++ b/src/backends/reference/test/RefPerAxisIteratorTests.cpp @@ -0,0 +1,252 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include + +#include + +#include +#include + + +template +void CompareVector(std::vector vec1, std::vector vec2) +{ + BOOST_TEST(vec1.size() == vec2.size()); + + bool mismatch = false; + for (uint i = 0; i < vec1.size(); ++i) + { + if (vec1[i] != vec2[i]) + { + /*std::stringstream ss; + ss << "Vector value mismatch: index=" << i << " " << vec1[i] << "!=" << vec2[i];*/ + BOOST_TEST_MESSAGE(fmt::format("Vector value mismatch: index={} {} != {}", + i, + vec1[i], + vec2[i])); + mismatch = true; + } + } + + if (mismatch) + { + BOOST_FAIL("Error in CompareVector. Vectors don't match."); + } +} + +using namespace armnn; + +// Basically a per axis decoder but without any decoding/quantization +class MockPerAxisIterator : public PerAxisIterator> +{ +public: + MockPerAxisIterator(const int8_t* data, const armnn::TensorShape& tensorShape, const unsigned int axis) + : PerAxisIterator(data, tensorShape, axis), m_NumElements(tensorShape.GetNumElements()) + {} + + int8_t Get() const override + { + return *m_Iterator; + } + + virtual std::vector DecodeTensor(const TensorShape &tensorShape, + bool isDepthwise = false) override + { + IgnoreUnused(tensorShape, isDepthwise); + return std::vector{}; + }; + + // Iterates over data using operator[] and returns vector + std::vector Loop() + { + std::vector vec; + for (uint32_t i = 0; i < m_NumElements; ++i) + { + this->operator[](i); + vec.emplace_back(Get()); + } + return vec; + } + + unsigned int GetAxisIndex() + { + return m_AxisIndex; + } + unsigned int m_NumElements; +}; + +BOOST_AUTO_TEST_SUITE(RefPerAxisIterator) + +// Test Loop (Equivalent to DecodeTensor) and Axis = 0 +BOOST_AUTO_TEST_CASE(PerAxisIteratorTest1) +{ + std::vector input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8); + + // test axis=0 + std::vector expOutput = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 0); + std::vector output = iterator.Loop(); + CompareVector(output, expOutput); + + // Set iterator to index and check if the axis index is correct + iterator[5]; + BOOST_TEST(iterator.GetAxisIndex() == 1u); + + iterator[1]; + BOOST_TEST(iterator.GetAxisIndex() == 0u); + + iterator[10]; + BOOST_TEST(iterator.GetAxisIndex() == 2u); +} + +// Test Axis = 1 +BOOST_AUTO_TEST_CASE(PerAxisIteratorTest2) +{ + std::vector input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8); + + // test axis=1 + std::vector expOutput = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 1); + std::vector output = iterator.Loop(); + CompareVector(output, expOutput); + + // Set iterator to index and check if the axis index is correct + iterator[5]; + BOOST_TEST(iterator.GetAxisIndex() == 0u); + + iterator[1]; + BOOST_TEST(iterator.GetAxisIndex() == 0u); + + iterator[10]; + BOOST_TEST(iterator.GetAxisIndex() == 0u); +} + +// Test Axis = 2 +BOOST_AUTO_TEST_CASE(PerAxisIteratorTest3) +{ + std::vector input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8); + + // test axis=2 + std::vector expOutput = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 2); + std::vector output = iterator.Loop(); + CompareVector(output, expOutput); + + // Set iterator to index and check if the axis index is correct + iterator[5]; + BOOST_TEST(iterator.GetAxisIndex() == 0u); + + iterator[1]; + BOOST_TEST(iterator.GetAxisIndex() == 0u); + + iterator[10]; + BOOST_TEST(iterator.GetAxisIndex() == 1u); +} + +// Test Axis = 3 +BOOST_AUTO_TEST_CASE(PerAxisIteratorTest4) +{ + std::vector input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8); + + // test axis=3 + std::vector expOutput = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 3); + std::vector output = iterator.Loop(); + CompareVector(output, expOutput); + + // Set iterator to index and check if the axis index is correct + iterator[5]; + BOOST_TEST(iterator.GetAxisIndex() == 1u); + + iterator[1]; + BOOST_TEST(iterator.GetAxisIndex() == 1u); + + iterator[10]; + BOOST_TEST(iterator.GetAxisIndex() == 0u); +} + + +// Test Axis = 1. Different tensor shape +BOOST_AUTO_TEST_CASE(PerAxisIteratorTest5) +{ + using namespace armnn; + std::vector input = + { + 0, 1, 2, 3, + 4, 5, 6, 7, + 8, 9, 10, 11, + 12, 13, 14, 15 + }; + + std::vector expOutput = + { + 0, 1, 2, 3, + 4, 5, 6, 7, + 8, 9, 10, 11, + 12, 13, 14, 15 + }; + + TensorInfo tensorInfo ({2,2,2,2},DataType::QSymmS8); + auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 1); + std::vector output = iterator.Loop(); + CompareVector(output, expOutput); + + // Set iterator to index and check if the axis index is correct + iterator[5]; + BOOST_TEST(iterator.GetAxisIndex() == 1u); + + iterator[1]; + BOOST_TEST(iterator.GetAxisIndex() == 0u); + + iterator[10]; + BOOST_TEST(iterator.GetAxisIndex() == 0u); +} + +// Test the increment and decrement operator +BOOST_AUTO_TEST_CASE(PerAxisIteratorTest7) +{ + using namespace armnn; + std::vector input = + { + 0, 1, 2, 3, + 4, 5, 6, 7, + 8, 9, 10, 11 + }; + + std::vector expOutput = + { + 0, 1, 2, 3, + 4, 5, 6, 7, + 8, 9, 10, 11 + }; + + TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8); + auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 2); + + iterator += 3; + BOOST_TEST(iterator.Get(), expOutput[3]); + BOOST_TEST(iterator.GetAxisIndex() == 1u); + + iterator += 3; + BOOST_TEST(iterator.Get(), expOutput[6]); + BOOST_TEST(iterator.GetAxisIndex() == 1u); + + iterator -= 2; + BOOST_TEST(iterator.Get(), expOutput[4]); + BOOST_TEST(iterator.GetAxisIndex() == 0u); + + iterator -= 1; + BOOST_TEST(iterator.Get(), expOutput[3]); + BOOST_TEST(iterator.GetAxisIndex() == 1u); +} + + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/src/backends/reference/test/RefPerChannelDecoderTests.cpp b/src/backends/reference/test/RefPerChannelDecoderTests.cpp new file mode 100644 index 0000000000..c2e3cee7a0 --- /dev/null +++ b/src/backends/reference/test/RefPerChannelDecoderTests.cpp @@ -0,0 +1,156 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include + +#include + +#include + +BOOST_AUTO_TEST_SUITE(RefPerChannelDecoder) + +template +void CompareVector(std::vector vec1, std::vector vec2) +{ + BOOST_TEST(vec1.size() == vec2.size()); + + bool mismatch = false; + for (uint i = 0; i < vec1.size(); ++i) + { + if (vec1[i] != vec2[i]) + { + /*std::stringstream ss; + ss << "Vector value mismatch: index=" << i << " " << vec1[i] << "!=" << vec2[i];*/ + BOOST_TEST_MESSAGE(fmt::format("Vector value mismatch: index={} {} != {}", + i, + vec1[i], + vec2[i])); + mismatch = true; + } + } + + if (mismatch) + { + BOOST_FAIL("Error in CompareVector. Vectors don't match."); + } +} + +// Ensure quantization works for none depthwise convolutions +BOOST_AUTO_TEST_CASE(RefPerChannelDecoderTest1) +{ + using namespace armnn; + std::vector input = + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 + }; + + std::vector expOutput = + { + 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, + 24.0f, 26.0f, 28.0f, 30.0f, 32.0f, 34.0f, 36.0f, 38.0f, 40.0f, 42.0f, 44.0f, 46.0f + }; + + TensorInfo tensorInfo ({2,2,2,3},DataType::QSymmS8,{1.0f, 2.0f},0); + auto decoder = MakeDecoder(tensorInfo, input.data()); + + std::vector output = decoder->DecodeTensor(tensorInfo.GetShape()); + + CompareVector(output, expOutput); +} + +// Ensure quantization works for depthwise convolutions M=1 +BOOST_AUTO_TEST_CASE(RefPerChannelDecoderTest2) +{ + using namespace armnn; + std::vector input = + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 + }; + + std::vector expOutput = + { + 0.0f, 1.0f, 2.0f, 3.0f, + 8.0f, 10.0f, 12.0f, 14.0f, + 24.0f, 27.0f, 30.0f, 33.0f, + 48.0f, 52.0f, 56.0f, 60.0f + }; + + // [O,1,H,W] = [I*M,1,H,W] = [4*1,1,2,2] + TensorInfo tensorInfo ({4,1,2,2},DataType::QSymmS8,{1.0f, 2.0f, 3.0f, 4.0f},0); + auto decoder = MakeDecoder(tensorInfo, input.data()); + + std::vector output = decoder->DecodeTensor(tensorInfo.GetShape(), true); + + CompareVector(output, expOutput); +} + +// Ensure quantization works for depthwise convolutions M=2 +BOOST_AUTO_TEST_CASE(RefPerChannelDecoderTest3) +{ + using namespace armnn; + std::vector input = + { + 0, 1, 2, 3, + 4, 5, 6, 7, + 8, 9, 10, 11, + 12, 13, 14, 15, + 16, 17, 18, 19, + 20, 21, 22, 23 + }; + + std::vector expOutput = + { + 0.0f, 1.0f, 2.0f, 3.0f, + 8.0f, 10.0f, 12.0f, 14.0f, + 24.0f, 27.0f, 30.0f, 33.0f, + 48.0f, 52.0f, 56.0f, 60.0f, + 80.0f, 85.0f, 90.0f, 95.0f, + 120.0f, 126.0f, 132.0f, 138.0f + }; + + // [O,1,H,W] = [I*M,1,H,W] = [3*2,1,2,2] + TensorInfo tensorInfo ({6,1,2,2},DataType::QSymmS8,{1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f},0); + auto decoder = MakeDecoder(tensorInfo, input.data()); + + std::vector output = decoder->DecodeTensor(tensorInfo.GetShape(), true); + + CompareVector(output, expOutput); +} + +// Ensure quantization works for depthwise convolutions M=2 for int32 +BOOST_AUTO_TEST_CASE(RefPerChannelDecoderTest4) +{ + using namespace armnn; + std::vector input = + { + 0, 1, 2, 3, + 4, 5, 6, 7, + 8, 9, 10, 11, + 12, 13, 14, 15, + 16, 17, 18, 19, + 20, 21, 22, 23 + }; + + std::vector expOutput = + { + 0.0f, 1.0f, 2.0f, 3.0f, + 8.0f, 10.0f, 12.0f, 14.0f, + 24.0f, 27.0f, 30.0f, 33.0f, + 48.0f, 52.0f, 56.0f, 60.0f, + 80.0f, 85.0f, 90.0f, 95.0f, + 120.0f, 126.0f, 132.0f, 138.0f + }; + + // [O,1,H,W] = [I*M,1,H,W] = [3*2,1,2,2] + TensorInfo tensorInfo ({6,1,2,2},DataType::Signed32,{1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f},0); + auto decoder = MakeDecoder(tensorInfo, input.data()); + + std::vector output = decoder->DecodeTensor(tensorInfo.GetShape(), true); + + CompareVector(output, expOutput); +} + +BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.1