ArmNN
 24.02
TransposeOperator.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 "TransposeOperator.hpp"
7 
8 TosaSerializationBasicBlock* ConvertTransposeToTosaOperator(const Layer* layer,
9  const std::vector<const TensorInfo*>& inputs,
10  const std::vector<const TensorInfo*>& outputs,
11  const TransposeDescriptor* transposeDescriptor)
12 {
13  std::string input0Name = std::string("input0_");
14  std::string outputName = std::string("output0_");
15  std::string blockName = std::string("Op_TRANSPOSE_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 slot and determine unique tensor name.
22  Layer& connectedLayer0 = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
23  input0Name = GenerateUniqueName(connectedLayer0, 0);
24 
25  // Determine unique output tensor name.
26  outputName = GenerateUniqueOutputName(*layer, 0);
27  }
28 
29  std::vector<int32_t> mappings(transposeDescriptor->m_DimMappings.begin(),
30  transposeDescriptor->m_DimMappings.end());
31  TosaTransposeAttribute attribute(mappings);
32 
33  auto* op = new TosaSerializationOperator(Op_TRANSPOSE,
34  Attribute_TransposeAttribute,
35  &attribute,
36  {input0Name},
37  {outputName});
38 
39 
40  std::vector<TosaSerializationTensor*> tensors;
41 
42  // Only add input tensors if connected layer is an input layer.
43  // As intermediate or constant tensors will be created separately.
44  // There also can't be duplicate tensor.
45  if(input0Name.find("input0_") != std::string::npos)
46  {
47  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
48  DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType());
49 
50  tensors.push_back(new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {}));
51  }
52 
53  std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
54  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
55 
56  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
57 
58  // operatorInputNames/operatorOutputNames ends up being the same as
59  // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
60  return new TosaSerializationBasicBlock(blockName, // name
61  mainName, // region name
62  {op}, // operators
63  tensors, // tensors
64  {input0Name}, // inputs
65  {outputName}); // outputs
66 }
armnn::PermutationVector::begin
ConstIterator begin() const
Definition: Types.hpp:359
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::PermutationVector::end
ConstIterator end() const
Definition: Types.hpp:364
armnn::Layer
Definition: Layer.hpp:230
mainName
const std::string mainName
Definition: TosaOperatorUtils.hpp:19
armnn::OutputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:132
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:82
armnn::TransposeDescriptor
A TransposeDescriptor for the TransposeLayer.
Definition: Descriptors.hpp:1490
TransposeOperator.hpp
armnn::TransposeDescriptor::m_DimMappings
PermutationVector m_DimMappings
Indicates how to translate tensor elements from a given source into the target destination,...
Definition: Descriptors.hpp:1514
GenerateUniqueName
std::string GenerateUniqueName(const Layer &layer, uint32_t layerSlot)
Definition: TosaOperatorUtils.hpp:63
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:52
armnn::InputSlot::GetConnectedOutputSlot
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:56
ConvertTransposeToTosaOperator
TosaSerializationBasicBlock * ConvertTransposeToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const TransposeDescriptor *transposeDescriptor)
Definition: TransposeOperator.cpp:8
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:100