ArmNN
 25.11
Loading...
Searching...
No Matches
ArgMinMaxLayer.cpp
Go to the documentation of this file.
1//
2// Copyright © 2017,2019-2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ArgMinMaxLayer.hpp"
7#include "LayerCloneBase.hpp"
8
10
12
15
16namespace armnn
17{
18
20 : LayerWithParameters(1, 1, LayerType::ArgMinMax, param, name)
21{
22}
23
24std::unique_ptr<IWorkload> ArgMinMaxLayer::CreateWorkload(const IWorkloadFactory& factory) const
25{
26 ArgMinMaxQueueDescriptor descriptor;
27 SetAdditionalInfo(descriptor);
28
29 return factory.CreateWorkload(LayerType::ArgMinMax, descriptor, PrepInfoAndDesc(descriptor));
30}
31
36
37std::vector<TensorShape> ArgMinMaxLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
38{
39 if (inputShapes.size() != 1)
40 {
41 throw armnn::LayerValidationException("inputShapes' size is \"" + std::to_string(inputShapes.size()) +
42 "\" - should be \"1\".");
43 }
44
45 TensorShape inputShape = inputShapes[0];
46 auto inputNumDimensions = inputShape.GetNumDimensions();
47
48 auto axis = m_Param.m_Axis;
49 auto unsignedAxis = armnnUtils::GetUnsignedAxis(inputNumDimensions, axis);
50
51 if (unsignedAxis > inputNumDimensions)
52 {
53 throw armnn::LayerValidationException("Axis must not be greater than number of input dimensions (\""
54 + std::to_string(unsignedAxis) +
55 "\" vs \""
56 + std::to_string(inputNumDimensions) + "\").");
57 }
58
59 // 1D input shape results in scalar output
60 if (inputShape.GetNumDimensions() == 1)
61 {
62 std::vector<unsigned int> tensorDimensions(1, 1);
63 TensorShape outputShape(1, tensorDimensions.data());
64
65 return std::vector<TensorShape>({ outputShape });
66 }
67
68 std::vector<unsigned int> tensorDimensions(inputNumDimensions - 1, 0);
69 for (unsigned int i = 0; i < unsignedAxis; ++i)
70 {
71 tensorDimensions[i] = inputShape[i];
72 }
73
74 for (unsigned int i = unsignedAxis + 1; i < inputNumDimensions; ++i)
75 {
76 tensorDimensions[i - 1] = inputShape[i];
77 }
78
79 TensorShape outputShape = TensorShape(inputNumDimensions - 1, tensorDimensions.data());
80
81 return std::vector<TensorShape>({ outputShape });
82}
83
85{
87
88 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
89
91
92 auto inferredShapes = InferOutputShapes({ GetInputSlot(0).GetTensorInfo().GetShape() });
93
94 if (inferredShapes.size() != 1)
95 {
96 throw armnn::LayerValidationException("inferredShapes has "
97 + std::to_string(inferredShapes.size()) +
98 " elements - should only have 1.");
99 }
100
101 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "ArgMinMaxLayer");
102}
103
105{
106 strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
107}
108
109} // namespace armnn
#define CHECK_LOCATION()
ArgMinMaxLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
Infers the output shape from a given input shape and axis parameter.
ArgMinMaxLayer(const ArgMinMaxDescriptor &param, const char *name)
Constructor to create a ArgMinMaxLayer.
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of ArgMinMaxLayer.
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the ArgMinMax type.
virtual void ExecuteStrategy(const IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
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.
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition Layer.cpp:614
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition Layer.cpp:410
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition Layer.cpp:526
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition Layer.hpp:339
LayerType * CloneBase(Graph &graph, Params &&... params) const
const char * GetName() const override
Returns the name of the layer.
Definition Layer.hpp:332
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition Layer.cpp:457
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition Layer.cpp:303
ShapeInferenceMethod m_ShapeInferenceMethod
Definition Layer.hpp:441
LayerWithParameters(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const ArgMinMaxDescriptor &param, const char *name)
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
const ArgMinMaxDescriptor & GetParameters() const override
const TensorInfo & GetTensorInfo() const override
Definition Layer.cpp:100
const TensorShape & GetShape() const
Definition Tensor.hpp:193
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition Tensor.cpp:174
Copyright (c) 2021 ARM Limited and Contributors.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition Types.hpp:494
void ArgMinMax(Decoder< float > &in, OUT *out, const TensorInfo &inputTensorInfo, const TensorInfo &outputTensorInfo, ArgMinMaxFunction function, int axis)
Definition ArgMinMax.cpp:17
unsigned int GetUnsignedAxis(const unsigned int inputDimension, const int axis)
An ArgMinMaxDescriptor for ArgMinMaxLayer.