ArmNN
 20.02
ArgMinMaxLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ArgMinMaxLayer.hpp"
7 #include "LayerCloneBase.hpp"
8 
9 #include <armnn/TypesUtils.hpp>
10 
12 
15 
16 namespace armnn
17 {
18 
19 ArgMinMaxLayer::ArgMinMaxLayer(const ArgMinMaxDescriptor& param, const char* name)
20  : LayerWithParameters(1, 1, LayerType::ArgMinMax, param, name)
21 {
22 }
23 
24 std::unique_ptr<IWorkload> ArgMinMaxLayer::CreateWorkload(const IWorkloadFactory& factory) const
25 {
26  ArgMinMaxQueueDescriptor descriptor;
27  return factory.CreateArgMinMax(descriptor, PrepInfoAndDesc(descriptor));
28 }
29 
31 {
32  return CloneBase<ArgMinMaxLayer>(graph, m_Param, GetName());
33 }
34 
35 std::vector<TensorShape> ArgMinMaxLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
36 {
37  BOOST_ASSERT(inputShapes.size() == 1);
38 
39  TensorShape inputShape = inputShapes[0];
40  auto inputNumDimensions = inputShape.GetNumDimensions();
41 
42  auto axis = m_Param.m_Axis;
43  auto unsignedAxis = armnnUtils::GetUnsignedAxis(inputNumDimensions, axis);
44 
45  BOOST_ASSERT(unsignedAxis <= inputNumDimensions);
46 
47  // 1D input shape results in scalar output
48  if (inputShape.GetNumDimensions() == 1)
49  {
50  std::vector<unsigned int> tensorDimensions(1, 1);
51  TensorShape outputShape(1, tensorDimensions.data());
52 
53  return std::vector<TensorShape>({ outputShape });
54  }
55 
56  std::vector<unsigned int> tensorDimensions(inputNumDimensions - 1, 0);
57  for (unsigned int i = 0; i < unsignedAxis; ++i)
58  {
59  tensorDimensions[i] = inputShape[i];
60  }
61 
62  for (unsigned int i = unsignedAxis + 1; i < inputNumDimensions; ++i)
63  {
64  tensorDimensions[i - 1] = inputShape[i];
65  }
66 
67  TensorShape outputShape = TensorShape(inputNumDimensions - 1, tensorDimensions.data());
68 
69  return std::vector<TensorShape>({ outputShape });
70 }
71 
73 {
75 
76  auto inferredShapes = InferOutputShapes({ GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape() });
77 
78  BOOST_ASSERT(inferredShapes.size() == 1);
79 
80  ConditionalThrowIfNotEqual<LayerValidationException>(
81  "ArgMinMaxLayer: TensorShape set on OutputSlot does not match the inferred shape.",
83  inferredShapes[0]);
84 }
85 
87 {
88  visitor.VisitArgMinMaxLayer(this, GetParameters(), GetName());
89 }
90 
91 } // namespace armnn
ArgMinMaxDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const ArgMinMaxDescriptor & GetParameters() const
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
virtual std::unique_ptr< IWorkload > CreateArgMinMax(const ArgMinMaxQueueDescriptor &descriptor, const WorkloadInfo &info) const
void ArgMinMax(Decoder< float > &in, int32_t *out, const TensorInfo &inputTensorInfo, const TensorInfo &outputTensorInfo, ArgMinMaxFunction function, int axis)
Definition: ArgMinMax.cpp:15
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
ArgMinMaxLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Copyright (c) 2020 ARM Limited.
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
ArgMinMaxLayer(const ArgMinMaxDescriptor &param, const char *name)
Constructor to create a ArgMinMaxLayer.
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:338
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:310
An ArgMinMaxDescriptor for ArgMinMaxLayer.
Definition: Descriptors.hpp:43
unsigned int GetUnsignedAxis(const unsigned int inputDimension, const int axis)
This layer represents a ArgMinMax operation.
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the ArgMinMax type.
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:43
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:312
virtual const TensorInfo & GetTensorInfo() const =0
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of ArgMinMaxLayer.
int m_Axis
Axis to reduce across the input tensor.
Definition: Descriptors.hpp:58
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:305
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
Infers the output shape from a given input shape and axis parameter.
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
virtual void VisitArgMinMaxLayer(const IConnectableLayer *layer, const ArgMinMaxDescriptor &argMinMaxDescriptor, const char *name=nullptr)=0
Function that an arg min max layer should call back to when its Accept(ILayerVisitor&) function is in...