| 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/reference/clamp.hpp" |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <cstddef> |
| 11 | #include <functional> |
| 12 | #include <numeric> |
| 13 | #include <tuple> |
| 14 | #include <utility> |
| 15 | |
| 16 | #include "test/common/assert.hpp" |
| 17 | #include "test/common/buffer.hpp" |
| 18 | #include "test/common/data_type.hpp" |
| 19 | #include "test/common/float16.hpp" |
| 20 | #include "test/common/memory.hpp" |
| 21 | #include "test/common/numeric_limits.hpp" |
| 22 | #include "test/common/round.hpp" |
| 23 | #include "test/common/span.hpp" |
| 24 | #include "test/reference/clamp.hpp" |
| 25 | |
| 26 | namespace kai::test { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | template <typename T> |
| 31 | std::tuple<Buffer, Buffer> dynamic_clamp(float ratio, Shape shape, Span<const std::byte> data) { |
| 32 | KAI_TEST_ASSERT(ratio > 0.0F); |
| 33 | KAI_TEST_ASSERT(ratio <= 1.0F); |
| 34 | |
| 35 | const size_t num_dims = shape.size(); |
| 36 | const size_t width = shape.at(num_dims - 1); |
| 37 | const size_t height = std::accumulate(shape.begin(), shape.end() - 1, size_t{1}, std::multiplies<>()); |
| 38 | |
| 39 | // Finds the input range. |
| 40 | T src_min = numeric_highest<T>; |
| 41 | T src_max = numeric_lowest<T>; |
| 42 | |
| 43 | for (size_t row = 0; row < height; ++row) { |
| 44 | for (size_t col = 0; col < width; ++col) { |
| 45 | const T value = read_2d<T>(data, width, row, col); |
| 46 | |
| 47 | src_min = std::min(src_min, value); |
| 48 | src_max = std::max(src_max, value); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Determines the output range. |
| 53 | src_min = std::max(src_min, numeric_lowest<T>); |
| 54 | src_max = std::min(src_max, numeric_highest<T>); |
| 55 | |
| 56 | const T range = src_max - src_min; |
| 57 | const T reduction = static_cast<T>(static_cast<float>(range) * (1.0F - ratio) / 2); |
| 58 | |
| 59 | const T dst_min = src_min + reduction; |
| 60 | const T dst_max = src_max - reduction; |
| 61 | |
| 62 | Buffer limits(sizeof(ClampLimits<T>), 0); |
| 63 | *reinterpret_cast<ClampLimits<T>*>(limits.data()) = {dst_min, dst_max}; |
| 64 | |
| 65 | // Clamps the data. |
| 66 | Buffer dst(height * round_up_division(width * size_in_bits<T>, 8), 0); |
| 67 | |
| 68 | for (size_t row = 0; row < height; ++row) { |
| 69 | for (size_t col = 0; col < width; ++col) { |
| 70 | const T value = read_2d<T>(data, width, row, col); |
| 71 | const T dst_value = std::clamp(value, dst_min, dst_max); |
| 72 | write_2d<T>(dst.view(), width, row, col, dst_value); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return {std::move(limits), std::move(dst)}; |
| 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | |
| 81 | DynamicClampFn make_dynamic_clamp(DataType dtype) { |
| 82 | switch (dtype) { |
| 83 | case DataType::FP32: |
| 84 | return dynamic_clamp<float>; |
| 85 | |
| 86 | case DataType::FP16: |
| 87 | return dynamic_clamp<Float16>; |
| 88 | |
| 89 | default: |
| 90 | KAI_TEST_ERROR("Not implemented."); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace kai::test |