blob: 02dddab8bc2fc897c63b766d4d3634a973b5c947 [file] [log] [blame]
David Monahand7fca092023-01-12 14:53:34 +00001//
Colm Donelanb4ef1632024-02-01 15:00:43 +00002// Copyright © 2023-2024 Arm Ltd and Contributors. All rights reserved.
David Monahand7fca092023-01-12 14:53:34 +00003// SPDX-License-Identifier: MIT
4//
5
6#include "ElementwiseUnaryOperator.hpp"
7
8TosaSerializationBasicBlock* ConvertElementwiseUnaryOperator(const Layer* layer,
9 const std::vector<const TensorInfo*>& inputs,
10 const std::vector<const TensorInfo*>& outputs,
11 const ElementwiseUnaryDescriptor* unaryDescriptor)
12{
13 std::string input0Name = std::string("input0_");
14 std::string outputName = std::string("output0_");
15 std::string blockName = std::string("Op_ELEMENTWISEUNARY_block_") + GetUniqueTosaMappingID();
16
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 layer connected to the input slot and determine unique the tensor name.
23 Layer& connectedLayer0 = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
24 input0Name = GenerateUniqueName(connectedLayer0, 0);
25
26 // Determine unique output tensor name.
27 outputName = GenerateUniqueOutputName(*layer, 0);
28 }
29
30 TosaSerializationOperator* op = nullptr;
31 switch(unaryDescriptor->m_Operation)
32 {
33 case UnaryOperation::Rsqrt:
34 {
35 op = new TosaSerializationOperator(tosa::Op_RSQRT,
36 Attribute_NONE,
37 nullptr,
38 {input0Name},
39 {outputName});
40 blockName = std::string("Op_RSQRT_block_") + GetUniqueTosaMappingID();
41 break;
42 }
43 default:
44 throw armnn::Exception("ConvertElementwiseUnaryToTosaOperator: Unsupported layer type.");
45 }
46
David Monahand7fca092023-01-12 14:53:34 +000047 std::vector<TosaSerializationTensor*> tensors;
48 // Only add input tensor 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 if(input0Name.find("input0_") != std::string::npos)
52 {
53 std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
54 DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
55 tensors.push_back(new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {}));
56 }
57
58 std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
59 DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
60
61 tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
62
63 // operatorInputNames/operatorOutputNames ends up being the same as
64 // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
65 return new TosaSerializationBasicBlock(blockName, // name
Narumol Prangnawaratad323af2023-09-29 17:00:38 +010066 mainName, // region name
David Monahand7fca092023-01-12 14:53:34 +000067 {op}, // operators
68 tensors, // tensors
69 {input0Name}, // inputs
70 {outputName}); // outputs
71}