Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 2 | // Copyright © 2017-2023 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 8 | #include <armnn/backends/TensorHandle.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | |
| 10 | #include <armnn/Tensor.hpp> |
| 11 | #include <armnn/Types.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 12 | #include <armnn/utility/PolymorphicDowncast.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 14 | #include <reference/RefTensorHandle.hpp> |
| 15 | |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 16 | #include <BFloat16.hpp> |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 17 | #include <Half.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | |
| 19 | namespace armnn |
| 20 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 21 | /// Creates a profiling event that uses GetGuid() and GetName() from the calling class |
| 22 | #define ARMNN_SCOPED_PROFILING_EVENT_REF_NAME_GUID(label) \ |
| 23 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(armnn::Compute::CpuRef, \ |
| 24 | this->GetGuid(), \ |
| 25 | this->GetName() + "_" + label, \ |
| 26 | armnn::WallClockTimer()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 27 | |
| 28 | //////////////////////////////////////////// |
| 29 | /// float32 helpers |
| 30 | //////////////////////////////////////////// |
| 31 | |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 32 | template <typename TensorHandleType = RefTensorHandle> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 33 | inline const TensorInfo& GetTensorInfo(const ITensorHandle* tensorHandle) |
| 34 | { |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 35 | // We know that reference workloads use RefTensorHandles for inputs and outputs |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 36 | const TensorHandleType* refTensorHandle = |
| 37 | PolymorphicDowncast<const TensorHandleType*>(tensorHandle); |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 38 | return refTensorHandle->GetTensorInfo(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 39 | } |
| 40 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 41 | template <typename DataType, typename PayloadType> |
| 42 | const DataType* GetInputTensorData(unsigned int idx, const PayloadType& data) |
| 43 | { |
| 44 | const ITensorHandle* tensorHandle = data.m_Inputs[idx]; |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 45 | return reinterpret_cast<const DataType*>(tensorHandle->Map()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | template <typename DataType, typename PayloadType> |
| 49 | DataType* GetOutputTensorData(unsigned int idx, const PayloadType& data) |
| 50 | { |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 51 | ITensorHandle* tensorHandle = data.m_Outputs[idx]; |
| 52 | return reinterpret_cast<DataType*>(tensorHandle->Map()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 55 | template <typename DataType> |
| 56 | DataType* GetOutputTensorData(ITensorHandle* tensorHandle) |
| 57 | { |
| 58 | return reinterpret_cast<DataType*>(tensorHandle->Map()); |
| 59 | } |
| 60 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | template <typename PayloadType> |
| 62 | const float* GetInputTensorDataFloat(unsigned int idx, const PayloadType& data) |
| 63 | { |
| 64 | return GetInputTensorData<float>(idx, data); |
| 65 | } |
| 66 | |
| 67 | template <typename PayloadType> |
| 68 | float* GetOutputTensorDataFloat(unsigned int idx, const PayloadType& data) |
| 69 | { |
| 70 | return GetOutputTensorData<float>(idx, data); |
| 71 | } |
| 72 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | template <typename PayloadType> |
| 74 | const Half* GetInputTensorDataHalf(unsigned int idx, const PayloadType& data) |
| 75 | { |
| 76 | return GetInputTensorData<Half>(idx, data); |
| 77 | } |
| 78 | |
| 79 | template <typename PayloadType> |
| 80 | Half* GetOutputTensorDataHalf(unsigned int idx, const PayloadType& data) |
| 81 | { |
| 82 | return GetOutputTensorData<Half>(idx, data); |
| 83 | } |
| 84 | |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 85 | template <typename PayloadType> |
| 86 | const BFloat16* GetInputTensorDataBFloat16(unsigned int idx, const PayloadType& data) |
| 87 | { |
| 88 | return GetInputTensorData<BFloat16>(idx, data); |
| 89 | } |
| 90 | |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 91 | template <typename PayloadType> |
| 92 | BFloat16* GetOutputTensorDataBFloat16(unsigned int idx, const PayloadType& data) |
| 93 | { |
| 94 | return GetOutputTensorData<BFloat16>(idx, data); |
| 95 | } |
| 96 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | //////////////////////////////////////////// |
| 98 | /// u8 helpers |
| 99 | //////////////////////////////////////////// |
| 100 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | template<typename T> |
| 102 | std::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 Chaimanowong | 8a54ac0 | 2019-03-29 15:25:04 +0000 | [diff] [blame] | 112 | template<typename T> |
| 113 | inline 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 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 121 | inline 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 |