ArmNN
 20.02
ConvertConstants.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "Optimization.hpp"
9 
11 
13 
15 
16 #include <Half.hpp>
17 
18 namespace armnn
19 {
20 namespace optimizations
21 {
22 
24 {
25  static void Func(std::unique_ptr<ScopedCpuTensorHandle>& handle)
26  {
27  const TensorInfo& info = handle->GetTensorInfo();
28 
29  if (info.GetDataType() == DataType::Float16)
30  {
31  std::vector<float> newValues(info.GetNumElements());
32 
34  info.GetNumElements(),
35  newValues.data());
36 
37  TensorInfo newInfo(info.GetShape(), DataType::Float32);
38  ConstTensor newInput(newInfo, newValues);
39  handle.reset(new ScopedCpuTensorHandle(newInput));
40  }
41  }
42 };
43 
45 {
46  static void Func(std::unique_ptr<ScopedCpuTensorHandle>& handle)
47  {
48  const TensorInfo& info = handle->GetTensorInfo();
49 
50  if (info.GetDataType() == DataType::Float32)
51  {
52  std::vector<Half> newValues(info.GetNumElements());
53 
55  info.GetNumElements(),
56  newValues.data());
57 
58  TensorInfo newInfo(info.GetShape(), DataType::Float16);
59  ConstTensor newInput(newInfo, newValues);
60  handle.reset(new ScopedCpuTensorHandle(newInput));
61  }
62  }
63 };
64 
65 template<typename Converter, typename Predicate>
67 {
68 public:
69  ConvertConstants() = default;
70  ConvertConstants(const ConvertConstants&) = default;
71  virtual ~ConvertConstants() = default;
72 
73  void Run(Graph& graph, Layer& layer) const override
74  {
75  IgnoreUnused(graph);
76  if (Predicate::Test(layer))
77  {
78  layer.OperateOnConstantTensors(Converter::Func);
79  }
80  }
81 protected:
82 };
83 
85 {
86  static bool Test(const Layer& layer)
87  {
88  return layer.GetDataType() == DataType::Float32;
89  }
90 };
91 
93 {
94  static bool Test(const Layer& layer)
95  {
96  return layer.GetDataType() == DataType::Float16;
97  }
98 };
99 
102 
103 } //namespace optimizations
104 } //namespace armnn
static bool Test(const Layer &layer)
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
static bool Test(const Layer &layer)
void OperateOnConstantTensors(Op op)
Definition: Layer.hpp:292
static void Func(std::unique_ptr< ScopedCpuTensorHandle > &handle)
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
static void Func(std::unique_ptr< ScopedCpuTensorHandle > &handle)
DataType GetDataType() const
Definition: Tensor.hpp:95
static void ConvertFloat32To16(const float *srcFloat32Buffer, size_t numElements, void *dstFloat16Buffer)
Converts a buffer of FP32 values to FP16, and stores in the given dstFloat16Buffer.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:199
static void ConvertFloat16To32(const void *srcFloat16Buffer, size_t numElements, float *dstFloat32Buffer)
void Run(Graph &graph, Layer &layer) const override
DataType GetDataType() const
Definition: Layer.cpp:273
half_float::half Half
Definition: Half.hpp:16
unsigned int GetNumElements() const
Definition: Tensor.hpp:93