ArmNN
 21.02
ConvertFp32NetworkToFp16.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "Optimization.hpp"
8 #include "NetworkUtils.hpp"
9 
10 namespace armnn
11 {
12 namespace optimizations
13 {
14 
16 {
17 public:
18  void Run(Graph& graph, Layer& layer) const
19  {
20  if(layer.GetType() == LayerType::Input)
21  {
22  // if the outputs of this layer are DataType::Float32
23  // add a ConvertFloat32ToFloat16 layer after each of the outputs
24  if (layer.GetDataType() == DataType::Float32)
25  {
27  }
28  }
29  else if (layer.GetType() == LayerType::Output)
30  {
31  // For DetectionPostProcess Layer output is always Float32 regardless of input type
32  Layer& connectedLayer = layer.GetInputSlots()[0].GetConnectedOutputSlot()->GetOwningLayer();
33  if (connectedLayer.GetType() != LayerType::DetectionPostProcess)
34  {
35  // if the inputs of this layer are DataType::Float32
36  // add a ConvertFloat16ToFloat32 layer before each of the inputs
37  if (layer.GetDataType() == DataType::Float32)
38  {
39  // NOTE: We need to call InsertConvertFp16ToFp32LayersBefore with expectCorrectInputType = false
40  // here, otherwise it will expect the inputs to be DataType::Float16
41  InsertConvertFp16ToFp32LayersBefore(graph, layer, false);
42  }
43  }
44  }
46  {
47  // if the inputs/outputs of this layer are DataType::Float32
48  // change the data type for all inputs and outputs to DataType::Float16
49  for (auto&& input = layer.BeginInputSlots(); input != layer.EndInputSlots(); ++input)
50  {
51  // if it is connected to OutputSlot of the InputLayer do not change the DataType of connection
52  // InputSlots of the current layer will be updated when conversion layer is inserted after InputLayer
53  Layer& base = input->GetConnectedOutputSlot()->GetOwningLayer();
54  if (base.GetType() != LayerType::Input)
55  {
56  TensorInfo convertInfo = input->GetConnection()->GetTensorInfo();
57  if (convertInfo.GetDataType() == DataType::Float32)
58  {
59  convertInfo.SetDataType(DataType::Float16);
60  input->GetConnection()->SetTensorInfo(convertInfo);
61  }
62  }
63  }
64 
65  // For DetectionPostProcess Layer output is always Float32 regardless of input type
67  {
68  // change outputs to DataType::Float16
69  for (auto&& output = layer.BeginOutputSlots(); output != layer.EndOutputSlots(); ++output)
70  {
71  TensorInfo convertInfo = output->GetTensorInfo();
72  if (convertInfo.GetDataType() == DataType::Float32)
73  {
74  convertInfo.SetDataType(DataType::Float16);
75  output->SetTensorInfo(convertInfo);
76  }
77  }
78  }
79  }
80  }
81 
82 protected:
83  ConvertFp32NetworkToFp16Impl() = default;
85 };
86 
88 
89 } // namespace optimizations
90 } // namespace armnn
std::vector< InputSlot >::iterator EndInputSlots()
Definition: Layer.hpp:242
std::vector< ConvertFp32ToFp16Layer * > InsertConvertFp32ToFp16LayersAfter(Graph &graph, Layer &layer)
std::vector< ConvertFp16ToFp32Layer * > InsertConvertFp16ToFp32LayersBefore(Graph &graph, Layer &layer, bool expectCorrectInputType)
Copyright (c) 2021 ARM Limited and Contributors.
const std::vector< InputSlot > & GetInputSlots() const
Definition: Layer.hpp:237
std::vector< InputSlot >::iterator BeginInputSlots()
Definition: Layer.hpp:241
DataType GetDataType() const
Definition: Tensor.hpp:194
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:265
void SetDataType(DataType type)
Definition: Tensor.hpp:195
std::vector< OutputSlot >::iterator BeginOutputSlots()
Definition: Layer.hpp:245
std::vector< OutputSlot >::iterator EndOutputSlots()
Definition: Layer.hpp:246
DataType GetDataType() const
Definition: Layer.cpp:283