ArmNN
 24.02
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("input0_");
15  std::string input1Name = std::string("input1_");
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  // Get the layers connected to the input slots and determine unique tensor names.
24  Layer& connectedLayer0 = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
25  input0Name = GenerateUniqueName(connectedLayer0, 0);
26 
27  Layer& connectedLayer1 = layer->GetInputSlot(1).GetConnectedOutputSlot()->GetOwningLayer();
28  input1Name = GenerateUniqueName(connectedLayer1, 1);
29 
30  // Determine unique output tensor name.
31  outputName = GenerateUniqueOutputName(*layer, 0);
32  }
33 
34  TosaSerializationOperator* op = nullptr;
35  switch(type)
36  {
37  case LayerType::Addition:
38  {
39  op = new TosaSerializationOperator(Op_ADD,
40  Attribute_NONE,
41  nullptr,
42  {input0Name, input1Name},
43  {outputName});
44  blockName = std::string("Op_ADD_block_") + GetUniqueTosaMappingID();
45  break;
46  }
47  case LayerType::ElementwiseBinary:
48  {
49  switch (descriptor->m_Operation)
50  {
52  {
53  op = new TosaSerializationOperator(Op_MAXIMUM,
54  Attribute_NONE,
55  nullptr,
56  {input0Name, input1Name},
57  {outputName});
58  blockName = std::string("Op_MAXIMUM_block_") + GetUniqueTosaMappingID();
59  break;
60  }
61  default:
62  throw armnn::Exception("ConvertElementwiseBinaryToTosaOperator: Unsupported layer type.");
63  }
64  break;
65  }
66  case LayerType::Multiplication:
67  {
68  int32_t shift = 0;
69  TosaMulAttribute mulAttribute(shift);
70  op = new TosaSerializationOperator(Op_MUL,
71  Attribute_MulAttribute,
72  &mulAttribute,
73  {input0Name, input1Name},
74  {outputName});
75  blockName = std::string("Op_MUL_block_") + GetUniqueTosaMappingID();
76  break;
77  }
78  case LayerType::Subtraction:
79  {
80  op = new TosaSerializationOperator(Op_SUB,
81  Attribute_NONE,
82  nullptr,
83  {input0Name, input1Name},
84  {outputName});
85  blockName = std::string("Op_SUB_block_") + GetUniqueTosaMappingID();
86  break;
87  }
88  default:
89  throw armnn::Exception("ConvertElementwiseBinaryToTosaOperator: Unsupported layer type.");
90  }
91  ARMNN_ASSERT(op != nullptr);
92 
93  std::vector<TosaSerializationTensor*> tensors;
94  // Only add input tensors if connected layer is an input layer.
95  // As intermediate or constant tensors will be created separately.
96  // There also can't be duplicate tensor.
97  if(input0Name.find("input0_") != std::string::npos)
98  {
99  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
100  DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
101  tensors.push_back(new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {}));
102  }
103  if(input1Name.find("input1_") != std::string::npos)
104  {
105  std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
106  DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
107  tensors.push_back(new TosaSerializationTensor(input1Name, inputShape1, inputDType1, {}));
108  }
109 
110  std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
111  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
112 
113  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
114 
115  // operatorInputNames/operatorOutputNames ends up being the same as
116  // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
117  return new TosaSerializationBasicBlock(blockName, // name
118  mainName, // region name
119  {op}, // operators
120  tensors, // tensors
121  {input0Name, input1Name}, // inputs
122  {outputName}); // outputs
123 }

References ARMNN_ASSERT, GenerateUniqueName(), GenerateUniqueOutputName(), InputSlot::GetConnectedOutputSlot(), Layer::GetInputSlot(), OutputSlot::GetOwningLayer(), GetTosaTensorShape(), GetUniqueTosaMappingID(), ElementwiseBinaryDescriptor::m_Operation, and armnn::Maximum.

Referenced by GetTosaMapping().

ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
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
armnn::BinaryOperation::Maximum
@ Maximum
armnn::OutputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:132
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:82
armnn::Exception
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
GenerateUniqueName
std::string GenerateUniqueName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:63
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:52
armnn::ElementwiseBinaryDescriptor::m_Operation
BinaryOperation m_Operation
Specifies the elementwiseBinary operation to execute.
Definition: Descriptors.hpp:125
armnn::InputSlot::GetConnectedOutputSlot
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:56
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:100