ArmNN
 25.11
Loading...
Searching...
No Matches
ReshapeOperator.cpp
Go to the documentation of this file.
1//
2// Copyright © 2022-2024 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("input_");
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 inputName = GenerateUniqueInputName(layer->GetInputSlot(0));
22 outputName = GenerateUniqueOutputName(*layer);
23 }
24
25 TosaReshapeAttribute attribute(GetTosaTensorShape(reshapeDescriptor->m_TargetShape));
26
27 auto* op = new TosaSerializationOperator(Op_RESHAPE,
28 Attribute_ReshapeAttribute,
29 &attribute,
30 {inputName},
31 {outputName});
32
33 std::vector<TosaSerializationTensor*> tensors;
34
35 std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape());
36 DType outputDType = ArmNNToDType(outputs[0]->GetDataType());
37
38 // Only add input tensors if connected layer is an input layer.
39 // As intermediate or constant tensors will be created separately.
40 // There also can't be duplicate tensor.
41 if(inputName.find("input_") != std::string::npos)
42 {
43 // TOSA spec requires input and output shape to be the same size.
44 // Pad the inputShape with 1 values to meet this condition.
45 std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[0]->GetShape());
46 if (inputShape.size() < outputShape.size())
47 {
48 for (size_t i = inputShape.size(); i < outputShape.size(); ++i)
49 {
50 inputShape.emplace_back(1);
51 }
52 }
53
54 DType inputDType = ArmNNToDType(inputs[0]->GetDataType());
55
56 tensors.push_back(new TosaSerializationTensor(inputName, inputShape, inputDType, {}));
57 }
58
59 tensors.push_back(new TosaSerializationTensor(outputName, outputShape, outputDType, {}));
60
61 // operatorInputNames/operatorOutputNames ends up being the same as
62 // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
63 return new TosaSerializationBasicBlock(blockName, // name
64 mainName, // region name
65 {op}, // operators
66 tensors, // tensors
67 {inputName}, // inputs
68 {outputName}); // outputs
69}
TosaSerializationBasicBlock * ConvertReshapeToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ReshapeDescriptor *reshapeDescriptor)
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
const std::string mainName
DType ArmNNToDType(const DataType &type)
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
std::string GetUniqueTosaMappingID()
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
A ReshapeDescriptor for the ReshapeLayer.
TensorShape m_TargetShape
Target shape value.