Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include "NeonConvertFp32ToFp16Workload.hpp" |
| 7 | |
Matthew Bentham | 34336f9 | 2023-04-27 12:13:50 +0000 | [diff] [blame] | 8 | #include <arm_compute/runtime/NEON/functions/NECast.h> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | #include <Half.hpp> |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 10 | #include <Profiling.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 11 | |
| 12 | #include <armnnUtils/FloatingPointConverter.hpp> |
| 13 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 14 | #include <backendsCommon/WorkloadUtils.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | |
Matthew Bentham | 34336f9 | 2023-04-27 12:13:50 +0000 | [diff] [blame] | 16 | static constexpr arm_compute::ConvertPolicy g_AclConvertPolicy = arm_compute::ConvertPolicy::SATURATE; |
| 17 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 18 | namespace armnn |
| 19 | { |
| 20 | |
Matthew Bentham | 34336f9 | 2023-04-27 12:13:50 +0000 | [diff] [blame] | 21 | arm_compute::Status NeonConvertFp32ToFp16WorkloadValidate(const TensorInfo& input, const TensorInfo& output) |
| 22 | { |
| 23 | // Fallback to portable software implementation if Compute Library NECast won't work, so |
| 24 | // this method always returns success |
| 25 | |
| 26 | armnn::IgnoreUnused(input); |
| 27 | armnn::IgnoreUnused(output); |
| 28 | return arm_compute::Status(); |
| 29 | } |
| 30 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 31 | NeonConvertFp32ToFp16Workload::NeonConvertFp32ToFp16Workload(const ConvertFp32ToFp16QueueDescriptor& descriptor, |
| 32 | const WorkloadInfo& info) |
| 33 | : Float32ToFloat16Workload<ConvertFp32ToFp16QueueDescriptor>(descriptor, info) |
| 34 | { |
| 35 | this->m_Data.ValidateInputsOutputs("NeonConvertFp32ToFp16Workload", 1, 1); |
Matthew Bentham | 34336f9 | 2023-04-27 12:13:50 +0000 | [diff] [blame] | 36 | |
| 37 | arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 38 | arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 39 | |
| 40 | if (arm_compute::NECast::validate(input.info(), output.info(), g_AclConvertPolicy)) |
| 41 | { |
| 42 | // Use NECast if supported (needs hardware support for FP16) |
| 43 | m_Cast.reset(new arm_compute::NECast); |
| 44 | m_Cast->configure(&input, &output, g_AclConvertPolicy); |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | // Else use software implementation from Half.hpp |
| 49 | GatherTensorHandlePairs(descriptor, m_TensorHandlePairs); |
| 50 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void NeonConvertFp32ToFp16Workload::Execute() const |
| 54 | { |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 55 | ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonConvertFp32ToFp16Workload_Execute", this->GetGuid()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 56 | |
Matthew Bentham | 34336f9 | 2023-04-27 12:13:50 +0000 | [diff] [blame] | 57 | if (m_Cast) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | { |
Matthew Bentham | 34336f9 | 2023-04-27 12:13:50 +0000 | [diff] [blame] | 59 | // Use NECast if supported and initialised |
| 60 | m_Cast->run(); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | // Else use softwre implementabion using Half.hpp |
| 65 | auto convertFunc = [](uint8_t* dst, const uint8_t* src, size_t size) |
| 66 | { |
| 67 | auto input = reinterpret_cast<const float*>(src); |
| 68 | auto output = reinterpret_cast<Half*>(dst); |
| 69 | size_t numElements = size/2; // 2 bytes per fp16 |
| 70 | armnnUtils::FloatingPointConverter::ConvertFloat32To16(input, numElements, output); |
| 71 | }; |
| 72 | |
| 73 | for (const auto& pair : m_TensorHandlePairs) |
| 74 | { |
| 75 | CopyTensorContentsGeneric(pair.first, pair.second, convertFunc); |
| 76 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
David Monahan | ec81999 | 2022-02-10 14:47:13 +0000 | [diff] [blame] | 80 | void NeonConvertFp32ToFp16Workload::ReplaceInputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot) |
| 81 | { |
| 82 | ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot]; |
| 83 | this->m_Data.m_Inputs[slot] = tensorHandle; |
| 84 | try |
| 85 | { |
| 86 | Reconfigure(); |
| 87 | } |
| 88 | catch(armnn::UnimplementedException& e) |
| 89 | { |
| 90 | // Cannot reconfigure, revert the slot back and throw the exception. |
| 91 | this->m_Data.m_Inputs[slot] = backupHandle; |
| 92 | throw e; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Replace output tensor handle with the given TensorHandle |
| 97 | void NeonConvertFp32ToFp16Workload::ReplaceOutputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot) |
| 98 | { |
| 99 | ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot]; |
| 100 | this->m_Data.m_Inputs[slot] = tensorHandle; |
| 101 | try |
| 102 | { |
| 103 | Reconfigure(); |
| 104 | } |
| 105 | catch(armnn::UnimplementedException& e) |
| 106 | { |
| 107 | // Cannot reconfigure, revert the slot back and throw the exception. |
| 108 | this->m_Data.m_Inputs[slot] = backupHandle; |
| 109 | throw e; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void NeonConvertFp32ToFp16Workload::Reconfigure() |
| 114 | { |
| 115 | throw armnn::UnimplementedException("Reconfigure not implemented for this workload"); |
| 116 | } |
| 117 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 118 | } //namespace armnn |