prefill: key tape splits from ordered host KV bounds

This commit is contained in:
Constantin authored and StillDeadcode committed 2026-11-03 19:05:30 +01:00
1 parent aa14eea76c
commit 798fb21e51
14 files changed
+221 -65

No files matched your search

+34 -41
View File
@@ -14,18 +14,29 @@
#pragma once
#include "rad_prefill_split.h"
#include <atomic>
// Workgroups the card runs at once: one a WGP. HIP's multiProcessorCount counts WGPs on RDNA (32 on
// the R9700, see want_wg in r4d_hc_bf16.hip); 32 if it cannot be read.
static inline int r4d_prefill_wgps() {
static const int v = [] {
int dev = 0;
hipDeviceProp_t pr{};
if (hipGetDevice(&dev) != hipSuccess || hipGetDeviceProperties(&pr, dev) != hipSuccess)
return 32;
return pr.multiProcessorCount > 0 ? pr.multiProcessorCount : 32;
}();
return v;
// Workgroups the card runs at once: one a WGP. Kernel launches use RadArgs::wgp_count, the same
// per-rank property used by pass-key callbacks. Declaration-time scratch sizing has no Ctx yet, so
// cache that query per HIP device rather than once process-wide (TP ranks can bind unlike cards).
static inline int r4d_prefill_wgps(const RadArgs* a = nullptr) {
if (a && a->wgp_count > 0) return a->wgp_count;
int dev = 0;
if (hipGetDevice(&dev) != hipSuccess || dev < 0) return 32;
static std::atomic<int> cached[64]{};
if (dev < (int)(sizeof(cached) / sizeof(cached[0]))) {
const int old = cached[dev].load(std::memory_order_relaxed);
if (old > 0) return old;
}
hipDeviceProp_t pr{};
if (hipGetDeviceProperties(&pr, dev) != hipSuccess) return 32;
const int count = pr.multiProcessorCount > 0 ? pr.multiProcessorCount : 32;
if (dev < (int)(sizeof(cached) / sizeof(cached[0]))) {
int expected = 0;
cached[dev].compare_exchange_strong(expected, count, std::memory_order_relaxed);
return cached[dev].load(std::memory_order_relaxed);
}
return count;
}
// The split law, at the launch's own geometry: `bq` query positions a workgroup, `tile` keys a KV
@@ -52,43 +63,25 @@ static inline int r4d_prefill_splits_law(int bq, int tile, int q_len, int total_
// tells r4d_selftest a split launch from an unsplit one with the same bytes. Not atomic.
static int g_prefill_last_splits = 0;
/* Live KV length for the 8-tile floor. seqused_k is device-side (r4d_attn_args.h); max_ctx is
* the model bound and is too wide for a short chunk. A failed copy keeps max_ctx, which only
* over-splits. Occupancy already returned 1 skips the copy. */
static inline int r4d_prefill_used_ctx(const R4DArgs* r) {
if (!r->seqused_k || r->num_seqs <= 0) return 0;
const int n = r->num_seqs;
int used = 0;
int chunk[64];
for (int off = 0; off < n; off += 64) {
const int m = n - off < 64 ? n - off : 64;
if (hipMemcpy(chunk, r->seqused_k + off, (size_t)m * sizeof(int),
hipMemcpyDefault) != hipSuccess) {
(void)hipGetLastError();
return 0;
}
for (int i = 0; i < m; ++i)
if (chunk[i] > used) used = chunk[i];
}
return used;
}
/* The runtime supplies the live group maximum only when it can prove that the launch's full
* seqused operand is the just-built, non-speculative prefill batch. Other shapes use max_ctx below;
* never synchronously copy device seqused from a HIP queue independent of the AQL upload queue. */
/* The KV split's count before the scratch bound: the case's `splits` parameter when it names one
* (r4d_selftest; the prefill rows declare no such axis, so the engine passes none), else the law,
* at most 64. The 8-tile floor uses live seqused_k, not the model max_ctx. */
* at most 64. Use the runtime's host seqused maximum when exact for this launch; otherwise use the
* declared model bound. The launcher and recording callback share that same rule. */
static inline int r4d_prefill_splits_want(const RadArgs* a, const R4DArgs* r, int bq, int tile) {
const int sp0 = split_of(a);
if (sp0 > 0) return std::min(sp0, 64);
const int wgps = r4d_prefill_wgps();
const int wgps = r4d_prefill_wgps(a);
const int qtot = r4d_q_total(*r);
int sp = r4d_prefill_splits_law(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads, r->max_ctx,
wgps);
if (sp <= 1) return 1;
const int used = r4d_prefill_used_ctx(r);
const int used = a->kv_used_max_h;
if (used > 0)
sp = rad_prefill_splits_for_live(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads,
r->max_ctx, used, wgps);
return sp;
return rad_prefill_splits_for_live(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads,
r->max_ctx, used, wgps);
return r4d_prefill_splits_law(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads,
r->max_ctx, wgps);
}
/* The launch's split count, 1 for the plain form: what r4d_prefill_splits_want asks, cut to the
@@ -125,7 +118,7 @@ static inline int64_t r4d_prefill_scratch(const RadArgs* a, int head_dim, long l
const int kv = (int)std::max(1LL, gqa > 0 ? n_head / gqa : n_head);
const int band = (int)std::max(1LL, r4d_geti_or(a, "q_len", 1));
const int mctx = (int)r4d_geti_or(a, "max_ctx", 0);
const int w = r4d_prefill_wgps();
const int w = r4d_prefill_wgps(a);
const int64_t per_row = (int64_t)n_head * (head_dim + 2) * 4;
int64_t worst = 0;
for (long long n = 1; n <= max_seqs; ++n)
+16 -6
View File
@@ -1410,6 +1410,18 @@ static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* nam
const int64_t span0 = lead, span1 = cu[(size_t)ns]; /* the launch's own rows */
const int64_t total_q = span1 + tail;
const bool ragged = ns > 1 || lead || tail;
/* This direct-kernel fixture supplies an exact host maximum for a full sequence operand. Its
* synthetic partial-row operands do not carry the runtime's host slice/position view, so they
* exercise the safe declared-context fallback. */
int32_t used_max_h = -1;
if (!lead && !tail)
for (int32_t x : used) used_max_h = std::max(used_max_h, x);
int wgp_count = 32, current_device = 0;
hipDeviceProp_t current_props{};
if (hipGetDevice(&current_device) == hipSuccess &&
hipGetDeviceProperties(&current_props, current_device) == hipSuccess &&
current_props.multiProcessorCount > 0)
wgp_count = current_props.multiProcessorCount;
/* one disjoint run of mb blocks per sequence */
const std::vector<int32_t> bt = iota_i32(ns * mb);
@@ -1484,6 +1496,8 @@ static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* nam
a.t = ts.data(); a.n_t = (int)ts.size();
a.p = ps.data(); a.n_p = (int)ps.size();
a.world_size = 1;
a.kv_used_max_h = used_max_h;
a.wgp_count = wgp_count;
const int64_t need = r->scratch ? r->scratch(&a) : 0;
if (need > 0) {
a.scratch = dalloc((size_t)need);
@@ -1500,12 +1514,8 @@ static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* nam
const int s_un = run(1, d_o[0]);
const int s_fo = run(force, d_o[1]);
const int s_lw = run(0, d_o[2]);
int law_ctx = (int)(mb * bs);
if (decl_max_ctx > 0) {
law_ctx = 0;
for (const SplitSeq& s : seqs)
if ((int)s.ctx > law_ctx) law_ctx = (int)s.ctx;
}
int law_ctx = decl_max_ctx > 0 ? (int)decl_max_ctx : (int)(mb * bs);
if (used_max_h > 0 && used_max_h < law_ctx) law_ctx = used_max_h;
const int want = law((int)(ragged ? std::min<int64_t>(2048, total_q) : qmax), (int)total_q,
(int)ns, (int)kv_heads, law_ctx);
ok((tag + ": splits 1 / forced / law").c_str(),