telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "Optimization.hpp" |
arovir01 | 616e775 | 2018-10-01 17:08:59 +0100 | [diff] [blame] | 9 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 10 | #include <armnnUtils/FloatingPointConverter.hpp> |
| 11 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 12 | #include <backendsCommon/TensorHandle.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 13 | |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 14 | #include <armnn/utility/IgnoreUnused.hpp> |
Derek Lamberti | 9be6128 | 2019-12-10 21:42:57 +0000 | [diff] [blame] | 15 | |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 16 | #include <BFloat16.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 17 | #include <Half.hpp> |
arovir01 | 616e775 | 2018-10-01 17:08:59 +0100 | [diff] [blame] | 18 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 19 | namespace armnn |
| 20 | { |
| 21 | namespace optimizations |
| 22 | { |
| 23 | |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 24 | struct BFloat16ToFloat32 |
| 25 | { |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 26 | static void Func(std::shared_ptr<ConstTensorHandle>& handle) |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 27 | { |
| 28 | const TensorInfo& info = handle->GetTensorInfo(); |
| 29 | |
| 30 | if (info.GetDataType() == DataType::BFloat16) |
| 31 | { |
| 32 | std::vector<float> newValues(info.GetNumElements()); |
| 33 | |
Finn Williams | 4422cec | 2021-03-22 17:51:06 +0000 | [diff] [blame] | 34 | armnnUtils::FloatingPointConverter::ConvertBFloat16ToFloat32(handle->GetConstTensor<BFloat16>(), |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 35 | info.GetNumElements(), |
| 36 | newValues.data()); |
| 37 | |
| 38 | TensorInfo newInfo(info.GetShape(), DataType::Float32); |
| 39 | ConstTensor newInput(newInfo, newValues); |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 40 | handle.reset(new ScopedTensorHandle(newInput)); |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | }; |
| 44 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 45 | struct Float16ToFloat32 |
| 46 | { |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 47 | static void Func(std::shared_ptr<ConstTensorHandle>& handle) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | { |
| 49 | const TensorInfo& info = handle->GetTensorInfo(); |
| 50 | |
| 51 | if (info.GetDataType() == DataType::Float16) |
| 52 | { |
| 53 | std::vector<float> newValues(info.GetNumElements()); |
| 54 | |
Finn Williams | 4422cec | 2021-03-22 17:51:06 +0000 | [diff] [blame] | 55 | armnnUtils::FloatingPointConverter::ConvertFloat16To32(handle->GetConstTensor<Half>(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 56 | info.GetNumElements(), |
| 57 | newValues.data()); |
| 58 | |
| 59 | TensorInfo newInfo(info.GetShape(), DataType::Float32); |
| 60 | ConstTensor newInput(newInfo, newValues); |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 61 | handle.reset(new ScopedTensorHandle(newInput)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | }; |
| 65 | |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 66 | struct Float32ToBFloat16 |
| 67 | { |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 68 | static void Func(std::shared_ptr<ConstTensorHandle>& handle) |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 69 | { |
| 70 | const TensorInfo& info = handle->GetTensorInfo(); |
| 71 | |
| 72 | if (info.GetDataType() == DataType::Float32) |
| 73 | { |
| 74 | std::vector<BFloat16> newValues(info.GetNumElements()); |
| 75 | |
Finn Williams | 4422cec | 2021-03-22 17:51:06 +0000 | [diff] [blame] | 76 | armnnUtils::FloatingPointConverter::ConvertFloat32ToBFloat16(handle->GetConstTensor<float>(), |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 77 | info.GetNumElements(), |
| 78 | newValues.data()); |
| 79 | |
| 80 | TensorInfo newInfo(info.GetShape(), DataType::BFloat16); |
| 81 | ConstTensor newInput(newInfo, newValues); |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 82 | handle.reset(new ScopedTensorHandle(newInput)); |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | }; |
| 86 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 87 | struct Float32ToFloat16 |
| 88 | { |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 89 | static void Func(std::shared_ptr<ConstTensorHandle>& handle) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 90 | { |
| 91 | const TensorInfo& info = handle->GetTensorInfo(); |
| 92 | |
| 93 | if (info.GetDataType() == DataType::Float32) |
| 94 | { |
| 95 | std::vector<Half> newValues(info.GetNumElements()); |
| 96 | |
Finn Williams | 4422cec | 2021-03-22 17:51:06 +0000 | [diff] [blame] | 97 | armnnUtils::FloatingPointConverter::ConvertFloat32To16(handle->GetConstTensor<float>(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 98 | info.GetNumElements(), |
| 99 | newValues.data()); |
| 100 | |
| 101 | TensorInfo newInfo(info.GetShape(), DataType::Float16); |
| 102 | ConstTensor newInput(newInfo, newValues); |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 103 | handle.reset(new ScopedTensorHandle(newInput)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | template<typename Converter, typename Predicate> |
| 109 | class ConvertConstants : public Optimization |
| 110 | { |
| 111 | public: |
| 112 | ConvertConstants() = default; |
| 113 | ConvertConstants(const ConvertConstants&) = default; |
| 114 | virtual ~ConvertConstants() = default; |
| 115 | |
| 116 | void Run(Graph& graph, Layer& layer) const override |
| 117 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 118 | IgnoreUnused(graph); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 119 | if (Predicate::Test(layer)) |
| 120 | { |
| 121 | layer.OperateOnConstantTensors(Converter::Func); |
| 122 | } |
| 123 | } |
| 124 | protected: |
| 125 | }; |
| 126 | |
| 127 | struct IsFloat32Layer |
| 128 | { |
| 129 | static bool Test(const Layer& layer) |
| 130 | { |
| 131 | return layer.GetDataType() == DataType::Float32; |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | struct IsFloat16Layer |
| 136 | { |
| 137 | static bool Test(const Layer& layer) |
| 138 | { |
| 139 | return layer.GetDataType() == DataType::Float16; |
| 140 | } |
| 141 | }; |
| 142 | |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 143 | struct IsBFloat16Layer |
| 144 | { |
| 145 | static bool Test(const Layer& layer) |
| 146 | { |
| 147 | return layer.GetDataType() == DataType::BFloat16; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | using ConvertConstantsBFloatToFloat = ConvertConstants<BFloat16ToFloat32, IsFloat32Layer>; |
| 152 | using ConvertConstantsFloatToBFloat = ConvertConstants<Float32ToBFloat16, IsBFloat16Layer>; |
| 153 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 154 | using ConvertConstantsHalfToFloat = ConvertConstants<Float16ToFloat32, IsFloat32Layer>; |
| 155 | using ConvertConstantsFloatToHalf = ConvertConstants<Float32ToFloat16, IsFloat16Layer>; |
| 156 | |
| 157 | } //namespace optimizations |
| 158 | } //namespace armnn |