ArmNN
 21.05
AddBroadcastReshapeLayerImpl Class Reference

#include <AddBroadcastReshapeLayer.hpp>

Public Member Functions

void Run (Graph &graph, Layer &layer) const
 Run for every ElementwiseBaseLayer. Add Broadcast reshape layer if the inputs shape are different. More...
 

Protected Member Functions

 AddBroadcastReshapeLayerImpl ()=default
 
 ~AddBroadcastReshapeLayerImpl ()=default
 

Detailed Description

Definition at line 22 of file AddBroadcastReshapeLayer.hpp.

Constructor & Destructor Documentation

◆ AddBroadcastReshapeLayerImpl()

◆ ~AddBroadcastReshapeLayerImpl()

Member Function Documentation

◆ Run()

void Run ( Graph graph,
Layer layer 
) const
inline

Run for every ElementwiseBaseLayer. Add Broadcast reshape layer if the inputs shape are different.

Definition at line 26 of file AddBroadcastReshapeLayer.hpp.

References AddBroadcastReshapeLayerImpl::AddBroadcastReshapeLayerImpl(), armnn::Constant, InputSlot::GetConnectedOutputSlot(), Layer::GetInputSlot(), Layer::GetNameStr(), OutputSlot::GetNumConnections(), TensorInfo::GetNumDimensions(), Layer::GetOutputSlot(), OutputSlot::GetOwningLayer(), TensorInfo::GetShape(), OutputSlot::GetTensorInfo(), Layer::GetType(), Graph::InsertNewLayer(), OutputSlot::IsTensorInfoSet(), ConstantLayer::m_LayerOutput, TensorInfo::SetShape(), OutputSlot::SetTensorInfo(), and AddBroadcastReshapeLayerImpl::~AddBroadcastReshapeLayerImpl().

27  {
28  if (std::find(broadcastOps.begin(), broadcastOps.end(), layer.GetType()) != broadcastOps.end())
29  {
30  layer.GetInputSlot(0).GetConnectedOutputSlot()->IsTensorInfoSet();
31  layer.GetInputSlot(1).GetConnectedOutputSlot()->IsTensorInfoSet();
32 
33  const TensorInfo& inputInfo0 = layer.GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo();
34  const TensorInfo& inputInfo1 = layer.GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo();
35 
36  if (inputInfo0.GetNumDimensions() == inputInfo1.GetNumDimensions())
37  {
38  return;
39  }
40 
41  unsigned int reshapeSlot = 1;
42  TensorInfo reshapeInfo = inputInfo1;
43  TensorInfo inputInfo = inputInfo0;
44 
45  if (inputInfo0.GetNumDimensions() < inputInfo1.GetNumDimensions())
46  {
47  reshapeSlot = 0;
48  reshapeInfo = inputInfo0;
49  inputInfo = inputInfo1;
50  }
51 
52  uint32_t numDimensions = inputInfo.GetNumDimensions();
53 
54  std::vector<unsigned> reshapedDim;
55  for (unsigned int i = 0; i < reshapeInfo.GetNumDimensions(); ++i)
56  {
57  reshapedDim.push_back(reshapeInfo.GetShape()[i]);
58  }
59 
60  std::vector<unsigned int> reshapedDimensions(numDimensions, 1);
61  std::copy_backward(reshapedDim.begin(), reshapedDim.end(), reshapedDimensions.end());
62 
63  reshapeInfo.SetShape(armnn::TensorShape{ numDimensions, reshapedDimensions.data() });
64 
65  // If the parent layer is a Constant layer and it is only used once we can short circuit by just
66  // changing the tensor info rather than adding a reshape layer.
67  Layer& parentLayer = layer.GetInputSlot(reshapeSlot).GetConnectedOutputSlot()->GetOwningLayer();
68  if ((parentLayer.GetType() == armnn::LayerType::Constant) &&
69  (parentLayer.GetOutputSlot(0).GetNumConnections() == 1))
70  {
71  ConstantLayer& constantLayer = static_cast<ConstantLayer&>(parentLayer);
72 
73  constantLayer.m_LayerOutput = std::make_unique<ScopedTensorHandle>(
74  ConstTensor(reshapeInfo, constantLayer.m_LayerOutput.get()->GetConstTensor<void>()));
75  constantLayer.GetOutputSlot().SetTensorInfo(reshapeInfo);
76  }
77  else
78  {
79  const std::string layerName = "Reshape_for:" + layer.GetNameStr() + "-" + std::to_string(reshapeSlot);
80  const ReshapeDescriptor descriptor{ reshapeInfo.GetShape() };
81  ReshapeLayer* reshapeLayer =
82  graph.InsertNewLayer<ReshapeLayer>(layer.GetInputSlot(reshapeSlot), descriptor, layerName.c_str());
83  reshapeLayer->GetOutputSlot().SetTensorInfo(reshapeInfo);
84  }
85  }
86  }

The documentation for this class was generated from the following file: