Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Hy4 prefill HIP sparse MLA: two barriers per tile (V^T write moved into the QK phase), split QK accumulator chains, peaky-softmax tests, ablation switches, WMMA peak probes

YanissAmz0002518fea88
diff --git a/hipsp/occ.cu b/hipsp/occ.cu
new file mode 100644
index 0000000..377dece
--- /dev/null
+++ b/hipsp/occ.cu
@@ -0,0 +1,7 @@
+#include "sp_mla.cu"
+#include <cstdio>
+int main() {
+ int nb = 0; hipOccupancyMaxActiveBlocksPerMultiprocessor(&nb, (const void*)sp_mla<4, true>, 512, 0);
+ hipDeviceProp_t p; hipGetDeviceProperties(&p, 0);
+ printf("max blocks/CU %d, CUs %d, lds/block max %zu, lds/CU %zu, clock %d kHz, regs/block %d\n", nb, p.multiProcessorCount, p.sharedMemPerBlock, p.maxSharedMemoryPerMultiProcessor, p.clockRate, p.regsPerBlock);
+}
diff --git a/hipsp/sp_mla.cu b/hipsp/sp_mla.cu
index 1e55112..599aa88 100644
--- a/hipsp/sp_mla.cu
+++ b/hipsp/sp_mla.cu
@@ -14,6 +14,14 @@ typedef __attribute__((__vector_size__(8 * sizeof(float)))) float f8;
#define KPITCH 1168 // bytes per K row in LDS (73 * 16: conflict free b128 rows)
#define VPITCH 48 // bytes per V^T row (16 keys * 2 + 16 pad)
#define LAZY 8.0f
+#ifdef NO_SYNC
+#define SYNC() ((void)0)
+#else
+#define SYNC() __syncthreads()
+#endif
+#ifndef HALFK
+#define HALFK 0
+#endif
__device__ __forceinline__ uint32_t perm16(uint32_t x) { return __builtin_amdgcn_permlanex16(x, x, 0x76543210, 0xfedcba98, false, false); }
__device__ __forceinline__ float permf(float x) { return __builtin_bit_cast(float, perm16(__builtin_bit_cast(uint32_t, x))); }
@@ -29,7 +37,7 @@ __device__ __forceinline__ uint32_t pk2s(float a, float b)
}
template <int KP, bool SINK>
-__global__ __launch_bounds__(512, 1) void sp_mla(const _Float16* __restrict__ Q, const _Float16* __restrict__ KC, const float* __restrict__ sink, const int* __restrict__ gidx,
+__global__ __launch_bounds__(512) void sp_mla(const _Float16* __restrict__ Q, const _Float16* __restrict__ KC, const float* __restrict__ sink, const int* __restrict__ gidx,
const int* __restrict__ nsel, _Float16* __restrict__ O, int T, int qstart, int H, int GS, int N, float scale, int tail, int fixed_tiles)
{
__shared__ __attribute__((aligned(16))) char Kn[16 * KPITCH];
@@ -93,31 +101,62 @@ __global__ __launch_bounds__(512, 1) void sp_mla(const _Float16* __restrict__ Q,
}
};
__syncthreads();
- if (ntiles > 0) { gload(0); wK(); wV(); }
+ if (ntiles > 0) { gload(0); wK(); }
__syncthreads();
const char* kb = Kn + n * KPITCH + 2 * 144 * qq;
float* spw = Sp + ((mt * 4 + qq) * 16 + hh) * 16 + n;
for (int tau = 0; tau < ntiles; tau++) {
const bool more = tau + 1 < ntiles;
+#ifndef NO_GATHER
+ wV();
+#endif
+ // Vt_tau (free: every wave passed the end barrier of tau-1)
+#ifndef NO_GATHER
if (more) gload(tau + 1);
+#endif
f8 s = (f8){0, 0, 0, 0, 0, 0, 0, 0};
+#ifndef NO_QK
+ f8 s1 = s, s2 = s;
#pragma unroll
for (int k = 0; k < 9; k++) {
const uint4* kp = (const uint4*)(kb + 32 * k);
- s = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(mk16(kp[0], kp[1]), qf[k], s);
+ #ifdef NO_LDSRD
+ const h16 af = qf[(k + 1) % 9];
+#else
+ const h16 af = mk16(kp[0], HALFK ? kp[0] : kp[1]);
+#endif
+ if (k % 3 == 0) s = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(af, qf[k], s);
+ else if (k % 3 == 1) s1 = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(af, qf[k], s1);
+ else s2 = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(af, qf[k], s2);
}
#pragma unroll
+ for (int i = 0; i < 8; i++) s[i] = (s[i] + s1[i]) + s2[i];
+#endif
+#pragma unroll
+#ifndef NO_SP
for (int i = 0; i < 8; i++) spw[2 * i * 16] = s[i];
- __syncthreads();
+#endif
+ SYNC();
+#ifndef NO_GATHER
if (more) wK();
+#endif
+ // Kn_{tau+1}: every wave finished QK(tau)
float sv[8];
#pragma unroll
for (int i = 0; i < 8; i++) {
float p[4];
#pragma unroll
+#ifdef NO_SP
+ for (int q2 = 0; q2 < 4; q2++) p[q2] = s[i];
+#else
for (int q2 = 0; q2 < 4; q2++) p[q2] = (q2 == qq) ? s[i] : Sp[(((mt * 4 + q2) * 16 + 2 * i + hh) * 16) + n];
+#endif
sv[i] = (((p[0] + p[1]) + p[2]) + p[3]) * c;
}
+#ifdef NO_SOFT
+ uint32_t pw[4]; const float alpha = 1.f; const bool upd = false;
+ for (int i = 0; i < 8; i += 2) pw[i >> 1] = pk2(sv[i], sv[i + 1]);
+#else
float mx = -INFINITY;
#pragma unroll
for (int i = 0; i < 8; i++) {
@@ -145,18 +184,28 @@ __global__ __launch_bounds__(512, 1) void sp_mla(const _Float16* __restrict__ Q,
#pragma unroll
for (int j = 0; j < 8; j++) acc[i][j] *= alpha;
}
+#endif
+ #ifdef DEPFREE
+ for (int i = 0; i < 8; i++) ls += s[i];
+ const h16 pb = mk16(make_uint4(tau, n, hh, 7), make_uint4(3, 4, tau, 5));
+#else
const h16 pb = mk16(make_uint4(pw[0], pw[1], pw[2], pw[3]), make_uint4(perm16(pw[0]), perm16(pw[1]), perm16(pw[2]), perm16(pw[3])));
+#endif
+#ifndef NO_PV
#pragma unroll
for (int tt = 0; tt < 8; tt++) {
const int d = (16 * (8 * qq + tt) + n);
const int pd = d ^ ((d >> 3) & 3);
const char* vp = Vt + pd * VPITCH;
- const uint4 a0 = *(const uint4*)(vp + 16 * hh), a1 = *(const uint4*)(vp + 16 * (hh ^ 1));
+ #ifdef NO_LDSRD
+ const uint4 a0 = make_uint4(pw[0] + tt, pw[1], pw[2], pw[3] ^ tt), a1 = make_uint4(pw[1], pw[2] + tt, pw[3], pw[0]);
+#else
+ const uint4 a0 = *(const uint4*)(vp + 16 * hh), a1 = HALFK ? a0 : *(const uint4*)(vp + 16 * (hh ^ 1));
+#endif
acc[tt] = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(mk16(a0, a1), pb, acc[tt]);
}
- __syncthreads();
- if (more) wV();
- __syncthreads();
+#endif
+ SYNC();
}
ls += permf(ls);
const float inv = 1.f / fmaxf(ls, 1e-30f);
diff --git a/hipsp/test_sp_hip.py b/hipsp/test_sp_hip.py
index ac3fc54..51273bb 100644
--- a/hipsp/test_sp_hip.py
+++ b/hipsp/test_sp_hip.py
@@ -10,7 +10,7 @@ import sp_hip
dev = "cuda"
H, R, Pd, KP, NTOP = 64, 512, 64, 4, 511
scale = 1 / 16.0
-def make(N, T, tail, ntop, seed, sc_scale=1.0, sink=True):
+def make(N, T, tail, ntop, seed, sc_scale=1.0, sink=True, qmul=1.0):
g = torch.Generator(device=dev); g.manual_seed(seed)
qstart = N - T; P = (N + KP - 1) // KP
sc = torch.randn(T, P, device=dev, generator=g) * sc_scale
@@ -18,7 +18,7 @@ def make(N, T, tail, ntop, seed, sc_scale=1.0, sink=True):
gend = torch.arange(P, device=dev) * KP + KP - 1
sc = sc.masked_fill(~(gend[None] < pos[:, None]), float("-inf"))
sp = PK.ix_select(sc, qstart, N, ntop, KP, tail, want_bias=False, sparse=True)
- qc = (torch.randn(H, T, R + Pd, device=dev, generator=g) * 0.3).half()
+ qc = (torch.randn(H, T, R + Pd, device=dev, generator=g) * 0.3 * qmul).half()
kc = torch.randn(N, R + Pd, device=dev, generator=g) * 0.3
sinks = torch.randn(H, device=dev, generator=g) if sink else None
return qstart, sp, qc, kc, sinks
@@ -40,6 +40,7 @@ def ref64(qstart, sp, qc, kc, sinks):
ok = True
cases = [(3000, 256, True, 511), (3000, 256, False, 511), (2999, 129, True, 511), (2998, 64, False, 40), (41, 41, True, 511), (41, 41, False, 511), (7, 7, True, 511), (1, 1, False, 511), (130, 64, True, 3),
(33001, 48, True, 511), (33000, 48, False, 511), (4100, 32, True, 5)]
+peaky = [(3000, 128, True, 511), (3001, 128, False, 100), (700, 64, True, 20)]
for (N, T, tail, ntop) in cases:
for sink in (True, False):
qstart, sp, qc, kc, sinks = make(N, T, tail, ntop, N + T, sink=sink)
@@ -56,4 +57,13 @@ for (N, T, tail, ntop) in cases:
good = err < 1.5e-3 and bool(torch.isfinite(o).all())
ok &= good
print(f"N {N} T {T} tail {int(tail)} ntop {ntop} sink {int(sink)} nsel mean {float(sp.nsel.float().mean()):.0f}: err vs fp64 {err:.2e} (max {mx:.1e}) | triton vs fp64 {etr0:.2e} | hip vs triton {etr:.2e} {'OK' if good else 'FAIL'}", flush=True)
+for (N, T, tail, ntop) in peaky:
+ for qmul in (6.0, 30.0):
+ for sink in (True, False):
+ qstart, sp, qc, kc, sinks = make(N, T, tail, ntop, N + T + 7, sink=sink, qmul=qmul)
+ kc16 = kc.clamp(-65504.0, 65504.0).half().contiguous()
+ o = sp_hip.mla_sparse_hip(R, kc16, qstart, scale, sinks, sp, qc).double(); torch.cuda.synchronize()
+ ref = ref64(qstart, sp, qc, kc, sinks)
+ err = float((o - ref).norm() / ref.norm().clamp_min(1e-30)); good = err < 2e-3 and bool(torch.isfinite(o).all()); ok &= good
+ print(f"PEAKY N {N} T {T} tail {int(tail)} ntop {ntop} qmul {qmul} sink {int(sink)}: err vs fp64 {err:.2e} {'OK' if good else 'FAIL'}", flush=True)
print("ALL OK" if ok else "FAILURES")
diff --git a/hipsp/wpeak.cu b/hipsp/wpeak.cu
new file mode 100644
index 0000000..60b4693
--- /dev/null
+++ b/hipsp/wpeak.cu
@@ -0,0 +1,25 @@
+#include <hip/hip_runtime.h>
+#include <cstdio>
+typedef __attribute__((__vector_size__(16 * sizeof(__fp16)))) __fp16 h16;
+typedef __attribute__((__vector_size__(8 * sizeof(float)))) float f8;
+template <int NACC> __global__ __launch_bounds__(512) void k(float* out, int iters) {
+ h16 a, b; for (int i = 0; i < 16; i++) { a[i] = (__fp16)(threadIdx.x * 0.001f + i); b[i] = (__fp16)(0.5f); }
+ f8 acc[NACC]; for (int i = 0; i < NACC; i++) acc[i] = (f8){0,0,0,0,0,0,0,0};
+ for (int it = 0; it < iters; it++) {
+#pragma unroll
+ for (int i = 0; i < NACC; i++) acc[i] = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, acc[i]);
+ }
+ float s = 0; for (int i = 0; i < NACC; i++) for (int j = 0; j < 8; j++) s += acc[i][j];
+ out[blockIdx.x * 512 + threadIdx.x] = s;
+}
+template <int NACC> void run(int waves_per_block) {
+ float* o; hipMalloc(&o, 4 * 512 * 4096);
+ int iters = 4096 / NACC * 16; int blocks = 400;
+ k<NACC><<<blocks, 512>>>(o, 10); hipDeviceSynchronize();
+ hipEvent_t e0, e1; hipEventCreate(&e0); hipEventCreate(&e1); hipEventRecord(e0);
+ k<NACC><<<blocks, 512>>>(o, iters); hipEventRecord(e1); hipEventSynchronize(e1);
+ float ms; hipEventElapsedTime(&ms, e0, e1);
+ double fl = (double)blocks * 16 * iters * NACC * 8192.0;
+ printf("NACC %d: %.1f TF\n", NACC, fl / ms / 1e9);
+}
+int main() { run<1>(16); run<2>(16); run<4>(16); run<8>(16); }
diff --git a/hipsp/wpeak2.cu b/hipsp/wpeak2.cu
new file mode 100644
index 0000000..7b87754
--- /dev/null
+++ b/hipsp/wpeak2.cu
@@ -0,0 +1,25 @@
+#include <hip/hip_runtime.h>
+#include <cstdio>
+typedef __attribute__((__vector_size__(16 * sizeof(__fp16)))) __fp16 h16;
+typedef __attribute__((__vector_size__(8 * sizeof(float)))) float f8;
+template <int NACC> __global__ __launch_bounds__(512) void k(float* out, int iters) {
+ h16 a[4], b[4]; unsigned x = threadIdx.x * 2654435761u + blockIdx.x; for (int j = 0; j < 4; j++) for (int i = 0; i < 16; i++) { x = x * 1664525u + 1013904223u; a[j][i] = (__fp16)(((x >> 16) & 0xffff) / 65536.0f - 0.5f); x = x * 1664525u + 1013904223u; b[j][i] = (__fp16)(((x >> 16) & 0xffff) / 65536.0f - 0.5f); }
+ f8 acc[NACC]; for (int i = 0; i < NACC; i++) acc[i] = (f8){0,0,0,0,0,0,0,0};
+ for (int it = 0; it < iters; it++) {
+#pragma unroll
+ for (int i = 0; i < NACC; i++) acc[i] = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a[i & 3], b[(i + it) & 3], acc[i]);
+ }
+ float s = 0; for (int i = 0; i < NACC; i++) for (int j = 0; j < 8; j++) s += acc[i][j];
+ out[blockIdx.x * 512 + threadIdx.x] = s;
+}
+template <int NACC> void run(int waves_per_block) {
+ float* o; hipMalloc(&o, 4 * 512 * 4096);
+ int iters = 4096 / NACC * 16; int blocks = 400;
+ k<NACC><<<blocks, 512>>>(o, 10); hipDeviceSynchronize();
+ hipEvent_t e0, e1; hipEventCreate(&e0); hipEventCreate(&e1); hipEventRecord(e0);
+ k<NACC><<<blocks, 512>>>(o, iters); hipEventRecord(e1); hipEventSynchronize(e1);
+ float ms; hipEventElapsedTime(&ms, e0, e1);
+ double fl = (double)blocks * 16 * iters * NACC * 8192.0;
+ printf("NACC %d: %.1f TF\n", NACC, fl / ms / 1e9);
+}
+int main() { run<1>(16); run<2>(16); run<4>(16); run<8>(16); }