Archived
Kernels, each libref's arithmetic in libref's order: - gemm_nt_q at m2xfp4 (xe2_gemm_m2.cpp) with libref's m2 layout hooks, and givens_dense_quant (xe2_givens.cpp, the rotation in double as libref's) - the gated delta net (xe2_gdn.cpp): gdn_conv_prep, gdn_conv_update, gdn_kkt_solve, gdn_chunk_scan, gdn_recurrent_update (per-candidate and anchor state forms), gdn_gated_rmsnorm, gdn_conv_recurrent_update; each work-item one iteration of libref's parallel loop, the per-(sequence, head) workspaces in a shared device workspace (xe2_common: workspace()) - rope's multi-component modes (mrope, imrope, axial) and rope_table - logits_gemm over an fp8 128x128 joined head, gemm_nt_bias with its epilogue - mul, sigmoid, scatter_rows, layernorm, grid_embed, attn_dense (the vision tower) - dflash_conv and logit_rerank (plain and joined head) for the DFlash2 drafter libref's gemm_m2 gains the parts (pb1/pb2) every other ParoQuant grid and libr4d's mx_parts already define -- weight rows [0, e1), [e1, e2), [e2, N) read activation copies 0, 1, 2 side by side -- since this model's fused projections are rotated per piece and the oracle declined them; its shape hook widens a and a_scale by the part count. Both m2 arms now take the stored codes-then-meta blob as the engine describes it, u8 [N, K], beside the shape hook's rank-1 form (libref refused the engine's launch; only libr4d had been run on it). rad-kbench on the fixture: every new row at rel_l2 0 (90 gdn/givens cases, 108 rope, 80 elementwise, 20 attn_dense, the drafter's and the heads'). On the B65 the model starts with every op on the card and answers "The capital of France is" -> " Paris.\nThe capital of Germany is Berlin.\nThe capital of Italy is" (greedy), at 0.1 tok/s: the correctness-first kernels. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
94 lines
4.5 KiB
C++
94 lines
4.5 KiB
C++
/* xe2_givens.cpp -- givens_dense_quant: ParoQuant's activation side for the M2XFP4 linears.
|
|
* Correctness first, libref's ref_givens_dense_quant (ref_quant.cpp) step for step:
|
|
*
|
|
* per (row, part p, 128-column group gi):
|
|
* in[i] = x[gi*128 + i] / D[p*n + gi*128 + i] f32
|
|
* y[j] = (float) sum over ascending i of (double)T[p][gi][j][i] * (double)in[i]
|
|
* scale = amax(|y|) / qmax (1 for an all-zero group), code = clamp(y / scale, +-qmax)
|
|
*
|
|
* qmax 448 for an E4M3 `q`, 127 (rounded to nearest even) for an i8 one; `s` holds a scale per
|
|
* (row, part, group). One work-group a (row, part, group): the inputs in local memory, a work-item
|
|
* an output j, its dot in double in libref's order, so the codes and scales are libref's.
|
|
*/
|
|
#include "xe2_common.h"
|
|
#include "xe2_ops.h"
|
|
|
|
#include <cstring>
|
|
|
|
namespace xe2 {
|
|
|
|
namespace {
|
|
|
|
int64_t row_stride(const RadTensor* t, int64_t cols) {
|
|
if (!t || t->rank < 2) return cols;
|
|
int64_t span = 1;
|
|
for (int i = (int)t->rank - 1; i >= 0; --i) {
|
|
span *= t->shape[i];
|
|
if (span == cols) return (i > 0) ? t->stride[i - 1] : cols;
|
|
if (span > cols) break;
|
|
}
|
|
return t->stride[t->rank - 2];
|
|
}
|
|
|
|
int64_t last_stride(const RadTensor* t) { return t->rank ? t->stride[t->rank - 1] : 1; }
|
|
|
|
constexpr int kG = 128;
|
|
|
|
} // namespace
|
|
|
|
int givens_dense_quant(const RadArgs* a, RadStream st) {
|
|
if (!rad_args_have(a, 5)) return RAD_E_INVAL;
|
|
const RadTensor *X = &a->t[0], *T = &a->t[1], *D = &a->t[2], *Q = &a->t[3], *S = &a->t[4];
|
|
const int64_t n = rad_args_geti_or(a, "n", 0), P = rad_args_geti_or(a, "parts", 1);
|
|
const int64_t g = rad_args_geti_or(a, "group", 0);
|
|
if (n <= 0 || P <= 0 || g != kG || n % g != 0) return RAD_E_SHAPE;
|
|
const bool i8 = Q->dtype == RAD_I8;
|
|
if (!i8 && Q->dtype != RAD_F8E4M3) return RAD_E_DTYPE;
|
|
if (std::strcmp(rad_args_gets_or(a, "out", ""), i8 ? "i8" : "fp8") != 0) return RAD_E_DTYPE;
|
|
const int64_t rows = rad_tensor_numel(X) / n, ngrp = n / g;
|
|
if (rows <= 0 || rad_tensor_numel(T) < P * n * g || rad_tensor_numel(D) < P * n ||
|
|
rad_tensor_numel(Q) < rows * P * n || rad_tensor_numel(S) < rows * P * ngrp)
|
|
return RAD_E_SHAPE;
|
|
if (!rad_tensor_is_contiguous(T) || !rad_tensor_is_contiguous(D)) return refuse("givens_dense_quant: tables not contiguous");
|
|
const float qmax = i8 ? 127.0f : 448.0f;
|
|
const int64_t xrs = row_stride(X, n), xcs = last_stride(X);
|
|
const int64_t qrs = row_stride(Q, P * n), qcs = last_stride(Q);
|
|
const int64_t srs = row_stride(S, P * ngrp), scs = last_stride(S);
|
|
const char *xb = (const char*)X->data, *tb = (const char*)T->data, *db = (const char*)D->data;
|
|
char *qb = (char*)Q->data, *sb = (char*)S->data;
|
|
const uint32_t xdt = X->dtype, tdt = T->dtype, ddt = D->dtype, qdt = Q->dtype, sdt = S->dtype;
|
|
sycl::queue* q = queue_of(st);
|
|
if (!q) return RAD_E_DEVICE;
|
|
const size_t groups = (size_t)(rows * P * ngrp);
|
|
return submit([&] {
|
|
q->submit([&](sycl::handler& h) {
|
|
sycl::local_accessor<float, 1> in(sycl::range<1>(kG), h);
|
|
h.parallel_for(sycl::nd_range<1>{groups * kG, kG}, [=](sycl::nd_item<1> it) {
|
|
const int64_t gid = (int64_t)it.get_group(0);
|
|
const int64_t gi = gid % ngrp, p = (gid / ngrp) % P, r = gid / (ngrp * P);
|
|
const int j = (int)it.get_local_id(0);
|
|
const int64_t c = gi * kG + j;
|
|
in[j] = rad_load_f32(xb, xdt, r * xrs + c * xcs) / rad_load_f32(db, ddt, p * n + c);
|
|
sycl::group_barrier(it.get_group());
|
|
const int64_t t0 = ((p * ngrp + gi) * kG + j) * kG;
|
|
double acc = 0.0;
|
|
for (int i = 0; i < kG; ++i)
|
|
acc += (double)rad_load_f32(tb, tdt, t0 + i) * (double)in[i];
|
|
const float y = (float)acc;
|
|
const float amax = sycl::reduce_over_group(it.get_group(), sycl::fabs(y),
|
|
sycl::maximum<float>());
|
|
const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f;
|
|
const float inv = 1.0f / scl;
|
|
float v = y * inv;
|
|
if (v > qmax) v = qmax;
|
|
if (v < -qmax) v = -qmax;
|
|
if (i8) v = sycl::rint(v);
|
|
rad_store_f32(qb, qdt, r * qrs + (p * n + c) * qcs, v);
|
|
if (j == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
} // namespace xe2
|