diff --git a/libxe2/xe2_dflash.cpp b/libxe2/xe2_dflash.cpp index d6d7300f..17e0e67a 100644 --- a/libxe2/xe2_dflash.cpp +++ b/libxe2/xe2_dflash.cpp @@ -79,6 +79,86 @@ int rerank(const RadArgs* a, RadStream st, bool joined) { float* pairs = PAIRS ? (float*)PAIRS->data : nullptr; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; + if (!exact_only() && R <= 32) { + /* The fast form: a work-group a row, sub-group r candidate r, its lanes along K. libref sums + * in double; here each product is split exactly (two-product) and summed as a float pair + * (two-sum), the lanes' pairs combined in a fixed tree: ~48 significant bits, rounded once, + * so the score is libref's unless its exact value sits within ~2^-24 ulp of an f32 + * rounding boundary -- a fast kernel, held to the tolerance (and the drafter's acceptance + * changes only through such ties). The selection is libref's, one work-item a row. */ + return submit([&] { + q->submit([&](sycl::handler& h) { + sycl::local_accessor scl(sycl::range<1>((size_t)R), h); + sycl::local_accessor idl(sycl::range<1>((size_t)R), h); + h.parallel_for(sycl::nd_range<1>{(size_t)M * (size_t)R * kSub, (size_t)R * kSub}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const int64_t m = (int64_t)it.get_group(0); + const sycl::sub_group sg = it.get_sub_group(); + const int64_t r = (int64_t)sg.get_group_linear_id(); + const int lane = (int)sg.get_local_linear_id(); + HPlane cp, sp; + if (joined) { + cp = hplane(wb, "codes"); + sp = hplane(wb, "scale"); + if (!cp.p || !sp.p) return; + } + const int64_t g = ld_index(in.dtype, in.base, m * in.pitch + r); + const int64_t local = g - voff; + const bool pad = g < 0 || local < 0 || local >= NV; /* uniform in the sub-group */ + auto two_sum = [](float a, float b, float& s2, float& e) { + s2 = a + b; + const float bb2 = s2 - a; + e = (a - (s2 - bb2)) + (b - bb2); + }; + float hi = 0.0f, lo = 0.0f; + if (!pad) + for (int64_t k = lane; k < K; k += kSub) { + const float xv = rad_load_f32(x.base, x.dtype, m * x.pitch + k); + const float wv = joined ? cp.at(local, k) * sp.cover(local, k) + : rad_load_f32(w.base, w.dtype, local * w.pitch + k); + const float pr = xv * wv; + const float pe = sycl::fma(xv, wv, -pr); + float sv, se; + two_sum(hi, pr, sv, se); + hi = sv; + lo += se + pe; + } + for (int off = 8; off > 0; off >>= 1) { + const float h2 = sycl::shift_group_left(sg, hi, off); + const float l2 = sycl::shift_group_left(sg, lo, off); + float sv, se; + two_sum(hi, h2, sv, se); + hi = sv; + lo = lo + l2 + se; + } + if (lane == 0) { + idl[r] = pad ? -1 : (int32_t)g; + scl[r] = pad ? -INFINITY : hi + lo; + } + sycl::group_barrier(it.get_group()); + if (it.get_local_id(0) != 0) return; + uint32_t used = 0; + for (int64_t o = 0; o < R; ++o) { + int64_t best = -1; + for (int64_t c = 0; c < R; ++c) { + if (((used >> c) & 1u) || idl[c] < 0) continue; + if (best < 0 || scl[c] > scl[best] || (scl[c] == scl[best] && idl[c] < idl[best])) best = c; + } + const int32_t oid = best >= 0 ? idl[best] : -1; + const float ov = best >= 0 ? scl[best] : -INFINITY; + if (best >= 0) used |= 1u << best; + st_index(idx.dtype, idx.base, m * idx.pitch + o, (int64_t)oid); + rad_store_f32(val.base, val.dtype, m * val.pitch + o, ov); + if (pairs) { + float* pp = pairs + (m * R + o) * 2; + pp[0] = sycl::bit_cast(oid); + pp[1] = ov; + } + } + }); + }); + }); + } float* ws = workspace(*q, (size_t)(M * R * 3)); if (!ws) return RAD_E_DEVICE; const size_t wg = 16; diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index 146a80ce..4d49fb14 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -982,6 +982,23 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t const int64_t nblk = V / kCb; float* orow = workspace(qu, (size_t)(Tk * Hv * V + 64), 3); if (!orow) return RAD_E_DEVICE; + /* Each token's gate, beta and decay -- libref's double exp / log1p, which this card emulates -- + * once a (token, head) here, not once a block below: [Tk][Hv][3]. */ + float* gates = workspace(qu, (size_t)(Tk * Hv * 3 + 64), 4); + if (!gates) return RAD_E_DEVICE; + if (int rc0 = submit([&] { + qu.parallel_for(sycl::range<1>((size_t)(Tk * Hv)), [=](sycl::id<1> i) { + const int64_t tok = (int64_t)i[0] / Hv, h = (int64_t)i[0] % Hv; + const float alog = r.alog.p ? r.alog.ld(h * r.alog_s) : 0.0f; + const float dtb = r.dtb.p ? r.dtb.ld(h * r.dtb_s) : 0.0f; + const float gv = -rexp(alog) * softplus(r.av.ld(at2(r.a2, tok, h)) + dtb, r.spthr); + gates[i[0] * 3 + 0] = gv; + gates[i[0] * 3 + 1] = sigm(r.bv.ld(at2(r.b2, tok, h))); + gates[i[0] * 3 + 2] = rexp(gv); + }); + }); + rc0 < 0) + return rc0; /* Under `anchor` the replay is its own launch (phase 0): every block reads the stamp and the * previous step's factors before any block of the head overwrites them in phase 1. */ int rc = RAD_OK; @@ -1040,10 +1057,7 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t }; load_state(slot_in); sycl::group_barrier(grp); - const float alog = r.alog.p ? r.alog.ld(h * r.alog_s) : 0.0f; - const float dtb = r.dtb.p ? r.dtb.ld(h * r.dtb_s) : 0.0f; - auto update_col = [&](float g, float bt, float ve) { - const float dec = rexp(g); + auto update_dec = [&](float dec, float bt, float ve) { float sk2 = 0.0f; for (int64_t d = 0; d < K; ++d) { St[d * kCb + l] *= dec; @@ -1052,6 +1066,7 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t const float uu = bt * (ve - sk2); for (int64_t d = 0; d < K; ++d) St[d * kCb + l] += uu * kn[d]; }; + auto update_col = [&](float g, float bt, float ve) { update_dec(rexp(g), bt, ve); }; if (r.anchor && phase == 0) { for (int64_t t = 0; t < na; ++t) { const int64_t f = fb + t * al.fstr; @@ -1072,8 +1087,9 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t for (int c = 0; c < 3; ++c) r.state.st(fbs[c] + al.mark, (float)(c + 1)); for (int64_t t = 0; t < q_len; ++t) { const int64_t tok = bos + t; - const float gv = -rexp(alog) * softplus(r.av.ld(at2(r.a2, tok, h)) + dtb, r.spthr); - const float bt = sigm(r.bv.ld(at2(r.b2, tok, h))); + const float gv = gates[(tok * Hv + h) * 3 + 0]; + const float bt = gates[(tok * Hv + h) * 3 + 1]; + const float dec = gates[(tok * Hv + h) * 3 + 2]; sycl::group_barrier(grp); for (int64_t d = l; d < K; d += kCb) { qraw[d] = r.q.ld(at3(r.qv, tok, hk, d)); @@ -1092,7 +1108,7 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t kn[d] = kraw[d] * ks; } sycl::group_barrier(grp); - update_col(gv, bt, r.v.ld(at3(r.vv, tok, h, e))); + update_dec(dec, bt, r.v.ld(at3(r.vv, tok, h, e))); float oe = 0.0f; for (int64_t d = 0; d < K; ++d) oe += St[d * kCb + l] * qn[d]; orow[(tok * Hv + h) * V + e] = oe; diff --git a/libxe2/xe2_gemm_zp.cpp b/libxe2/xe2_gemm_zp.cpp index d606502f..6b43297a 100644 --- a/libxe2/xe2_gemm_zp.cpp +++ b/libxe2/xe2_gemm_zp.cpp @@ -207,3 +207,130 @@ int gemm_nt_q_zp(const RadArgs* a, RadStream st) { } } // namespace xe2 + +/* ------------------------------------------------------------------ the fast form */ +/* The same products and order, spread: a 16-lane sub-group an output column, lane l the groups + * l, l + 16, ... Within a group the sum of a * code is an exact integer (|a| <= 127, code <= 15, + * a group of <= 512: under 2^24), formed in f32 in any order; each group's term + * ((is - zero * a_sum) * a_scale) * scale is libref's; and the terms are added in group order by + * every lane from the lanes' registers (select_from_group), libref's chain. So the result is + * libref's bit for bit, at the cost of a read of the weight rather than one per output row. For + * the DFlash2 drafter's 2-bit head: N = 248320, 252 ms a call in the per-output form. */ +namespace xe2 { + +namespace { + +constexpr int kZpMaxM = 16; +constexpr int kZpMaxPer = 4; /* groups a lane: G <= 64 */ + +template +int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint32_t sadt, int64_t sa_rs, + const char* sumb, uint32_t sumdt, int64_t asum_s, int64_t mpad, const uint8_t* bb, int64_t brow, + int cbits, const char* bsb, char* yb, uint32_t ydt, int64_t ys_r, int64_t ys_c, int64_t N, + int64_t K, int64_t group, int mm) { + const int64_t G = K / group; + const size_t groups = (size_t)((N + 15) / 16); + return submit([&] { + syclex::nd_launch(q, sycl::nd_range<1>{groups * 256, 256}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const sycl::sub_group sg = it.get_sub_group(); + const int64_t n = (int64_t)it.get_group(0) * 16 + (int64_t)sg.get_group_linear_id(); + if (n >= N) return; + const DPlane sc = find_plane(bsb, "scale"), zp = find_plane(bsb, "zero"); + if (!sc.p || !zp.p) return; + const int lane = (int)sg.get_local_linear_id(); + const uint8_t* crow = bb + n * brow; + float term[MM][kZpMaxPer]; + for (int m = 0; m < MM; ++m) + for (int i = 0; i < kZpMaxPer; ++i) term[m][i] = 0.0f; + for (int i = 0; i < kZpMaxPer; ++i) { + const int64_t g = lane + (int64_t)i * kSub; + if (g >= G) break; + float is[MM]; + for (int m = 0; m < MM; ++m) is[m] = 0.0f; + for (int64_t k0 = g * group; k0 < (g + 1) * group; k0 += 16) { + float cf[16]; + for (int j = 0; j < 16; ++j) cf[j] = (float)rad_load_bits(crow, cbits, k0 + j); + for (int m = 0; m < MM; ++m) { + if (m >= mm) break; + /* the step order: logical column j of the step sits at byte + * (j & ~12) | ((j & 4) << 1) | ((j & 8) >> 1) */ + const sycl::vec av = *(const sycl::vec*)(ab + m * ars + k0); + float p = 0.0f; + for (int j = 0; j < 16; ++j) { + const int pos = (j & ~12) | ((j & 4) << 1) | ((j & 8) >> 1); + p += (float)av[pos] * cf[j]; + } + is[m] += p; + } + } + const float sb = sc.at(n, g); + const float z = (float)(int32_t)zp.at(n, g); + for (int m = 0; m < MM; ++m) { + if (m >= mm) break; + const float sa = rad_load_f32(sab, sadt, m * sa_rs); + const float sub = is[m] - z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s); + term[m][i] = sub * sa * sb; + } + } + for (int m = 0; m < MM; ++m) { + if (m >= mm) break; + float acc = 0.0f; + for (int i = 0; i < kZpMaxPer; ++i) + for (int l = 0; l < kSub; ++l) { + const int64_t g = (int64_t)i * kSub + l; + if (g < G) acc += sycl::select_from_group(sg, term[m][i], l); + } + if (lane == 0) rad_store_f32(yb, ydt, m * ys_r + n * ys_c, acc); + } + }); + }); +} + +} // namespace + +int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) { + if (exact_only()) return gemm_nt_q_zp(a, st); + if (!a || a->n_t < 5 || !a->t[0].data || !a->t[2].data || !a->t[4].data) return RAD_E_INVAL; + const RadTensor *A = &a->t[0], *B = &a->t[2], *Y = &a->t[4]; + const RadTensor* AS = rad_arg_in(a, 1); + const RadTensor* BS = rad_arg_in(a, 3); + const RadTensor* ASUM = rad_arg_in(a, 5); + const ZpGrid* zg = zp_grid(rad_args_gets_or(a, "dtype", nullptr)); + if (!zg || !zg->i8_row || rad_arg_in(a, 6) || rad_args_geti_or(a, "pb1", 0) > 0 || + rad_args_geti_or(a, "pb2", 0) > 0) + return gemm_nt_q_zp(a, st); + const int64_t K = rad_args_geti_or(a, "K", B->rank >= 2 ? B->shape[B->rank - 1] : 0); + const int64_t N = rad_args_geti_or(a, "N", B->rank >= 2 ? B->shape[B->rank - 2] : 0); + const int64_t group = rad_args_geti_or(a, "group", 0); + if (K <= 0 || N <= 0 || group <= 0 || group % 16 || group > 512 || K % group) return gemm_nt_q_zp(a, st); + const int64_t M = rad_tensor_numel(Y) / N, G = K / group; + if (M < 1 || M > kZpMaxM || G > kZpMaxPer * kSub || !BS || !AS || !ASUM || A->dtype != RAD_I8 || + B->dtype != zg->codes || !rad_tensor_is_contiguous(B) || (A->rank && A->stride[A->rank - 1] != 1)) + return gemm_nt_q_zp(a, st); + const int64_t ars = row_stride(A, K); + if (ars % 16 || !rad_ptr_aligned(A->data, 16)) return gemm_nt_q_zp(a, st); + const int64_t mpad = (M + 15) / 16 * 16; + if (rad_tensor_numel(AS) < M || rad_tensor_numel(ASUM) < G * mpad || rad_tensor_numel(A) < M * K || + rad_tensor_numel(B) < N * K) + return RAD_E_SHAPE; + sycl::queue* q = queue_of(st); + if (!q) return RAD_E_DEVICE; + const int64_t brow = rad_dtype_bytes(B->dtype, K); + const int cbits = rad_dtype_bits(B->dtype); + const int64_t sa_rs = row_stride(AS, 1); + const int64_t asum_s = ASUM->rank ? ASUM->stride[ASUM->rank - 1] : 1; + const int64_t ys_r = row_stride(Y, N), ys_c = Y->rank ? Y->stride[Y->rank - 1] : 1; + auto go = [&](auto tag) { + constexpr int MM = decltype(tag)::value; + return zp_gemv(*q, (const int8_t*)A->data, ars, (const char*)AS->data, AS->dtype, sa_rs, + (const char*)ASUM->data, ASUM->dtype, asum_s, mpad, (const uint8_t*)B->data, + brow, cbits, (const char*)BS->data, (char*)Y->data, Y->dtype, ys_r, ys_c, N, K, + group, (int)M); + }; + if (M == 1) return go(std::integral_constant{}); + if (M <= 8) return go(std::integral_constant{}); + return go(std::integral_constant{}); +} + +} // namespace xe2 diff --git a/libxe2/xe2_gemv_m2.cpp b/libxe2/xe2_gemv_m2.cpp index 65a2641e..1eb2298f 100644 --- a/libxe2/xe2_gemv_m2.cpp +++ b/libxe2/xe2_gemv_m2.cpp @@ -126,6 +126,157 @@ int launch(sycl::queue& q, const half* a16, const float* rr, const uint8_t* code }); } +/* The verify band (2 <= M <= 16) on XMX. A lane per output column: it reads its column's codes a + * 64-byte line (four 32-groups) at a time and writes the decoded fp16 pairs -- (u, u + 4) of each + * word, the subnormal-bits form times (1 + k/4) * 2^(e - e_max + 14) by one half2 multiply, + * exactly e2m1 * (1 + k/4) * 2^(e - e_max) -- into its sub-group's VNNI slice of local memory, + * consecutive words across the lanes; then the sub-group runs eight K16 DPAS against the fp16 + * activation fp16(a * a_scale / R) laid out in the same permuted order. The vector ALUs keep the + * decode, DPAS takes the rows: on the ALUs alone M = 8 costs five times M = 1 (m2bench in scratch). + * K is split when the columns alone give too few work-groups; the partials are added in split + * order, then y = acc * R_m * 2^(e_max - 127). Products in f32 (DPAS), the activation rounded once + * to fp16: held to the gemm tolerance and the KL bound like the rest. */ +namespace jm = sycl::ext::oneapi::experimental::matrix; + +constexpr int kKs = 4; /* 32-groups a step: one 64-byte line of a column's codes */ + +template +int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, const int32_t* emax, + const uint8_t* codes0, const uint8_t* meta0, const uint8_t* expe0, int64_t brs, + float* pbuf, int64_t N, int64_t K, int64_t e1, int64_t e2, int64_t mpad, int splits) { + const int64_t ntile = (N + 127) / 128; + const size_t groups = (size_t)(ntile * splits); + const int64_t kg = K / 32; + const int64_t per = ((kg + splits - 1) / splits + kKs - 1) / kKs * kKs; + const int64_t ldp = ntile * 128; + return submit([&] { + q.submit([&](sycl::handler& h) { + sycl::local_accessor bt(sycl::range<1>((size_t)8 * kKs * 16 * 16), h); + h.parallel_for(sycl::nd_range<1>{groups * 128, 128}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + using H2 = sycl::vec; + const sycl::sub_group sg = it.get_sub_group(); + const int s = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id(); + const int64_t tile = (int64_t)it.get_group(0) % ntile, sp = (int64_t)it.get_group(0) / ntile; + const int64_t g0s = sp * per, g1s = sycl::min(kg, g0s + per); + const int64_t n0 = tile * 128 + s * 16; + if (n0 >= N) return; /* uniform */ + const int64_t n = sycl::min(n0 + lane, N - 1); + const int64_t part = n0 < e1 ? 0 : (n0 < e2 ? 1 : 2); + const half* abase = a16 + part * K; + const uint8_t* crow = codes0 + n * (K / 2); + const int emx = emax[n]; + uint32_t* mine = &bt[(size_t)s * kKs * 16 * 16]; + jm::joint_matrix ma; + jm::joint_matrix mb; + jm::joint_matrix mc[MT]; + for (int i = 0; i < MT; ++i) jm::joint_matrix_fill(sg, mc[i], 0.0f); + for (int64_t g0 = g0s; g0 < g1s; g0 += kKs) { + const sycl::vec line = *(const sycl::vec*)(crow + g0 * 16); + const uint32_t e4 = *(const uint32_t*)(expe0 + n * brs + g0); + const uint32_t m4 = *(const uint32_t*)(meta0 + n * kg + g0); + for (int gg = 0; gg < kKs; ++gg) { + const int d = (int)((e4 >> (8 * gg)) & 0xFF) - emx; + const float sc = d < -30 ? 0.0f : sycl::ldexp(16384.0f, d); + const uint32_t mt = (m4 >> (8 * gg)) & 0xFF; + for (int j = 0; j < 4; ++j) { + const uint32_t word = line[gg * 4 + j]; + const half kh = (half)((1.0f + 0.25f * (float)((mt >> (2 * j)) & 3)) * sc); + const H2 k2(kh, kh); + for (int u = 0; u < 4; ++u) { + const uint32_t mb2 = u < 3 ? (word << (9 - 4 * u)) & 0x0E000E00u : (word >> 3) & 0x0E000E00u; + const uint32_t sb2 = (word << (12 - 4 * u)) & 0x80008000u; + mine[(gg * 16 + j * 4 + u) * 16 + lane] = sycl::bit_cast(sycl::bit_cast

(mb2 | sb2) * k2); + } + } + } + sycl::group_barrier(sg); + for (int kk = 0; kk < kKs * 32; kk += 16) { + auto lb = sycl::address_space_cast((half*)mine + (size_t)(kk / 2) * 32); + jm::joint_matrix_load(sg, mb, lb, 32); + for (int i = 0; i < MT; ++i) { + auto ga = sycl::address_space_cast(abase + (int64_t)i * 8 * lda + g0 * 32 + kk); + jm::joint_matrix_load(sg, ma, ga, (size_t)lda); + jm::joint_matrix_mad(sg, mc[i], ma, mb, mc[i]); + } + } + sycl::group_barrier(sg); + } + for (int i = 0; i < MT; ++i) { + auto gp = sycl::address_space_cast(pbuf + (sp * mpad + (int64_t)i * 8) * ldp + n0); + jm::joint_matrix_store(sg, mc[i], gp, (size_t)ldp, jm::layout::row_major); + } + }); + }); + }); +} + +int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8_t* codes0, + const uint8_t* meta0, const uint8_t* expe0, int64_t brs, const RadTensor* Y, int64_t M, + int64_t N, int64_t K, int64_t acols, int64_t scols, int64_t ars, int64_t srs, int64_t yrs, + int64_t ycs, int64_t e1, int64_t e2) { + const int mt = (int)((M + 7) / 8); + const int64_t mpad = (int64_t)mt * 8, kg = K / 32, kb = kg; + const int64_t ntile = (N + 127) / 128, ldp = ntile * 128; + constexpr int64_t kFill = 160; + const int splits = (int)std::min(std::max(1, (kFill + ntile - 1) / ntile), std::max(1, kg / kKs)); + const int64_t abytes = (mpad * acols * 2 + 255) / 256 * 256, rbytes = (mpad * 4 + 255) / 256 * 256; + float* ws = workspace(q, (size_t)((abytes + rbytes + N * 4) / 4 + 64)); + float* pbuf = workspace(q, (size_t)(splits * mpad * ldp), 2); + if (!ws || !pbuf) return RAD_E_DEVICE; + half* a16 = (half*)ws; + float* rr = (float*)((char*)ws + abytes); + int32_t* emax = (int32_t*)((char*)ws + abytes + rbytes); + const uint8_t* ab = (const uint8_t*)A->data; + const char* sab = (const char*)AS->data; + const uint32_t sadt = AS->dtype; + int rc = submit([&] { + 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) { + const int64_t p = c & 7, src = (c & ~(int64_t)7) + (p >> 1) + 4 * (p & 1); + a16[m * acols + c] = m < M ? (half)(e4m3(ab[m * ars + src]) * rad_load_f32(sab, sadt, m * srs + src / 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; + int64_t g = 0; + if ((kb & 15) == 0 && ((uintptr_t)er & 15) == 0) + for (; g < kb; g += 16) { + const sycl::vec ev = *(const sycl::vec*)(er + g); + for (int u = 0; u < 16; ++u) mx = sycl::max(mx, (int32_t)ev[u]); + } + for (; g < kb; ++g) mx = sycl::max(mx, (int32_t)er[g]); + emax[i[0]] = mx; + }); + }); + if (rc < 0) return rc; + rc = mt == 1 ? launch_dpas<1>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits) + : launch_dpas<2>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits); + if (rc < 0) return rc; + char* yb = (char*)Y->data; + const uint32_t ydt = Y->dtype; + return submit([&] { + q.parallel_for(sycl::range<1>((size_t)(M * N)), [=](sycl::id<1> i) { + const int64_t m = (int64_t)i[0] / N, c = (int64_t)i[0] % N; + float acc = 0.0f; + for (int sp = 0; sp < splits; ++sp) acc += pbuf[(sp * mpad + m) * ldp + c]; + rad_store_f32(yb, ydt, m * yrs + c * ycs, acc * rr[m] * sycl::ldexp(1.0f, emax[c] - 127)); + }); + }); +} + } // namespace int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) { @@ -161,6 +312,11 @@ int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) { const uint8_t* codes0 = (const uint8_t*)B->data; const uint8_t* meta0 = codes0 + N * (K / 2); const int mm = (int)M; + /* the verify band on XMX, where its line loads fit: K a multiple of 128, aligned rows */ + if (mm >= 2 && K % 128 == 0 && brs % 4 == 0 && rad_ptr_aligned(BS->data, 4) && + ((uintptr_t)meta0 & 3) == 0 && (K / 2) % 64 == 0 && rad_ptr_aligned(B->data, 64)) + return m2_dpas(*q, A, AS, codes0, meta0, (const uint8_t*)BS->data, brs, Y, M, N, K, acols, scols, + ars, srs, yrs, ycs, e1, e2); /* The activation once as fp16 in the weight pairs' order, with its row factor R. */ const int64_t abytes = (M * acols * 2 + 255) / 256 * 256; float* ws = workspace(*q, (size_t)(abytes / 4 + M + 64)); diff --git a/libxe2/xe2_ops.h b/libxe2/xe2_ops.h index e59cc9ab..3eba4b39 100644 --- a/libxe2/xe2_ops.h +++ b/libxe2/xe2_ops.h @@ -68,6 +68,7 @@ int gemm_zp_unrelayout(const RadParam* p, int n_p, int operand, const RadEncodin const int* sel, const void* src, int64_t src_bytes, const RadTensor* planes, int n); 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); diff --git a/libxe2/xe2_rows.cpp b/libxe2/xe2_rows.cpp index f16d921c..fc2256c4 100644 --- a/libxe2/xe2_rows.cpp +++ b/libxe2/xe2_rows.cpp @@ -24,6 +24,7 @@ const RadConstraint cFp8Dtype[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16") }; const RadConstraint cFp8Gated[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16"), RAD_CLE("N", 57344) }; /* The zero-point grids over quant_act_i8's activation (xe2_gemm_zp.cpp). */ const RadConstraint cZpI8Dtype[] = { RAD_CIN("dtype", "w2a8 w4a8_asym") }; +const RadConstraint cZpGemv[] = { RAD_CIN("dtype", "w2a8 w4a8_asym"), RAD_CLE("M", 16) }; /* The M2XFP4 grid over givens_dense_quant's fp8 activation (xe2_gemm_m2.cpp). */ const RadConstraint cM2Dtype[] = { RAD_CIN("dtype", "m2xfp4") }; /* The fp8 head's decode band. */ @@ -260,6 +261,15 @@ const RadKernelInfo kKernels[] = { "sequence", .shape = "any steps, top_k and rank", .dtypes = "integer candidates, float scores and factors", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::dflash_select }, + { .name = "xe2_gemm_nt_q_zp_gemv", .op = "gemm_nt_q", .family = "gemm", + .computes = "the zero-point grids at M <= 16: a sub-group a column, exact integer group sums, " + "libref's group terms added in order through lane broadcasts -- libref's result", + .shape = "M <= 16; group a multiple of 16 up to 512, at most 64 groups", + .dtypes = "w2a8, w4a8_asym: u2/u4 codes, f16 scale and unsigned zero per (row, group), i8 " + "activation in step order with row scale and group sums", + .domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cZpGemv, .n_constraints = 2, + .launch = xe2::gemm_nt_q_zp_gemv, .layout = xe2::gemm_zp_layout, + .relayout = xe2::gemm_zp_relayout, .unrelayout = xe2::gemm_zp_unrelayout }, { .name = "xe2_gemm_nt_q_zp_ref", .op = "gemm_nt_q", .family = "gemm", .computes = "Y = A B^T over an unsigned zero-point grid against quant_act_i8's activation, " "libref's gemm_zp per output; the correctness-first form", diff --git a/scripts/sycl/profile_decode.sh b/scripts/sycl/profile_decode.sh index ccb8db56..068da209 100755 --- a/scripts/sycl/profile_decode.sh +++ b/scripts/sycl/profile_decode.sh @@ -11,6 +11,7 @@ # PROMPT_FILE plain text, sent as one /v1/completions prompt # STEPS decode steps measured (default 128) # Needs the oneAPI environment (it sources setvars.sh if icpx is not on PATH) and build-sycl/. +# KEEP=DIR keeps the two servers' logs (the per-geometry tables) in DIR. set -eu model=${1:?usage: profile_decode.sh MODEL PROMPT_FILE [STEPS] [flags...]} prompt=${2:?usage: profile_decode.sh MODEL PROMPT_FILE [STEPS] [flags...]} @@ -48,6 +49,7 @@ run() { # run TOKENS FLAGS... -> $work/TOKENS.tsv: op, total ms, calls run 1 "$@" run $((steps + 1)) "$@" +[ -n "${KEEP:-}" ] && cp "$work"/*.log "$KEEP"/ join "$work/1.tsv" "$work/$((steps + 1)).tsv" | awk -v n="$steps" ' { d = $4 - $2; T += d; a[$1] = d; k[$1] = ($5 - $3) / n } END {