blob: 3659617b7f2ee26cee7bf4f0e3f2a905fd703e96 [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
James Conroy1f58f032021-04-27 17:13:27 +01008#include <backendsCommon/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
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 =
Jan Eilersbb446e52020-04-02 13:56:54 +010030 PolymorphicDowncast<const RefTensorHandle*>(tensorHandle);
Matthew Bentham4cefc412019-06-18 16:14:34 +010031 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
Finn Williams01097942021-04-26 12:06:34 +010048template <typename DataType>
49DataType* GetOutputTensorData(ITensorHandle* tensorHandle)
50{
51 return reinterpret_cast<DataType*>(tensorHandle->Map());
52}
53
telsoa014fcda012018-03-09 14:13:49 +000054template <typename PayloadType>
55const float* GetInputTensorDataFloat(unsigned int idx, const PayloadType& data)
56{
57 return GetInputTensorData<float>(idx, data);
58}
59
60template <typename PayloadType>
61float* GetOutputTensorDataFloat(unsigned int idx, const PayloadType& data)
62{
63 return GetOutputTensorData<float>(idx, data);
64}
65
telsoa01c577f2c2018-08-31 09:22:23 +010066template <typename PayloadType>
67const Half* GetInputTensorDataHalf(unsigned int idx, const PayloadType& data)
68{
69 return GetInputTensorData<Half>(idx, data);
70}
71
72template <typename PayloadType>
73Half* GetOutputTensorDataHalf(unsigned int idx, const PayloadType& data)
74{
75 return GetOutputTensorData<Half>(idx, data);
76}
77
Narumol Prangnawarat7ddbbae2020-03-13 10:26:05 +000078template <typename PayloadType>
79const BFloat16* GetInputTensorDataBFloat16(unsigned int idx, const PayloadType& data)
80{
81 return GetInputTensorData<BFloat16>(idx, data);
82}
83
Narumol Prangnawaratea54a012020-03-16 16:36:10 +000084template <typename PayloadType>
85BFloat16* GetOutputTensorDataBFloat16(unsigned int idx, const PayloadType& data)
86{
87 return GetOutputTensorData<BFloat16>(idx, data);
88}
89
telsoa014fcda012018-03-09 14:13:49 +000090////////////////////////////////////////////
91/// u8 helpers
92////////////////////////////////////////////
93
telsoa014fcda012018-03-09 14:13:49 +000094template<typename T>
95std::vector<float> Dequantize(const T* quant, const TensorInfo& info)
96{
97 std::vector<float> ret(info.GetNumElements());
98 for (size_t i = 0; i < info.GetNumElements(); i++)
99 {
100 ret[i] = armnn::Dequantize(quant[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
101 }
102 return ret;
103}
104
Nattapat Chaimanowong8a54ac02019-03-29 15:25:04 +0000105template<typename T>
106inline void Dequantize(const T* inputData, float* outputData, const TensorInfo& info)
107{
108 for (unsigned int i = 0; i < info.GetNumElements(); i++)
109 {
110 outputData[i] = Dequantize<T>(inputData[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
111 }
112}
113
telsoa014fcda012018-03-09 14:13:49 +0000114inline void Quantize(uint8_t* quant, const float* dequant, const TensorInfo& info)
115{
116 for (size_t i = 0; i < info.GetNumElements(); i++)
117 {
118 quant[i] = armnn::Quantize<uint8_t>(dequant[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
119 }
120}
121
122} //namespace armnn