ArmNN
 21.02
NetworkQuantizerUtils.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include <algorithm>
9 #include <cmath>
10 #include <stdint.h>
11 
12 namespace armnn
13 {
14 
15 ConstTensor CreateQuantizedConst(const ConstTensor& tensor, std::vector<uint8_t>& backing)
16 {
17  float scale = 0.0f;
18  int offset = 0;
19 
20  // Reserve the backing memory
21  backing.resize(tensor.GetInfo().GetNumElements());
22 
23  DataType type = tensor.GetInfo().GetDataType();
24  switch(type)
25  {
26  case DataType::Float32:
27  {
28  QuantizeConstant(static_cast<const float*>(tensor.GetMemoryArea()),
29  backing.data(),
30  backing.size(),
31  scale,
32  offset);
33  }
34  break;
35  default:
36  ARMNN_ASSERT_MSG(false, "Can't quantize unsupported data type");
37  }
38 
39  TensorInfo qInfo(tensor.GetInfo().GetShape(), DataType::QAsymmU8, scale, offset);
40  return ConstTensor(qInfo, backing);
41 }
42 
43 } // namespace armnn
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
MemoryType GetMemoryArea() const
Definition: Tensor.hpp:292
Copyright (c) 2021 ARM Limited and Contributors.
DataType
Definition: Types.hpp:32
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
DataType GetDataType() const
Definition: Tensor.hpp:194
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:314
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:282
void QuantizeConstant(const srcType *src, uint8_t *dst, size_t numElements, float &scale, int &offset)
ConstTensor CreateQuantizedConst(const ConstTensor &tensor, std::vector< uint8_t > &backing)
unsigned int GetNumElements() const
Definition: Tensor.hpp:192