ArmNN
 24.05
ActivationOperator.hpp File Reference
#include <Layer.hpp>
#include <tosa_serialization_handler.h>
#include "TosaOperatorUtils.hpp"
Include dependency graph for ActivationOperator.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

TosaSerializationBasicBlock * ConvertActivationToTosaOperator (const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ActivationDescriptor *activationDescriptor)
 

Function Documentation

◆ ConvertActivationToTosaOperator()

TosaSerializationBasicBlock* ConvertActivationToTosaOperator ( const Layer layer,
const std::vector< const TensorInfo * > &  inputs,
const std::vector< const TensorInfo * > &  outputs,
const ActivationDescriptor activationDescriptor 
)

Definition at line 17 of file ActivationOperator.cpp.

21 {
22  if (inputs.size() != 1)
23  {
24  throw armnn::Exception("ConvertActivationToTosaOperator: 1 input tensors required.");
25  }
26 
27  if (outputs.size() != 1)
28  {
29  throw armnn::Exception("ConvertActivationToTosaOperator: 1 output tensor required.");
30  }
31 
32  std::string inputName = std::string("input_");
33  std::string outputNameAlpha = std::string("intermediate1_") + GetUniqueTosaMappingID();
34  std::string outputNameMul = std::string("intermediate2_") + GetUniqueTosaMappingID();
35  std::string outputName = std::string("output0_");
36  std::string blockName = std::string("Op_ACTIVATION_block_") + GetUniqueTosaMappingID();
37 
38  // If a layer is present then the block will be used for execution, so input and output names need to be determined
39  // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
40  if (layer != nullptr)
41  {
42  inputName = GenerateUniqueInputName(layer->GetInputSlot(0));
43  outputName = GenerateUniqueOutputName(*layer);
44  }
45 
46  std::vector<TosaSerializationTensor*> tensors;
47 
48  // Only add input tensors if connected layer is an input layer.
49  // As intermediate or constant tensors will be created separately.
50  // There also can't be duplicate tensor.
51  std::vector<int32_t> inputShape0;
52  DType inputDType0 = DType::DType_UNKNOWN;
53  if(inputName.find("input_") != std::string::npos)
54  {
55  inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
56  inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
57  tensors.push_back(new TosaSerializationTensor(inputName, inputShape0, inputDType0, {}));
58  }
59 
60  std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
61  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
62  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
63 
64 #if TOSA_COMPAT_VERSION(0, 60, 0)
65  std::string outputNameMAXMIN= std::string("intermediate3_") + GetUniqueTosaMappingID();
66 
67  if (inputDType0 == DType::DType_FP32 ||
68  inputDType0 == DType::DType_FP16)
69  {
70  // const_alpha
71  TosaSerializationOperator* alphaOp = nullptr;
72  TosaSerializationTensor* alphaTensor = nullptr;
73  CreateConstTosaOperator<float>(outputNameAlpha,
74  activationDescriptor->m_A,
75  inputDType0,
76  inputShape0,
77  alphaOp,
78  alphaTensor);
79  tensors.push_back(alphaTensor);
80 
81  // mul
82  int32_t shift = 0;
83  TosaMulAttribute mulAttribute(shift);
84  TosaSerializationOperator* mulOp = new TosaSerializationOperator(Op_MUL,
85  Attribute_MulAttribute,
86  &mulAttribute,
87  {inputName, outputNameAlpha},
88  {outputNameMul});
89  tensors.push_back(new TosaSerializationTensor(outputNameMul, inputShape0, inputDType0, {}));
90 
91  TosaSerializationOperator* op = nullptr;
92  if (activationDescriptor->m_A <= 1.0)
93  {
94  op = new TosaSerializationOperator(Op_MAXIMUM,
95  Attribute_NONE,
96  nullptr,
97  {inputName, outputNameMul},
98  {outputName});
99  }
100  else
101  {
102  op = new TosaSerializationOperator(Op_MINIMUM,
103  Attribute_NONE,
104  nullptr,
105  {inputName, outputNameMul},
106  {outputName});
107 
108  }
109 
110  // operatorInputNames/operatorOutputNames ends up being the same as
111  // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
112  return new TosaSerializationBasicBlock(blockName, // name
113  mainName, // region name
114  {alphaOp, mulOp, op}, // operators
115  tensors, // tensors
116  {inputName}, // inputs
117  {outputName}); // outputs
118  }
119  else
120  {
121  std::string outputNameRescaleAlpha = std::string("intermediate3_") + GetUniqueTosaMappingID();
122  std::string outputNameRescaleIdentity = std::string("intermediate4_") + GetUniqueTosaMappingID();
123  std::string outputNameRescaleMaxMin = std::string("intermediate5_") + GetUniqueTosaMappingID();
124 
125  DType rescale_type = DType::DType_INT32;
126  float alpha = activationDescriptor->m_A;
127  double scale_alpha = inputs[0]->GetQuantizationScale() * alpha / outputs[0]->GetQuantizationScale();
128  double scale_identity = inputs[0]->GetQuantizationScale() / outputs[0]->GetQuantizationScale();
129  int32_t input_zp = inputs[0]->GetQuantizationOffset();
130  int32_t output_zp = outputs[0]->GetQuantizationOffset();
131 
132  // Value op_rescale_alpha_in =
133  // buildRescale(rewriter, op, rescale_type, input, scale_alpha,
134  // input_qtype.getZeroPoint(), 0, true, true);
135  TosaSerializationOperator* rescaleAlphaOp = nullptr;
136  CreateRescaleTosaOperator(inputName,
137  outputNameRescaleAlpha,
138  scale_alpha,
139  input_zp,
140  0,
141  true,
142  true,
143  &rescaleAlphaOp);
144  tensors.push_back(new TosaSerializationTensor(outputNameRescaleAlpha,
145  GetTosaTensorShape(inputs[0]->GetShape()),
146  rescale_type, {}));
147  // Value op_rescale_identity_in =
148  // buildRescale(rewriter, op, rescale_type, input, scale_identity,
149  // input_qtype.getZeroPoint(), 0, true, true);
150  TosaSerializationOperator* rescaleIdentityOp = nullptr;
151  CreateRescaleTosaOperator(inputName,
152  outputNameRescaleIdentity,
153  scale_identity,
154  input_zp,
155  0,
156  true,
157  true,
158  &rescaleIdentityOp);
159  tensors.push_back(new TosaSerializationTensor(outputNameRescaleIdentity,
160  GetTosaTensorShape(inputs[0]->GetShape()),
161  rescale_type, {}));
162  // Value result_int32;
163  // if (alpha <= 1.0) {
164  // auto max_op = CreateOpAndInfer<tosa::MaximumOp>(
165  // rewriter, op->getLoc(), rescale_type, op_rescale_identity_in,
166  // op_rescale_alpha_in);
167  // result_int32 = max_op.getResult();
168  // } else {
169  // auto min_op = CreateOpAndInfer<tosa::MinimumOp>(
170  // rewriter, op->getLoc(), rescale_type, op_rescale_identity_in,
171  // op_rescale_alpha_in);
172  // result_int32 = min_op.getResult();
173  // }
174  TosaSerializationOperator* op = nullptr;
175  if (alpha <= 1.0)
176  {
177  op = new TosaSerializationOperator(Op_MAXIMUM,
178  Attribute_NONE,
179  nullptr,
180  {outputNameRescaleAlpha, outputNameRescaleIdentity},
181  {outputNameRescaleMaxMin});
182  }
183  else
184  {
185  op = new TosaSerializationOperator(Op_MINIMUM,
186  Attribute_NONE,
187  nullptr,
188  {outputNameRescaleAlpha, outputNameRescaleIdentity},
189  {outputNameRescaleMaxMin});
190 
191  }
192  tensors.push_back(new TosaSerializationTensor(outputNameRescaleMaxMin,
193  GetTosaTensorShape(inputs[0]->GetShape()),
194  rescale_type, {}));
195 
196  // Value output = buildRescaleFromInt32(rewriter, op, output_type, result_int32,
197  // 1.0, output_qtype.getZeroPoint());
198  TosaSerializationOperator* rescaleOutputOp = nullptr;
199  CreateFromInt32RescaleTosaOperator(outputNameRescaleMaxMin,
200  outputName,
201  1.0,
202  output_zp,
203  &rescaleOutputOp);
204 
205  // operatorInputNames/operatorOutputNames ends up being the same as
206  // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
207  return new TosaSerializationBasicBlock(blockName, // name
208  mainName, // region name
209  {rescaleAlphaOp, rescaleIdentityOp, op, rescaleOutputOp}, // operators
210  tensors, // tensors
211  {inputName}, // inputs
212  {outputName}); // outputs
213  }
214 #else
215  std::string outputNameZero = std::string("intermediate3_") + GetUniqueTosaMappingID();
216  std::string outputNameGE = std::string("intermediate4_") + GetUniqueTosaMappingID();
217 
218  // const_zero
219  TosaSerializationOperator* zeroOp = nullptr;
220  TosaSerializationTensor* zeroTensor = nullptr;
221  CreateConstTosaOperator<float>(outputNameZero,
222  0.0f,
223  inputDType0,
224  inputShape0,
225  zeroOp,
226  zeroTensor);
227  tensors.push_back(zeroTensor);
228 
229  // const_alpha
230  TosaSerializationOperator* alphaOp = nullptr;
231  TosaSerializationTensor* alphaTensor = nullptr;
232  CreateConstTosaOperator<float>(outputNameAlpha,
233  activationDescriptor->m_A,
234  inputDType0,
235  inputShape0,
236  alphaOp,
237  alphaTensor);
238  tensors.push_back(alphaTensor);
239 
240  // mul
241  int32_t shift = 0;
242  TosaMulAttribute mulAttribute(shift);
243  TosaSerializationOperator* mulOp = new TosaSerializationOperator(Op_MUL,
244  Attribute_MulAttribute,
245  &mulAttribute,
246  {inputName, outputNameAlpha},
247  {outputNameMul});
248  tensors.push_back(new TosaSerializationTensor(outputNameMul, inputShape0, inputDType0, {}));
249 
250  // greater_equal
251  TosaSerializationOperator* geOp = new TosaSerializationOperator(Op_GREATER_EQUAL,
252  Attribute_NONE,
253  nullptr,
254  {inputName, outputNameZero},
255  {outputNameGE});
256  tensors.push_back(new TosaSerializationTensor(outputNameGE, outputShape0, DType::DType_BOOL, {}));
257 
258  // select
259  TosaSerializationOperator* selOp = new TosaSerializationOperator(Op_SELECT,
260  Attribute_NONE,
261  nullptr,
262  {outputNameGE, inputName, outputNameMul},
263  {outputName});
264 
265  // operatorInputNames/operatorOutputNames ends up being the same as
266  // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
267  return new TosaSerializationBasicBlock(blockName, // name
268  mainName, // region name
269  {zeroOp, alphaOp, mulOp, geOp, selOp}, // operators
270  tensors, // tensors
271  {inputName}, // inputs
272  {outputName}); // outputs
273 #endif
274 }

References GenerateUniqueInputName(), GenerateUniqueOutputName(), Layer::GetInputSlot(), GetTosaTensorShape(), and GetUniqueTosaMappingID().

Referenced by GetTosaMapping().

armnn::ActivationDescriptor::m_A
float m_A
Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH,...
Definition: Descriptors.hpp:61
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
Definition: TosaOperatorUtils.hpp:120
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
mainName
const std::string mainName
Definition: TosaOperatorUtils.hpp:19
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
armnn::Exception
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:79
CreateFromInt32RescaleTosaOperator
void CreateFromInt32RescaleTosaOperator(const std::string &inputName, const std::string &outputName, double output_scale, int32_t output_zp, TosaSerializationOperator **op)
Definition: TosaRescaleOperatorUtils.hpp:215
CreateRescaleTosaOperator
void CreateRescaleTosaOperator(const std::string &inputName, const std::string &outputName, const std::vector< int32_t > &multipliers, const std::vector< int32_t > &shifts, int32_t input_zp, int32_t output_zp, bool double_round, bool scale32, bool per_channel, TosaSerializationOperator **op)
Definition: TosaRescaleOperatorUtils.hpp:10
GenerateUniqueInputName
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
Definition: TosaOperatorUtils.hpp:109
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138