| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 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/quantization/asymm_linear_quantizer.hpp" |
| 8 | |
| 9 | #include <array> |
| 10 | #include <cstddef> |
| 11 | #include <utility> |
| 12 | |
| 13 | #include "test/common/assert.hpp" |
| 14 | #include "test/common/buffer.hpp" |
| 15 | #include "test/common/data_type.hpp" |
| 16 | #include "test/common/round.hpp" |
| 17 | #include "test/common/span.hpp" |
| 18 | #include "test/nextgen/common/poly.hpp" |
| 19 | #include "test/nextgen/format/plain_format.hpp" |
| 20 | #include "test/nextgen/harness/tensor.hpp" |
| 21 | #include "test/nextgen/reference/dequantize.hpp" |
| 22 | #include "test/nextgen/reference/quantize.hpp" |
| 23 | |
| 24 | namespace kai::test { |
| 25 | |
| 26 | void AsymmLinearQuantizer::dynamic_quantize( |
| 27 | DataType fp_dtype, Shape shape, Span<const std::byte> fp_data, Tensor& qdata, Tensor& qscale, Tensor& qzp) const { |
| 28 | KAI_TEST_ASSERT_MSG(shape.size() == 2, "Only 2D quantization is supported."); |
| 29 | |
| 30 | const size_t height = shape.at(0); |
| 31 | const size_t width = shape.at(1); |
| 32 | |
| 33 | const size_t block_height = m_block_height != 0 ? m_block_height : height; |
| 34 | const size_t block_width = m_block_width != 0 ? m_block_width : width; |
| 35 | |
| 36 | const size_t quant_height = round_up_division(height, block_height); |
| 37 | const size_t quant_width = round_up_division(width, block_width); |
| 38 | const std::array quant_shape{quant_height, quant_width}; |
| 39 | |
| 40 | const DynamicQuantizeLinearFn quantize_fn = make_dynamic_asymmetric_quantize_linear( |
| 41 | fp_dtype, m_qdata_dtype, m_qscale_dtype, m_qzp_dtype, m_qdata_round_mode, m_qzp_round_mode); |
| 42 | auto [qdata_buffer, qscale_buffer, qzp_buffer] = quantize_fn(height, width, block_height, block_width, fp_data); |
| 43 | |
| 44 | qdata.set_shape(shape).set_format(make_poly<PlainFormat>(m_qdata_dtype)).set_data(std::move(qdata_buffer)); |
| 45 | qscale.set_shape(quant_shape).set_format(make_poly<PlainFormat>(m_qscale_dtype)).set_data(std::move(qscale_buffer)); |
| 46 | qzp.set_shape(quant_shape).set_format(make_poly<PlainFormat>(m_qzp_dtype)).set_data(std::move(qzp_buffer)); |
| 47 | } |
| 48 | |
| 49 | Buffer AsymmLinearQuantizer::dequantize( |
| 50 | DataType fp_dtype, Shape shape, Span<const std::byte> qdata, Span<const std::byte> qscale, |
| 51 | Span<const std::byte> qzp) const { |
| 52 | KAI_TEST_ASSERT_MSG(shape.size() == 2, "Only 2D quantization is supported."); |
| 53 | |
| 54 | const size_t height = shape.at(0); |
| 55 | const size_t width = shape.at(1); |
| 56 | |
| 57 | const size_t block_height = m_block_height != 0 ? m_block_height : height; |
| 58 | const size_t block_width = m_block_width != 0 ? m_block_width : width; |
| 59 | |
| 60 | const DequantizeLinearFn fn = make_dequantize_linear(fp_dtype, m_qdata_dtype, m_qscale_dtype, m_qzp_dtype); |
| 61 | Buffer fp_data = fn(height, width, block_height, block_width, qdata, qscale, qzp); |
| 62 | |
| 63 | return fp_data; |
| 64 | } |
| 65 | |
| 66 | std::string AsymmLinearQuantizer::uid() const { |
| 67 | return "asymm_linear<qdata=" + data_type_uid(m_qdata_dtype) + ",qscale=" + data_type_uid(m_qscale_dtype) + |
| 68 | ",qzp=" + data_type_uid(m_qzp_dtype) + ",rqdata=" + std::to_string(static_cast<int>(m_qdata_round_mode)) + |
| 69 | ",rqzp=" + std::to_string(static_cast<int>(m_qzp_round_mode)) + ",block=" + std::to_string(m_block_height) + |
| 70 | "x" + std::to_string(m_block_width) + ">"; |
| 71 | } |
| 72 | |
| 73 | } // namespace kai::test |