gdn_rec_parity: both geometries, the engine's launches against a host reference, the negative controls
The copied kernel variants become templates on <HV, HK, C>; the bit checks run at 48/16/10240 and 32/16/8192. New: a double-precision transcription of the recurrence (decay before the rank-1 update, v-head h paired with k-head h % HK, the gated RMS-normed output) that the engine's own gdn_recurrence and gdn_conv are held to (FP16 output 2e-3, FP32 state 1e-4 relative L1), with the rival readings the oracle names - the other model's gate, the interleaved pairing - required to sit more than 5 % away; and the geometry refusals (a third value head per key head on 32 heads, 4 per key head). Links strata_prefill. 4090 (sm_89): 0 failures; the rivals 0.86 to 1.33 relative L1 away. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017tPVMg2wGSNR1KANEiUtwr
This commit is contained in:
1 parent
258ccb9280
commit
4f3edf9229
3 files changed
+256
-47
No files matched your search
+3
-2
@@ -700,11 +700,12 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
|
||||
target_include_directories(iq_multi_parity PRIVATE ${_strata_gpu_include_directories})
|
||||
target_link_libraries(iq_multi_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})
|
||||
add_test(NAME iq_multi_parity COMMAND iq_multi_parity)
|
||||
# the prompt path's DeltaNet recurrence: gdn_rec_kh_kernel against gdn_rec_cols_pipe_kernel, bitwise (GPU,
|
||||
# the prompt path's DeltaNet recurrence: gdn_rec_kh_kernel against gdn_rec_cols_pipe_kernel, bitwise, and the engine's
|
||||
# launches against a host reference at both geometries with the negative controls (Qwen3.6 step 3; GPU,
|
||||
# synthetic, no model; --bench adds timings). NVIDIA only: cp.async and the CUDA occupancy API (not on gfx906)
|
||||
if(STRATA_ENABLE_CUDA AND NOT STRATA_HIP_GFX906)
|
||||
add_executable(gdn_rec_parity src/prefill/gdn_rec_parity.cu)
|
||||
target_link_libraries(gdn_rec_parity PRIVATE ${_strata_gpu_runtime_target})
|
||||
target_link_libraries(gdn_rec_parity PRIVATE strata_prefill ${_strata_gpu_runtime_target})
|
||||
add_test(NAME gdn_rec_parity COMMAND gdn_rec_parity)
|
||||
# the prompt path's BF16 products against cuBLAS's own BF16 GEMM, within fp32 rounding (sm_7x runs them on the
|
||||
# FP16 tensor cores; GPU, synthetic, no model; --bench adds timings)
|
||||
|
||||
+251
-43
@@ -1,23 +1,38 @@
|
||||
// src/prefill/gdn_rec_parity.cu - the prompt path's DeltaNet recurrence, bit for bit and timed.
|
||||
// src/prefill/gdn_rec_parity.cu - the prompt path's DeltaNet recurrence, bit for bit, against a reference, and timed.
|
||||
//
|
||||
// src/prefill/kernels.cu runs gdn_rec_kh_kernel (the three value heads that share a key head in one thread, the
|
||||
// inputs staged in blocks of 8 tokens) on CUDA sm_80+ cards that hold all 64 of its blocks at once, and
|
||||
// src/prefill/kernels.cu runs gdn_rec_kh_kernel (the value heads that share a key head in one thread, the inputs
|
||||
// staged in blocks of 8 tokens) on CUDA sm_80+ cards that hold all 64 of its blocks at once, and
|
||||
// gdn_rec_cols_pipe_kernel elsewhere and with STRATA_GDN_KEYHEAD=0. Both are copied below verbatim, with the output
|
||||
// norm (gdn_out_norm_kernel) and the norm as it was before, which also stored the normalized value in FP32 although
|
||||
// nothing reads it. The two recurrences do the same arithmetic in the same order per value head and column, so the
|
||||
// output, the state after it and the FP16 output must be the same bits.
|
||||
//
|
||||
// Qwen3.6 port (step 3): the kernels are templates on the head geometry, <HV, HK, C> = <48, 16, 10240> (Flash-Next,
|
||||
// 3 value heads per key head, the sigmoid(z) output gate) and <32, 16, 8192> (Qwen3.6, 2 per key head, SiLU(z)), and
|
||||
// the engine's own launches (gdn_conv, gdn_recurrence through their GdnGeom) are held to a double-precision host
|
||||
// transcription of the recurrence (docs/qwen36-port/oracle-crosscheck.md, "Gated DeltaNet": decay before the rank-1
|
||||
// update, v-head h paired with k-head h % HK, the gated RMS-normed output). Negative controls, in the repo's
|
||||
// discipline (gdn_parity.cpp): each rival reading is computed with the same reference and must sit more than 5 %
|
||||
// relative L1 from the right one before the kernel is judged - the sigmoid gate on the SiLU model, the interleaved
|
||||
// head pairing (h // VPK) - and a geometry the kernels are not built for (a third value head per key head on 32 heads,
|
||||
// i.e. Flash-Next's 48-head channel layout on Qwen3.6's heads) is refused by gdn_geometry_supported, not mis-indexed.
|
||||
//
|
||||
// Synthetic inputs of the model's shape (q/k L2-normalized per key head as gdn_l2_kernel leaves them), no model.
|
||||
// gdn_rec_parity the bit checks (T from 1 to 4099, a non-zero state as after an earlier chunk)
|
||||
// gdn_rec_parity --bench and the timings at T = 8192 and 32768 (one layer, all 48 value heads); the two
|
||||
// variants' launches alternate, since a card under load steps its clock down after a
|
||||
// fraction of a second (see native_grouped_parity); then both at 1 to 6 blocks per
|
||||
// SM, as on cards with more or fewer SMs (scale), and both with the engine's grids on
|
||||
// 32 to all of this card's SMs, the others held by a sleeping kernel (fewer_sms)
|
||||
// gdn_rec_parity the bit checks (T from 1 to 4099, a non-zero state as after an earlier chunk), both
|
||||
// geometries; the reference checks and the negative controls
|
||||
// gdn_rec_parity --bench and the timings at T = 8192 and 32768 (one layer, all value heads, both geometries);
|
||||
// the two variants' launches alternate, since a card under load steps its clock down
|
||||
// after a fraction of a second (see native_grouped_parity); then (Flash-Next) both at 1
|
||||
// to 6 blocks per SM, as on cards with more or fewer SMs (scale), and both with the
|
||||
// engine's grids on 32 to all of this card's SMs, the others held by a sleeping kernel
|
||||
// (fewer_sms)
|
||||
#include "strata/prefill/kernels.hpp"
|
||||
|
||||
#include <cuda_fp16.h>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -27,9 +42,11 @@
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int S = 128, HK = 16, HV = 48, C = 10240; // as kernels.cu: head size, key heads, value heads, q|k|v width
|
||||
constexpr int S = 128; // as kernels.cu: the head size (both models)
|
||||
constexpr int RG = 4, RPG = S / RG;
|
||||
constexpr int CB = 32, NCB = S / CB;
|
||||
// the Flash-Next geometry the timing sections below keep (the engine's 192- and 64-block grids they describe)
|
||||
constexpr int FN_HV = 48, FN_HK = 16, FN_C = 10240;
|
||||
|
||||
void ck(cudaError_t e, const char* what) {
|
||||
if (e != cudaSuccess) {
|
||||
@@ -50,6 +67,7 @@ __device__ __forceinline__ uint16_t hf(float f) { return __half_as_ushort(__floa
|
||||
// gdn_rec_cols_kernel with the next token's inputs (q/k rows, v, gate, beta) loaded into registers while this token
|
||||
// computes (software pipelining). The same arithmetic in the same order: the same bits, and the same CB-column split.
|
||||
// STRATA_GDN_PIPELINE=0: gdn_rec_cols_kernel.
|
||||
template <int HV, int HK, int C>
|
||||
__global__ void __launch_bounds__(CB * RG) gdn_rec_cols_pipe_kernel(float* __restrict__ state, const float* __restrict__ h,
|
||||
const float* __restrict__ gate,
|
||||
const float* __restrict__ beta,
|
||||
@@ -146,11 +164,13 @@ __device__ __forceinline__ void gdn_cp_wait_prev() { // every group but the ne
|
||||
asm volatile("cp.async.wait_group 1;\n" ::);
|
||||
#endif
|
||||
}
|
||||
constexpr int GDN_TB = 8, VPK = HV / HK; // tokens per staged block, value heads per key head
|
||||
constexpr int GDN_TB = 8; // tokens per staged block
|
||||
template <int HV, int HK, int C>
|
||||
__global__ void __launch_bounds__(CB * RG) gdn_rec_kh_kernel(float* __restrict__ state, const float* __restrict__ h,
|
||||
const float* __restrict__ gate,
|
||||
const float* __restrict__ beta,
|
||||
float* __restrict__ oc_out, int64_t T) {
|
||||
constexpr int VPK = HV / HK; // value heads per key head
|
||||
constexpr int TB = GDN_TB, NT = CB * RG, QKP = S / 4, VP = CB / 4; // threads, 16-byte pieces of a q/k row, of v
|
||||
__shared__ __align__(16) float sq[2][TB][S];
|
||||
__shared__ __align__(16) float sk[2][TB][S];
|
||||
@@ -240,8 +260,10 @@ __global__ void __launch_bounds__(CB * RG) gdn_rec_kh_kernel(float* __restrict__
|
||||
for (int r = 0; r < RPG; ++r) base[r * rs] = s[j][r];
|
||||
}
|
||||
}
|
||||
template <bool SILU> __device__ __forceinline__ float gdn_gate(float z) { return SILU ? z * sigm(z) : sigm(z); }
|
||||
// the output norm over a head's 128 columns, into the FP16 copy the out projection reads (the FP32 output before the
|
||||
// norm stays in its scratch buffer)
|
||||
template <int HV, bool SILU>
|
||||
__global__ void __launch_bounds__(S) gdn_out_norm_kernel(const float* __restrict__ z, const float* __restrict__ gamma,
|
||||
float eps, const float* __restrict__ y,
|
||||
uint16_t* __restrict__ y16) {
|
||||
@@ -254,11 +276,12 @@ __global__ void __launch_bounds__(S) gdn_out_norm_kernel(const float* __restrict
|
||||
if ((col & 31) == 0) wsum[col >> 5] = sp;
|
||||
__syncthreads();
|
||||
const float ss = wsum[0] + wsum[1] + wsum[2] + wsum[3];
|
||||
const float v = oc * rsqrtf(ss / (float) S + eps) * gamma[col] * sigm(z[t * HV * S + head * S + col]);
|
||||
const float v = oc * rsqrtf(ss / (float) S + eps) * gamma[col] * gdn_gate<SILU>(z[t * HV * S + head * S + col]);
|
||||
y16[at] = hf(v);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ the output norm before (0.1.31)
|
||||
template <int HV, bool SILU>
|
||||
__global__ void __launch_bounds__(S) gdn_out_norm_old_kernel(const float* __restrict__ z, const float* __restrict__ gamma,
|
||||
float eps, float* __restrict__ y,
|
||||
uint16_t* __restrict__ y16) {
|
||||
@@ -271,7 +294,7 @@ __global__ void __launch_bounds__(S) gdn_out_norm_old_kernel(const float* __rest
|
||||
if ((col & 31) == 0) wsum[col >> 5] = sp;
|
||||
__syncthreads();
|
||||
const float ss = wsum[0] + wsum[1] + wsum[2] + wsum[3];
|
||||
const float v = oc * rsqrtf(ss / (float) S + eps) * gamma[col] * sigm(z[t * HV * S + head * S + col]);
|
||||
const float v = oc * rsqrtf(ss / (float) S + eps) * gamma[col] * gdn_gate<SILU>(z[t * HV * S + head * S + col]);
|
||||
y[at] = v;
|
||||
y16[at] = hf(v);
|
||||
}
|
||||
@@ -291,6 +314,7 @@ __global__ void fill_gauss(float* x, int64_t n, float scale, uint64_t seed, floa
|
||||
x[i] = offset + scale * gauss(seed * 0x9E3779B97F4A7C15ULL + (uint64_t) i);
|
||||
}
|
||||
// the q and k rows L2-normalized per key head, as gdn_l2_kernel leaves them for the recurrence
|
||||
template <int C>
|
||||
__global__ void l2_rows(float* h, float eps) {
|
||||
float* x = h + (int64_t) blockIdx.y * C + (int64_t) blockIdx.x * S;
|
||||
const float v = x[threadIdx.x];
|
||||
@@ -388,6 +412,7 @@ unsigned long long diff_words(const void* a, const void* b, size_t bytes) {
|
||||
return h;
|
||||
}
|
||||
|
||||
template <int HV, int HK, int C>
|
||||
struct Inputs {
|
||||
int64_t T = 0;
|
||||
float *h = nullptr, *gate = nullptr, *beta = nullptr, *z = nullptr, *gamma = nullptr, *state0 = nullptr;
|
||||
@@ -399,7 +424,7 @@ struct Inputs {
|
||||
gamma = dalloc<float>(S);
|
||||
state0 = dalloc<float>((size_t) S * HV * S);
|
||||
fill_gauss<<<4096, 256>>>(h, T * C, 1.0f, seed);
|
||||
l2_rows<<<dim3(2 * HK, (unsigned) T), S>>>(h, 1e-6f);
|
||||
l2_rows<C><<<dim3(2 * HK, (unsigned) T), S>>>(h, 1e-6f);
|
||||
fill_gates<<<1024, 256>>>(gate, beta, T * HV, seed + 1);
|
||||
fill_gauss<<<4096, 256>>>(z, T * HV * S, 1.0f, seed + 2);
|
||||
fill_gauss<<<1, 128>>>(gamma, S, 0.1f, seed + 3, 1.0f);
|
||||
@@ -412,15 +437,17 @@ struct Inputs {
|
||||
};
|
||||
|
||||
enum Rec { kBefore, kKeyHead, kRecCount };
|
||||
void run_rec(Rec v, float* state, const Inputs& in, float* oc, cudaStream_t s = 0) {
|
||||
template <int HV, int HK, int C>
|
||||
void run_rec(Rec v, float* state, const Inputs<HV, HK, C>& in, float* oc, cudaStream_t s = 0) {
|
||||
if (v == kBefore)
|
||||
gdn_rec_cols_pipe_kernel<<<HV * NCB, dim3(CB, RG), 0, s>>>(state, in.h, in.gate, in.beta, oc, in.T);
|
||||
gdn_rec_cols_pipe_kernel<HV, HK, C><<<HV * NCB, dim3(CB, RG), 0, s>>>(state, in.h, in.gate, in.beta, oc, in.T);
|
||||
else
|
||||
gdn_rec_kh_kernel<<<HK * NCB, dim3(CB, RG), 0, s>>>(state, in.h, in.gate, in.beta, oc, in.T);
|
||||
gdn_rec_kh_kernel<HV, HK, C><<<HK * NCB, dim3(CB, RG), 0, s>>>(state, in.h, in.gate, in.beta, oc, in.T);
|
||||
}
|
||||
|
||||
template <int HV, int HK, int C>
|
||||
int check_bits(int64_t T, uint64_t seed) {
|
||||
Inputs in(T, seed);
|
||||
Inputs<HV, HK, C> in(T, seed);
|
||||
const size_t st = (size_t) S * HV * S, oc_n = (size_t) T * HV * S;
|
||||
float* state[kRecCount];
|
||||
float* oc[kRecCount];
|
||||
@@ -428,7 +455,7 @@ int check_bits(int64_t T, uint64_t seed) {
|
||||
state[v] = dalloc<float>(st);
|
||||
oc[v] = dalloc<float>(oc_n);
|
||||
ck(cudaMemcpy(state[v], in.state0, st * 4, cudaMemcpyDeviceToDevice), "state copy");
|
||||
run_rec((Rec) v, state[v], in, oc[v]);
|
||||
run_rec<HV, HK, C>((Rec) v, state[v], in, oc[v]);
|
||||
}
|
||||
ck(cudaDeviceSynchronize(), "recurrence");
|
||||
int fails = 0;
|
||||
@@ -444,15 +471,16 @@ int check_bits(int64_t T, uint64_t seed) {
|
||||
uint16_t* y16b = dalloc<uint16_t>(oc_n);
|
||||
float* yb = dalloc<float>(oc_n);
|
||||
ck(cudaMemcpy(yb, oc[kBefore], oc_n * 4, cudaMemcpyDeviceToDevice), "y copy");
|
||||
gdn_out_norm_kernel<<<dim3((unsigned) T, HV), S>>>(in.z, in.gamma, 1e-6f, yb, y16b);
|
||||
gdn_out_norm_old_kernel<<<dim3((unsigned) T, HV), S>>>(in.z, in.gamma, 1e-6f, oc[kBefore], y16a);
|
||||
constexpr bool SILU = HV == 32; // the Qwen3.6 geometry gates with SiLU
|
||||
gdn_out_norm_kernel<HV, SILU><<<dim3((unsigned) T, HV), S>>>(in.z, in.gamma, 1e-6f, yb, y16b);
|
||||
gdn_out_norm_old_kernel<HV, SILU><<<dim3((unsigned) T, HV), S>>>(in.z, in.gamma, 1e-6f, oc[kBefore], y16a);
|
||||
ck(cudaDeviceSynchronize(), "norm");
|
||||
const unsigned long long d16 = diff_words(y16a, y16b, oc_n * 2);
|
||||
if (d16) {
|
||||
std::printf("FAIL T=%lld output norm: %llu of %zu FP16 pairs differ\n", (long long) T, d16, oc_n / 2);
|
||||
++fails;
|
||||
}
|
||||
std::printf("T=%-6lld %s\n", (long long) T, fails ? "DIFFERENT" : "the same bits (output, state, FP16 output)");
|
||||
std::printf("%d/%d/%d T=%-6lld %s\n", HV, HK, C, (long long) T, fails ? "DIFFERENT" : "the same bits (output, state, FP16 output)");
|
||||
for (int v = 0; v < kRecCount; ++v) { cudaFree(state[v]); cudaFree(oc[v]); }
|
||||
cudaFree(y16a); cudaFree(y16b); cudaFree(yb);
|
||||
return fails;
|
||||
@@ -463,8 +491,10 @@ float median(std::vector<float> v) {
|
||||
return v.empty() ? 0.0f : v[v.size() / 2];
|
||||
}
|
||||
|
||||
template <int HV, int HK, int C>
|
||||
void bench(int64_t T) {
|
||||
Inputs in(T, 0xB3);
|
||||
constexpr bool SILU = HV == 32;
|
||||
Inputs<HV, HK, C> in(T, 0xB3);
|
||||
const size_t st = (size_t) S * HV * S, oc_n = (size_t) T * HV * S;
|
||||
float* state = dalloc<float>(st);
|
||||
float* oc = dalloc<float>(oc_n);
|
||||
@@ -487,7 +517,7 @@ void bench(int64_t T) {
|
||||
for (int i = 0; i < warm + rounds; ++i) {
|
||||
for (int j = 0; j < kRecCount; ++j) {
|
||||
const Rec v = (Rec) ((i + j) % kRecCount); // a different variant first in each round
|
||||
const float ms = timed([&] { run_rec(v, state, in, oc); });
|
||||
const float ms = timed([&] { run_rec<HV, HK, C>(v, state, in, oc); });
|
||||
if (i >= warm) t_rec[v].push_back(ms);
|
||||
}
|
||||
}
|
||||
@@ -495,22 +525,23 @@ void bench(int64_t T) {
|
||||
for (int j = 0; j < 2; ++j) {
|
||||
const int v = (i + j) % 2;
|
||||
const float ms = timed([&] {
|
||||
if (v == 0) gdn_out_norm_old_kernel<<<dim3((unsigned) T, HV), S>>>(in.z, in.gamma, 1e-6f, oc, y16);
|
||||
else gdn_out_norm_kernel<<<dim3((unsigned) T, HV), S>>>(in.z, in.gamma, 1e-6f, oc, y16);
|
||||
if (v == 0) gdn_out_norm_old_kernel<HV, SILU><<<dim3((unsigned) T, HV), S>>>(in.z, in.gamma, 1e-6f, oc, y16);
|
||||
else gdn_out_norm_kernel<HV, SILU><<<dim3((unsigned) T, HV), S>>>(in.z, in.gamma, 1e-6f, oc, y16);
|
||||
});
|
||||
if (i >= warm) t_norm[v].push_back(ms);
|
||||
}
|
||||
}
|
||||
const float r0 = median(t_rec[kBefore]), r1 = median(t_rec[kKeyHead]), n0 = median(t_norm[0]), n1 = median(t_norm[1]);
|
||||
std::printf(" T = %lld tokens, one layer (48 value heads), medians of %d alternating runs:\n", (long long) T, rounds);
|
||||
std::printf(" T = %lld tokens, one layer (%d value heads / %d key heads), medians of %d alternating runs:\n", (long long) T, HV, HK, rounds);
|
||||
std::printf(" recurrence, before (gdn_rec_cols_pipe_kernel) %8.2f ms (%.3f us per token)\n", r0,
|
||||
1000.0f * r0 / (float) T);
|
||||
std::printf(" recurrence, gdn_rec_kh_kernel %8.2f ms (%.3f us per token) %.2fx\n", r1,
|
||||
1000.0f * r1 / (float) T, r0 / r1);
|
||||
std::printf(" output norm, before (FP32 and FP16 store) %8.2f ms\n", n0);
|
||||
std::printf(" output norm, FP16 store only %8.2f ms %.2fx\n", n1, n0 / n1);
|
||||
std::printf(" 36 DeltaNet layers per %lld-token block: recurrence + norm %.0f ms -> %.0f ms (-%.0f ms)\n",
|
||||
(long long) T, 36.0f * (r0 + n0), 36.0f * (r1 + n1), 36.0f * (r0 + n0 - r1 - n1));
|
||||
const float nl = HV == 32 ? 30.0f : 36.0f; // the model's DeltaNet layers (Qwen3.6: 30 of 40; Flash-Next: 36 of 48)
|
||||
std::printf(" %.0f DeltaNet layers per %lld-token block: recurrence + norm %.0f ms -> %.0f ms (-%.0f ms)\n",
|
||||
nl, (long long) T, nl * (r0 + n0), nl * (r1 + n1), nl * (r0 + n0 - r1 - n1));
|
||||
cudaEventDestroy(e0);
|
||||
cudaEventDestroy(e1);
|
||||
cudaFree(state); cudaFree(oc); cudaFree(y16);
|
||||
@@ -523,15 +554,16 @@ void bench(int64_t T) {
|
||||
// its own state and output, all started together, so the block scheduler puts one block of each round on every SM.
|
||||
// The kernels are the ones above, unchanged.
|
||||
void scale(int64_t T) {
|
||||
constexpr int HV = FN_HV, HK = FN_HK, C = FN_C;
|
||||
int dev = 0, sms = 0, per_sm[kRecCount] = {};
|
||||
ck(cudaGetDevice(&dev), "device");
|
||||
ck(cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev), "SMs");
|
||||
ck(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm[kBefore], gdn_rec_cols_pipe_kernel, CB * RG, 0), "occupancy");
|
||||
ck(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm[kKeyHead], gdn_rec_kh_kernel, CB * RG, 0), "occupancy");
|
||||
ck(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm[kBefore], gdn_rec_cols_pipe_kernel<HV, HK, C>, CB * RG, 0), "occupancy");
|
||||
ck(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm[kKeyHead], gdn_rec_kh_kernel<HV, HK, C>, CB * RG, 0), "occupancy");
|
||||
constexpr int kMaxB = 6, kMaxLaunches = 6;
|
||||
const int full[kRecCount] = {HV * NCB, HK * NCB}; // the grids as the engine launches them
|
||||
auto parts = [&](int v) { return (sms + full[v] - 1) / full[v]; }; // launches per round
|
||||
Inputs in(T, 0xC7);
|
||||
Inputs<HV, HK, C> in(T, 0xC7);
|
||||
const size_t st = (size_t) S * HV * S, oc_n = (size_t) T * HV * S;
|
||||
float* state[kMaxLaunches];
|
||||
float* oc[kMaxLaunches];
|
||||
@@ -559,9 +591,9 @@ void scale(int64_t T) {
|
||||
const int k = c % np, grid = sms / np + (k < sms % np ? 1 : 0); // round c / np, part k
|
||||
if (c > 0) cudaStreamWaitEvent(stream[c], e0, 0);
|
||||
if (cs.v == kBefore)
|
||||
gdn_rec_cols_pipe_kernel<<<grid, dim3(CB, RG), 0, stream[c]>>>(state[c], in.h, in.gate, in.beta, oc[c], T);
|
||||
gdn_rec_cols_pipe_kernel<HV, HK, C><<<grid, dim3(CB, RG), 0, stream[c]>>>(state[c], in.h, in.gate, in.beta, oc[c], T);
|
||||
else
|
||||
gdn_rec_kh_kernel<<<grid, dim3(CB, RG), 0, stream[c]>>>(state[c], in.h, in.gate, in.beta, oc[c], T);
|
||||
gdn_rec_kh_kernel<HV, HK, C><<<grid, dim3(CB, RG), 0, stream[c]>>>(state[c], in.h, in.gate, in.beta, oc[c], T);
|
||||
cudaEventRecord(done[c], stream[c]);
|
||||
}
|
||||
for (int c = 1; c < n; ++c) cudaStreamWaitEvent(stream[0], done[c], 0);
|
||||
@@ -628,6 +660,7 @@ void scale(int64_t T) {
|
||||
// shows for every N where the two grids land: none on a held SM, and how many blocks share the busiest SM. The
|
||||
// recurrences are the ones above, unchanged; L2, memory and clock stay this card's.
|
||||
void fewer_sms(int64_t T) {
|
||||
constexpr int HV = FN_HV, HK = FN_HK, C = FN_C;
|
||||
int dev = 0, sms = 0, optin = 0, smem_sm = 0, reserved = 0, hold_per_sm = 0, per_sm[kRecCount] = {};
|
||||
ck(cudaGetDevice(&dev), "device");
|
||||
ck(cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev), "SMs");
|
||||
@@ -642,13 +675,13 @@ void fewer_sms(int64_t T) {
|
||||
// Loaded now rather than at a first launch: with lazy loading (CUDA 12's default) a kernel's first launch may wait
|
||||
// until the device is idle, and so for hold_sm, which waits for release_sms.
|
||||
cudaFuncAttributes fa[kRecCount], fx;
|
||||
ck(cudaFuncGetAttributes(&fa[kBefore], gdn_rec_cols_pipe_kernel), "attributes");
|
||||
ck(cudaFuncGetAttributes(&fa[kKeyHead], gdn_rec_kh_kernel), "attributes");
|
||||
ck(cudaFuncGetAttributes(&fa[kBefore], gdn_rec_cols_pipe_kernel<HV, HK, C>), "attributes");
|
||||
ck(cudaFuncGetAttributes(&fa[kKeyHead], gdn_rec_kh_kernel<HV, HK, C>), "attributes");
|
||||
for (const void* f : {(const void*) hold_sm, (const void*) wait_sitting, (const void*) release_sms, (const void*) land})
|
||||
ck(cudaFuncGetAttributes(&fx, f), "attributes");
|
||||
ck(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&hold_per_sm, hold_sm, 1, optin), "occupancy");
|
||||
ck(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm[kBefore], gdn_rec_cols_pipe_kernel, CB * RG, 0), "occupancy");
|
||||
ck(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm[kKeyHead], gdn_rec_kh_kernel, CB * RG, 0), "occupancy");
|
||||
ck(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm[kBefore], gdn_rec_cols_pipe_kernel<HV, HK, C>, CB * RG, 0), "occupancy");
|
||||
ck(cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm[kKeyHead], gdn_rec_kh_kernel<HV, HK, C>, CB * RG, 0), "occupancy");
|
||||
const int grid[kRecCount] = {HV * NCB, HK * NCB}; // as the engine launches them
|
||||
std::printf(" the engine's grids on N of this card's %d SMs (%lld tokens; the other SMs held by a sleeping kernel, see "
|
||||
"fewer_sms):\n", sms, (long long) T);
|
||||
@@ -679,7 +712,7 @@ void fewer_sms(int64_t T) {
|
||||
if (n < sms && (int64_t) n * per_sm[kBefore] >= grid[kBefore] && (int64_t) n * per_sm[kKeyHead] >= grid[kKeyHead])
|
||||
ns.push_back(n);
|
||||
|
||||
Inputs in(T, 0xD5);
|
||||
Inputs<HV, HK, C> in(T, 0xD5);
|
||||
const size_t st = (size_t) S * HV * S, oc_n = (size_t) T * HV * S;
|
||||
float* state = dalloc<float>(st);
|
||||
float* oc = dalloc<float>(oc_n);
|
||||
@@ -783,7 +816,7 @@ void fewer_sms(int64_t T) {
|
||||
ck(cudaMemcpyAsync(state, in.state0, st * 4, cudaMemcpyDeviceToDevice, s_run), "state");
|
||||
hold(n);
|
||||
ck(cudaEventRecord(e0, s_run), "event");
|
||||
run_rec(cs.v, state, in, oc, s_run);
|
||||
run_rec<HV, HK, C>(cs.v, state, in, oc, s_run);
|
||||
ck(cudaEventRecord(e1, s_run), "event");
|
||||
let_go(n);
|
||||
settle("fewer SMs");
|
||||
@@ -861,6 +894,165 @@ void fewer_sms(int64_t T) {
|
||||
done();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ the reference (a host transcription, double)
|
||||
// docs/qwen36-port/oracle-crosscheck.md, "Gated DeltaNet": per value head hv with key head qh = hv % HK (tiled; the
|
||||
// rival pairs hv with hv / VPK), per token: S *= exp(g); d = (v - S^T k) * beta; S += k (x) d; o = S^T q / sqrt(S);
|
||||
// y = rms_norm(o) * gamma * gate(z) with gate SiLU (Qwen3.6) or sigmoid (Flash-Next). The state is [k][hv][v].
|
||||
struct RefOpts { bool silu = true, tiled = true; };
|
||||
struct HostIn {
|
||||
int64_t T, HV, HK, C;
|
||||
std::vector<float> h, gate, beta, z, gamma, state0;
|
||||
template <int HV_, int HK_, int C_> explicit HostIn(const Inputs<HV_, HK_, C_>& in)
|
||||
: T(in.T), HV(HV_), HK(HK_), C(C_), h((size_t) in.T * C_), gate((size_t) in.T * HV_), beta((size_t) in.T * HV_),
|
||||
z((size_t) in.T * HV_ * S), gamma(S), state0((size_t) S * HV_ * S) {
|
||||
ck(cudaMemcpy(h.data(), in.h, h.size() * 4, cudaMemcpyDeviceToHost), "h");
|
||||
ck(cudaMemcpy(gate.data(), in.gate, gate.size() * 4, cudaMemcpyDeviceToHost), "gate");
|
||||
ck(cudaMemcpy(beta.data(), in.beta, beta.size() * 4, cudaMemcpyDeviceToHost), "beta");
|
||||
ck(cudaMemcpy(z.data(), in.z, z.size() * 4, cudaMemcpyDeviceToHost), "z");
|
||||
ck(cudaMemcpy(gamma.data(), in.gamma, gamma.size() * 4, cudaMemcpyDeviceToHost), "gamma");
|
||||
ck(cudaMemcpy(state0.data(), in.state0, state0.size() * 4, cudaMemcpyDeviceToHost), "state0");
|
||||
}
|
||||
};
|
||||
// y [T][HV][S] (the gated, normed output) and the state after the chunk
|
||||
void ref_rec(const HostIn& in, const RefOpts& o, std::vector<double>& y, std::vector<double>& state) {
|
||||
const int64_t T = in.T, HV = in.HV, HK = in.HK, C = in.C, VPK = HV / HK;
|
||||
state.assign(in.state0.begin(), in.state0.end());
|
||||
y.assign((size_t) T * HV * S, 0.0);
|
||||
std::vector<double> Sh((size_t) S * S), kv(S), d(S), oc(S);
|
||||
for (int64_t hv = 0; hv < HV; ++hv) {
|
||||
const int64_t qh = o.tiled ? hv % HK : hv / VPK;
|
||||
for (int i = 0; i < S; ++i) for (int j = 0; j < S; ++j) Sh[(size_t) i * S + j] = state[((size_t) i * HV + hv) * S + j];
|
||||
for (int64_t t = 0; t < T; ++t) {
|
||||
const float* ht = in.h.data() + t * C;
|
||||
const float *q = ht + qh * S, *k = ht + HK * S + qh * S, *v = ht + 2 * HK * S + hv * S;
|
||||
const double g = std::exp((double) in.gate[(size_t) t * HV + hv]), b = in.beta[(size_t) t * HV + hv];
|
||||
for (int j = 0; j < S; ++j) { kv[j] = 0.0; for (int i = 0; i < S; ++i) kv[j] += Sh[(size_t) i * S + j] * g * k[i]; }
|
||||
for (int j = 0; j < S; ++j) d[j] = (v[j] - kv[j]) * b;
|
||||
double ss = 0.0;
|
||||
for (int j = 0; j < S; ++j) {
|
||||
double acc = 0.0;
|
||||
for (int i = 0; i < S; ++i) {
|
||||
Sh[(size_t) i * S + j] = g * Sh[(size_t) i * S + j] + k[i] * d[j];
|
||||
acc += Sh[(size_t) i * S + j] * q[i];
|
||||
}
|
||||
oc[j] = acc / std::sqrt((double) S);
|
||||
ss += oc[j] * oc[j];
|
||||
}
|
||||
const double rs = 1.0 / std::sqrt(ss / S + 1e-6);
|
||||
for (int j = 0; j < S; ++j) {
|
||||
const double zz = in.z[((size_t) t * HV + hv) * S + j], sg = 1.0 / (1.0 + std::exp(-zz));
|
||||
y[((size_t) t * HV + hv) * S + j] = oc[j] * rs * in.gamma[j] * (o.silu ? zz * sg : sg);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < S; ++i) for (int j = 0; j < S; ++j) state[((size_t) i * HV + hv) * S + j] = Sh[(size_t) i * S + j];
|
||||
}
|
||||
}
|
||||
double rel_l1(const std::vector<double>& a, const std::vector<double>& b) { // ||a - b||_1 / ||b||_1, NaN-safe (NaN -> inf)
|
||||
double num = 0.0, den = 0.0;
|
||||
for (size_t i = 0; i < a.size(); ++i) {
|
||||
if (!std::isfinite(a[i]) || !std::isfinite(b[i])) return INFINITY;
|
||||
num += std::fabs(a[i] - b[i]);
|
||||
den += std::fabs(b[i]);
|
||||
}
|
||||
return den > 0.0 ? num / den : (num > 0.0 ? INFINITY : 0.0);
|
||||
}
|
||||
float h2f(uint16_t h) { return __half2float(__ushort_as_half(h)); }
|
||||
|
||||
// The engine's own launches (gdn_recurrence through its GdnGeom: the kh kernel where the card takes it, the pipe
|
||||
// kernel otherwise, then the output norm) against the reference, with the negative controls. Tolerances: the FP16
|
||||
// output 2e-3 relative L1 (the FP16 store is 2^-11), the FP32 state 1e-4; a negative must sit above 5 %.
|
||||
template <int HV, int HK, int C>
|
||||
int check_reference(int64_t T, uint64_t seed) {
|
||||
constexpr bool SILU = HV == 32;
|
||||
Inputs<HV, HK, C> in(T, seed);
|
||||
const HostIn hin(in);
|
||||
const size_t st = (size_t) S * HV * S, n = (size_t) T * HV * S;
|
||||
float* state = dalloc<float>(st);
|
||||
float* y = dalloc<float>(n);
|
||||
uint16_t* y16 = dalloc<uint16_t>(n);
|
||||
ck(cudaMemcpy(state, in.state0, st * 4, cudaMemcpyDeviceToDevice), "state");
|
||||
const strata::prefill::GdnGeom geom{HV, HK, C, SILU};
|
||||
strata::prefill::gdn_recurrence(state, in.h, in.gate, in.beta, in.z, in.gamma, 1e-6f, y, y16, T, geom, nullptr);
|
||||
ck(cudaDeviceSynchronize(), "gdn_recurrence");
|
||||
std::vector<uint16_t> hy16(n);
|
||||
std::vector<float> hstate(st);
|
||||
ck(cudaMemcpy(hy16.data(), y16, n * 2, cudaMemcpyDeviceToHost), "y16");
|
||||
ck(cudaMemcpy(hstate.data(), state, st * 4, cudaMemcpyDeviceToHost), "state");
|
||||
std::vector<double> ey(n), es(hstate.begin(), hstate.end());
|
||||
for (size_t i = 0; i < n; ++i) ey[i] = h2f(hy16[i]);
|
||||
std::vector<double> ry, rs;
|
||||
ref_rec(hin, RefOpts{SILU, true}, ry, rs);
|
||||
const double ey_err = rel_l1(ey, ry), es_err = rel_l1(es, rs);
|
||||
int fails = 0;
|
||||
const bool ok = ey_err <= 2e-3 && es_err <= 1e-4;
|
||||
std::printf("%d/%d/%d T=%-6lld engine vs reference (%s gate): y16 %.2e, state %.2e %s\n", HV, HK, C, (long long) T,
|
||||
SILU ? "SiLU" : "sigmoid", ey_err, es_err, ok ? "ok" : "*** FAIL ***");
|
||||
fails += !ok;
|
||||
// the negative controls: the rival readings, computed by the same reference, must be visibly wrong
|
||||
struct Neg { const char* what; RefOpts o; };
|
||||
const Neg negs[] = {{SILU ? "sigmoid(z) gate (the Flash-Next reading)" : "SiLU(z) gate (the Qwen3.6 reading)", RefOpts{!SILU, true}},
|
||||
{"interleaved head pairing (h // VPK instead of h % HK)", RefOpts{SILU, false}}};
|
||||
for (const Neg& ng : negs) {
|
||||
std::vector<double> ny, ns;
|
||||
ref_rec(hin, ng.o, ny, ns);
|
||||
const double dy = rel_l1(ny, ry), de = rel_l1(ey, ny); // rival vs right; the engine vs the rival
|
||||
const bool seen = dy > 0.05 && de > 0.05;
|
||||
std::printf(" rival: %-58s %.3f relative L1 from the right reading, the engine %.3f from it: %s\n", ng.what, dy, de,
|
||||
seen ? "yes" : "*** NO ***");
|
||||
fails += !seen;
|
||||
}
|
||||
cudaFree(state); cudaFree(y); cudaFree(y16);
|
||||
return fails;
|
||||
}
|
||||
|
||||
// gdn_conv (the 4-tap causal conv + SiLU, the conv history, the q/k L2 norm) against a host transcription, both geometries
|
||||
template <int HV, int HK, int C>
|
||||
int check_conv(int64_t T, uint64_t seed) {
|
||||
float* qkv = dalloc<float>((size_t) T * C);
|
||||
float* w = dalloc<float>((size_t) C * 4);
|
||||
float* hist = dalloc<float>((size_t) C * 3);
|
||||
float* h = dalloc<float>((size_t) T * C);
|
||||
fill_gauss<<<4096, 256>>>(qkv, T * C, 1.0f, seed);
|
||||
fill_gauss<<<256, 256>>>(w, (int64_t) C * 4, 0.5f, seed + 1);
|
||||
fill_gauss<<<256, 256>>>(hist, (int64_t) C * 3, 1.0f, seed + 2);
|
||||
ck(cudaDeviceSynchronize(), "conv inputs");
|
||||
std::vector<float> hq((size_t) T * C), hw((size_t) C * 4), hh((size_t) C * 3);
|
||||
ck(cudaMemcpy(hq.data(), qkv, hq.size() * 4, cudaMemcpyDeviceToHost), "qkv");
|
||||
ck(cudaMemcpy(hw.data(), w, hw.size() * 4, cudaMemcpyDeviceToHost), "w");
|
||||
ck(cudaMemcpy(hh.data(), hist, hh.size() * 4, cudaMemcpyDeviceToHost), "hist");
|
||||
const strata::prefill::GdnGeom geom{HV, HK, C, HV == 32};
|
||||
strata::prefill::gdn_conv(hist, qkv, w, h, T, 1e-6f, geom, nullptr);
|
||||
ck(cudaDeviceSynchronize(), "gdn_conv");
|
||||
std::vector<float> eh((size_t) T * C), ehist((size_t) C * 3);
|
||||
ck(cudaMemcpy(eh.data(), h, eh.size() * 4, cudaMemcpyDeviceToHost), "h");
|
||||
ck(cudaMemcpy(ehist.data(), hist, ehist.size() * 4, cudaMemcpyDeviceToHost), "hist out");
|
||||
// the reference: y[t][c] = sum_i w[c][i] * in[c][t - 3 + i] over [hist | qkv], then silu; q/k rows L2-normed per head
|
||||
std::vector<double> ry((size_t) T * C), rh((size_t) C * 3);
|
||||
for (int c = 0; c < C; ++c) {
|
||||
auto at = [&](int64_t t) -> double { return t >= 0 ? hq[(size_t) t * C + c] : hh[(size_t) c * 3 + (t + 3)]; };
|
||||
for (int64_t t = 0; t < T; ++t) {
|
||||
double v = 0.0;
|
||||
for (int i = 0; i < 4; ++i) v += (double) hw[(size_t) c * 4 + i] * at(t - 3 + i);
|
||||
ry[(size_t) t * C + c] = v / (1.0 + std::exp(-v));
|
||||
}
|
||||
for (int i = 0; i < 3; ++i) rh[(size_t) c * 3 + i] = at(T - 3 + i);
|
||||
}
|
||||
for (int64_t t = 0; t < T; ++t)
|
||||
for (int hd = 0; hd < 2 * HK; ++hd) {
|
||||
double ss = 0.0;
|
||||
for (int j = 0; j < S; ++j) ss += ry[(size_t) t * C + hd * S + j] * ry[(size_t) t * C + hd * S + j];
|
||||
const double rs = 1.0 / std::sqrt(ss + 1e-6);
|
||||
for (int j = 0; j < S; ++j) ry[(size_t) t * C + hd * S + j] *= rs;
|
||||
}
|
||||
std::vector<double> ey(eh.begin(), eh.end()), ehd(ehist.begin(), ehist.end());
|
||||
const double e1 = rel_l1(ey, ry), e2 = rel_l1(ehd, rh);
|
||||
const bool ok = e1 <= 1e-5 && e2 <= 1e-6;
|
||||
std::printf("%d/%d/%d T=%-6lld gdn_conv vs reference: h %.2e, history %.2e %s\n", HV, HK, C, (long long) T, e1, e2,
|
||||
ok ? "ok" : "*** FAIL ***");
|
||||
cudaFree(qkv); cudaFree(w); cudaFree(hist); cudaFree(h);
|
||||
return !ok;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
@@ -871,13 +1063,29 @@ int main(int argc, char** argv) {
|
||||
ck(cudaGetDeviceProperties(&p, 0), "device");
|
||||
std::printf("gdn_rec_parity on %s (sm_%d%d)\n", p.name, p.major, p.minor);
|
||||
int fails = 0;
|
||||
// the geometries: built for exactly these two; a third value head per key head on 32 heads (Flash-Next's 48-head
|
||||
// channel layout, C 10240, on Qwen3.6's heads) and 4 per key head are refused by name
|
||||
const bool g1 = strata::prefill::gdn_geometry_supported(48, 16, 10240), g2 = strata::prefill::gdn_geometry_supported(32, 16, 8192);
|
||||
const bool r1 = strata::prefill::gdn_geometry_supported(32, 16, 10240), r2 = strata::prefill::gdn_geometry_supported(32, 8, 8192),
|
||||
r3 = strata::prefill::gdn_geometry_supported(48, 16, 8192);
|
||||
std::printf("geometries: 48/16/10240 %s, 32/16/8192 %s; refused: 32 heads with the 48-head channel layout (32/16/10240) %s, "
|
||||
"4 value heads per key head (32/8/8192) %s, 48/16/8192 %s\n", g1 ? "built" : "*** MISSING ***",
|
||||
g2 ? "built" : "*** MISSING ***", r1 ? "*** NO ***" : "yes", r2 ? "*** NO ***" : "yes", r3 ? "*** NO ***" : "yes");
|
||||
fails += !g1 + !g2 + r1 + r2 + r3;
|
||||
const int64_t Ts[] = {1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 33, 257, 1000, 4099};
|
||||
uint64_t seed = 11;
|
||||
for (const int64_t T : Ts) fails += check_bits(T, seed++);
|
||||
for (const int64_t T : Ts) fails += check_bits<48, 16, 10240>(T, seed++);
|
||||
for (const int64_t T : Ts) fails += check_bits<32, 16, 8192>(T, seed++);
|
||||
const int64_t Tr[] = {1, 7, 33, 257};
|
||||
for (const int64_t T : Tr) fails += check_reference<48, 16, 10240>(T, seed++);
|
||||
for (const int64_t T : Tr) fails += check_reference<32, 16, 8192>(T, seed++);
|
||||
for (const int64_t T : {1, 2, 5, 70, 257}) fails += check_conv<48, 16, 10240>(T, seed++) + check_conv<32, 16, 8192>(T, seed++);
|
||||
if (do_bench) {
|
||||
std::printf("--bench: the prompt path's DeltaNet recurrence\n");
|
||||
bench(8192);
|
||||
bench(32768);
|
||||
bench<48, 16, 10240>(8192);
|
||||
bench<48, 16, 10240>(32768);
|
||||
bench<32, 16, 8192>(8192);
|
||||
bench<32, 16, 8192>(32768);
|
||||
scale(8192);
|
||||
fewer_sms(8192);
|
||||
}
|
||||
|
||||
@@ -1640,7 +1640,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
const core::ModelGeometry& g = *m.g;
|
||||
core::SessionState& ss = *m.ss;
|
||||
const Geom& G = m.geo;
|
||||
const int64_t N = G.n, HC = G.hc, D = G.d, K = G.k, ZV = G.zv, HV = G.hv;
|
||||
const int64_t N = G.n, HC = G.hc, D = G.d, K = G.k, HV = G.hv;
|
||||
const bool plain = HC == 1; // the plain residual block (Qwen3.6): no hyper-connection read or write
|
||||
const GdnGeom gdn_geom = G.gdn();
|
||||
const auto t_start = Clock::now();
|
||||
@@ -2383,7 +2383,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
pt.mark(kPfRouter, cs);
|
||||
if (!bf16_proj(m.gemm, wr, m.mixed_bf, m.logits, T, v.name("ffn_gate_inp.weight"), err, 0, m.mixed_bf_lo)) return false;
|
||||
route(m.logits, m.ids, m.w, T, m.g->n_expert, K, m.cs);
|
||||
if (on_seam && !on_seam("ffn_moe_topk", l, (const float*) m.ids, T, K, p0, err)) return false;
|
||||
if (on_seam && !on_seam("ffn_moe_topk", l, (float*) m.ids, T, K, p0, err)) return false; // int32 ids as floats
|
||||
// the shared expert and its scalar gate
|
||||
if (!native_proj(m.gemm, wsg, m.mixed_h, m.sgate, T, v.name("ffn_gate_shexp.weight"), err)) return false;
|
||||
if (!native_proj(m.gemm, wsu, m.mixed_h, m.sup, T, v.name("ffn_up_shexp.weight"), err)) return false;
|
||||
|
||||
Reference in new issue
Block a user