ArmNN
 22.05
LayerSupportRules.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
9 #include <algorithm>
10 
11 namespace armnn
12 {
13 
15 {
16  if (!weightsType)
17  {
18  return weightsType;
19  }
20 
21  switch(weightsType.value())
22  {
25  return weightsType;
31  default:
32  ARMNN_ASSERT_MSG(false, "GetBiasTypeFromWeightsType(): Unsupported data type.");
33  }
34  return armnn::EmptyOptional();
35 }
36 
37 template<typename F>
38 bool CheckSupportRule(F rule, Optional<std::string&> reasonIfUnsupported, const char* reason)
39 {
40  bool supported = rule();
41  if (!supported && reason)
42  {
43  reasonIfUnsupported.value() += std::string(reason) + "\n"; // Append the reason on a new line
44  }
45  return supported;
46 }
47 
48 struct Rule
49 {
50  bool operator()() const
51  {
52  return m_Res;
53  }
54 
55  bool m_Res = true;
56 };
57 
58 template<typename T>
60 {
61  return true;
62 }
63 
64 template<typename T, typename... Rest>
65 bool AllTypesAreEqualImpl(T t1, T t2, Rest... rest)
66 {
67  static_assert(std::is_same<T, TensorInfo>::value, "Type T must be a TensorInfo");
68 
69  return (t1.GetDataType() == t2.GetDataType()) && AllTypesAreEqualImpl(t2, rest...);
70 }
71 
72 struct TypesAreEqual : public Rule
73 {
74  template<typename ... Ts>
75  TypesAreEqual(const Ts&... ts)
76  {
77  m_Res = AllTypesAreEqualImpl(ts...);
78  }
79 };
80 
82 {
84  {
85  m_Res = info0.GetQuantizationScale() == info1.GetQuantizationScale() &&
87  }
88 };
89 
90 struct TypeAnyOf : public Rule
91 {
92  template<typename Container>
93  TypeAnyOf(const TensorInfo& info, const Container& c)
94  {
95  m_Res = std::any_of(c.begin(), c.end(), [&info](DataType dt)
96  {
97  return dt == info.GetDataType();
98  });
99  }
100 };
101 
102 struct TypeIs : public Rule
103 {
105  {
106  m_Res = dt == info.GetDataType();
107  }
108 };
109 
111 {
113  {
114  m_Res = !info.IsQuantized() || !info.HasPerAxisQuantization();
115  }
116 };
117 
119 {
120  BiasAndWeightsTypesMatch(const TensorInfo& biases, const TensorInfo& weights)
121  {
122  m_Res = biases.GetDataType() == GetBiasTypeFromWeightsType(weights.GetDataType()).value();
123  }
124 };
125 
127 {
128  template<typename Container>
129  BiasAndWeightsTypesCompatible(const TensorInfo& info, const Container& c)
130  {
131  m_Res = std::any_of(c.begin(), c.end(), [&info](DataType dt)
132  {
133  return dt == GetBiasTypeFromWeightsType(info.GetDataType()).value();
134  });
135  }
136 };
137 
138 struct ShapesAreSameRank : public Rule
139 {
140  ShapesAreSameRank(const TensorInfo& info0, const TensorInfo& info1)
141  {
142  m_Res = info0.GetShape().GetNumDimensions() == info1.GetShape().GetNumDimensions();
143  }
144 };
145 
147 {
148  ShapesAreSameTotalSize(const TensorInfo& info0, const TensorInfo& info1)
149  {
150  m_Res = info0.GetNumElements() == info1.GetNumElements();
151  }
152 };
153 
155 {
156  unsigned int CalcInputSize(const TensorShape& in, const TensorShape& out, unsigned int idx)
157  {
158  unsigned int offset = out.GetNumDimensions() - in.GetNumDimensions();
159  unsigned int sizeIn = (idx < offset) ? 1 : in[idx-offset];
160  return sizeIn;
161  }
162 
163  ShapesAreBroadcastCompatible(const TensorInfo& in0, const TensorInfo& in1, const TensorInfo& out)
164  {
165  const TensorShape& shape0 = in0.GetShape();
166  const TensorShape& shape1 = in1.GetShape();
167  const TensorShape& outShape = out.GetShape();
168 
169  for (unsigned int i=0; i < outShape.GetNumDimensions() && m_Res; i++)
170  {
171  unsigned int sizeOut = outShape[i];
172  unsigned int sizeIn0 = CalcInputSize(shape0, outShape, i);
173  unsigned int sizeIn1 = CalcInputSize(shape1, outShape, i);
174 
175  m_Res &= ((sizeIn0 == sizeOut) || (sizeIn0 == 1)) &&
176  ((sizeIn1 == sizeOut) || (sizeIn1 == 1));
177  }
178  }
179 };
180 
182 {
183  TensorNumDimensionsAreCorrect(const TensorInfo& info, unsigned int expectedNumDimensions)
184  {
185  m_Res = info.GetNumDimensions() == expectedNumDimensions;
186  }
187 };
188 
189 } //namespace armnn
TypeNotPerAxisQuantized(const TensorInfo &info)
TypeAnyOf(const TensorInfo &info, const Container &c)
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
TypeIs(const TensorInfo &info, DataType dt)
bool HasPerAxisQuantization() const
Definition: Tensor.cpp:446
QuantizationParametersAreEqual(const TensorInfo &info0, const TensorInfo &info1)
TypesAreEqual(const Ts &... ts)
bool operator()() const
Copyright (c) 2021 ARM Limited and Contributors.
ShapesAreSameTotalSize(const TensorInfo &info0, const TensorInfo &info1)
armnn::Optional< armnn::DataType > GetBiasTypeFromWeightsType(armnn::Optional< armnn::DataType > weightsType)
DataType
Definition: Types.hpp:48
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
ShapesAreBroadcastCompatible(const TensorInfo &in0, const TensorInfo &in1, const TensorInfo &out)
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:478
BiasAndWeightsTypesCompatible(const TensorInfo &info, const Container &c)
float GetQuantizationScale() const
Definition: Tensor.cpp:461
DataType GetDataType() const
Definition: Tensor.hpp:198
BiasAndWeightsTypesMatch(const TensorInfo &biases, const TensorInfo &weights)
ShapesAreSameRank(const TensorInfo &info0, const TensorInfo &info1)
bool AllTypesAreEqualImpl(T)
unsigned int CalcInputSize(const TensorShape &in, const TensorShape &out, unsigned int idx)
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
TensorNumDimensionsAreCorrect(const TensorInfo &info, unsigned int expectedNumDimensions)
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:195
bool IsQuantized() const
Definition: Tensor.cpp:504
bool CheckSupportRule(F rule, Optional< std::string &> reasonIfUnsupported, const char *reason)
unsigned int GetNumElements() const
Definition: Tensor.hpp:196