|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Arm Limited. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to |
| 8 | + * deal in the Software without restriction, including without limitation the |
| 9 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +#ifndef ACL_SRC_CPU_KERNELS_TOPKV_GENERIC_SVE_IMPL_H |
| 25 | +#define ACL_SRC_CPU_KERNELS_TOPKV_GENERIC_SVE_IMPL_H |
| 26 | + |
| 27 | +#include "arm_compute/core/Coordinates.h" |
| 28 | +#include "arm_compute/core/Error.h" |
| 29 | +#include "arm_compute/core/Helpers.h" |
| 30 | +#include "arm_compute/core/ITensor.h" |
| 31 | +#include "arm_compute/core/Types.h" |
| 32 | +#include "arm_compute/core/Window.h" |
| 33 | + |
| 34 | +#include "src/core/NEON/wrapper/wrapper.h" |
| 35 | + |
| 36 | +#include <cstdint> |
| 37 | +#include <cstring> |
| 38 | + |
| 39 | +#if defined(__ARM_FEATURE_SVE) |
| 40 | +#include <arm_sve.h> |
| 41 | +#endif // defined(__ARM_FEATURE_SVE) |
| 42 | + |
| 43 | +namespace arm_compute |
| 44 | +{ |
| 45 | +namespace cpu |
| 46 | +{ |
| 47 | +namespace detail |
| 48 | +{ |
| 49 | + |
| 50 | +/* |
| 51 | + * Type-specific hooks (declared here, defined in each cpp). |
| 52 | + * |
| 53 | + * - vector_length<Scalar>() |
| 54 | + * Return the SVE vector length in elements for Scalar (no clamping). |
| 55 | + * |
| 56 | + * - count_gt_block<Scalar>(ptr, thr_vec, block_elems) |
| 57 | + * Count how many elements in [ptr, ptr + block_elems) are > threshold. |
| 58 | + * Tail-safe via predicate. block_elems is always <= vector_length<Scalar>(). |
| 59 | + * |
| 60 | + t contains the SVE intrinsics |
| 61 | + * (e.g., qasymm8.cpp, qasymm8_signed.cpp, fp16.cpp, fp32.cpp, integer.cpp). |
| 62 | + */ |
| 63 | + |
| 64 | +template <typename Scalar> |
| 65 | +uint32_t vector_length(); |
| 66 | + |
| 67 | +template <typename Scalar, typename ThresholdVector> |
| 68 | +uint32_t count_gt_block(const Scalar *ptr, ThresholdVector thr_vec, uint32_t block_elems); |
| 69 | + |
| 70 | +// ---------------------------------------------------------------------------- |
| 71 | +// Generic wrapper (type-agnostic) - uses the above hooks. |
| 72 | +// Semantics (matching TopKV tests you showed): |
| 73 | +// - predictions is N x C |
| 74 | +// - window iterates across output elements (classes) => id.x() == class index c |
| 75 | +// - for each class c, targets[c] gives the sample index t |
| 76 | +// - scan across N samples and compute rank (#samples with value > predictions[t]) |
| 77 | +// - output is U8 boolean: (rank < k) |
| 78 | +// ---------------------------------------------------------------------------- |
| 79 | +template <typename Scalar> |
| 80 | +inline void |
| 81 | +topkv_sve_wrapper(const ITensor *predictions, const ITensor *targets, ITensor *out, uint32_t k, const Window &window) |
| 82 | +{ |
| 83 | + ARM_COMPUTE_ERROR_ON_NULLPTR(predictions, targets, out); |
| 84 | + ARM_COMPUTE_ERROR_ON(k == 0); |
| 85 | + |
| 86 | + const ITensorInfo *pred_info = predictions->info(); |
| 87 | + const uint32_t N = pred_info->dimension(0); // samples |
| 88 | + const uint32_t C = pred_info->dimension(1); // classes |
| 89 | + |
| 90 | + const uint32_t vl = vector_length<Scalar>(); // cache once per kernel invocation |
| 91 | + |
| 92 | + Iterator tgt_it(targets, window); |
| 93 | + Iterator out_it(out, window); |
| 94 | + |
| 95 | + execute_window_loop( |
| 96 | + window, |
| 97 | + [&](const Coordinates &id) |
| 98 | + { |
| 99 | + const uint32_t c = static_cast<uint32_t>(id.x()); // class index |
| 100 | + ARM_COMPUTE_ERROR_ON(c >= C); |
| 101 | + |
| 102 | + uint32_t t = {*reinterpret_cast<uint32_t *>(tgt_it.ptr())}; |
| 103 | + ARM_COMPUTE_ERROR_ON(t >= N); |
| 104 | + |
| 105 | + const Scalar *col_ptr = reinterpret_cast<const Scalar *>(predictions->ptr_to_element(Coordinates(0, c))); |
| 106 | + ARM_COMPUTE_ERROR_ON(col_ptr == nullptr); |
| 107 | + |
| 108 | + const Scalar thr = col_ptr[t]; |
| 109 | + const auto thr_vec = wrapper::svdup_n(thr); |
| 110 | + |
| 111 | + uint32_t rank = 0; |
| 112 | + uint32_t idx = 0; |
| 113 | + |
| 114 | + while (idx < N) |
| 115 | + { |
| 116 | + const uint32_t remaining = N - idx; |
| 117 | + const uint32_t bw = (remaining < vl) ? remaining : vl; |
| 118 | + |
| 119 | + rank += count_gt_block<Scalar>(col_ptr + idx, thr_vec, bw); |
| 120 | + |
| 121 | + if (rank >= k) |
| 122 | + { |
| 123 | + break; |
| 124 | + } |
| 125 | + |
| 126 | + idx += bw; |
| 127 | + } |
| 128 | + |
| 129 | + *reinterpret_cast<uint8_t *>(out_it.ptr()) = static_cast<uint8_t>(rank < k); |
| 130 | + }, |
| 131 | + tgt_it, out_it); |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace detail |
| 135 | +} // namespace cpu |
| 136 | +} // namespace arm_compute |
| 137 | + |
| 138 | +#endif // ACL_SRC_CPU_KERNELS_TOPKV_GENERIC_SVE_IMPL_H |
0 commit comments