blob: 69713145c0eb99e8cc931a409246ce0411632996 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
6#pragma once
7
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00008#include <backendsCommon/CpuTensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +00009
10#include <armnn/Tensor.hpp>
11#include <armnn/Types.hpp>
12
Matthew Bentham4cefc412019-06-18 16:14:34 +010013#include <reference/RefTensorHandle.hpp>
14
Narumol Prangnawarat7ddbbae2020-03-13 10:26:05 +000015#include <BFloat16.hpp>
Matthew Bentham4cefc412019-06-18 16:14:34 +010016#include <Half.hpp>
telsoa014fcda012018-03-09 14:13:49 +000017#include <boost/polymorphic_cast.hpp>
18
19namespace armnn
20{
21
22////////////////////////////////////////////
23/// float32 helpers
24////////////////////////////////////////////
25
26inline const TensorInfo& GetTensorInfo(const ITensorHandle* tensorHandle)
27{
Matthew Bentham4cefc412019-06-18 16:14:34 +010028 // We know that reference workloads use RefTensorHandles for inputs and outputs
29 const RefTensorHandle* refTensorHandle =
30 boost::polymorphic_downcast<const RefTensorHandle*>(tensorHandle);
31 return refTensorHandle->GetTensorInfo();
telsoa014fcda012018-03-09 14:13:49 +000032}
33
telsoa014fcda012018-03-09 14:13:49 +000034template <typename DataType, typename PayloadType>
35const DataType* GetInputTensorData(unsigned int idx, const PayloadType& data)
36{
37 const ITensorHandle* tensorHandle = data.m_Inputs[idx];
Matthew Bentham4cefc412019-06-18 16:14:34 +010038 return reinterpret_cast<const DataType*>(tensorHandle->Map());
telsoa014fcda012018-03-09 14:13:49 +000039}
40
41template <typename DataType, typename PayloadType>
42DataType* GetOutputTensorData(unsigned int idx, const PayloadType& data)
43{
Matthew Bentham4cefc412019-06-18 16:14:34 +010044 ITensorHandle* tensorHandle = data.m_Outputs[idx];
45 return reinterpret_cast<DataType*>(tensorHandle->Map());
telsoa014fcda012018-03-09 14:13:49 +000046}
47
48template <typename PayloadType>
49const float* GetInputTensorDataFloat(unsigned int idx, const PayloadType& data)
50{
51 return GetInputTensorData<float>(idx, data);
52}
53
54template <typename PayloadType>
55float* GetOutputTensorDataFloat(unsigned int idx, const PayloadType& data)
56{
57 return GetOutputTensorData<float>(idx, data);
58}
59
telsoa01c577f2c2018-08-31 09:22:23 +010060template <typename PayloadType>
61const Half* GetInputTensorDataHalf(unsigned int idx, const PayloadType& data)
62{
63 return GetInputTensorData<Half>(idx, data);
64}
65
66template <typename PayloadType>
67Half* GetOutputTensorDataHalf(unsigned int idx, const PayloadType& data)
68{
69 return GetOutputTensorData<Half>(idx, data);
70}
71
Narumol Prangnawarat7ddbbae2020-03-13 10:26:05 +000072template <typename PayloadType>
73const BFloat16* GetInputTensorDataBFloat16(unsigned int idx, const PayloadType& data)
74{
75 return GetInputTensorData<BFloat16>(idx, data);
76}
77
telsoa014fcda012018-03-09 14:13:49 +000078////////////////////////////////////////////
79/// u8 helpers
80////////////////////////////////////////////
81
telsoa014fcda012018-03-09 14:13:49 +000082template<typename T>
83std::vector<float> Dequantize(const T* quant, const TensorInfo& info)
84{
85 std::vector<float> ret(info.GetNumElements());
86 for (size_t i = 0; i < info.GetNumElements(); i++)
87 {
88 ret[i] = armnn::Dequantize(quant[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
89 }
90 return ret;
91}
92
Nattapat Chaimanowong8a54ac02019-03-29 15:25:04 +000093template<typename T>
94inline void Dequantize(const T* inputData, float* outputData, const TensorInfo& info)
95{
96 for (unsigned int i = 0; i < info.GetNumElements(); i++)
97 {
98 outputData[i] = Dequantize<T>(inputData[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
99 }
100}
101
telsoa014fcda012018-03-09 14:13:49 +0000102inline void Quantize(uint8_t* quant, const float* dequant, const TensorInfo& info)
103{
104 for (size_t i = 0; i < info.GetNumElements(); i++)
105 {
106 quant[i] = armnn::Quantize<uint8_t>(dequant[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
107 }
108}
109
110} //namespace armnn