Elffuss BraidHub

kikocis/kleidiai public

main · 2.9 KiBRawPermalink
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/symm_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
24namespace kai::test {
25
26void SymmLinearQuantizer::dynamic_quantize(
27 DataType fp_dtype, Shape shape, Span<const std::byte> fp_data, Tensor& qdata, Tensor& qscale,
28 [[maybe_unused]] Tensor& qzp) const {
29 KAI_TEST_ASSERT_MSG(shape.size() == 2, "Only 2D quantization is supported.");
30
31 const size_t height = shape.at(0);
32 const size_t width = shape.at(1);
33
34 const size_t block_height = m_block_height != 0 ? m_block_height : height;
35 const size_t block_width = m_block_width != 0 ? m_block_width : width;
36
37 const size_t quant_height = round_up_division(height, block_height);
38 const size_t quant_width = round_up_division(width, block_width);
39 const std::array quant_shape{quant_height, quant_width};
40
41 const DynamicQuantizeLinearFn quantize_fn =
42 make_dynamic_symmetric_quantize_linear(fp_dtype, m_qdata_dtype, m_qscale_dtype, m_qdata_round_mode);
43 auto [qdata_buffer, qscale_buffer, qzp_buffer] = quantize_fn(height, width, block_height, block_width, fp_data);
44
45 qdata.set_shape(shape).set_format(make_poly<PlainFormat>(m_qdata_dtype)).set_data(std::move(qdata_buffer));
46 qscale.set_shape(quant_shape).set_format(make_poly<PlainFormat>(m_qscale_dtype)).set_data(std::move(qscale_buffer));
47}
48
49Buffer SymmLinearQuantizer::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);
61 Buffer fp_data = fn(height, width, block_height, block_width, qdata, qscale, qzp);
62
63 return fp_data;
64}
65
66std::string SymmLinearQuantizer::uid() const {
67 return "symm_linear<qdata=" + data_type_uid(m_qdata_dtype) + ",qscale=" + data_type_uid(m_qscale_dtype) +
68 ",rqdata=" + std::to_string(static_cast<int>(m_qdata_round_mode)) + ",block=" + std::to_string(m_block_height) +
69 "x" + std::to_string(m_block_width) + ">";
70}
71
72} // namespace kai::test