blob: b88a6ef894e97c8ec30a3058c59790c90aefac4e [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 {
21 // Get the layers connected to the input slots and determine unique layer names.
22 Layer& connectedLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
23 inputName = GenerateUniqueName(connectedLayer, 0);
24
25 // Get the layer connected to the output slot and determine unique layer name.
26 Layer& connectedOutputLayer = layer->GetOutputSlot().GetConnection(0)->GetOwningLayer();
27 outputName = GenerateUniqueName(connectedOutputLayer, 0);
28 }
29
30 TosaReshapeAttribute attribute(GetTosaTensorShape(reshapeDescriptor->m_TargetShape));
31
32 auto* op = new TosaSerializationOperator(Op_RESHAPE,
33 Attribute_ReshapeAttribute,
34 &attribute,
35 {inputName},
36 {outputName});
37
38 std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[0]->GetShape());
39 DType inputDType = ArmNNToDType(inputs[0]->GetDataType());
40
41 std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape());
42 DType outputDType = ArmNNToDType(outputs[0]->GetDataType());
43
44 auto* inputTensor = new TosaSerializationTensor(inputName, inputShape, inputDType, {});
45 auto* outputTensor = new TosaSerializationTensor(outputName, outputShape, outputDType, {});
46
47 // operatorInputNames/operatorOutputNames ends up being the same as
48 // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
49 return new TosaSerializationBasicBlock(blockName, // name
50 {op}, // operators
51 {inputTensor, outputTensor}, // tensors
52 {inputName}, // inputs
53 {outputName}); // outputs
54}