ArmNN
 24.08
ConstantOperator.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 "ConstantOperator.hpp"
7 
9 
10 TosaSerializationBasicBlock* ConvertConstantToTosaOperator(const Layer* layer,
11  const std::vector<const TensorInfo*>& outputs,
12  bool isDepthwiseConv2dWeights = false)
13 {
14  std::string outputName = std::string("constant_");
15  std::string blockName = std::string("Op_CONST_block_") + GetUniqueTosaMappingID();
16 
17  std::vector<uint8_t> uint8Data;
18 
19  // If a layer is present then the block will be used for execution, so names need to be unique.
20  // Also, set constant tensor data.
21  if(layer != nullptr)
22  {
23  outputName.append(std::to_string(layer->GetGuid()));
24  blockName.append(std::to_string(layer->GetGuid()));
25 
26  auto constantLayer = PolymorphicDowncast<const armnn::ConstantLayer*>(layer);
27  auto tensorInfo = constantLayer->GetOutputSlot().GetTensorInfo();
28 
29  uint8Data = ConvertConstantTensorDataToBuffer(constantLayer->m_LayerOutput);
30  }
31 
32  auto* op = new TosaSerializationOperator(Op_CONST, Attribute_NONE, nullptr, {}, {outputName});
33 
34  std::vector<int32_t> outputShape0;
35 
36  if(isDepthwiseConv2dWeights)
37  {
38  // Constant weights are connected to a depthwise conv2d layer. From this get the depthwise conv2d input shape.
39  TensorShape inputShape =
41 
42  unsigned int multiplier = outputs[0]->GetShape()[3]/inputShape[3];
43 
44  // TOSA requires depthwise conv2d kernel to be converted from [1, H, W, C * M] to layout [H, W, C, M]
45  outputShape0 = {
46  static_cast<int32_t>(outputs[0]->GetShape()[1]),
47  static_cast<int32_t>(outputs[0]->GetShape()[2]),
48  static_cast<int32_t>(inputShape[3]),
49  static_cast<int32_t>(multiplier)
50  };
51  }
52  else
53  {
54  outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
55  }
56 
57  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
58 
59  // Setup output tensor with constant tensor data if available.
60  auto* outputTensor0 = new TosaSerializationTensor(outputName, outputShape0, outputDType0, uint8Data);
61 
62  return new TosaSerializationBasicBlock(blockName, // name
63  mainName, // region name
64  {op}, // operators
65  {outputTensor0}, // tensors
66  {}, // inputs
67  {outputName}); // outputs
68 }
armnn::InputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:53
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
ConvertConstantTensorDataToBuffer
std::vector< uint8_t > ConvertConstantTensorDataToBuffer(const std::shared_ptr< ConstTensorHandle > &tensorHandle)
Definition: TosaOperatorUtils.hpp:333
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::InputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition: Layer.cpp:614
mainName
const std::string mainName
Definition: TosaOperatorUtils.hpp:19
armnn::TensorShape
Definition: Tensor.hpp:20
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
armnn::Layer::GetGuid
LayerGuid GetGuid() const final
Returns the unique id of the layer.
Definition: Layer.hpp:343
ConstantOperator.hpp
ConvertConstantToTosaOperator
TosaSerializationBasicBlock * ConvertConstantToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &outputs, bool isDepthwiseConv2dWeights=false)
Definition: ConstantOperator.cpp:10
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
ConstantLayer.hpp
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:79
armnn::OutputSlot::GetConnection
const InputSlot * GetConnection(unsigned int index) const override
Definition: Layer.cpp:83
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138