ArmNN
 23.08
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)
 

Function Documentation

◆ ConvertElementwiseBinaryToTosaOperator()

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

Definition at line 8 of file ElementwiseBinaryOperator.cpp.

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

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

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
armnn::OutputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:132
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:20
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:79
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:60
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:49
armnn::InputSlot::GetConnectedOutputSlot
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:56
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:97