blob: 7c35966f0f45687200c7cc5f7b405eca129e7498 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa014fcda012018-03-09 14:13:49 +00002// 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
Colm Donelan0c479742021-12-10 12:43:54 +00008#include <armnn/backends/TensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +00009
10#include <armnn/Tensor.hpp>
11#include <armnn/Types.hpp>
Jan Eilersbb446e52020-04-02 13:56:54 +010012#include <armnn/utility/PolymorphicDowncast.hpp>
telsoa014fcda012018-03-09 14:13:49 +000013
Matthew Bentham4cefc412019-06-18 16:14:34 +010014#include <reference/RefTensorHandle.hpp>
15
Narumol Prangnawarat7ddbbae2020-03-13 10:26:05 +000016#include <BFloat16.hpp>
Matthew Bentham4cefc412019-06-18 16:14:34 +010017#include <Half.hpp>
telsoa014fcda012018-03-09 14:13:49 +000018
19namespace armnn
20{
21
22////////////////////////////////////////////
23/// float32 helpers
24////////////////////////////////////////////
25
Francis Murtaghbf354142022-08-12 13:54:17 +010026template <typename TensorHandleType = RefTensorHandle>
telsoa014fcda012018-03-09 14:13:49 +000027inline const TensorInfo& GetTensorInfo(const ITensorHandle* tensorHandle)
28{
Matthew Bentham4cefc412019-06-18 16:14:34 +010029 // We know that reference workloads use RefTensorHandles for inputs and outputs
Francis Murtaghbf354142022-08-12 13:54:17 +010030 const TensorHandleType* refTensorHandle =
31 PolymorphicDowncast<const TensorHandleType*>(tensorHandle);
Matthew Bentham4cefc412019-06-18 16:14:34 +010032 return refTensorHandle->GetTensorInfo();
telsoa014fcda012018-03-09 14:13:49 +000033}
34
telsoa014fcda012018-03-09 14:13:49 +000035template <typename DataType, typename PayloadType>
36const DataType* GetInputTensorData(unsigned int idx, const PayloadType& data)
37{
38 const ITensorHandle* tensorHandle = data.m_Inputs[idx];
Matthew Bentham4cefc412019-06-18 16:14:34 +010039 return reinterpret_cast<const DataType*>(tensorHandle->Map());
telsoa014fcda012018-03-09 14:13:49 +000040}
41
42template <typename DataType, typename PayloadType>
43DataType* GetOutputTensorData(unsigned int idx, const PayloadType& data)
44{
Matthew Bentham4cefc412019-06-18 16:14:34 +010045 ITensorHandle* tensorHandle = data.m_Outputs[idx];
46 return reinterpret_cast<DataType*>(tensorHandle->Map());
telsoa014fcda012018-03-09 14:13:49 +000047}
48
Finn Williams01097942021-04-26 12:06:34 +010049template <typename DataType>
50DataType* GetOutputTensorData(ITensorHandle* tensorHandle)
51{
52 return reinterpret_cast<DataType*>(tensorHandle->Map());
53}
54
telsoa014fcda012018-03-09 14:13:49 +000055template <typename PayloadType>
56const float* GetInputTensorDataFloat(unsigned int idx, const PayloadType& data)
57{
58 return GetInputTensorData<float>(idx, data);
59}
60
61template <typename PayloadType>
62float* GetOutputTensorDataFloat(unsigned int idx, const PayloadType& data)
63{
64 return GetOutputTensorData<float>(idx, data);
65}
66
telsoa01c577f2c2018-08-31 09:22:23 +010067template <typename PayloadType>
68const Half* GetInputTensorDataHalf(unsigned int idx, const PayloadType& data)
69{
70 return GetInputTensorData<Half>(idx, data);
71}
72
73template <typename PayloadType>
74Half* GetOutputTensorDataHalf(unsigned int idx, const PayloadType& data)
75{
76 return GetOutputTensorData<Half>(idx, data);
77}
78
Narumol Prangnawarat7ddbbae2020-03-13 10:26:05 +000079template <typename PayloadType>
80const BFloat16* GetInputTensorDataBFloat16(unsigned int idx, const PayloadType& data)
81{
82 return GetInputTensorData<BFloat16>(idx, data);
83}
84
Narumol Prangnawaratea54a012020-03-16 16:36:10 +000085template <typename PayloadType>
86BFloat16* GetOutputTensorDataBFloat16(unsigned int idx, const PayloadType& data)
87{
88 return GetOutputTensorData<BFloat16>(idx, data);
89}
90
telsoa014fcda012018-03-09 14:13:49 +000091////////////////////////////////////////////
92/// u8 helpers
93////////////////////////////////////////////
94
telsoa014fcda012018-03-09 14:13:49 +000095template<typename T>
96std::vector<float> Dequantize(const T* quant, const TensorInfo& info)
97{
98 std::vector<float> ret(info.GetNumElements());
99 for (size_t i = 0; i < info.GetNumElements(); i++)
100 {
101 ret[i] = armnn::Dequantize(quant[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
102 }
103 return ret;
104}
105
Nattapat Chaimanowong8a54ac02019-03-29 15:25:04 +0000106template<typename T>
107inline void Dequantize(const T* inputData, float* outputData, const TensorInfo& info)
108{
109 for (unsigned int i = 0; i < info.GetNumElements(); i++)
110 {
111 outputData[i] = Dequantize<T>(inputData[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
112 }
113}
114
telsoa014fcda012018-03-09 14:13:49 +0000115inline void Quantize(uint8_t* quant, const float* dequant, const TensorInfo& info)
116{
117 for (size_t i = 0; i < info.GetNumElements(); i++)
118 {
119 quant[i] = armnn::Quantize<uint8_t>(dequant[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
120 }
121}
122
123} //namespace armnn