ArmNN
 24.08
LeakyReluOperator.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 //
6 // Copyright © 2020 The TensorFlow Authors. All Rights Reserved.
7 // SPDX-License-Identifier: Apache-2.0
8 //
9 
10 #include "LeakyReluOperator.hpp"
12 
14 
15 // This function is paraphrased from:
16 // tensorflow/compiler/mlir/tosa/transforms/legalize_tfl.cc from function ConvertTFLLeakyReluOp
17 TosaSerializationBasicBlock* ConvertLeakyReluToTosaOperator(const Layer* layer,
18  const std::vector<const TensorInfo*>& inputs,
19  const std::vector<const TensorInfo*>& outputs,
20  const ActivationDescriptor* activationDescriptor)
21 {
22  if (inputs.size() != 1)
23  {
24  throw armnn::Exception("ConvertLeakyReluToTosaOperator: 1 input tensors required.");
25  }
26 
27  if (outputs.size() != 1)
28  {
29  throw armnn::Exception("ConvertLeakyReluToTosaOperator: 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_LEAKY_RELU_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  std::string outputNameMAXMIN= std::string("intermediate3_") + GetUniqueTosaMappingID();
65 
66  if (inputDType0 == DType::DType_FP32 ||
67  inputDType0 == DType::DType_FP16)
68  {
69  // const_alpha
70  TosaSerializationOperator* alphaOp = nullptr;
71  TosaSerializationTensor* alphaTensor = nullptr;
72  CreateConstTosaOperator<float>(outputNameAlpha,
73  activationDescriptor->m_A,
74  inputDType0,
75  inputShape0,
76  alphaOp,
77  alphaTensor);
78  tensors.push_back(alphaTensor);
79 
80  // mul
81  int32_t shift = 0;
82  TosaMulAttribute mulAttribute(shift);
83  TosaSerializationOperator* mulOp = new TosaSerializationOperator(Op_MUL,
84  Attribute_MulAttribute,
85  &mulAttribute,
86  {inputName, outputNameAlpha},
87  {outputNameMul});
88  tensors.push_back(new TosaSerializationTensor(outputNameMul, inputShape0, inputDType0, {}));
89 
90  TosaSerializationOperator* op = nullptr;
91  if (activationDescriptor->m_A <= 1.0)
92  {
93  op = new TosaSerializationOperator(Op_MAXIMUM,
94  Attribute_NONE,
95  nullptr,
96  {inputName, outputNameMul},
97  {outputName});
98  }
99  else
100  {
101  op = new TosaSerializationOperator(Op_MINIMUM,
102  Attribute_NONE,
103  nullptr,
104  {inputName, outputNameMul},
105  {outputName});
106 
107  }
108 
109  // operatorInputNames/operatorOutputNames ends up being the same as
110  // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
111  return new TosaSerializationBasicBlock(blockName, // name
112  mainName, // region name
113  {alphaOp, mulOp, op}, // operators
114  tensors, // tensors
115  {inputName}, // inputs
116  {outputName}); // outputs
117  }
118  else
119  {
120  std::string outputNameRescaleAlpha = std::string("intermediate3_") + GetUniqueTosaMappingID();
121  std::string outputNameRescaleIdentity = std::string("intermediate4_") + GetUniqueTosaMappingID();
122  std::string outputNameRescaleMaxMin = std::string("intermediate5_") + GetUniqueTosaMappingID();
123 
124  DType rescale_type = DType::DType_INT32;
125  float alpha = activationDescriptor->m_A;
126  double scale_alpha = inputs[0]->GetQuantizationScale() * alpha / outputs[0]->GetQuantizationScale();
127  double scale_identity = inputs[0]->GetQuantizationScale() / outputs[0]->GetQuantizationScale();
128  int32_t input_zp = inputs[0]->GetQuantizationOffset();
129  int32_t output_zp = outputs[0]->GetQuantizationOffset();
130 
131  // Value op_rescale_alpha_in =
132  // buildRescale(rewriter, op, rescale_type, input, scale_alpha,
133  // input_qtype.getZeroPoint(), 0, true, true);
134  TosaSerializationOperator* rescaleAlphaOp = nullptr;
135  CreateRescaleTosaOperator(inputName,
136  outputNameRescaleAlpha,
137  scale_alpha,
138  input_zp,
139  0,
140  true,
141  true,
142  &rescaleAlphaOp);
143  tensors.push_back(new TosaSerializationTensor(outputNameRescaleAlpha,
144  GetTosaTensorShape(inputs[0]->GetShape()),
145  rescale_type, {}));
146  // Value op_rescale_identity_in =
147  // buildRescale(rewriter, op, rescale_type, input, scale_identity,
148  // input_qtype.getZeroPoint(), 0, true, true);
149  TosaSerializationOperator* rescaleIdentityOp = nullptr;
150  CreateRescaleTosaOperator(inputName,
151  outputNameRescaleIdentity,
152  scale_identity,
153  input_zp,
154  0,
155  true,
156  true,
157  &rescaleIdentityOp);
158  tensors.push_back(new TosaSerializationTensor(outputNameRescaleIdentity,
159  GetTosaTensorShape(inputs[0]->GetShape()),
160  rescale_type, {}));
161  // Value result_int32;
162  // if (alpha <= 1.0) {
163  // auto max_op = CreateOpAndInfer<tosa::MaximumOp>(
164  // rewriter, op->getLoc(), rescale_type, op_rescale_identity_in,
165  // op_rescale_alpha_in);
166  // result_int32 = max_op.getResult();
167  // } else {
168  // auto min_op = CreateOpAndInfer<tosa::MinimumOp>(
169  // rewriter, op->getLoc(), rescale_type, op_rescale_identity_in,
170  // op_rescale_alpha_in);
171  // result_int32 = min_op.getResult();
172  // }
173  TosaSerializationOperator* op = nullptr;
174  if (alpha <= 1.0)
175  {
176  op = new TosaSerializationOperator(Op_MAXIMUM,
177  Attribute_NONE,
178  nullptr,
179  {outputNameRescaleAlpha, outputNameRescaleIdentity},
180  {outputNameRescaleMaxMin});
181  }
182  else
183  {
184  op = new TosaSerializationOperator(Op_MINIMUM,
185  Attribute_NONE,
186  nullptr,
187  {outputNameRescaleAlpha, outputNameRescaleIdentity},
188  {outputNameRescaleMaxMin});
189 
190  }
191  tensors.push_back(new TosaSerializationTensor(outputNameRescaleMaxMin,
192  GetTosaTensorShape(inputs[0]->GetShape()),
193  rescale_type, {}));
194 
195  // Value output = buildRescaleFromInt32(rewriter, op, output_type, result_int32,
196  // 1.0, output_qtype.getZeroPoint());
197  TosaSerializationOperator* rescaleOutputOp = nullptr;
198  CreateFromInt32RescaleTosaOperator(outputNameRescaleMaxMin,
199  outputName,
200  1.0,
201  output_zp,
202  &rescaleOutputOp);
203 
204  // operatorInputNames/operatorOutputNames ends up being the same as
205  // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
206  return new TosaSerializationBasicBlock(blockName, // name
207  mainName, // region name
208  {rescaleAlphaOp, rescaleIdentityOp, op, rescaleOutputOp}, // operators
209  tensors, // tensors
210  {inputName}, // inputs
211  {outputName}); // outputs
212  }
213 }
armnn::ActivationDescriptor
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
armnn::ActivationDescriptor::m_A
float m_A
Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH,...
Definition: Descriptors.hpp:61
ActivationLayer.hpp
TosaRescaleOperatorUtils.hpp
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
Definition: TosaOperatorUtils.hpp:120
LeakyReluOperator.hpp
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
armnn::Layer
Definition: Layer.hpp:230
mainName
const std::string mainName
Definition: TosaOperatorUtils.hpp:19
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
ConvertLeakyReluToTosaOperator
TosaSerializationBasicBlock * ConvertLeakyReluToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ActivationDescriptor *activationDescriptor)
Definition: LeakyReluOperator.cpp:17
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:216
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