ArmNN
 24.08
ScatterNdLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ScatterNdLayer.hpp"
7 #include "LayerCloneBase.hpp"
8 
9 #include <armnn/TypesUtils.hpp>
12 
13 namespace armnn
14 {
15 
16 ScatterNdLayer::ScatterNdLayer(const ScatterNdDescriptor &param, const char* name)
17  : LayerWithParameters(3, 1, LayerType::ScatterNd, param, name)
18 {
19 }
20 
21 std::unique_ptr<IWorkload> ScatterNdLayer::CreateWorkload(const armnn::IWorkloadFactory& factory) const
22 {
23  ScatterNdQueueDescriptor descriptor;
24  SetAdditionalInfo(descriptor);
25 
26  return factory.CreateWorkload(LayerType::ScatterNd, descriptor, PrepInfoAndDesc(descriptor));
27 }
28 
30 {
31  auto layer = CloneBase<ScatterNdLayer>(graph, m_Param, GetName());
32 
33  return std::move(layer);
34 }
35 
36 std::vector<TensorShape> ScatterNdLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
37 {
38  const auto inputDims = inputShapes[0].GetNumDimensions();
39 
40  std::vector<unsigned int> dimSizes(inputDims);
41 
42  for (unsigned i = 0; i < inputDims; ++i)
43  {
44  dimSizes[i] = inputShapes[0][i];
45  }
46 
47  TensorShape outputShape({ inputDims, dimSizes.data() });
48 
49  return std::vector<TensorShape>({ outputShape });
50 }
51 
53 {
55 
56  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
57 
59 
61  {
62  std::vector<TensorShape> inferredShapes = InferOutputShapes(
66 
67  if (inferredShapes.size() != 1) {
68  throw armnn::LayerValidationException("inferredShape has " +
69  std::to_string(inferredShapes.size()) +
70  " elements - should only have 1.");
71  }
72 
73  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "ScatterLayer");
74  }
75  else
76  {
77  // No input tensor, only shape provided via input slot
78  // In this case, we cannot validate the output shape from the input shape, but we can
79  // validate that the dimensions of shape and output tensor matched
80  unsigned int shapeDims = GetInputSlot(0).GetTensorInfo().GetShape().GetNumElements();
81  unsigned int outputDims = GetOutputSlot(0).GetTensorInfo().GetNumDimensions();
82 
83  if (shapeDims != outputDims)
84  {
85  throw armnn::LayerValidationException("shape dimension " +
86  std::to_string(shapeDims) +
87  " and output dimension " +
88  std::to_string(outputDims) +
89  " are not matched.");
90  }
91  }
92 }
93 
94 } // namespace armnn
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:100
WorkloadData.hpp
armnn::ScatterNdLayer::InferOutputShapes
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
Infers the output shapes from given input shapes and layer properties.
Definition: ScatterNdLayer.cpp:36
TypesUtils.hpp
armnn::ScatterNdDescriptor::m_InputEnabled
bool m_InputEnabled
Flag to show if input tensor is accepted.
Definition: Descriptors.hpp:1722
armnn::TensorInfo::GetNumDimensions
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:197
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::Layer::ValidateAndCopyShape
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:457
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
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
WorkloadFactory.hpp
armnn::LayerWithParameters
Definition: LayerWithParameters.hpp:14
armnn::Layer::GetName
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:332
armnn::ScatterNdLayer::Clone
ScatterNdLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: ScatterNdLayer.cpp:29
armnn::ScatterNdLayer::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the ScatterNd type.
Definition: ScatterNdLayer.cpp:21
armnn::InputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition: Layer.cpp:614
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::LayerWithParameters< ScatterNdDescriptor >::m_Param
ScatterNdDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
Definition: LayerWithParameters.hpp:52
armnn::LayerType::ScatterNd
@ ScatterNd
armnn::LayerWithParameters< ScatterNdDescriptor >::PrepInfoAndDesc
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *Layer::CreateWorkload.
Definition: LayerWithParameters.hpp:44
armnn::ScatterNdQueueDescriptor
Definition: WorkloadData.hpp:773
armnn::LayerValidationException
Definition: Exceptions.hpp:105
armnn::IWorkloadFactory
Definition: WorkloadFactory.hpp:22
armnn::Layer::VerifyShapeInferenceType
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:526
armnn::Layer::SetAdditionalInfo
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:303
armnn::ScatterNdLayer
This layer represents a ScatterNd operator.
Definition: ScatterNdLayer.hpp:14
armnn::ScatterNd
void ScatterNd(const TensorInfo &inputInfo, const TensorInfo &indicesInfo, const TensorInfo &updatesInfo, Decoder< float > &input, Decoder< int > &indices, Decoder< float > &updates, Encoder< float > &output, ScatterNdDescriptor descriptor)
Definition: ScatterNd.cpp:41
armnn::ScatterNdLayer::ScatterNdLayer
ScatterNdLayer(const ScatterNdDescriptor &param, const char *name)
Constructor to create a ScatterNdLayer.
Definition: ScatterNdLayer.cpp:16
ScatterNdLayer.hpp
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
armnn::ScatterNdLayer::ValidateTensorShapesFromInputs
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of ScatterNdLayer.
Definition: ScatterNdLayer.cpp:52
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::Layer::VerifyLayerConnections
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:410
armnn::ScatterNdDescriptor
A ScatterNdDescriptor for the ScatterNdLayer.
Definition: Descriptors.hpp:1679
armnn::Layer::m_ShapeInferenceMethod
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:441
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:491
armnn::TensorShape::GetNumElements
unsigned int GetNumElements() const
Function that calculates the tensor elements by multiplying all dimension size which are Specified.
Definition: Tensor.cpp:181
armnn::Graph
Definition: Graph.hpp:30
armnn::IWorkloadFactory::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const =0
Backends should implement their own CreateWorkload function with a switch statement.
LayerCloneBase.hpp