ArmNN
 20.02
StridedSliceLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "StridedSliceLayer.hpp"
6 
7 #include "LayerCloneBase.hpp"
8 
11 
12 #include <boost/numeric/conversion/cast.hpp>
13 
14 namespace armnn
15 {
16 
18  : LayerWithParameters(1, 1, LayerType::StridedSlice, param, name)
19 {
20 }
21 
22 std::unique_ptr<IWorkload> StridedSliceLayer::CreateWorkload(const IWorkloadFactory& factory) const
23 {
24  StridedSliceQueueDescriptor descriptor;
25 
26  descriptor.m_Parameters.m_Begin = m_Param.m_Begin;
27  descriptor.m_Parameters.m_End = m_Param.m_End;
29 
30  // Optional parameters
36 
37  return factory.CreateStridedSlice(descriptor, PrepInfoAndDesc(descriptor));
38 }
39 
41 {
42  return CloneBase<StridedSliceLayer>(graph, m_Param, GetName());
43 }
44 
45 std::vector<TensorShape> StridedSliceLayer::InferOutputShapes(
46  const std::vector<TensorShape>& inputShapes) const
47 {
48  BOOST_ASSERT(inputShapes.size() == 1);
49 
50  TensorShape inputShape = inputShapes[0];
51  std::vector<unsigned int> outputShape;
52 
53  for (unsigned int i = 0; i < inputShape.GetNumDimensions(); i++)
54  {
55  if (m_Param.m_ShrinkAxisMask & (1 << i))
56  {
57  continue;
58  }
59 
60  int stride = m_Param.m_Stride[i];
61  int start = m_Param.GetStartForAxis(inputShape, i);
62  int stop = m_Param.GetStopForAxis(inputShape, i, start);
63 
64  int newSize = stride > 0 ? ((stop - start) + stride - 1) / stride :
65  ((start - stop) - stride - 1) / -stride;
66 
67  newSize = std::max(0, newSize);
68 
69  outputShape.push_back(boost::numeric_cast<unsigned int>(newSize));
70  }
71 
72  return std::vector<TensorShape>({
73  TensorShape(boost::numeric_cast<unsigned int>(outputShape.size()), &outputShape[0]) });
74 }
75 
77 {
79 
80  auto inferredShapes = InferOutputShapes({GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape()});
81 
82  BOOST_ASSERT(inferredShapes.size() == 1);
83 
84  ConditionalThrowIfNotEqual<LayerValidationException>(
85  "StridedSlice: TensorShape set on OutputSlot[0] does not match the inferred shape.",
87  inferredShapes[0]);
88 }
89 
91 {
92  visitor.VisitStridedSliceLayer(this, GetParameters(), GetName());
93 }
94 
95 } // namespace armnn
StridedSliceDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const StridedSliceDescriptor & GetParameters() const
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the StridedSlice type.
int32_t m_ShrinkAxisMask
Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1...
std::vector< int > m_Begin
Begin values for the input that will be sliced.
int GetStartForAxis(const TensorShape &inputShape, unsigned int axis) const
virtual std::unique_ptr< IWorkload > CreateStridedSlice(const StridedSliceQueueDescriptor &descriptor, const WorkloadInfo &Info) const
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of StridedSliceLayer.
Copyright (c) 2020 ARM Limited.
int32_t m_BeginMask
Begin mask value.
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
int32_t m_EndMask
End mask value.
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
virtual void VisitStridedSliceLayer(const IConnectableLayer *layer, const StridedSliceDescriptor &stridedSliceDescriptor, const char *name=nullptr)=0
Function a strided slice layer should call back to when its Accept(ILayerVisitor&) function is invoke...
int32_t m_NewAxisMask
New axis mask value.
int32_t m_EllipsisMask
Ellipsis mask value.
std::vector< int > m_Stride
Stride values for the input that will be sliced.
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
std::vector< int > m_End
End values for the input that will be sliced.
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
void StridedSlice(const TensorInfo &inputInfo, const StridedSliceDescriptor &params, const void *inputData, void *outputData, unsigned int dataTypeSize)
StridedSliceLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
This layer represents a strided slice operation.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:312
A StridedSliceDescriptor for the StridedSliceLayer.
virtual const TensorInfo & GetTensorInfo() const =0
int GetStopForAxis(const TensorShape &inputShape, unsigned int axis, int startForAxis) const
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:305
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
StridedSliceLayer(const StridedSliceDescriptor &param, const char *name)
Constructor to create a StridedSliceLayer.
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
By default returns inputShapes if the number of inputs are equal to number of outputs, otherwise infers the output shapes from given input shapes and layer properties.