ArmNN
 24.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
armnn::InsertConvertFp16ToFp32LayersBefore
std::vector< ConvertFp16ToFp32Layer * > InsertConvertFp16ToFp32LayersBefore(Graph &graph, Layer &layer, bool expectCorrectInputType)
Definition: NetworkUtils.cpp:40
armnn::LayerType::ConvertFp16ToFp32
@ ConvertFp16ToFp32
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::TensorInfo::SetDataType
void SetDataType(DataType type)
Definition: Tensor.hpp:201
armnn::DataType::Float32
@ Float32
NetworkUtils.hpp
armnn::Layer::GetInputSlots
const std::vector< InputSlot > & GetInputSlots() const
Definition: Layer.hpp:258
Optimization.hpp
armnn::LayerType::ConvertFp32ToFp16
@ ConvertFp32ToFp16
armnn::Layer
Definition: Layer.hpp:230
armnn::DataType::Float16
@ Float16
armnn::optimizations::ConvertFp32NetworkToFp16Impl::Run
void Run(Graph &graph, Layer &layer) const
Definition: ConvertFp32NetworkToFp16.hpp:18
armnn::InsertConvertFp32ToFp16LayersAfter
std::vector< ConvertFp32ToFp16Layer * > InsertConvertFp32ToFp16LayersAfter(Graph &graph, Layer &layer)
Definition: NetworkUtils.cpp:79
armnn::Layer::EndInputSlots
std::vector< InputSlot >::iterator EndInputSlots()
Definition: Layer.hpp:263
armnn::Layer::BeginInputSlots
std::vector< InputSlot >::iterator BeginInputSlots()
Definition: Layer.hpp:262
armnn::LayerType::DetectionPostProcess
@ DetectionPostProcess
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:200
armnn::OptimizeForType
Definition: Optimization.hpp:67
armnn::Layer::GetDataType
DataType GetDataType() const
Definition: Layer.cpp:326
armnn::optimizations::ConvertFp32NetworkToFp16Impl
Definition: ConvertFp32NetworkToFp16.hpp:15
armnn::Layer::GetType
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:286
armnn::Layer::BeginOutputSlots
std::vector< OutputSlot >::iterator BeginOutputSlots()
Definition: Layer.hpp:266
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::LayerType::Input
@ Input
armnn::optimizations::ConvertFp32NetworkToFp16Impl::ConvertFp32NetworkToFp16Impl
ConvertFp32NetworkToFp16Impl()=default
armnn::optimizations::ConvertFp32NetworkToFp16Impl::~ConvertFp32NetworkToFp16Impl
~ConvertFp32NetworkToFp16Impl()=default
armnn::Layer::EndOutputSlots
std::vector< OutputSlot >::iterator EndOutputSlots()
Definition: Layer.hpp:267
armnn::Graph
Definition: Graph.hpp:30
armnn::LayerType::Output
@ Output