| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include "test/nextgen/reference/dequantize.hpp" |
| 8 | |
| 9 | #include <cstddef> |
| 10 | #include <cstdint> |
| 11 | #include <tuple> |
| 12 | #include <type_traits> |
| 13 | |
| 14 | #include "test/common/assert.hpp" |
| 15 | #include "test/common/buffer.hpp" |
| 16 | #include "test/common/data_type.hpp" |
| 17 | #include "test/common/int4.hpp" |
| 18 | #include "test/common/memory.hpp" |
| 19 | #include "test/common/numeric_limits.hpp" |
| 20 | #include "test/common/round.hpp" |
| 21 | #include "test/common/span.hpp" |
| 22 | #include "test/common/type_traits.hpp" |
| 23 | |
| 24 | namespace kai::test { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | template <typename FpData, typename QData, typename QScale, typename QZp> |
| 29 | [[nodiscard]] Buffer dequantize_linear( |
| 30 | size_t height, size_t width, size_t block_height, size_t block_width, Span<const std::byte> qdata, |
| 31 | Span<const std::byte> qscale, Span<const std::byte> qzp) { |
| 32 | Buffer fp_data(height * round_up_division(width * size_in_bits<FpData>, 8)); |
| 33 | |
| 34 | const size_t quant_width = round_up_division(width, block_width); |
| 35 | |
| 36 | for (size_t row = 0; row < height; ++row) { |
| 37 | for (size_t col = 0; col < width; ++col) { |
| 38 | const QData qdata_value = read_2d<QData>(qdata, width, row, col); |
| 39 | FpData fp_value = static_cast<FpData>(qdata_value); |
| 40 | |
| 41 | if constexpr (!std::is_same_v<QZp, void>) { |
| 42 | const QZp qzp_value = read_2d<QZp>(qzp, quant_width, row / block_height, col / block_width); |
| 43 | fp_value -= static_cast<FpData>(qzp_value); |
| 44 | } else if constexpr (is_unsigned<QData>) { |
| 45 | static_assert(size_in_bits<QData> <= 64); |
| 46 | constexpr FpData zp_value = static_cast<FpData>(static_cast<uint64_t>(1) << (size_in_bits<QData> - 1)); |
| 47 | fp_value -= zp_value; |
| 48 | } |
| 49 | |
| 50 | const QScale qscale_value = read_2d<QScale>(qscale, quant_width, row / block_height, col / block_width); |
| 51 | fp_value *= static_cast<FpData>(qscale_value); |
| 52 | |
| 53 | write_2d<FpData>(fp_data.view(), width, row, col, fp_value); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return fp_data; |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
| 62 | DequantizeLinearFn make_dequantize_linear( |
| 63 | DataType fp_dtype, DataType qdata_dtype, DataType qscale_dtype, DataType qzp_dtype) { |
| 64 | const auto dtypes = std::make_tuple(fp_dtype, qdata_dtype, qscale_dtype, qzp_dtype); |
| 65 | |
| 66 | if (dtypes == std::make_tuple(DataType::FP32, DataType::I8, DataType::FP32, DataType::I32)) { |
| 67 | return dequantize_linear<float, int8_t, float, int32_t>; |
| 68 | } |
| 69 | |
| 70 | if (dtypes == std::make_tuple(DataType::FP32, DataType::U4, DataType::FP32, DataType::UNKNOWN)) { |
| 71 | return dequantize_linear<float, UInt4, float, void>; |
| 72 | } |
| 73 | |
| 74 | KAI_TEST_ERROR("Not implemented."); |
| 75 | } |
| 76 | |
| 77 | } // namespace kai::test |