ArmNN
 20.02
Tensor.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "TensorFwd.hpp"
8 
9 #include "Exceptions.hpp"
10 #include "Optional.hpp"
11 #include "Types.hpp"
12 
13 #include <array>
14 #include <initializer_list>
15 #include <vector>
16 
17 namespace armnn
18 {
19 
21 {
22 public:
23  /// Empty (invalid) constructor.
24  TensorShape();
25 
26  TensorShape(unsigned int numDimensions);
27 
28  TensorShape(unsigned int numDimensions, const unsigned int* dimensionSizes);
29 
30  TensorShape(std::initializer_list<unsigned int> dimensionSizeList);
31 
32  TensorShape(const TensorShape& other);
33 
34  TensorShape& operator=(const TensorShape& other);
35 
36  unsigned int operator[](unsigned int i) const;
37 
38  unsigned int& operator[](unsigned int i);
39 
40  bool operator==(const TensorShape& other) const;
41  bool operator!=(const TensorShape& other) const;
42 
43  unsigned int GetNumDimensions() const { return m_NumDimensions; }
44  unsigned int GetNumElements() const;
45 
46 private:
47  std::array<unsigned int, MaxNumOfTensorDimensions> m_Dimensions;
48  unsigned int m_NumDimensions;
49 
50  void CheckDimensionIndex(unsigned int i) const;
51 };
52 
54 {
55 public:
56  /// Empty (invalid) constructor.
57  TensorInfo();
58 
59  TensorInfo(const TensorShape& shape,
60  DataType dataType,
61  float quantizationScale = 0.0f,
62  int32_t quantizationOffset = 0);
63 
64  TensorInfo(unsigned int numDimensions,
65  const unsigned int* dimensionSizes,
66  DataType dataType,
67  float quantizationScale = 0.0f,
68  int32_t quantizationOffset = 0);
69 
70  TensorInfo(const TensorShape& shape,
71  DataType dataType,
72  const std::vector<float>& quantizationScales,
73  unsigned int quantizationDim);
74 
75  TensorInfo(unsigned int numDimensions,
76  const unsigned int* dimensionSizes,
77  DataType dataType,
78  const std::vector<float>& quantizationScales,
79  unsigned int quantizationDim);
80 
81  TensorInfo(const TensorInfo& other);
82 
83  TensorInfo& operator=(const TensorInfo& other);
84 
85  bool operator==(const TensorInfo& other) const;
86  bool operator!=(const TensorInfo& other) const;
87 
88  const TensorShape& GetShape() const { return m_Shape; }
89  TensorShape& GetShape() { return m_Shape; }
90  void SetShape(const TensorShape& newShape) { m_Shape = newShape; }
91 
92  unsigned int GetNumDimensions() const { return m_Shape.GetNumDimensions(); }
93  unsigned int GetNumElements() const { return m_Shape.GetNumElements(); }
94 
95  DataType GetDataType() const { return m_DataType; }
96  void SetDataType(DataType type) { m_DataType = type; }
97 
98  bool HasMultipleQuantizationScales() const { return m_Quantization.m_Scales.size() > 1; }
99 
100  bool HasPerAxisQuantization() const;
101 
102  std::vector<float> GetQuantizationScales() const;
103  void SetQuantizationScales(const std::vector<float>& scales);
104 
105  float GetQuantizationScale() const;
106  void SetQuantizationScale(float scale);
107 
108  int32_t GetQuantizationOffset() const;
109  void SetQuantizationOffset(int32_t offset);
110 
111  Optional<unsigned int> GetQuantizationDim() const;
112  void SetQuantizationDim(const Optional<unsigned int>& quantizationDim);
113 
114  bool IsQuantized() const;
115 
116  /// Check that the types are the same and, if quantize, that the quantization parameters are the same.
117  bool IsTypeSpaceMatch(const TensorInfo& other) const;
118 
119  unsigned int GetNumBytes() const;
120 
121 private:
122  TensorShape m_Shape;
123  DataType m_DataType;
124 
125  /// Vectors of scale and offset are used for per-axis quantization.
126  struct Quantization
127  {
128  Quantization()
129  : m_Scales{}
130  , m_Offset(EmptyOptional())
131  , m_QuantizationDim(EmptyOptional()) {}
132 
133  bool operator==(const Quantization& other) const
134  {
135  return ((m_Scales == other.m_Scales) && (m_Offset == other.m_Offset) &&
136  (m_QuantizationDim == other.m_QuantizationDim));
137  }
138 
139  std::vector<float> m_Scales;
140  Optional<int32_t> m_Offset;
141  Optional<unsigned int> m_QuantizationDim;
142 
143  } m_Quantization;
144 };
145 
146 using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>;
147 
148 template<typename MemoryType>
150 {
151 public:
152  /// Empty (invalid) constructor.
153  BaseTensor();
154 
155  /// Constructor from a raw memory pointer.
156  /// @param memoryArea - Region of CPU-addressable memory where tensor data will be stored. Must be valid while
157  /// workloads are on the fly. Tensor instances do not claim ownership of referenced memory regions, that is,
158  /// no attempt will be made by ArmNN to free these memory regions automatically.
159  BaseTensor(const TensorInfo& info, MemoryType memoryArea);
160 
161  /// Tensors are copyable.
162  BaseTensor(const BaseTensor& other);
163 
164  /// Tensors are copyable.
166 
167  const TensorInfo& GetInfo() const { return m_Info; }
168  TensorInfo& GetInfo() { return m_Info; }
169  const TensorShape& GetShape() const { return m_Info.GetShape(); }
170  TensorShape& GetShape() { return m_Info.GetShape(); }
171 
172  DataType GetDataType() const { return m_Info.GetDataType(); }
173  unsigned int GetNumDimensions() const { return m_Info.GetNumDimensions(); }
174  unsigned int GetNumBytes() const { return m_Info.GetNumBytes(); }
175  unsigned int GetNumElements() const { return m_Info.GetNumElements(); }
176 
177  MemoryType GetMemoryArea() const { return m_MemoryArea; }
178 
179 protected:
180  /// Protected destructor to stop users from making these
181  /// (could still new one on the heap and then leak it...)
183 
184  MemoryType m_MemoryArea;
185 
186 private:
187  TensorInfo m_Info;
188 };
189 
190 /// A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
191 class Tensor : public BaseTensor<void*>
192 {
193 public:
194  /// Brings in the constructors and assignment operator.
196 };
197 
198 /// A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
199 class ConstTensor : public BaseTensor<const void*>
200 {
201 public:
202  /// Brings in the constructors and assignment operator.
204  ConstTensor() : BaseTensor<const void*>() {} // This needs to be redefined explicitly??
205 
206  /// Can be implicitly constructed from non-const Tensor.
207  ConstTensor(const Tensor& other) : BaseTensor<const void*>(other.GetInfo(), other.GetMemoryArea()) {}
208 
209  /// Constructor from a backing container.
210  /// @param container - An stl-like container type which implements data() and size() methods.
211  /// Presence of data() and size() is a strong indicator of the continuous memory layout of the container,
212  /// which is a requirement for Tensor data. Tensor instances do not claim ownership of referenced memory regions,
213  /// that is, no attempt will be made by ArmNN to free these memory regions automatically.
214  template < template<typename, typename...> class ContainerType, typename T, typename...ContainerArgs >
215  ConstTensor(const TensorInfo& info, const ContainerType<T, ContainerArgs...>& container)
216  : BaseTensor<const void*>(info, container.data())
217  {
218  if (container.size() * sizeof(T) != info.GetNumBytes())
219  {
220  throw InvalidArgumentException("Container size is not correct");
221  }
222  }
223 };
224 
225 using InputTensors = std::vector<std::pair<LayerBindingId, class ConstTensor>>;
226 using OutputTensors = std::vector<std::pair<LayerBindingId, class Tensor>>;
227 
228 } // namespace armnn
unsigned int GetNumElements() const
Definition: Tensor.cpp:106
bool operator!=(const TensorShape &other) const
Definition: Tensor.cpp:101
unsigned int operator[](unsigned int i) const
Definition: Tensor.cpp:83
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
TensorShape & operator=(const TensorShape &other)
Definition: Tensor.cpp:76
boxEncodingsInfo SetQuantizationOffset(1)
const TensorShape & GetShape() const
Definition: Tensor.hpp:169
unsigned int GetNumBytes() const
Definition: Tensor.cpp:213
unsigned int GetNumElements() const
Definition: Tensor.hpp:175
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:225
MemoryType GetMemoryArea() const
Definition: Tensor.hpp:177
Copyright (c) 2020 ARM Limited.
bool HasMultipleQuantizationScales() const
Definition: Tensor.hpp:98
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:90
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:191
bool operator==(const TensorShape &other) const
Definition: Tensor.cpp:95
boxEncodingsInfo SetQuantizationScale(1.0f)
TensorShape & GetShape()
Definition: Tensor.hpp:89
TensorShape()
Empty (invalid) constructor.
Definition: Tensor.cpp:23
DataType
Definition: Types.hpp:32
DataType GetDataType() const
Definition: Tensor.hpp:95
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:199
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:226
TensorShape & GetShape()
Definition: Tensor.hpp:170
MemoryType m_MemoryArea
Definition: Tensor.hpp:184
TensorInfo & GetInfo()
Definition: Tensor.hpp:168
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:167
void SetDataType(DataType type)
Definition: Tensor.hpp:96
ConstTensor(const TensorInfo &info, const ContainerType< T, ContainerArgs... > &container)
Constructor from a backing container.
Definition: Tensor.hpp:215
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
std::pair< armnn::LayerBindingId, armnn::TensorInfo > BindingPointInfo
Definition: Tensor.hpp:146
ConstTensor(const Tensor &other)
Can be implicitly constructed from non-const Tensor.
Definition: Tensor.hpp:207
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:173
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:43
~BaseTensor()
Protected destructor to stop users from making these (could still new one on the heap and then leak i...
Definition: Tensor.hpp:182
DataType GetDataType() const
Definition: Tensor.hpp:172
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:92
unsigned int GetNumElements() const
Definition: Tensor.hpp:93
unsigned int GetNumBytes() const
Definition: Tensor.hpp:174