/* Derived from libr4d/r4d_kv_len_trim.hip by the libhalo rename transform (2026-10-26), * then ported to gfx1151; see libhalo/README.md. */ // halo_kv_len_trim.hip - halo_rad_kv_len_trim: a batch's per-sequence context lengths less `trim`. // // The 31B's prefill overlap runs attention per slice of a one-sequence step: slice A's rows are the // step's first rows, so its attention must see the context ending at its own last row, not the // step's (rows past it are not yet in the cache, and their positions are counted from the end). // `trim` is the rows after the slice, a runtime count. A launch sees its op's parameters collapsed // to the band's upper bound (RadArgs.p), so the count is the extent of operand `tail`, those rows' // positions, whose values are never read; the ranged parameter only picks the band. #include "halo.h" #include "halo_args.h" #include "halo_emit.h" #include namespace { __global__ __launch_bounds__(64) void kv_len_trim_kernel(const int32_t* __restrict__ in, int32_t* __restrict__ out, int n, int trim) { const int i = (int)threadIdx.x; if (i < n) { const int v = in[i] - trim; out[i] = v > 0 ? v : 0; } } } // namespace extern "C" int halo_rad_kv_len_trim(const RadArgs* a, RadStream stream) { const RadTensor* in = halo_opt(a, 0); const RadTensor* tail = halo_opt(a, 1); const RadTensor* out = halo_opt(a, 2); if (!in || !tail || !out || !in->data || !out->data) return HALO_NO(RAD_E_INVAL); if (in->dtype != RAD_I32 || out->dtype != RAD_I32) return HALO_NO(RAD_E_DTYPE); const long long n = halo_numel(in); if (n < 1 || n > 64 || halo_numel(out) < n) return HALO_NO(RAD_E_SHAPE); const long long trim = tail->rank ? halo_numel(tail) : 0; if (trim < 0 || trim > 0x7fffffffLL) return HALO_NO(RAD_E_SHAPE); return HALO_EMIT(stream, kv_len_trim_kernel, dim3(1), dim3(64), 0, (const int32_t*)in->data, (int32_t*)out->data, (int)n, (int)trim); }