diff --git a/libxe2/xe2_common.h b/libxe2/xe2_common.h index ec6740e8..ac1052f8 100644 --- a/libxe2/xe2_common.h +++ b/libxe2/xe2_common.h @@ -54,6 +54,11 @@ sycl::queue* queue_of(RadStream s); * allocation fails. */ float* workspace(sycl::queue& q, size_t floats, int slot = 0); +/* fp16 XMX GEMM with a per-row and per-column output factor (xe2_gemm_xmx.cpp). */ +int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs, + char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t mpad, int64_t N, + int64_t K, const float* rs, const float* cs); + /* Runs `submit`, turning a SYCL exception into RAD_E_DEVICE with its message. */ template int submit(F&& f) { diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index 1527305a..110c9347 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -495,6 +495,154 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) { const IV cuv = iv_of(cu); sycl::queue* qu = queue_of(st); if (!qu) return RAD_E_DEVICE; + if (K == 128 && V % 32 == 0 && chunk == 64 && !std::getenv("XE2_GDN_SCAN1")) { + /* A work-group of 256 a (sequence, v head, block of 32 value columns), the chunk's k, q, + * the block's slices of v, S, W, V' and the sA rows R in local memory (~124 KB). Each phase + * of a chunk is a set of independent chains -- W[j][e], V'[i][e], R[i][j], O[i][e], S[d][e] + * -- each run by one work-item in libref's order, so the scan is libref's; the parallelism + * is the chains' (thousands a phase), not one column a work-item. R and the decays depend + * only on q, k, g, so each column block forms them again. */ + constexpr int CB = 32, CK = 64, KK = 128, WG = 256; + constexpr int KP = KK + 1; /* k and q rows padded: the R phase's lanes read different rows */ + const int64_t nblk = V / CB; + /* e^{g_i - g_j} for every (row i, chunk position j <= i of i's chunk), libref's double exp + * rounded once, a (token, head) row of 64: once here instead of once a column block. */ + float* dec = workspace(*qu, (size_t)(T * H * CK + 64), 5); + if (!dec) return RAD_E_DEVICE; + if (int rc0 = submit([&] { + qu->parallel_for(sycl::range<1>((size_t)(T * H)), [=](sycl::id<1> id) { + const int64_t th = (int64_t)id[0]; + const int64_t tok = th / H, h = th % H; + /* the token's sequence and its chunk start */ + int64_t n = 0; + while (n + 1 < N && cuv.at(n + 1) <= tok) ++n; + const int64_t bos = cuv.at(n), c0 = bos + (tok - bos) / CK * CK; + const float gi = gs.ld(at2(gv, tok, h)); + for (int64_t j = 0; j < CK; ++j) { + const int64_t tj = c0 + j; + dec[th * CK + j] = tj <= tok ? rexp(gi - gs.ld(at2(gv, tj, h))) : 0.0f; + } + }); + }); + rc0 < 0) + return rc0; + return submit([&] { + qu->submit([&](sycl::handler& hd) { + sycl::local_accessor lm(sycl::range<1>((size_t)(2 * CK * KP + 3 * CK * CB + KK * CB + CK * CK + 4 * CK)), hd); + hd.parallel_for(sycl::nd_range<1>{(size_t)(N * H * nblk) * WG, (size_t)WG}, [=](sycl::nd_item<1> it) { + const int64_t gid = (int64_t)it.get_group(0); + const int64_t blk = gid % nblk, nh = gid / nblk; + const int64_t n = nh / H, h = nh % H, hk = h / grp; + const int64_t e0 = blk * CB; + const int t = (int)it.get_local_id(0); + auto gw = it.get_group(); + const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos; + const int64_t sn = has_si ? sis.li(n * sw) : n; + if (sn < 0) return; /* uniform */ + float* kc = &lm[0]; /* [CK][KP] */ + float* qc = kc + CK * KP; /* [CK][KP] */ + float* vc = qc + CK * KP; /* [CK][CB] */ + float* Wm = vc + CK * CB; /* [CK][CB] */ + float* Vp = Wm + CK * CB; /* [CK][CB] */ + float* St = Vp + CK * CB; /* [KK][CB] */ + float* R = St + KK * CB; /* [CK][CK] */ + float* gc = R + CK * CK; /* [CK] */ + float* bc = gc + CK; + float* ge = bc + CK; /* e^{g_j} */ + float* gsj = ge + CK; /* e^{g_last - g_j} */ + for (int idx = t; idx < KK * CB; idx += WG) { + const int d = idx / CB, e = idx % CB; + St[idx] = has_h0 ? h0s.ld(at4(h0v, sn, h, e0 + e, d)) : 0.0f; + } + for (int64_t c0 = 0; c0 < slen; c0 += CK) { + const int64_t L = (c0 + CK < slen) ? CK : slen - c0; + sycl::group_barrier(gw); + for (int idx = t; idx < (int)L * KK; idx += WG) { + const int j = idx / KK, d = idx % KK; + const int64_t tj = bos + c0 + j; + kc[j * KP + d] = ks.ld(at3(kv, tj, hk, d)); + qc[j * KP + d] = qs.ld(at3(qv, tj, hk, d)); + } + for (int idx = t; idx < (int)L * CB; idx += WG) { + const int j = idx / CB, e = idx % CB; + vc[idx] = vs.ld(at3(vv, bos + c0 + j, h, e0 + e)); + } + if (t < L) { + const int64_t tj = bos + c0 + t; + gc[t] = gs.ld(at2(gv, tj, h)); + bc[t] = bs.ld(at2(bv, tj, h)); + } + sycl::group_barrier(gw); + const float glast = gc[L - 1]; + if (t < L) { + ge[t] = rexp(gc[t]); + gsj[t] = rexp(glast - gc[t]); + } + sycl::group_barrier(gw); + /* W[j][e] = beta_j (v[j][e] - e^{g_j} sum_d k[j][d] S[d][e]) */ + for (int idx = t; idx < (int)L * CB; idx += WG) { + const int j = idx / CB, e = idx % CB; + float acc = 0.0f; + for (int d = 0; d < KK; ++d) acc += kc[j * KP + d] * St[d * CB + e]; + Wm[idx] = bc[j] * (vc[idx] - ge[j] * acc); + } + /* the chunk's rows of A, widened into the R buffer (R is formed after V') */ + for (int idx = t; idx < (int)(L * L); idx += WG) { + const int i = idx / (int)L, j = idx % (int)L; + R[i * CK + j] = j <= i ? As.ld(at3(Av, bos + c0 + i, h, j)) : 0.0f; + } + sycl::group_barrier(gw); + /* V'[i][e] = sum_{j <= i} A[i][j] W[j][e] */ + for (int idx = t; idx < (int)L * CB; idx += WG) { + const int i = idx / CB, e = idx % CB; + float vp = 0.0f; + for (int j = 0; j <= i; ++j) vp += R[i * CK + j] * Wm[j * CB + e]; + Vp[idx] = vp; + } + sycl::group_barrier(gw); + /* R[i][j] = scale e^{g_i - g_j} sum_d q[i][d] k[j][d], j <= i */ + for (int idx = t; idx < (int)(L * L); idx += WG) { + const int i = idx / (int)L, j = idx % (int)L; + if (j > i) continue; + float rj = 0.0f; + for (int d = 0; d < KK; ++d) rj += qc[i * KP + d] * kc[j * KP + d]; + R[i * CK + j] = scale * dec[((bos + c0 + i) * H + h) * CK + j] * rj; + } + sycl::group_barrier(gw); + /* O[i][e] = scale e^{g_i} sum_d q[i][d] S[d][e] + sum_{j <= i} R[i][j] V'[j][e] */ + for (int idx = t; idx < (int)L * CB; idx += WG) { + const int i = idx / CB, e = idx % CB; + float inr = 0.0f; + for (int d = 0; d < KK; ++d) inr += qc[i * KP + d] * St[d * CB + e]; + float acc = (scale * ge[i]) * inr; + for (int j = 0; j <= i; ++j) acc += R[i * CK + j] * Vp[j * CB + e]; + os.st(at3(ov, bos + c0 + i, h, e0 + e), acc); + } + sycl::group_barrier(gw); + /* k[j][d] *= e^{g_last - g_j}; S[d][e] = e^{g_last} S[d][e] + sum_j V'[j][e] k[j][d] */ + for (int idx = t; idx < (int)L * KK; idx += WG) { + const int j = idx / KK, d = idx % KK; + kc[j * KP + d] = gsj[j] * kc[j * KP + d]; + } + sycl::group_barrier(gw); + const float el = rexp(glast); + for (int idx = t; idx < KK * CB; idx += WG) { + const int d = idx / CB, e = idx % CB; + float sr = el * St[idx]; + for (int j = 0; j < (int)L; ++j) sr += Vp[j * CB + e] * kc[j * KP + d]; + St[idx] = sr; + } + } + sycl::group_barrier(gw); + if (has_ht) + for (int idx = t; idx < KK * CB; idx += WG) { + const int d = idx / CB, e = idx % CB; + hts.st(at4(htv, sn, h, e0 + e, d), St[idx]); + } + }); + }); + }); + } if (V % kSub == 0 && V <= 256 && chunk <= V && K * V <= 128 * 128) { /* A work-group a (sequence, v head), a work-item a value column e (and, for the Q K^T * rows, a chunk position j): the state in local memory, the chunk's widened operands in diff --git a/libxe2/xe2_gemm_xmx.cpp b/libxe2/xe2_gemm_xmx.cpp index 94f1357b..7de58fd5 100644 --- a/libxe2/xe2_gemm_xmx.cpp +++ b/libxe2/xe2_gemm_xmx.cpp @@ -151,3 +151,62 @@ int logits_gemm_xmx(const RadArgs* a, RadStream s) { } } // namespace xe2 + +/* ------------------------------------------------------------------ the fp16 form, scaled */ +/* The same tiling over fp16 operands with a per-row and per-column factor on the way out: + * y[m][n] = acc * rs[m] * cs[n]. For the m2xfp4 prefill (xe2_gemv_m2.cpp), whose weight is exact + * in fp16 once scaled by its column's largest exponent and whose activation is fp16(a / R): fp16's + * 2^-11 step beside bf16's 2^-8. A is [mpad, K] with mpad a multiple of 8 (padding rows zero); + * rows past M are not written. N a multiple of 64, K of 16. */ +namespace xe2 { + +int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs, + char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t mpad, int64_t N, + int64_t K, const float* rs, const float* cs) { + using hf = sycl::half; + const size_t gm = (size_t)((mpad + kTm * kSgs - 1) / (kTm * kSgs)); + const size_t gn = (size_t)(N / (kTn * kTiles)); + return submit([&] { + q.submit([&](sycl::handler& h) { + sycl::local_accessor stage(sycl::range<1>((size_t)kSgs * kTm * kTn), h); + h.parallel_for(sycl::nd_range<2>{{gm, gn * kWg}, {1, kWg}}, + [=](sycl::nd_item<2> it) [[sycl::reqd_sub_group_size(kSub)]] { + const sycl::sub_group sg = it.get_sub_group(); + const int s = (int)sg.get_group_linear_id(); + const int64_t m0 = ((int64_t)it.get_group(0) * kSgs + s) * kTm; + const int64_t n0 = (int64_t)it.get_group(1) * kTn * kTiles; + if (m0 >= mpad) return; + auto ga = sycl::address_space_cast(ab + m0 * ars); + jm::joint_matrix ma; + jm::joint_matrix mb; + jm::joint_matrix mc[kTiles]; + for (int t = 0; t < kTiles; ++t) jm::joint_matrix_fill(sg, mc[t], 0.0f); + for (int64_t k = 0; k < K; k += kTk) { + jm::joint_matrix_load(sg, ma, ga + k, (size_t)ars); + for (int t = 0; t < kTiles; ++t) { + auto gb = sycl::address_space_cast(bb + (n0 + t * kTn) * brs + k); + jm::joint_matrix_load(sg, mb, gb, (size_t)brs); + jm::joint_matrix_mad(sg, mc[t], ma, mb, mc[t]); + } + } + float* mine = &stage[(size_t)s * kTm * kTn]; + auto ls = sycl::address_space_cast(mine); + const int lane = (int)sg.get_local_linear_id(); + for (int t = 0; t < kTiles; ++t) { + jm::joint_matrix_store(sg, mc[t], ls, kTn, jm::layout::row_major); + sycl::group_barrier(sg); + for (int e = lane; e < kTm * kTn; e += kSub) { + const int64_t r = e / kTn, c = e % kTn, m = m0 + r, n = n0 + t * kTn + c; + if (m < M) rad_store_f32(yb, ydt, m * yrs + n * ycs, mine[e] * rs[m] * cs[n]); + } + sycl::group_barrier(sg); + } + }); + }); + }); +} + +} // namespace xe2 diff --git a/libxe2/xe2_gemv_m2.cpp b/libxe2/xe2_gemv_m2.cpp index 1eb2298f..5918aa6e 100644 --- a/libxe2/xe2_gemv_m2.cpp +++ b/libxe2/xe2_gemv_m2.cpp @@ -396,15 +396,23 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) { rad_tensor_numel(A) >= M * acols && rad_tensor_numel(AS) >= M * scols && rad_tensor_numel(BS) >= N * kb && (!(pb1 > 0 || pb2 > 0) || ((e1 % 16) == 0 && (e2 % 16) == 0 && !(e1 == e2 && e2 < N))); - if (!ok) return gemm_nt_q_m2(a, st); + if (!ok || K % 16 || e1 % 64 || e2 % 64 || N % 64) return gemm_nt_q_m2(a, st); sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; - /* The workspace: the bf16 activation [M, parts * K], then the bf16 weight [N, K]. */ - const int64_t abytes = (M * acols * 2 + 255) / 256 * 256; - float* ws = workspace(*q, (size_t)((abytes + N * K * 2) / 4 + 64)); + /* fp16 operands: the activation fp16(a * a_scale / R_m), R_m = 224 * the row's largest scale + * (|a16| <= 2), rows padded to a multiple of 8 with zeros; the weight + * e2m1 * (1 + k/4) * 2^(e - e_max(n)), exact in fp16 (a group more than 2^14 below its row's + * largest goes subnormal); y = acc * R_m * 2^(e_max(n) - 127). */ + const int64_t mpad = (M + 7) / 8 * 8; + const int64_t abytes = (mpad * acols * 2 + 255) / 256 * 256; + const int64_t wbytes = (N * K * 2 + 255) / 256 * 256; + float* ws = workspace(*q, (size_t)((abytes + wbytes) / 4 + mpad + 2 * N + 64)); if (!ws) return RAD_E_DEVICE; - uint16_t* a16 = (uint16_t*)ws; - uint16_t* w16 = (uint16_t*)((char*)ws + abytes); + half* a16 = (half*)ws; + half* w16 = (half*)((char*)ws + abytes); + float* rr = (float*)((char*)ws + abytes + wbytes); + float* cs = rr + mpad; + int32_t* emax = (int32_t*)(cs + N); const uint8_t* ab = (const uint8_t*)A->data; const char* sab = (const char*)AS->data; const uint32_t sadt = AS->dtype; @@ -412,11 +420,25 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) { const uint8_t* meta0 = codes0 + N * (K / 2); const uint8_t* expe0 = (const uint8_t*)BS->data; int rc = submit([&] { - const int64_t na = M * acols; - q->parallel_for(sycl::range<1>((size_t)na), [=](sycl::id<1> i) { - const int64_t m = (int64_t)i[0] / acols, c = (int64_t)i[0] % acols; - const float v = e4m3(ab[m * ars + c]) * rad_load_f32(sab, sadt, m * srs + c / 128); - a16[m * acols + c] = rad_f32_to_bf16(v); + q->parallel_for(sycl::nd_range<1>{(size_t)mpad * 256, 256}, [=](sycl::nd_item<1> it) { + const int64_t m = (int64_t)it.get_group(0), t = (int64_t)it.get_local_id(0); + float mx = 0.0f; + if (m < M) + for (int64_t c = t; c < scols; c += 256) mx = sycl::fmax(mx, rad_load_f32(sab, sadt, m * srs + c)); + mx = sycl::reduce_over_group(it.get_group(), mx, sycl::maximum()); + const float R = mx > 0.0f ? 224.0f * mx : 1.0f; + if (t == 0) rr[m] = R; + const float f = 1.0f / R; + for (int64_t c = t; c < acols; c += 256) + a16[m * acols + c] = m < M ? (half)(e4m3(ab[m * ars + c]) * rad_load_f32(sab, sadt, m * srs + c / 128) * f) + : (half)0.0f; + }); + q->parallel_for(sycl::range<1>((size_t)N), [=](sycl::id<1> i) { + const uint8_t* er = expe0 + (int64_t)i[0] * brs; + int32_t mx = 0; + for (int64_t g = 0; g < kb; ++g) mx = sycl::max(mx, (int32_t)er[g]); + emax[i[0]] = mx; + cs[i[0]] = sycl::ldexp(1.0f, mx - 127); }); const int64_t nw = N * (K / 2); /* a work-item a code byte: two weights */ q->parallel_for(sycl::range<1>((size_t)nw), [=](sycl::id<1> i) { @@ -424,11 +446,12 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) { const uint8_t byte = codes0[n * (K / 2) + j]; constexpr float lut[16] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f, -0.0f, -0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f }; - for (int h = 0; h < 2; ++h) { - const int64_t k = 2 * j + h; - const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + (k >> 5)] - 127); + const int mx = emax[n]; + for (int hh = 0; hh < 2; ++hh) { + const int64_t k = 2 * j + hh; + const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + (k >> 5)] - mx); const float km = 1.0f + 0.25f * (float)((meta0[n * kb + (k >> 5)] >> (2 * ((k & 31) >> 3))) & 3); - w16[n * K + k] = rad_f32_to_bf16(lut[(byte >> (4 * h)) & 0xF] * ev * km); + w16[n * K + k] = (half)(lut[(byte >> (4 * hh)) & 0xF] * km * ev); } }); }); @@ -438,18 +461,9 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) { const int64_t n0 = bound[p], n1 = bound[p + 1]; if (n1 <= n0) continue; const int64_t pi = p == 0 ? 0 : (p == 1 ? 1 : (parts == 3 ? 2 : 1)); - RadTensor t[3] = {}; - t[0].data = a16 + pi * K; t[0].dtype = RAD_BF16; t[0].rank = 2; - t[0].shape[0] = M; t[0].shape[1] = K; t[0].stride[0] = acols; t[0].stride[1] = 1; - t[1].data = w16 + n0 * K; t[1].dtype = RAD_BF16; t[1].rank = 2; - t[1].shape[0] = n1 - n0; t[1].shape[1] = K; t[1].stride[0] = K; t[1].stride[1] = 1; - t[2].data = (char*)Y->data + (n0 * ycs) * ((rad_dtype_bits(Y->dtype) + 7) / 8); - t[2].dtype = Y->dtype; t[2].rank = 2; - t[2].shape[0] = M; t[2].shape[1] = n1 - n0; t[2].stride[0] = yrs; t[2].stride[1] = ycs; - RadParam pr[2] = { RAD_INT("N", n1 - n0), RAD_INT("K", K) }; - RadArgs b = *a; - b.t = t; b.n_t = 3; b.p = pr; b.n_p = 2; - rc = gemm_nt_xmx(&b, st); + rc = xmx_f16_scaled(*q, a16 + pi * K, acols, w16 + n0 * K, K, + (char*)Y->data + (n0 * ycs) * ((rad_dtype_bits(Y->dtype) + 7) / 8), Y->dtype, + yrs, ycs, M, mpad, n1 - n0, K, rr, cs + n0); } return rc; } diff --git a/libxe2/xe2_norm.cpp b/libxe2/xe2_norm.cpp index 27ca5b49..4c9b3eb4 100644 --- a/libxe2/xe2_norm.cpp +++ b/libxe2/xe2_norm.cpp @@ -269,16 +269,21 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) { * knows), a memory round trip an element. */ constexpr int kPer = (int)(kRowMax / kWg); float xv[kPer], rv[kPer], wv[kPer]; + /* fully unrolled and predicated (no early exit), so every load is in flight + * before the first is used */ +#pragma unroll for (int j = 0; j < kPer; ++j) { const int64_t i = t + (int64_t)j * kWg; - if (i >= n) break; - xv[j] = rad_load_f32(x.base, x.dtype, r * x.pitch + i); - rv[j] = has_res ? rad_load_f32(res.base, res.dtype, r * res.pitch + i) : 0.0f; - wv[j] = rad_load_f32(gw.base, gw.dtype, i * gw.stride) + wadd; + const bool ok = i < n; + const int64_t ii = ok ? i : 0; + xv[j] = rad_load_f32(x.base, x.dtype, r * x.pitch + ii); + rv[j] = has_res ? rad_load_f32(res.base, res.dtype, r * res.pitch + ii) : 0.0f; + wv[j] = rad_load_f32(gw.base, gw.dtype, ii * gw.stride) + wadd; } +#pragma unroll for (int j = 0; j < kPer; ++j) { const int64_t i = t + (int64_t)j * kWg; - if (i >= n) break; + if (i >= n) continue; float v = xv[j]; if (has_res) { const float s0 = v + rv[j]; diff --git a/libxe2/xe2_ops.h b/libxe2/xe2_ops.h index 3eba4b39..c8ea2708 100644 --- a/libxe2/xe2_ops.h +++ b/libxe2/xe2_ops.h @@ -71,6 +71,7 @@ int gemm_nt_q_m2(const RadArgs* a, RadStream s); int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream s); int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream s); int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream s); + int givens_dense_quant(const RadArgs* a, RadStream s); int gemm_nt_bias(const RadArgs* a, RadStream s); int dflash_conv(const RadArgs* a, RadStream s);