blob: f1fb34c5e29c1e8c97402f4581b7e1e32e74f96a [file] [log] [blame]
Matthew Sloyan164bf4f2022-10-28 18:02:17 +01001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "AdditionOperator.hpp"
7
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +00008TosaSerializationBasicBlock* ConvertAdditionToTosaOperator(const Layer* layer,
9 const std::vector<const TensorInfo*>& inputs,
10 const std::vector<const TensorInfo*>& outputs)
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010011{
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000012 std::string input0Name = std::string("input0_");
13 std::string input1Name = std::string("input1_");
14 std::string outputName = std::string("output0_");
15 std::string blockName = std::string("Op_ADD_block_") + GetUniqueTosaMappingID();
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010016
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000017 // If a layer is present then the block will be used for execution, so input and output names need to be determined
18 // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
19 if(layer != nullptr)
Matthew Sloyan5c54c382022-11-09 16:28:51 +000020 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000021 // Get the layers connected to the input slots and determine unique layer names.
22 Layer& connectedLayer0 = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
23 input0Name = GenerateUniqueName(connectedLayer0, 0);
24
25 Layer& connectedLayer1 = layer->GetInputSlot(1).GetConnectedOutputSlot()->GetOwningLayer();
26 input1Name = GenerateUniqueName(connectedLayer1, 1);
27
28 // Get the layer connected to the output slot and determine unique layer name.
29 Layer& connectedOutputLayer = layer->GetOutputSlot().GetConnection(0)->GetOwningLayer();
30 outputName = GenerateUniqueName(connectedOutputLayer, 0);
Matthew Sloyan5c54c382022-11-09 16:28:51 +000031 }
32
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000033 auto* op = new TosaSerializationOperator(Op_ADD,
34 Attribute_NONE,
35 nullptr,
36 {input0Name, input1Name},
37 {outputName});
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010038
39 std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
40 DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
41
42 std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape());
43 DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType());
44
45 std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
46 DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
47
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000048 auto* inputTensor0 = new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {});
49 auto* inputTensor1 = new TosaSerializationTensor(input1Name, inputShape1, inputDType1, {});
50 auto* outputTensor0 = new TosaSerializationTensor(outputName, outputShape0, outputDType0, {});
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010051
52 // operatorInputNames/operatorOutputNames ends up being the same as
Cathal Corbettb30e6552022-12-07 11:50:50 +000053 // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010054 return new TosaSerializationBasicBlock(blockName, // name
55 {op}, // operators
56 {inputTensor0, inputTensor1, outputTensor0}, // tensors
57 {input0Name, input1Name}, // inputs
58 {outputName}); // outputs
59}