blob: c840887fc04bc0841dd4523cf80e08ce2c686344 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Mike Kelly7cbe7812023-07-25 17:37:33 +01002// Copyright © 2017-2023 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{
Mike Kelly7cbe7812023-07-25 17:37:33 +010021/// Creates a profiling event that uses GetGuid() and GetName() from the calling class
22#define ARMNN_SCOPED_PROFILING_EVENT_REF_NAME_GUID(label) \
23ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(armnn::Compute::CpuRef, \
24 this->GetGuid(), \
25 this->GetName() + "_" + label, \
26 armnn::WallClockTimer())
telsoa014fcda012018-03-09 14:13:49 +000027
28////////////////////////////////////////////
29/// float32 helpers
30////////////////////////////////////////////
31
Francis Murtagh9270d9e2022-08-12 13:54:17 +010032template <typename TensorHandleType = RefTensorHandle>
telsoa014fcda012018-03-09 14:13:49 +000033inline const TensorInfo& GetTensorInfo(const ITensorHandle* tensorHandle)
34{
Matthew Bentham4cefc412019-06-18 16:14:34 +010035 // We know that reference workloads use RefTensorHandles for inputs and outputs
Francis Murtagh9270d9e2022-08-12 13:54:17 +010036 const TensorHandleType* refTensorHandle =
37 PolymorphicDowncast<const TensorHandleType*>(tensorHandle);
Matthew Bentham4cefc412019-06-18 16:14:34 +010038 return refTensorHandle->GetTensorInfo();
telsoa014fcda012018-03-09 14:13:49 +000039}
40
telsoa014fcda012018-03-09 14:13:49 +000041template <typename DataType, typename PayloadType>
42const DataType* GetInputTensorData(unsigned int idx, const PayloadType& data)
43{
44 const ITensorHandle* tensorHandle = data.m_Inputs[idx];
Matthew Bentham4cefc412019-06-18 16:14:34 +010045 return reinterpret_cast<const DataType*>(tensorHandle->Map());
telsoa014fcda012018-03-09 14:13:49 +000046}
47
48template <typename DataType, typename PayloadType>
49DataType* GetOutputTensorData(unsigned int idx, const PayloadType& data)
50{
Matthew Bentham4cefc412019-06-18 16:14:34 +010051 ITensorHandle* tensorHandle = data.m_Outputs[idx];
52 return reinterpret_cast<DataType*>(tensorHandle->Map());
telsoa014fcda012018-03-09 14:13:49 +000053}
54
Finn Williams01097942021-04-26 12:06:34 +010055template <typename DataType>
56DataType* GetOutputTensorData(ITensorHandle* tensorHandle)
57{
58 return reinterpret_cast<DataType*>(tensorHandle->Map());
59}
60
telsoa014fcda012018-03-09 14:13:49 +000061template <typename PayloadType>
62const float* GetInputTensorDataFloat(unsigned int idx, const PayloadType& data)
63{
64 return GetInputTensorData<float>(idx, data);
65}
66
67template <typename PayloadType>
68float* GetOutputTensorDataFloat(unsigned int idx, const PayloadType& data)
69{
70 return GetOutputTensorData<float>(idx, data);
71}
72
telsoa01c577f2c2018-08-31 09:22:23 +010073template <typename PayloadType>
74const Half* GetInputTensorDataHalf(unsigned int idx, const PayloadType& data)
75{
76 return GetInputTensorData<Half>(idx, data);
77}
78
79template <typename PayloadType>
80Half* GetOutputTensorDataHalf(unsigned int idx, const PayloadType& data)
81{
82 return GetOutputTensorData<Half>(idx, data);
83}
84
Narumol Prangnawarat7ddbbae2020-03-13 10:26:05 +000085template <typename PayloadType>
86const BFloat16* GetInputTensorDataBFloat16(unsigned int idx, const PayloadType& data)
87{
88 return GetInputTensorData<BFloat16>(idx, data);
89}
90
Narumol Prangnawaratea54a012020-03-16 16:36:10 +000091template <typename PayloadType>
92BFloat16* GetOutputTensorDataBFloat16(unsigned int idx, const PayloadType& data)
93{
94 return GetOutputTensorData<BFloat16>(idx, data);
95}
96
telsoa014fcda012018-03-09 14:13:49 +000097////////////////////////////////////////////
98/// u8 helpers
99////////////////////////////////////////////
100
telsoa014fcda012018-03-09 14:13:49 +0000101template<typename T>
102std::vector<float> Dequantize(const T* quant, const TensorInfo& info)
103{
104 std::vector<float> ret(info.GetNumElements());
105 for (size_t i = 0; i < info.GetNumElements(); i++)
106 {
107 ret[i] = armnn::Dequantize(quant[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
108 }
109 return ret;
110}
111
Nattapat Chaimanowong8a54ac02019-03-29 15:25:04 +0000112template<typename T>
113inline void Dequantize(const T* inputData, float* outputData, const TensorInfo& info)
114{
115 for (unsigned int i = 0; i < info.GetNumElements(); i++)
116 {
117 outputData[i] = Dequantize<T>(inputData[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
118 }
119}
120
telsoa014fcda012018-03-09 14:13:49 +0000121inline void Quantize(uint8_t* quant, const float* dequant, const TensorInfo& info)
122{
123 for (size_t i = 0; i < info.GetNumElements(); i++)
124 {
125 quant[i] = armnn::Quantize<uint8_t>(dequant[i], info.GetQuantizationScale(), info.GetQuantizationOffset());
126 }
127}
128
129} //namespace armnn