blob: 55d66806b790aad93edbb4f7186dafa33341adb6 [file] [log] [blame]
Cathal Corbettb30e6552022-12-07 11:50:50 +00001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ReshapeOperator.hpp"
7
8TosaSerializationBasicBlock* ConvertReshapeToTosaOperator(const Layer* layer,
9 const std::vector<const TensorInfo*>& inputs,
10 const std::vector<const TensorInfo*>& outputs,
11 const ReshapeDescriptor* reshapeDescriptor)
12{
13 std::string inputName = std::string("input0_");
14 std::string outputName = std::string("output0_");
15 std::string blockName = std::string("Op_RESHAPE_block_") + GetUniqueTosaMappingID();
16
17 // 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)
20 {
Kevin May5b58e312022-12-15 10:15:21 +000021 // Get the layers connected to the input slots and determine unique tensor names.
Cathal Corbettb30e6552022-12-07 11:50:50 +000022 Layer& connectedLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
23 inputName = GenerateUniqueName(connectedLayer, 0);
24
Kevin May5b58e312022-12-15 10:15:21 +000025 // Determine unique output tensor name.
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000026 outputName = GenerateUniqueOutputName(*layer, 0);
Cathal Corbettb30e6552022-12-07 11:50:50 +000027 }
28
29 TosaReshapeAttribute attribute(GetTosaTensorShape(reshapeDescriptor->m_TargetShape));
30
31 auto* op = new TosaSerializationOperator(Op_RESHAPE,
32 Attribute_ReshapeAttribute,
33 &attribute,
34 {inputName},
35 {outputName});
36
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000037 std::vector<TosaSerializationTensor*> tensors;
38
39 // Only add input tensors if connected layer is an input layer.
40 // As intermediate or constant tensors will be created separately.
41 // There also can't be duplicate tensor.
42 if(inputName.find("input0_") != std::string::npos)
43 {
44 std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[0]->GetShape());
45 DType inputDType = ArmNNToDType(inputs[0]->GetDataType());
46
47 tensors.push_back(new TosaSerializationTensor(inputName, inputShape, inputDType, {}));
48 }
Cathal Corbettb30e6552022-12-07 11:50:50 +000049
50 std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape());
51 DType outputDType = ArmNNToDType(outputs[0]->GetDataType());
52
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000053 tensors.push_back(new TosaSerializationTensor(outputName, outputShape, outputDType, {}));
Cathal Corbettb30e6552022-12-07 11:50:50 +000054
55 // operatorInputNames/operatorOutputNames ends up being the same as
56 // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
57 return new TosaSerializationBasicBlock(blockName, // name
Narumol Prangnawaratad323af2023-09-29 17:00:38 +010058 mainName, // region name
Cathal Corbettb30e6552022-12-07 11:50:50 +000059 {op}, // operators
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000060 tensors, // tensors
Cathal Corbettb30e6552022-12-07 11:50:50 +000061 {inputName}, // inputs
62 {outputName}); // outputs
63}