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 | |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 10 | #include <backends/CpuTensorHandle.hpp> |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 11 | #include <FloatingPointConverter.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | |
arovir01 | 616e775 | 2018-10-01 17:08:59 +0100 | [diff] [blame^] | 13 | #include <armnnUtils/Half.hpp> |
| 14 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | namespace armnn |
| 16 | { |
| 17 | namespace optimizations |
| 18 | { |
| 19 | |
| 20 | struct Float16ToFloat32 |
| 21 | { |
| 22 | static void Func(std::unique_ptr<ScopedCpuTensorHandle>& handle) |
| 23 | { |
| 24 | const TensorInfo& info = handle->GetTensorInfo(); |
| 25 | |
| 26 | if (info.GetDataType() == DataType::Float16) |
| 27 | { |
| 28 | std::vector<float> newValues(info.GetNumElements()); |
| 29 | |
| 30 | armnnUtils::FloatingPointConverter::ConvertFloat16To32(handle->GetTensor<Half>(), |
| 31 | info.GetNumElements(), |
| 32 | newValues.data()); |
| 33 | |
| 34 | TensorInfo newInfo(info.GetShape(), DataType::Float32); |
| 35 | ConstTensor newInput(newInfo, newValues); |
| 36 | handle.reset(new ScopedCpuTensorHandle(newInput)); |
| 37 | } |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | struct Float32ToFloat16 |
| 42 | { |
| 43 | static void Func(std::unique_ptr<ScopedCpuTensorHandle>& handle) |
| 44 | { |
| 45 | const TensorInfo& info = handle->GetTensorInfo(); |
| 46 | |
| 47 | if (info.GetDataType() == DataType::Float32) |
| 48 | { |
| 49 | std::vector<Half> newValues(info.GetNumElements()); |
| 50 | |
| 51 | armnnUtils::FloatingPointConverter::ConvertFloat32To16(handle->GetTensor<float>(), |
| 52 | info.GetNumElements(), |
| 53 | newValues.data()); |
| 54 | |
| 55 | TensorInfo newInfo(info.GetShape(), DataType::Float16); |
| 56 | ConstTensor newInput(newInfo, newValues); |
| 57 | handle.reset(new ScopedCpuTensorHandle(newInput)); |
| 58 | } |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | template<typename Converter, typename Predicate> |
| 63 | class ConvertConstants : public Optimization |
| 64 | { |
| 65 | public: |
| 66 | ConvertConstants() = default; |
| 67 | ConvertConstants(const ConvertConstants&) = default; |
| 68 | virtual ~ConvertConstants() = default; |
| 69 | |
| 70 | void Run(Graph& graph, Layer& layer) const override |
| 71 | { |
| 72 | if (Predicate::Test(layer)) |
| 73 | { |
| 74 | layer.OperateOnConstantTensors(Converter::Func); |
| 75 | } |
| 76 | } |
| 77 | protected: |
| 78 | }; |
| 79 | |
| 80 | struct IsFloat32Layer |
| 81 | { |
| 82 | static bool Test(const Layer& layer) |
| 83 | { |
| 84 | return layer.GetDataType() == DataType::Float32; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | struct IsFloat16Layer |
| 89 | { |
| 90 | static bool Test(const Layer& layer) |
| 91 | { |
| 92 | return layer.GetDataType() == DataType::Float16; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | using ConvertConstantsHalfToFloat = ConvertConstants<Float16ToFloat32, IsFloat32Layer>; |
| 97 | using ConvertConstantsFloatToHalf = ConvertConstants<Float32ToFloat16, IsFloat16Layer>; |
| 98 | |
| 99 | } //namespace optimizations |
| 100 | } //namespace armnn |