Matteo Martincigh | a8d572d | 2019-02-07 17:51:09 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Nattapat Chaimanowong | 7ac07f3 | 2019-03-20 11:51:14 +0000 | [diff] [blame^] | 8 | #include "NetworkQuantizationScheme.hpp" |
| 9 | |
Matteo Martincigh | a8d572d | 2019-02-07 17:51:09 +0000 | [diff] [blame] | 10 | #include <armnn/Tensor.hpp> |
| 11 | #include <armnn/TypesUtils.hpp> |
| 12 | #include <armnn/ILayerVisitor.hpp> |
| 13 | |
| 14 | #include <utility> |
| 15 | #include <limits> |
| 16 | |
| 17 | #include <boost/assert.hpp> |
| 18 | |
| 19 | namespace armnn |
| 20 | { |
| 21 | |
Matteo Martincigh | a8d572d | 2019-02-07 17:51:09 +0000 | [diff] [blame] | 22 | template<typename srcType> |
Nattapat Chaimanowong | 7ac07f3 | 2019-03-20 11:51:14 +0000 | [diff] [blame^] | 23 | void QuantizeConstant(const srcType* src, uint8_t* dst, size_t numElements, float& scale, int& offset) |
Matteo Martincigh | a8d572d | 2019-02-07 17:51:09 +0000 | [diff] [blame] | 24 | { |
| 25 | BOOST_ASSERT(src); |
| 26 | BOOST_ASSERT(dst); |
| 27 | |
| 28 | float min = std::numeric_limits<srcType>::max(); |
| 29 | float max = std::numeric_limits<srcType>::lowest(); |
| 30 | for (size_t i = 0; i < numElements; ++i) |
| 31 | { |
| 32 | min = std::min(min, src[i]); |
| 33 | max = std::max(max, src[i]); |
| 34 | } |
| 35 | |
Nattapat Chaimanowong | 7ac07f3 | 2019-03-20 11:51:14 +0000 | [diff] [blame^] | 36 | QAsymm8QuantizationScheme quantizationScheme; |
| 37 | OffsetScalePair qParams = quantizationScheme.ComputeScheme(min, max); |
Ferran Balaguer | b284565 | 2019-02-27 09:42:06 +0000 | [diff] [blame] | 38 | scale = qParams.first; |
| 39 | offset = qParams.second; |
Nattapat Chaimanowong | 7ac07f3 | 2019-03-20 11:51:14 +0000 | [diff] [blame^] | 40 | |
Matteo Martincigh | a8d572d | 2019-02-07 17:51:09 +0000 | [diff] [blame] | 41 | for (size_t i = 0; i < numElements; ++i) |
| 42 | { |
| 43 | dst[i] = armnn::Quantize<uint8_t>(src[i], scale, offset); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | ConstTensor CreateQuantizedConst(const ConstTensor& tensor, std::vector<uint8_t>& backing); |
| 48 | |
| 49 | template <typename LayerContainer> |
| 50 | void VisitLayers(const LayerContainer& layerContainer, ILayerVisitor& visitor) |
| 51 | { |
| 52 | for (auto layer : layerContainer) |
| 53 | { |
| 54 | layer->Accept(visitor); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | } // namespace armnn |