[Core] flat VRAM: SWA KV ring + fixed global KV + bounded scratch bucketing (reviewer p1+p2)
Punto 2: the 40 SWA pools now store a circular ring of swa_win+SWA_CHUNK_CAP(512)
=1536 positions (slot=pos%seq) instead of the whole 32768 context (~5GiB wasted);
the 8 global pools keep a linear max_context cache allocated ONCE. KV capacities
are fixed per layer kind => no drop+realloc when a session crosses a 4096 multiple.
kv_store{,_f16,_f16_batched} write pos%seq; flash decode/prefill/prefill_wmma read
p%seq (global seq>pos => identity). snapshot/restore map the window onto ring slots
with 1 or 2 pitched copies (wrap). serve clamps prefill chunk to SWA_CHUNK_CAP so
ring>=swa_win+chunk holds. proofs/prefix_cache.lean Part E (0 sorryAx): slot_inj,
window_slots_distinct, ring_no_clobber, global_slot_id. New GPU swa_ring_kv_parity
(R=1536, P=2000) vs an independent host LINEAR oracle: 1.48e-5.
Punto 1: BUF_POOL buckets pooled allocs in (4KiB,64MiB] to a power of two so variable
batch tails / future speculative B converge to a closed size set (no hipMalloc/Free
churn); larger allocs stay exact. Weight uploads (load_raw/load_bf16_as_f32/expert
matrix/arena) switched to alloc_unpooled: resident, exact, never pooled (rounding
them had added ~11GiB).
MEASURED gfx1150 FP16 KV chunk256: READY ~21.0GB; multi-turn 2048->13000->2048->5000
vram flat ~23.1-23.7GB (only bounded cap-8 checkpoint turnover, no KV resize);
prefill 2048 ~12.2s, 13000 ~78.6s (~6ms/tok). Suite GREEN hip37 ld23 gw4, CPU23, Lean6.
This commit is contained in:
1 parent
a4752953fb
commit
ca3a6b0e15
6 files changed
+210
-55
No files matched your search
@@ -270,7 +270,7 @@ __global__ void k_flash_gqa_decode(const float* __restrict__ q,
|
||||
|
||||
float lmax = -INFINITY;
|
||||
for (int s = tid; s < seq; s += nt) {
|
||||
const float* ks = kh + (size_t)s * hd;
|
||||
const float* ks = kh + (size_t)(s % seq) * hd;
|
||||
float dot = 0.f;
|
||||
for (int d = 0; d < hd; ++d) dot += qh[d] * ks[d];
|
||||
dot *= scale;
|
||||
@@ -304,7 +304,7 @@ __global__ void k_flash_gqa_decode(const float* __restrict__ q,
|
||||
float* oh = out + (size_t)h * hd;
|
||||
for (int d = tid; d < hd; d += nt) {
|
||||
float acc = 0.f;
|
||||
for (int s = 0; s < seq; ++s) acc += scores[s] * inv * vh[(size_t)s * hd + d];
|
||||
for (int s = 0; s < seq; ++s) acc += scores[s] * inv * vh[(size_t)(s % seq) * hd + d];
|
||||
oh[d] = acc;
|
||||
}
|
||||
}
|
||||
@@ -358,7 +358,7 @@ __global__ void k_flash_swa_gqa_decode(const float* __restrict__ q,
|
||||
float lmax = sink;
|
||||
for (int p = tid; p < n_win; p += nt) {
|
||||
const int s = k_start + p;
|
||||
const float* ks = kh + (size_t)s * hd;
|
||||
const float* ks = kh + (size_t)(s % seq) * hd;
|
||||
float dot = 0.f;
|
||||
for (int d = 0; d < hd; ++d) dot += qh[d] * ks[d];
|
||||
dot *= scale;
|
||||
@@ -397,7 +397,7 @@ __global__ void k_flash_swa_gqa_decode(const float* __restrict__ q,
|
||||
float acc = 0.f;
|
||||
for (int p = 0; p < n_win; ++p) {
|
||||
const int s = k_start + p;
|
||||
acc += scores[p] * inv * vh[(size_t)s * hd + d];
|
||||
acc += scores[p] * inv * vh[(size_t)(s % seq) * hd + d];
|
||||
}
|
||||
if (gate) acc *= 1.f / (1.f + __expf(-gate[(size_t)h * hd + d]));
|
||||
oh[d] = acc;
|
||||
@@ -411,7 +411,7 @@ extern "C" int vetta_flash_swa_gqa_decode(const float* q, const float* k, const
|
||||
const float* sink_logit, const float* gate,
|
||||
float* scores_scratch, vetta_stream_t stream) {
|
||||
if (n_head <= 0 || n_kv <= 0 || seq <= 0 || hd <= 0 || (n_head % n_kv) != 0) return 1;
|
||||
if (cur_pos < 0 || cur_pos >= seq) return 1;
|
||||
if (cur_pos < 0) return 1;
|
||||
const int k_start = (swa_window > 0 && cur_pos >= swa_window) ? (cur_pos - swa_window + 1) : 0;
|
||||
const int n_win = cur_pos - k_start + 1;
|
||||
const int nt = 256;
|
||||
@@ -456,7 +456,7 @@ __global__ void k_flash_swa_gqa_decode_f16(const float* __restrict__ q,
|
||||
float lmax = sink;
|
||||
for (int p = tid; p < n_win; p += nt) {
|
||||
const int s = k_start + p;
|
||||
const uint16_t* ks = kh + (size_t)s * hd;
|
||||
const uint16_t* ks = kh + (size_t)(s % seq) * hd;
|
||||
float dot = 0.f;
|
||||
for (int d = 0; d < hd; ++d) dot += qh[d] * f16_to_f32(ks[d]);
|
||||
dot *= scale;
|
||||
@@ -495,7 +495,7 @@ __global__ void k_flash_swa_gqa_decode_f16(const float* __restrict__ q,
|
||||
float acc = 0.f;
|
||||
for (int p = 0; p < n_win; ++p) {
|
||||
const int s = k_start + p;
|
||||
acc += scores[p] * inv * f16_to_f32(vh[(size_t)s * hd + d]);
|
||||
acc += scores[p] * inv * f16_to_f32(vh[(size_t)(s % seq) * hd + d]);
|
||||
}
|
||||
if (gate) acc *= 1.f / (1.f + __expf(-gate[(size_t)h * hd + d]));
|
||||
oh[d] = acc;
|
||||
@@ -509,7 +509,7 @@ extern "C" int vetta_flash_swa_gqa_decode_f16(const float* q, const uint16_t* k,
|
||||
const float* sink_logit, const float* gate,
|
||||
float* scores_scratch, vetta_stream_t stream) {
|
||||
if (n_head <= 0 || n_kv <= 0 || seq <= 0 || hd <= 0 || (n_head % n_kv) != 0) return 1;
|
||||
if (cur_pos < 0 || cur_pos >= seq) return 1;
|
||||
if (cur_pos < 0) return 1;
|
||||
const int k_start = (swa_window > 0 && cur_pos >= swa_window) ? (cur_pos - swa_window + 1) : 0;
|
||||
const int n_win = cur_pos - k_start + 1;
|
||||
const int nt = 256;
|
||||
@@ -561,7 +561,7 @@ __global__ void k_flash_swa_gqa_prefill_f16(const float* __restrict__ q,
|
||||
float lmax = sink;
|
||||
for (int p = tid; p < n_win; p += nt) {
|
||||
const int s = k_start + p;
|
||||
const uint16_t* ks = kh + (size_t)s * hd;
|
||||
const uint16_t* ks = kh + (size_t)(s % seq) * hd;
|
||||
float dot = 0.f;
|
||||
for (int d = 0; d < hd; ++d) dot += qh[d] * f16_to_f32(ks[d]);
|
||||
dot *= scale;
|
||||
@@ -601,7 +601,7 @@ __global__ void k_flash_swa_gqa_prefill_f16(const float* __restrict__ q,
|
||||
float acc = 0.f;
|
||||
for (int p = 0; p < n_win; ++p) {
|
||||
const int s = k_start + p;
|
||||
acc += scores[p] * inv * f16_to_f32(vh[(size_t)s * hd + d]);
|
||||
acc += scores[p] * inv * f16_to_f32(vh[(size_t)(s % seq) * hd + d]);
|
||||
}
|
||||
if (gh) acc *= 1.f / (1.f + __expf(-gh[d]));
|
||||
oh[d] = acc;
|
||||
@@ -1896,7 +1896,7 @@ __global__ void k_kv_store(const float* __restrict__ k_in, const float* __restri
|
||||
if (tid == 0) fac_s = rsqrtf(red[0] / (float)hd + rms_eps);
|
||||
__syncthreads();
|
||||
if (tid < hd) {
|
||||
const size_t off = (size_t)h * seq * hd + (size_t)pos * hd + tid;
|
||||
const size_t off = (size_t)h * seq * hd + (size_t)(pos % seq) * hd + tid;
|
||||
k_cache[off] = kv[tid];
|
||||
v_cache[off] = v * fac_s;
|
||||
}
|
||||
@@ -1905,7 +1905,7 @@ __global__ void k_kv_store(const float* __restrict__ k_in, const float* __restri
|
||||
extern "C" int vetta_kv_store(const float* k_in, const float* v_in, float* k_cache,
|
||||
float* v_cache, int pos, int n_kv, int hd, int seq,
|
||||
float rms_eps, vetta_stream_t stream) {
|
||||
if (n_kv <= 0 || hd <= 0 || hd > 256 || seq <= 0 || pos < 0 || pos >= seq) return 1;
|
||||
if (n_kv <= 0 || hd <= 0 || hd > 256 || seq <= 0 || pos < 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_kv_store<<<n_kv, 256, 0, s>>>(k_in, v_in, k_cache, v_cache, pos, n_kv, hd, seq, rms_eps);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
@@ -1933,7 +1933,7 @@ __global__ void k_kv_store_f16(const float* __restrict__ k_in, const float* __re
|
||||
if (tid == 0) fac_s = rsqrtf(red[0] / (float)hd + rms_eps);
|
||||
__syncthreads();
|
||||
if (tid < hd) {
|
||||
const size_t off = (size_t)h * seq * hd + (size_t)pos * hd + tid;
|
||||
const size_t off = (size_t)h * seq * hd + (size_t)(pos % seq) * hd + tid;
|
||||
k_cache[off] = __half_as_ushort(__float2half_rn(kv[tid]));
|
||||
v_cache[off] = __half_as_ushort(__float2half_rn(v * fac_s));
|
||||
}
|
||||
@@ -1942,7 +1942,7 @@ __global__ void k_kv_store_f16(const float* __restrict__ k_in, const float* __re
|
||||
extern "C" int vetta_kv_store_f16(const float* k_in, const float* v_in, uint16_t* k_cache,
|
||||
uint16_t* v_cache, int pos, int n_kv, int hd, int seq,
|
||||
float rms_eps, vetta_stream_t stream) {
|
||||
if (n_kv <= 0 || hd <= 0 || hd > 256 || seq <= 0 || pos < 0 || pos >= seq) return 1;
|
||||
if (n_kv <= 0 || hd <= 0 || hd > 256 || seq <= 0 || pos < 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_kv_store_f16<<<n_kv, 256, 0, s>>>(k_in, v_in, k_cache, v_cache, pos, n_kv, hd, seq, rms_eps);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
@@ -1973,7 +1973,7 @@ __global__ void k_kv_store_f16_batched(const float* __restrict__ k_in, const flo
|
||||
if (tid == 0) fac_s = rsqrtf(red[0] / (float)hd + rms_eps);
|
||||
__syncthreads();
|
||||
if (tid < hd) {
|
||||
const size_t off = (size_t)h * seq * hd + (size_t)pos[m] * hd + tid;
|
||||
const size_t off = (size_t)h * seq * hd + (size_t)(pos[m] % seq) * hd + tid;
|
||||
k_cache[off] = __half_as_ushort(__float2half_rn(kv[tid]));
|
||||
v_cache[off] = __half_as_ushort(__float2half_rn(v * fac_s));
|
||||
}
|
||||
@@ -2588,8 +2588,8 @@ __global__ void __launch_bounds__(128) k_flash_swa_gqa_prefill_wmma(
|
||||
const int r = idx >> 5, c4 = idx & 31;
|
||||
const int kp = kb + r;
|
||||
uint16_t* dst = &kv.sK[r * FQ_LDK + c4 * 4];
|
||||
if (kp <= q_max_pos && kp < seq) {
|
||||
*reinterpret_cast<uint2*>(dst) = *reinterpret_cast<const uint2*>(k + ((size_t)kvh * seq + kp) * hd + c4 * 4);
|
||||
if (kp <= q_max_pos) {
|
||||
*reinterpret_cast<uint2*>(dst) = *reinterpret_cast<const uint2*>(k + ((size_t)kvh * seq + (kp % seq)) * hd + c4 * 4);
|
||||
} else { *reinterpret_cast<uint2*>(dst) = make_uint2(0u, 0u); }
|
||||
}
|
||||
__syncthreads();
|
||||
@@ -2655,7 +2655,7 @@ __global__ void __launch_bounds__(128) k_flash_swa_gqa_prefill_wmma(
|
||||
for (int idx = tid; idx < FQ_HD * FQ_BK; idx += 128) {
|
||||
const int d = idx >> 6, c = idx & 63;
|
||||
const int kp = kb + c;
|
||||
kv.sVt[d * FQ_LDV + c] = (kp <= q_max_pos && kp < seq) ? v[((size_t)kvh * seq + kp) * hd + d] : (uint16_t)0;
|
||||
kv.sVt[d * FQ_LDV + c] = (kp <= q_max_pos) ? v[((size_t)kvh * seq + (kp % seq)) * hd + d] : (uint16_t)0;
|
||||
}
|
||||
__syncthreads();
|
||||
// 3E. O[16,128] += P[16,64] @ V[64,128] (4 kt x 8 d-tiles)
|
||||
|
||||
Reference in new issue
Block a user