Archived
libxe2, arch/common: the 28 Oct pending set, the bf16 cast fused, the vectorized norm, a draft-vocab prefix
- gdn_kkt_solve on DPAS (~0.4 ms at 2048 tokens against 7.7), the int8 head's DPAS forms at 9-16 rows (verify past depth 7), the plain block norm (RadBuildCtx::plain_norm, set on the SYCL backend: `add` + `rmsnorm` where no reader takes the E4M3 codes) with the fast rmsnorm rows. - rmsnorm's bf16 rows up to 8192 wide: a work-item eight elements by one 16-byte load (the generic form walked 20 dependent loads a work-item): 24.7 -> 3.3 us a decode norm, ~3.5 ms a round. - w4a16: a bf16 activation narrowed to fp16 at the kernel's own load (the separate cast launch before every GEMM is gone), bit for bit the same. - RADIANCE_MTP_DRAFT_VOCAB=N: the 2-bit draft head over the vocabulary's first N ids (a prefix shortlist, GRIMOIRE's idea); the u2 layout hook takes an op N below the weight's rows. 131072: greedy acceptance identical on the decode proxy, draft head 650 -> 351 us a pass. KL against the exact-kernel reference (2002 positions): 0.002386, top-1 97.30% (was 0.002376 / 96.80%). Decode proxy (greedy, depth 5, mtp4): 72.0 -> 74.3 tok/s with the prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7580ec3d17
commit
726e532114
18 files changed
+472
-54
No files matched your search
@@ -423,6 +423,183 @@ int launch_scan(sycl::queue& q, const ScanArgs& a, const char* ws) {
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- gdn_kkt_solve on DPAS */
|
||||
struct KktArgs {
|
||||
const uint16_t* k;
|
||||
int64_t ks0, ks1;
|
||||
const float *g, *beta;
|
||||
int64_t gs0, gs1, bs0, bs1;
|
||||
uint16_t* A;
|
||||
int64_t As0, As1;
|
||||
const char* cu; /* base and element stride, as ScanArgs's */
|
||||
uint32_t cudt;
|
||||
int64_t cus;
|
||||
int64_t T, N, H, Hg;
|
||||
int64_t cu_at(int64_t i) const { return ld_index(cudt, cu, i * cus); }
|
||||
};
|
||||
|
||||
/* C = A B over 16 x 16 f32 blocks, row-major, each row's sum over l in order. */
|
||||
inline esimd::simd<float, 256> mm16(esimd::simd<float, 256> A, esimd::simd<float, 256> B) SYCL_ESIMD_FUNCTION {
|
||||
using namespace esimd;
|
||||
simd<float, 256> C;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
simd<float, 16> acc = 0.0f;
|
||||
#pragma unroll
|
||||
for (int l = 0; l < 16; ++l) acc += (float)A[16 * i + l] * B.template select<16, 1>(16 * l);
|
||||
C.template select<16, 1>(16 * i) = acc;
|
||||
}
|
||||
return C;
|
||||
}
|
||||
|
||||
/* The local-memory offset of X block (I, J), J <= I: 1 KiB blocks in row order. */
|
||||
inline uint32_t xoff(int I, int J) { return (uint32_t)((I * (I + 1) / 2 + J) * 1024); }
|
||||
|
||||
/* A work-group of 4 threads a (sequence, chunk, v head), thread t the rows 16t..16t+15:
|
||||
* M[i][j] = (beta_i (k_i . k_j)) e^{g_i - g_j}, j < i: k_i . k_j on DPAS (bf16 k, exact
|
||||
* products, f32 sums), the factors in f32
|
||||
* X = (I + M)^{-1} by 16 x 16 blocks: the diagonal block by forward substitution in registers,
|
||||
* then X_tJ = -X_tt sum_{K=J}^{t-1} M_tK X_KJ, a level (t - J) a barrier, the blocks
|
||||
* passed through local memory -- all in f32
|
||||
* Not libref's order (it substitutes row by row over the whole chunk): held to gdn_kkt_solve's
|
||||
* tolerance; XE2_EXACT=1 keeps the exact kernel. */
|
||||
int launch_kkt(sycl::queue& q, const KktArgs& a, int64_t C) {
|
||||
syclex::properties props{ intelex::grf_size<256> };
|
||||
/* local memory: the X blocks (I, J), J <= I, at I (I + 1) / 2 + J, 1 KiB each */
|
||||
constexpr uint32_t kBlkB = 16 * 16 * 4;
|
||||
return submit([&] {
|
||||
q.parallel_for(sycl::nd_range<1>{ (size_t)(a.N * C * a.H) * 4, 4 }, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
||||
using namespace esimd;
|
||||
slm_init<10 * kBlkB>();
|
||||
const int t = (int)it.get_local_id(0);
|
||||
const int64_t wg = (int64_t)it.get_group(0);
|
||||
const int64_t h = wg % a.H, c = (wg / a.H) % C, n = wg / (a.H * C);
|
||||
const int64_t bos = a.cu_at(n), slen = a.cu_at(n + 1) - bos;
|
||||
const int64_t c0 = c * CK;
|
||||
if (c0 >= slen) return; /* uniform over the work-group */
|
||||
const int L = (int)(slen - c0 < CK ? slen - c0 : CK);
|
||||
const int64_t tok0 = bos + c0;
|
||||
const int64_t hk = h / (a.H / a.Hg);
|
||||
const uint16_t* kh = a.k + hk * a.ks1;
|
||||
const unsigned TH = (unsigned)(a.T - 1), kp = (unsigned)(a.ks0 * 2 - 1);
|
||||
const int r0 = 16 * t;
|
||||
const simd<int32_t, 16> lane(0, 1);
|
||||
|
||||
/* g at the chunk's positions (past L, g_last), beta at the thread's rows */
|
||||
const float* gp = a.g + tok0 * a.gs0 + h * a.gs1;
|
||||
const float* bp = a.beta + tok0 * a.bs0 + h * a.bs1;
|
||||
const float glast = gp[(int64_t)(L - 1) * a.gs0];
|
||||
simd<float, 64> gj;
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 4; ++s) {
|
||||
const simd<int32_t, 16> jj = lane + 16 * s;
|
||||
simd<float, 16> gv = gather<float, 16>(gp, simd<uint32_t, 16>(jj) * (uint32_t)(a.gs0 * 4), jj < L);
|
||||
gv.merge(simd<float, 16>(glast), jj >= L);
|
||||
gj.template select<16, 1>(16 * s) = gv;
|
||||
}
|
||||
simd<float, 16> gi, bi;
|
||||
{
|
||||
const simd<int32_t, 16> ii = lane + r0;
|
||||
gi = gather<float, 16>(gp, simd<uint32_t, 16>(ii) * (uint32_t)(a.gs0 * 4), ii < L);
|
||||
bi = gather<float, 16>(bp, simd<uint32_t, 16>(ii) * (uint32_t)(a.bs0 * 4), ii < L);
|
||||
gi.merge(simd<float, 16>(glast), ii >= L);
|
||||
bi.merge(simd<float, 16>(0.0f), ii >= L);
|
||||
}
|
||||
|
||||
/* M's blocks of the thread's rows, Mb[J] for J <= t: k_i . k_j on DPAS */
|
||||
simd<float, 128> Cc[4][2];
|
||||
#pragma unroll
|
||||
for (int J = 0; J < 4; ++J) Cc[J][0] = Cc[J][1] = 0.0f;
|
||||
#pragma unroll 1
|
||||
for (int ks = 0; ks < KK / 16; ++ks) {
|
||||
simd<uint16_t, 128> a0 = load_2d<uint16_t, 16, 8, 1, false>(kh, 255u, TH, kp, ks * 16, (int)(tok0 + r0));
|
||||
simd<uint16_t, 128> a1 = load_2d<uint16_t, 16, 8, 1, false>(kh, 255u, TH, kp, ks * 16, (int)(tok0 + r0 + 8));
|
||||
#pragma unroll
|
||||
for (int J = 0; J < 4; ++J) {
|
||||
if (J > t) continue;
|
||||
simd<uint32_t, 128> kb = load_2d<uint32_t, 8, 16, 1, true>((const uint32_t*)kh, 255u, TH, kp, ks * 8,
|
||||
(int)(tok0 + 16 * J));
|
||||
Cc[J][0] = mma(Cc[J][0], kb, a0);
|
||||
Cc[J][1] = mma(Cc[J][1], kb, a1);
|
||||
}
|
||||
}
|
||||
simd<float, 256> Mb[4], Md;
|
||||
#pragma unroll
|
||||
for (int J = 0; J < 4; ++J) {
|
||||
if (J > t) continue;
|
||||
simd<float, 256> m;
|
||||
m.template select<128, 1>(0) = Cc[J][0];
|
||||
m.template select<128, 1>(128) = Cc[J][1];
|
||||
const simd<float, 16> gjJ = gj.template select<16, 1>(16 * J);
|
||||
const simd<int32_t, 16> jc = lane + 16 * J;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 16; ++r) {
|
||||
const int i = r0 + r;
|
||||
const simd_mask<16> hide = (jc >= i) | (i >= L);
|
||||
simd<float, 16> dg = (float)gi[r] - gjJ;
|
||||
dg.merge(simd<float, 16>(0.0f), hide); /* keep exp finite where hidden */
|
||||
simd<float, 16> v = ((float)bi[r] * m.template select<16, 1>(16 * r)) * exp(dg);
|
||||
v.merge(simd<float, 16>(0.0f), hide);
|
||||
m.template select<16, 1>(16 * r) = v;
|
||||
}
|
||||
Mb[J] = m;
|
||||
if (J == t) Md = m;
|
||||
}
|
||||
/* the diagonal block's inverse: row r = e_r - sum_{l < r} M[r][l] X[l] */
|
||||
simd<float, 256> Xd = 0.0f;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 16; ++r) {
|
||||
simd<float, 16> row = 0.0f;
|
||||
row[r] = 1.0f;
|
||||
#pragma unroll
|
||||
for (int l = 0; l < r; ++l) row -= (float)Md[16 * r + l] * Xd.template select<16, 1>(16 * l);
|
||||
Xd.template select<16, 1>(16 * r) = row;
|
||||
}
|
||||
slm_block_store<float, 256>(xoff(t, t), Xd);
|
||||
/* the off-diagonal blocks, a level a barrier */
|
||||
#pragma unroll
|
||||
for (int lvl = 1; lvl < 4; ++lvl) {
|
||||
barrier();
|
||||
if (t >= lvl) {
|
||||
const int J = t - lvl;
|
||||
simd<float, 256> S = 0.0f;
|
||||
#pragma unroll
|
||||
for (int K = 0; K < 4; ++K) {
|
||||
if (K < J || K >= t) continue;
|
||||
simd<float, 256> xkj = slm_block_load<float, 256>(xoff(K, J));
|
||||
S += mm16(Mb[K], xkj);
|
||||
}
|
||||
slm_block_store<float, 256>(xoff(t, J), mm16(Xd, S) * -1.0f);
|
||||
}
|
||||
}
|
||||
barrier();
|
||||
/* the thread's rows of A: X_tJ for J <= t, zero right of the diagonal block */
|
||||
uint16_t* Ah = a.A + h * a.As1;
|
||||
const unsigned Ap = (unsigned)(a.As0 * 2 - 1);
|
||||
#pragma unroll
|
||||
for (int J = 0; J < 4; ++J) {
|
||||
simd<float, 256> x = 0.0f;
|
||||
if (J <= t) x = slm_block_load<float, 256>(xoff(t, J));
|
||||
simd<uint16_t, 256> xb = bf16_of<256>(x);
|
||||
#pragma unroll
|
||||
for (int m = 0; m < 2; ++m) {
|
||||
const int rb = r0 + 8 * m;
|
||||
if (rb + 8 <= L) {
|
||||
store_2d<uint16_t, 16, 8>(Ah, 127u, TH, Ap, 16 * J, (int)(tok0 + rb),
|
||||
simd<uint16_t, 128>(xb.template select<128, 1>(128 * m)));
|
||||
} else {
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 8; ++r)
|
||||
if (rb + r < L)
|
||||
block_store<uint16_t, 16>(Ah + (tok0 + rb + r) * a.As0 + 16 * J,
|
||||
simd<uint16_t, 16>(xb.template select<16, 1>(128 * m + 16 * r)));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
bool al64(const void* p) { return ((uintptr_t)p & 63u) == 0; }
|
||||
|
||||
} // namespace
|
||||
@@ -468,4 +645,23 @@ int gdn_chunk_scan_dpas(sycl::queue& q, const uint16_t* qp, int64_t qs0, int64_t
|
||||
return launch_scan(q, a, ws);
|
||||
}
|
||||
|
||||
/* gdn_kkt_solve's DPAS form; RAD_E_UNSUPPORTED when the call is not one it serves. bf16 k
|
||||
* ([T, Hg, 128]) and A ([T, H, 64]) with unit last strides, f32 g and beta ([T, H]). */
|
||||
int gdn_kkt_solve_dpas(sycl::queue& q, const uint16_t* kp, int64_t ks0, int64_t ks1, const float* g, int64_t gs0,
|
||||
int64_t gs1, const float* beta, int64_t bs0, int64_t bs1, uint16_t* Ap, int64_t As0,
|
||||
int64_t As1, const RadTensor* cu, int64_t T, int64_t N, int64_t H, int64_t Hg) {
|
||||
if (!al64(kp) || (ks1 * 2) % 64 != 0 || (ks0 * 2) % 16 != 0 || ks0 * 2 < 256) return RAD_E_UNSUPPORTED;
|
||||
if (!al64(Ap) || (As1 * 2) % 64 != 0 || (As0 * 2) % 16 != 0 || As0 * 2 < 128) return RAD_E_UNSUPPORTED;
|
||||
if (T >= (int64_t)1 << 24 || H % Hg != 0) return RAD_E_UNSUPPORTED;
|
||||
KktArgs a{};
|
||||
a.k = kp; a.ks0 = ks0; a.ks1 = ks1;
|
||||
a.g = g; a.gs0 = gs0; a.gs1 = gs1; a.beta = beta; a.bs0 = bs0; a.bs1 = bs1;
|
||||
a.A = Ap; a.As0 = As0; a.As1 = As1;
|
||||
const Lin cl = Lin::of(cu);
|
||||
if (!cl.contig && cl.rank != 1) return RAD_E_UNSUPPORTED;
|
||||
a.cu = cl.base; a.cudt = cl.dtype; a.cus = cl.contig ? 1 : cl.stride[0];
|
||||
a.T = T; a.N = N; a.H = H; a.Hg = Hg;
|
||||
return launch_kkt(q, a, (T + CK - 1) / CK);
|
||||
}
|
||||
|
||||
} // namespace xe2
|
||||
Reference in new issue
Block a user