diff options
author | Matteo Martincigh <matteo.martincigh@arm.com> | 2019-02-07 17:51:09 +0000 |
---|---|---|
committer | Matteo Martincigh <matteo.martincigh@arm.com> | 2019-02-08 12:23:05 +0000 |
commit | a8d572dc48f47e66cd7abd6ad9b2d3a0f40ea94b (patch) | |
tree | 5de7809a8fbc19d6d2a940a51a982bd633156945 /src/armnn/NetworkQuantizerUtils.hpp | |
parent | e0a4ad8a8e6ef271883e8029985eeab16d838972 (diff) | |
download | armnn-a8d572dc48f47e66cd7abd6ad9b2d3a0f40ea94b.tar.gz |
IVGCVSW-2607 Implement Input range override mechanism
* Added the OverrideInputRange method to the Quantizer API
* Created OverrideInputRangeVisitor to implement the override mechanism
* Moved the quantizer utility functions to the new NetworkQuantizerUtils files
* Moved the map of quantization ranges out of the StaticRangeVisitor
and into the NetworkQuantizer
* Added unit tests
* Code refactoring and cleanup
Change-Id: I9c1d006c1b6a35fbc04584a832fbe489f8f9276d
Signed-off-by: Matteo Martincigh <matteo.martincigh@arm.com>
Diffstat (limited to 'src/armnn/NetworkQuantizerUtils.hpp')
-rw-r--r-- | src/armnn/NetworkQuantizerUtils.hpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/armnn/NetworkQuantizerUtils.hpp b/src/armnn/NetworkQuantizerUtils.hpp new file mode 100644 index 0000000000..458d21a974 --- /dev/null +++ b/src/armnn/NetworkQuantizerUtils.hpp @@ -0,0 +1,56 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include <armnn/Tensor.hpp> +#include <armnn/TypesUtils.hpp> +#include <armnn/ILayerVisitor.hpp> + +#include <utility> +#include <limits> + +#include <boost/assert.hpp> + +namespace armnn +{ + +std::pair<int, float> ComputeQAsymmParams(int numBits, double min, double max); + +template<typename srcType> +void Quantize(const srcType* src, uint8_t* dst, size_t numElements, float& scale, int& offset) +{ + BOOST_ASSERT(src); + BOOST_ASSERT(dst); + + float min = std::numeric_limits<srcType>::max(); + float max = std::numeric_limits<srcType>::lowest(); + for (size_t i = 0; i < numElements; ++i) + { + min = std::min(min, src[i]); + max = std::max(max, src[i]); + } + + auto qParams = ComputeQAsymmParams(8, min, max); + offset = qParams.first; + scale = qParams.second; + for (size_t i = 0; i < numElements; ++i) + { + dst[i] = armnn::Quantize<uint8_t>(src[i], scale, offset); + } +} + +ConstTensor CreateQuantizedConst(const ConstTensor& tensor, std::vector<uint8_t>& backing); + +template <typename LayerContainer> +void VisitLayers(const LayerContainer& layerContainer, ILayerVisitor& visitor) +{ + for (auto layer : layerContainer) + { + layer->Accept(visitor); + } +} + +} // namespace armnn |