// // Copyright © 2023-2024 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // #pragma once #include #include #include #include namespace { armnn::INetworkPtr CreateQuantizationNetwork(const armnn::TensorInfo& inputInfo, const armnn::TensorInfo& outputInfo) { using namespace armnn; INetworkPtr network(INetwork::Create()); IConnectableLayer *input= network->AddInputLayer(0, "input"); IConnectableLayer *quantization = network->AddQuantizeLayer("quantization"); IConnectableLayer *output = network->AddOutputLayer(0, "output"); Connect(input, quantization, inputInfo, 0, 0); Connect(quantization, output, outputInfo, 0, 0); return network; } template, typename Tout = armnn::ResolveType> void QuantizeEndToEndLayerTestImpl(const std::vector& backends, const armnn::TensorShape& tensorShape, const std::vector& input, const std::vector& expectedOutput, float scale, int32_t offset) { using namespace armnn; TensorInfo inputInfo(tensorShape, ArmnnIType); TensorInfo outputInfo(tensorShape, ArmnnOType, scale, offset); inputInfo.SetConstant(true); // Builds up the structure of the network INetworkPtr net = CreateQuantizationNetwork(inputInfo, outputInfo); CHECK(net); const std::map> inputTensorData = { { 0, input } }; const std::map> expectedOutputData = { { 0, expectedOutput } }; EndToEndLayerTestImpl(std::move(net), inputTensorData, expectedOutputData, backends); } template> void QuantizationEndToEndFloat32(const std::vector& backends) { using namespace armnn; const TensorShape tensorShape({ 1, 1, 1, 5 }); std::vector inputData = { 63.5f, 49.5f, 14.0f, 0.0f, 50.0f }; float qScale = 0.5f; int32_t qOffset = 127; std::vector expectedOutputData = armnnUtils::QuantizedVector(inputData, qScale, qOffset); QuantizeEndToEndLayerTestImpl(backends, tensorShape, inputData, expectedOutputData, qScale, qOffset); }; template> void QuantizationEndToEndFloat16(const std::vector& backends) { using namespace armnn; using namespace half_float::literal; using Half = half_float::half; const TensorShape tensorShape({ 1, 1, 1, 5 }); std::vector floatInputData = { 63.f, 49.f, 14.f, 0.f, 50.f }; std::vector inputData = { 63._h, 49._h, 14._h, 0._h, 50._h }; float qScale = 0.25f; int32_t qOffset = 1; std::vector expectedOutputData = armnnUtils::QuantizedVector(floatInputData, qScale, qOffset); QuantizeEndToEndLayerTestImpl(backends, tensorShape, inputData, expectedOutputData, qScale, qOffset); }; inline void QuantizationEndToEndInt8(const std::vector& backends) { using namespace armnn; const TensorShape tensorShape({ 1, 1, 1, 5 }); std::vector inputData = { 113, 16, 13, 101, 13 }; std::vector expectedOutputData = { 127, 45, 41, 127, 41 }; float qScale = 0.75f; int32_t qOffset = 24; QuantizeEndToEndLayerTestImpl(backends, tensorShape, inputData, expectedOutputData, qScale, qOffset); }; }