ArmNN
 23.08
ReshapeOperator.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ReshapeOperator.hpp"
7 
8 TosaSerializationBasicBlock* 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 tensor names.
22  Layer& connectedLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
23  inputName = GenerateUniqueName(connectedLayer, 0);
24 
25  // Determine unique output tensor name.
26  outputName = GenerateUniqueOutputName(*layer, 0);
27  }
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 
37  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  }
49 
50  std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape());
51  DType outputDType = ArmNNToDType(outputs[0]->GetDataType());
52 
53  tensors.push_back(new TosaSerializationTensor(outputName, outputShape, outputDType, {}));
54 
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
58  {op}, // operators
59  tensors, // tensors
60  {inputName}, // inputs
61  {outputName}); // outputs
62 }
ConvertReshapeToTosaOperator
TosaSerializationBasicBlock * ConvertReshapeToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ReshapeDescriptor *reshapeDescriptor)
Definition: ReshapeOperator.cpp:8
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
armnn::Layer
Definition: Layer.hpp:230
armnn::OutputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:132
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:20
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:79
armnn::ReshapeDescriptor
A ReshapeDescriptor for the ReshapeLayer.
Definition: Descriptors.hpp:1002
armnn::ReshapeDescriptor::m_TargetShape
TensorShape m_TargetShape
Target shape value.
Definition: Descriptors.hpp:1018
GenerateUniqueName
std::string GenerateUniqueName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:60
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:49
ReshapeOperator.hpp
armnn::InputSlot::GetConnectedOutputSlot
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:56
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:97