prefill: key recordings by live split geometry

This commit is contained in:
Constantin authored and StillDeadcode committed 2026-11-03 19:05:30 +01:00
1 parent 61e275e30f
commit ded64c0bd3
16 files changed
+204 -16

No files matched your search

+6
View File
@@ -1933,6 +1933,12 @@ int rad_arch_declare(RadBuilder* b, const RadModelMeta* meta, const RadB
struct RadCtx;
struct RadBatch;
void rad_arch_step(struct RadCtx* c, const struct RadBatch* batch);
/* Optional. Return a stable fingerprint of actual dispatch geometry whose grid depends on live
* per-group seqused values. `kv_used_max_h[g]` is the host maximum for batch->kv[g], and
* rad_ctx_wgp_count(c) supplies the device's WGP count. Return 0 when exact geometry is unknown;
* that pass runs live and is not recorded. */
uint64_t rad_arch_pass_key(struct RadCtx* c, const struct RadBatch* batch,
const int32_t* kv_used_max_h);
#ifdef __cplusplus
} /* extern "C" */
+55
View File
@@ -0,0 +1,55 @@
/* rad_prefill_split.h -- host-pure live-KV prefill split law shared by libr4d launchers and
* architecture pass-key callbacks. Keep this independent of HIP so callbacks can run before tape
* lookup without a device copy or launch.
*/
#pragma once
#include <algorithm>
#include <cstdint>
/* `bq` query positions per workgroup, `tile` keys per KV tile. `units` counts the workgroups that
* do work: a ragged batch's grid is sized from its longest sequence, but a shorter sequence's
* tiles past its end exit at once. Each split keeps at least eight tiles of the supplied context.
*/
static inline int rad_prefill_splits_law(int bq, int tile, int q_len, int total_q, int num_seqs,
int kv_heads, int max_ctx, int wgps) {
const long n = std::max(1, num_seqs);
const long qt = std::min(n * ((std::max(1, q_len) + bq - 1) / bq),
(std::max(1, total_q) + (long)(bq - 1) * n) / bq);
const long units = (long)std::max(1, kv_heads) * std::max(1L, qt);
if (units >= wgps) return 1;
const int s1 = (int)(wgps / units), s2 = (int)(2L * wgps / units);
int sp = s2 > 2 * s1 ? s2 : s1;
const int cap = ((max_ctx > 0 ? max_ctx : (1 << 30) - 1) + tile - 1) / tile / 8;
sp = std::min(sp, std::max(1, cap));
return std::max(1, std::min(sp, 64));
}
/* The launcher's live-context refinement of the initial model-bound law. */
static inline int rad_prefill_splits_for_live(int bq, int tile, int q_len, int total_q,
int num_seqs, int kv_heads, int max_ctx,
int used_ctx, int wgps) {
int splits = rad_prefill_splits_law(bq, tile, q_len, total_q, num_seqs, kv_heads, max_ctx,
wgps);
if (splits > 1 && used_ctx > 0 && (max_ctx <= 0 || used_ctx < max_ctx))
splits = rad_prefill_splits_law(bq, tile, q_len, total_q, num_seqs, kv_heads, used_ctx,
wgps);
return splits;
}
/* Fingerprint one actual launch tuple in packet-global sizes. Use this instead of the raw context
* so equal split grids share a recording key as live context advances. */
static inline uint64_t rad_prefill_grid_key(int q_len, int bq, int splits, int kv_heads,
int num_seqs, int threads) {
const uint64_t gx = (uint64_t)((std::max(1, q_len) + bq - 1) / bq) *
(uint64_t)std::max(1, splits) * (uint64_t)threads;
const uint64_t gy = (uint64_t)std::max(1, kv_heads);
const uint64_t gz = (uint64_t)std::max(1, num_seqs);
uint64_t h = 0x9e3779b97f4a7c15ull;
const uint64_t grid[3] = {gx, gy, gz};
for (uint64_t v : grid) {
h ^= v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2);
h *= 0xbf58476d1ce4e5b9ull;
}
return h | 1;
}
+7
View File
@@ -330,6 +330,10 @@ typedef struct RadBatch {
/* A shape generator's pass (rad_declare_shape_generator; `gen` is one of its
* RAD_GEN_PASS_S*): its payload. Null on every other step. */
const struct RadShapeGenBatch* sgen_b;
/* ---- appended in ABI 34: host-side max seqused per entry of kv[], for an architecture's
* optional recorded-pass geometry key. Null when the batch has no host summary. */
const int32_t* kv_used_max_h;
} RadBatch;
/* What a generator pass carries beside the step batch's own fields, which keep their meanings:
@@ -904,6 +908,9 @@ int rad_kv_stage_layer(RadCtx* c, rad_kvgroup group, const RadKVGrou
int rad_kv_stage_next(RadCtx* c, rad_kvgroup group, int layer);
int rad_rank(RadCtx* c);
int rad_world_size(RadCtx* c);
/* Workgroups the current rank's card can run concurrently (HIP multiProcessorCount on RDNA;
* 32 on the host backend or when the backend has no positive count). */
int rad_ctx_wgp_count(RadCtx* c);
RadStream rad_stream(RadCtx* c);
/* The routing result for a MoE layer, resolved on device against the current residency table.