ArmNN
 24.05
ElementwiseBinaryOperator.cpp File Reference
Include dependency graph for ElementwiseBinaryOperator.cpp:

Go to the source code of this file.

Functions

TosaSerializationBasicBlock * ConvertElementwiseBinaryToTosaOperator (const Layer *layer, const LayerType type, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ElementwiseBinaryDescriptor *descriptor)
 

Function Documentation

◆ ConvertElementwiseBinaryToTosaOperator()

TosaSerializationBasicBlock* ConvertElementwiseBinaryToTosaOperator ( const Layer layer,
const LayerType  type,
const std::vector< const TensorInfo * > &  inputs,
const std::vector< const TensorInfo * > &  outputs,
const ElementwiseBinaryDescriptor descriptor 
)

Definition at line 8 of file ElementwiseBinaryOperator.cpp.

13 {
14  std::string input0Name = std::string("input_0");
15  std::string input1Name = std::string("input_1");
16  std::string outputName = std::string("output0_");
17  std::string blockName;
18 
19  // If a layer is present then the block will be used for execution, so input and output names need to be determined
20  // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
21  if(layer != nullptr)
22  {
23  input0Name = GenerateUniqueInputName(layer->GetInputSlot(0));
24  input1Name = GenerateUniqueInputName(layer->GetInputSlot(1));
25  outputName = GenerateUniqueOutputName(*layer);
26  }
27 
28  TosaSerializationOperator* op = nullptr;
29  switch(type)
30  {
31  case LayerType::Addition:
32  {
33  op = new TosaSerializationOperator(Op_ADD,
34  Attribute_NONE,
35  nullptr,
36  {input0Name, input1Name},
37  {outputName});
38  blockName = std::string("Op_ADD_block_") + GetUniqueTosaMappingID();
39  break;
40  }
41  case LayerType::ElementwiseBinary:
42  {
43  switch (descriptor->m_Operation)
44  {
46  {
47  op = new TosaSerializationOperator(Op_MAXIMUM,
48  Attribute_NONE,
49  nullptr,
50  {input0Name, input1Name},
51  {outputName});
52  blockName = std::string("Op_MAXIMUM_block_") + GetUniqueTosaMappingID();
53  break;
54  }
55  default:
56  throw armnn::Exception("ConvertElementwiseBinaryToTosaOperator: Unsupported layer type.");
57  }
58  break;
59  }
60  case LayerType::Multiplication:
61  {
62  int32_t shift = 0;
63  TosaMulAttribute mulAttribute(shift);
64  op = new TosaSerializationOperator(Op_MUL,
65  Attribute_MulAttribute,
66  &mulAttribute,
67  {input0Name, input1Name},
68  {outputName});
69  blockName = std::string("Op_MUL_block_") + GetUniqueTosaMappingID();
70  break;
71  }
72  case LayerType::Subtraction:
73  {
74  op = new TosaSerializationOperator(Op_SUB,
75  Attribute_NONE,
76  nullptr,
77  {input0Name, input1Name},
78  {outputName});
79  blockName = std::string("Op_SUB_block_") + GetUniqueTosaMappingID();
80  break;
81  }
82  default:
83  throw armnn::Exception("ConvertElementwiseBinaryToTosaOperator: Unsupported layer type.");
84  }
85 
86  std::vector<TosaSerializationTensor*> tensors;
87  // Only add input tensors if connected layer is an input layer.
88  // As intermediate or constant tensors will be created separately.
89  // There also can't be duplicate tensor.
90  if(input0Name.find("input_") != std::string::npos)
91  {
92  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
93  DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
94  tensors.push_back(new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {}));
95  }
96  if(input1Name.find("input_") != std::string::npos)
97  {
98  std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
99  DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
100  tensors.push_back(new TosaSerializationTensor(input1Name, inputShape1, inputDType1, {}));
101  }
102 
103  std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
104  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
105 
106  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
107 
108  // operatorInputNames/operatorOutputNames ends up being the same as
109  // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
110  return new TosaSerializationBasicBlock(blockName, // name
111  mainName, // region name
112  {op}, // operators
113  tensors, // tensors
114  {input0Name, input1Name}, // inputs
115  {outputName}); // outputs
116 }

References GenerateUniqueInputName(), GenerateUniqueOutputName(), Layer::GetInputSlot(), GetTosaTensorShape(), GetUniqueTosaMappingID(), ElementwiseBinaryDescriptor::m_Operation, and armnn::Maximum.

Referenced by GetTosaMapping().

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
armnn::BinaryOperation::Maximum
@ Maximum
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
armnn::ElementwiseBinaryDescriptor::m_Operation
BinaryOperation m_Operation
Specifies the elementwiseBinary operation to execute.
Definition: Descriptors.hpp:125
GenerateUniqueInputName
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
Definition: TosaOperatorUtils.hpp:109
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138