Archived
New models and formats: - IQR for Qwen3.8-27B: weights on the int8 grid under GPTQ, a trellis down each GPTQ column (IQR-V2), and IQR-V3, a computed trellis code under one scale a row, kept at its codes' width in VRAM and decoded in the GEMM; 15 IQR-V3 recipes from ~2 to ~7 bits a weight (data/recipes/q38-27b-*-v3.recipe, docs/IQR.md) - libhalo: a kernel library for Strix Halo (gfx1151), preferred there over libr3d - Nemotron 3.5 Embed 1B and 8B (nemotron35_embed): text and image embeddings through a Pixtral tower, on the card and on the CPU - Nemotron 3.5 Rerank 8B - /v1/systemone: Cloudflare's clef and clef-flash, Convai Innovations' Laya and Mica beside LiquidAI d1; scored steps read the candidates' logits from the head table with no lm_head pass - LFM2's 8-bit trunk: block-fp8 or int8 linears with the fused producers, and the d1-3B recipe - DeepSeek-V4.1's reply format with DSML tool calls and typed argument tags Weights on the drive: - The file tier reads only the experts the pass routes rows to; the draft head's weights stay in the host pool - A hot VRAM cache for routed experts read off the drive, reads deposited straight into free host pool slots, and copyless evictions; the file tier's counters are now ssd_* - GLM-5 Next shards its routed experts by expert, not by width, so they can live on the drive - The stall watch counts drive reads as progress, and the DMA lane's room check reads the lane's own completion fence Quantisation tooling: - --calib-corpus, --calib-dir and --calib-layers record each linear's input Hessian; beside --kld-record the reference's own run records them - --kld-top N records a sparse KL reference (the N most likely tokens and the tail's mass) - GPTQ in a third of the time with the same codes, trellis GPTQ on the card, vector codebooks with coordinate descent, and rad-convert --reuse repeated, each container supplying what the earlier ones lack Speculation: - Prompt lookup alone (--spec-ngram K, --spec-ngram-min/-max): each sequence keeps an index of its n-grams, and a proposal is verified only when its measured step cost says it pays; --draft-window adaptive follows it Speed: - IQR decode: the narrow tile splits K for four times the int8 tile's waves and folds each 128-K block once; at 8 tokens down and the output projections run in 0.61-0.82x and 0.70-0.92x FP8's time (2-6 bits); IQ6R-S-V3 with its DFlash2 drafter steps in 26.3 ms at TP2, against 29.4 ms before and FP8's 27.3 - Qwen MXFP4 feed-forward: the gated fold up to 16 rows, not 8 - libr4d: the gated prefill fold, the short-prompt split row, and the rows forms of rmsnorm and rope; KLATE again for the head-256 gqa6 paged prefill; gram_accum on the matrix cores Features: - /health, /v1/health and /ping answer 503 with "status": "stuck" once the device has advanced nothing for a minute or a queue has faulted, so a supervisor can restart a wedged server (#356) - --image-gen-max-pixels and --image-gen-max-refs size an image generator's buffers (#344) - The startup report lists only the options not at their default, with a count; -v lists all (#250) - --preempt-wait-ms bounds how long a preempted prefill waits for its whole-recompute room - --placement layer_offload spills dense layers so one --max-model-len sequence fits - Startup refuses amdgpu with CWSR off and says how to fix it; --proceed-with-incompatible-configuration starts anyway under an UNSUPPORTED banner Fixes: - The n-gram read-ahead hands each row to the reader once and reads only as far ahead as the row cache holds, and pauses while decode reads: about one drive read a row instead of two (#355) - A file-tier window reads each of its runs once, not once a run per run - The decode split law's verify floor is bounded by the batch's workgroups: max_seqs 256 no longer declares 64x the scratch (#342) - A prefetch DMA list's mark stays out of the signal pool until the engine has read it, and a prefill stage copies only the blocks its own tables name (#343) - The split-KV row sum is read with fetch-inactive (#328) - LFM2 on the host backend: libref and libavx serve the row-form ops it declares (#348) - The preempted-prefill gate skips a KV group that holds no blocks (#352) - Every request entry point bounded, gated and sanitised (JSON depth, invalid UTF-8, control bytes in the log, the dashboard's CSP, cross-origin browsers on the loopback-only routes); a prompt id outside the vocabulary refused at admission; --api-key "" refused at startup - The telemetry the HTTP thread reads is atomic - An RMS norm row no longer depends on how many rows its launch carries; prefill attention's rescale is decided by live rows only - A --kld-record prefill chunk that finishes no sequence gets its logits on a tied-head model - A kernel's lazily made counter block is zeroed outside the lock other ranks take (a TP2 first-step hang) Changes: Plugin ABI 32. Every plugin exports a layout fingerprint of the ABI structs and the loader refuses a plugin without it or with another (#302), so out-of-tree plugins must be rebuilt. Appended: RadBuildCtx calib_* and gen_max_*, RadArchProbe gen_max_*, RadBatch score_only and plugin_words, RadDeviceProps::unified, RadChatFormat arg_json_value_prefix.
335 lines
16 KiB
Plaintext
335 lines
16 KiB
Plaintext
/* Derived from libr4d/r4d_gemm_w5a16_nt_m64.hip by the libhalo rename transform (2026-10-26),
|
|
* then ported to gfx1151; see libhalo/README.md. */
|
|
// halo_gemm_w5a16_nt_m64.hip - halo_gemm_w5a16_nt_m64: 5-bit-weight skinny GEMM for M up to 64,
|
|
// gfx1151. C[M,N] = A[M,K] @ dequant(Wq)[N,K]^T, f16 A, bf16 C, fp32 accumulate.
|
|
//
|
|
// halo_gemm_w4a16_nt_m64 with a fifth bit per weight, for ParoQuant's int5 (asymmetric: an f16 scale
|
|
// and a 5-bit integer zero per 128 of K, with the rotation applied to the activation beforehand).
|
|
// That kernel's header applies; this one covers only what the fifth bit changes.
|
|
//
|
|
// The weight blob is two planes. The low four bits of each code are in w4's fragment order (offset
|
|
// binary: the nibble is q & 15, no sign flip). The fifth bits follow as 32-bit words, one per
|
|
// (n-tile, 64-wide k block, lane):
|
|
//
|
|
// hi[(t * (K/64) + kb) * 32 + lane] byte s = k step s of the block, bit e = element e
|
|
//
|
|
// where element e is the one nibble 2e / 2(e-4)+1 of dword s holds. A lane's 16 bytes of nibbles and
|
|
// 4 bytes of fifth bits cover the same 32 weights; the second load is one dword per block.
|
|
//
|
|
// The unpack adds two multiplies to w4's. The even elements' fifth bits are the byte's low nibble,
|
|
// the odd elements' its high one. n * 0x00204081 moves bit j of a 4-bit mask to bit 8j (no other
|
|
// term lands on a multiple of eight below bit 32); masking with 0x01010101 and shifting by 4 puts
|
|
// it at the 16s place. The code is then 0..31, 1024 + q is exact in f16, and the integer zero
|
|
// (0..31, stored as -(1024 + zero)) subtracts exactly.
|
|
//
|
|
// The dispatcher runs M past 64 as chunks of 64 rows, re-reading the weight per chunk (near the f16
|
|
// compute bound at a 2048-row prefill chunk anyway), and partitioned weights (ParoQuant's gate|up:
|
|
// `pb1`/`pb2`, see halo_gemm_args.h) as one launch per part.
|
|
#include "halo.h"
|
|
#include "halo_gemm_args.h"
|
|
#include "halo_gdn_wmma.h"
|
|
#include <hip/hip_bf16.h>
|
|
#include <hip/hip_fp16.h>
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
|
|
#define HALO_GEMM_W4_MAX_M 64
|
|
#define HALO_GEMM_W4_KPB 64 // K per packed block: 4 k steps, 16 bytes per lane
|
|
#define WAVE32 32
|
|
|
|
typedef _Float16 v16h __attribute__((ext_vector_type(16)));
|
|
typedef unsigned uint4v __attribute__((ext_vector_type(4)));
|
|
|
|
// NT and NPW: see halo_gemm_w4a16_nt_m64.hip. Only NPW 1 and 4 are instantiated.
|
|
|
|
// Address-space-qualified so loads stay global_load, not flat_load (on gfx12 a flat load also
|
|
// increments dscnt). See halo_gemm_bf16_nt_m64.hip for why the element type is a scalar.
|
|
typedef __attribute__((address_space(1))) const uint4v* halo_gw4;
|
|
typedef __attribute__((address_space(1))) const unsigned long long* halo_gu2;
|
|
typedef __attribute__((address_space(1))) const unsigned* halo_gu1;
|
|
|
|
/* gfx11 fragment: the row's whole 16-wide k run, 32 contiguous bytes, where the gfx12 form was
|
|
* two 8-byte halves sixteen bytes apart. The pointer step is 32 bytes either way. */
|
|
__device__ __forceinline__ v16h fragA(halo_gu2 p, int u) {
|
|
union { unsigned long long w[4]; v16h f; } x;
|
|
x.w[0] = p[4 * u];
|
|
x.w[1] = p[4 * u + 1];
|
|
x.w[2] = p[4 * u + 2];
|
|
x.w[3] = p[4 * u + 3];
|
|
return x.f;
|
|
}
|
|
|
|
// Do not fold the group scale into the B fragment; see halo_gemm_w4a16_nt_m64.hip.
|
|
|
|
// One dword of eight nibbles plus the byte of their fifth bits -> eight f16 values (four packed
|
|
// pairs), before the zero point is subtracted. Bits 0..3 of `hb` are the even nibbles' fifth
|
|
// bits, bits 4..7 the odd nibbles'. Each v_perm_b32 pairs two code bytes with the high byte 0x64
|
|
// (1024 + code). Codes are offset binary (0..31, or 0..15 without the fifth bit).
|
|
__device__ __forceinline__ unsigned spread16(unsigned n) { // 4 bits -> bit 4 of each byte
|
|
return ((n * 0x00204081u) & 0x01010101u) << 4;
|
|
}
|
|
__device__ __forceinline__ void dequant5_pairs(unsigned w, unsigned hb, unsigned* u) {
|
|
const unsigned K64 = 0x64646464u;
|
|
const unsigned t0 = (w & 0x0F0F0F0Fu) | spread16(hb & 15u);
|
|
const unsigned t1 = ((w >> 4) & 0x0F0F0F0Fu) | spread16(hb >> 4);
|
|
u[0] = __builtin_amdgcn_perm(K64, t0, 0x05010400u); // elements 0, 1
|
|
u[1] = __builtin_amdgcn_perm(K64, t0, 0x05030402u); // elements 2, 3
|
|
u[2] = __builtin_amdgcn_perm(K64, t1, 0x05010400u); // elements 4, 5
|
|
u[3] = __builtin_amdgcn_perm(K64, t1, 0x05030402u); // elements 6, 7
|
|
}
|
|
|
|
/* The stored weight keeps libr4d's gfx12 fragment order byte for byte, fifth-bit plane included:
|
|
* its byte per step travels with the nibble dword it belongs to. One cross-half exchange of each
|
|
* and a pair interleave reassemble the gfx11 fragment, as in halo_gemm_w4a16_nt_m64.hip. nz holds
|
|
* -(1024 + zero) in both halves. */
|
|
__device__ __forceinline__ v16h dequant5(unsigned w, unsigned hb, __half2 nz, int lane) {
|
|
const unsigned pw = (unsigned)__shfl_xor((int)w, 16);
|
|
const unsigned phb = (unsigned)__shfl_xor((int)hb, 16);
|
|
const bool hi = lane >> 4;
|
|
unsigned lo[4], hd[4];
|
|
dequant5_pairs(hi ? pw : w, hi ? phb : hb, lo);
|
|
dequant5_pairs(hi ? w : pw, hi ? hb : phb, hd);
|
|
union { unsigned u[8]; __half2 h2[8]; v16h f; } x;
|
|
x.u[0] = lo[0]; x.u[1] = lo[1]; x.u[2] = hd[0]; x.u[3] = hd[1];
|
|
x.u[4] = lo[2]; x.u[5] = lo[3]; x.u[6] = hd[2]; x.u[7] = hd[3];
|
|
#pragma unroll
|
|
for (int i = 0; i < 8; i++) x.h2[i] = __hadd2(x.h2[i], nz);
|
|
return x.f;
|
|
}
|
|
|
|
__device__ __forceinline__ v8f mmah(v16h a, v16h b, v8f c) {
|
|
return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c);
|
|
}
|
|
|
|
// MT = row tiles per block, as in the bf16 kernel. HI false is the 4-bit asymmetric grid (z-lab's
|
|
// plain int4), with no fifth-bit plane.
|
|
template <int MT, int NPW, bool NT, bool HI>
|
|
__global__ __launch_bounds__(1024) void halo_gemm_w5a16_nt_m64_kernel(
|
|
const unsigned short* __restrict__ A, const unsigned* __restrict__ Wq,
|
|
const unsigned* __restrict__ Whi, const unsigned* __restrict__ Wsz,
|
|
__hip_bfloat16* __restrict__ C, int M, int K, int N, int WV, int SK, int lda, int ldc) {
|
|
extern __shared__ float red[]; // WV*NPW*SK*256 floats: a 16x16 tile per (column, split)
|
|
const int tid = threadIdx.x, wave = tid / WAVE32, lane = tid % WAVE32;
|
|
const int col_local = wave / SK, ksplit = wave % SK;
|
|
const int nt0 = (blockIdx.x * WV + col_local) * NPW;
|
|
const int n0 = nt0 * 16;
|
|
const int mt0 = blockIdx.y * MT;
|
|
const int Ks = K / SK;
|
|
const int groups = Ks / HALO_GEMM_W4_GROUP; // groups this split owns
|
|
const int kbase = ksplit * Ks;
|
|
|
|
v8f acc[NPW][MT];
|
|
#pragma unroll
|
|
for (int j = 0; j < NPW; j++)
|
|
#pragma unroll
|
|
for (int i = 0; i < MT; i++) acc[j][i] = (v8f)(0.f);
|
|
|
|
if (n0 < N) {
|
|
// Activation rows are clamped, not masked; results past M are never stored.
|
|
unsigned aoff[MT];
|
|
#pragma unroll
|
|
for (int i = 0; i < MT; i++)
|
|
aoff[i] = rowGo((unsigned)lda, (mt0 + i) * 16, M - 1, lane) + (unsigned)kbase;
|
|
halo_gu2 pa[MT];
|
|
#pragma unroll
|
|
for (int i = 0; i < MT; i++) pa[i] = (halo_gu2)(const unsigned long long*)(A + aoff[i]);
|
|
|
|
// Weight: one uint4 per lane per four k steps, already permuted, so the k loop is
|
|
// global_load_b128 at a constant stride.
|
|
const int kpb = K / HALO_GEMM_W4_KPB; // packed blocks in a full row of tiles
|
|
const int nsz = K / HALO_GEMM_W4_GROUP; // (scale, zero) dwords per row of tiles
|
|
halo_gw4 pw[NPW];
|
|
halo_gu1 ph[NPW];
|
|
halo_gu1 ps[NPW];
|
|
const int ntlast = N / 16 - 1;
|
|
#pragma unroll
|
|
for (int j = 0; j < NPW; j++) {
|
|
// Column tiles past N are clamped to the last real tile; the store discards them.
|
|
const int t = (nt0 + j) < ntlast ? (nt0 + j) : ntlast;
|
|
pw[j] = (halo_gw4)(const uint4v*)(Wq) +
|
|
(size_t)(t * kpb + kbase / HALO_GEMM_W4_KPB) * WAVE32 + lane;
|
|
// Fifth-bit plane: one dword per lane per packed block (unused when !HI).
|
|
ph[j] = (halo_gu1)(const unsigned*)(HI ? Whi : Wq) +
|
|
(size_t)(t * kpb + kbase / HALO_GEMM_W4_KPB) * WAVE32 + lane;
|
|
// Quantiser parameters: 16 dwords per group, read by the 16 lanes that own the rows.
|
|
ps[j] = (halo_gu1)(const unsigned*)(Wsz) +
|
|
(size_t)(t * nsz + kbase / HALO_GEMM_W4_GROUP) * 16 + (lane & 15);
|
|
}
|
|
|
|
const int bpg = HALO_GEMM_W4_GROUP / HALO_GEMM_W4_KPB; // packed blocks per group
|
|
|
|
for (int g = 0; g < groups; g++) {
|
|
float sc[NPW];
|
|
__half2 nz[NPW];
|
|
#pragma unroll
|
|
for (int j = 0; j < NPW; j++) {
|
|
const unsigned sz = ps[j][(size_t)g * 16];
|
|
sc[j] = __half2float(__ushort_as_half((unsigned short)(sz & 0xFFFFu)));
|
|
nz[j] = __half2half2(__ushort_as_half((unsigned short)(sz >> 16)));
|
|
}
|
|
|
|
// The first WMMA of a group takes an inline zero for C, so no clear is needed.
|
|
v8f g_acc[NPW][MT];
|
|
#pragma unroll
|
|
for (int b = 0; b < bpg; b++) {
|
|
uint4v wv[NPW];
|
|
unsigned hv[NPW];
|
|
#pragma unroll
|
|
for (int j = 0; j < NPW; j++) {
|
|
const size_t o = (size_t)(g * bpg + b) * WAVE32;
|
|
wv[j] = NT ? __builtin_nontemporal_load(&pw[j][o]) : pw[j][o];
|
|
hv[j] = HI ? (NT ? __builtin_nontemporal_load(&ph[j][o]) : ph[j][o]) : 0u;
|
|
}
|
|
#pragma unroll
|
|
for (int s = 0; s < 4; s++) {
|
|
const int u = b * 4 + s;
|
|
v16h afrag[MT];
|
|
#pragma unroll
|
|
for (int i = 0; i < MT; i++) afrag[i] = fragA(pa[i], u); // loaded once, spent NPW times
|
|
#pragma unroll
|
|
for (int j = 0; j < NPW; j++) {
|
|
const v16h bfrag = dequant5(wv[j][s], (hv[j] >> (8 * s)) & 0xFFu, nz[j], lane);
|
|
#pragma unroll
|
|
for (int i = 0; i < MT; i++)
|
|
g_acc[j][i] = u == 0 ? mmah(afrag[i], bfrag, (v8f)(0.f))
|
|
: mmah(afrag[i], bfrag, g_acc[j][i]);
|
|
}
|
|
}
|
|
}
|
|
#pragma unroll
|
|
for (int j = 0; j < NPW; j++)
|
|
#pragma unroll
|
|
for (int i = 0; i < MT; i++)
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; e++) acc[j][i][e] = fmaf(g_acc[j][i][e], sc[j], acc[j][i][e]);
|
|
#pragma unroll
|
|
for (int i = 0; i < MT; i++) pa[i] += (HALO_GEMM_W4_GROUP / 16) * 4;
|
|
}
|
|
}
|
|
|
|
// Split-K reduction and store as in halo_gemm_bf16_nt_m64: one row tile at a time, fixed split
|
|
// order.
|
|
const int ncols = WV * NPW;
|
|
const int nthreads = WV * SK * WAVE32;
|
|
#pragma unroll
|
|
for (int i = 0; i < MT; i++) {
|
|
__syncthreads();
|
|
#pragma unroll
|
|
for (int j = 0; j < NPW; j++) {
|
|
const int slot = ((col_local * NPW + j) * SK + ksplit) * 256;
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; e++) red[slot + e * WAVE32 + lane] = acc[j][i][e];
|
|
}
|
|
__syncthreads();
|
|
for (int idx = tid; idx < ncols * 256; idx += nthreads) {
|
|
const int col = idx >> 8, off = idx & 255;
|
|
float v = 0.f;
|
|
for (int s = 0; s < SK; s++) v += red[(col * SK + s) * 256 + off];
|
|
const int l = off & 31;
|
|
const int n = (blockIdx.x * ncols + col) * 16 + (l & 15);
|
|
const int m = (mt0 + i) * 16 + accRow(l, off >> 5);
|
|
if (n < N && m < M) C[(size_t)m * ldc + n] = (__hip_bfloat16)v;
|
|
}
|
|
}
|
|
}
|
|
|
|
void halo_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, int K, int N,
|
|
int WV, int SK, int MB, int NPW, int NT, long stream, int lda, int ldc,
|
|
bool hi5) {
|
|
if (!lda) lda = K;
|
|
if (!ldc) ldc = N;
|
|
if (M < 1 || M > HALO_GEMM_W4_MAX_M)
|
|
throw std::runtime_error("r4d gemm_w5a16_nt_m64: M must be 1..64");
|
|
if (N % 16) throw std::runtime_error("r4d gemm_w5a16_nt_m64: N must be a multiple of 16");
|
|
if (K % (SK * HALO_GEMM_W4_GROUP))
|
|
throw std::runtime_error("r4d gemm_w5a16_nt_m64: K must be divisible by SK*128");
|
|
if (WV * SK * WAVE32 > 1024) throw std::runtime_error("r4d gemm_w5a16_nt_m64: threads/block > 1024");
|
|
if (MB < 1 || MB > 4) throw std::runtime_error("r4d gemm_w5a16_nt_m64: MB must be 1..4");
|
|
if (NPW != 1 && NPW != 4) throw std::runtime_error("r4d gemm_w5a16_nt_m64: NPW must be 1 or 4");
|
|
const int ntiles = N / 16, mtiles = (M + 15) / 16;
|
|
const int ncols = WV * NPW;
|
|
dim3 grid((ntiles + ncols - 1) / ncols, (mtiles + MB - 1) / MB), block(WV * SK * WAVE32);
|
|
size_t smem = (size_t)ncols * SK * 256 * sizeof(float);
|
|
if (smem > 64 * 1024) throw std::runtime_error("r4d gemm_w5a16_nt_m64: WV*NPW*SK too large");
|
|
auto Ap = (const unsigned short*)a;
|
|
auto Wp = (const unsigned*)wq;
|
|
auto Hp = (const unsigned*)whi;
|
|
auto Sp = (const unsigned*)wsz;
|
|
auto Cp = (__hip_bfloat16*)c;
|
|
hipStream_t st = (hipStream_t)stream;
|
|
#define LAUNCH(MT, NP, TT) do { \
|
|
if (hi5) hipLaunchKernelGGL((halo_gemm_w5a16_nt_m64_kernel<MT, NP, TT, true>), grid, block, smem, \
|
|
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); \
|
|
else hipLaunchKernelGGL((halo_gemm_w5a16_nt_m64_kernel<MT, NP, TT, false>), grid, block, smem, \
|
|
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); } while (0)
|
|
#define LAUNCH_MB(NP, TT) switch (MB) { \
|
|
case 1: LAUNCH(1, NP, TT); break; \
|
|
case 2: LAUNCH(2, NP, TT); break; \
|
|
case 3: LAUNCH(3, NP, TT); break; \
|
|
case 4: LAUNCH(4, NP, TT); break; \
|
|
default: throw std::runtime_error("r4d gemm_w5a16_nt_m64: MB unsupported"); }
|
|
if (NPW == 1) {
|
|
if (NT) { LAUNCH_MB(1, true); } else { LAUNCH_MB(1, false); }
|
|
} else {
|
|
if (NT) { LAUNCH_MB(4, true); } else { LAUNCH_MB(4, false); }
|
|
}
|
|
#undef LAUNCH_MB
|
|
#undef LAUNCH
|
|
}
|
|
|
|
int halo_gemm_w5a16_nt_m64_max_m(void) { return HALO_GEMM_W4_MAX_M; }
|
|
int halo_gemm_w5a16_nt_m64_group(void) { return HALO_GEMM_W4_GROUP; }
|
|
|
|
// ============================================================== the plugin ABI entry points
|
|
//
|
|
// The operand reader and the split law are in halo_gemm_args.h; see libhalo/halo_args.h.
|
|
/* ------------------------------------------------------------------ f16-activation grids */
|
|
|
|
static int w5_dispatch(const RadArgs* a, RadStream s, bool hi5) {
|
|
GemmOps g;
|
|
int rc = gemm_ops(a, &g, 1, /*parts=*/1);
|
|
if (rc != RAD_OK) return rc;
|
|
if (g.N % 16) return HALO_NO(RAD_E_SHAPE);
|
|
/* Any other activation dtype would read plausible but wrong numbers. */
|
|
if (g.a->dtype != RAD_F16) return HALO_NO(RAD_E_DTYPE);
|
|
MxParts mp;
|
|
rc = mx_parts(a, &g, &mp);
|
|
if (rc != RAD_OK) return rc;
|
|
if (g.K % HALO_GEMM_W4_GROUP) return HALO_NO(RAD_E_SHAPE);
|
|
const int group = halo_gemm_w5a16_nt_m64_group();
|
|
// Rows past 64 run as chunks of 64, each a pass over the weight.
|
|
const long long mc = std::min<long long>(g.M, halo_gemm_w5a16_nt_m64_max_m());
|
|
Cfg c;
|
|
rc = cfg_for(a, g.K, mc, group, BR_FLAT, &c);
|
|
if (rc != RAD_OK) return rc;
|
|
/* cfg_for pins MB = 1 in the decode band, but here the dequantise is the cost: with one block per
|
|
* row tile, each weight fragment is unpacked once per tile. Cover every row tile in one block
|
|
* unless the tuner pinned MB; the split is unchanged, so the output bits are too. */
|
|
HaloGemmCfg tuned;
|
|
halo_cfg_gemm(a, &tuned);
|
|
if (tuned.mb <= 0) c.mb = std::min<long long>(4, (mc + 15) / 16);
|
|
const long long Ntot = g.N, K = g.K, lda = mp.lda ? mp.lda : g.K;
|
|
const long long nsz = K / HALO_GEMM_W4_GROUP;
|
|
for (int p = 0; p < mp.n; ++p) {
|
|
const long long n0 = mp.n0[p], np = mp.n1[p] - mp.n0[p];
|
|
for (long long m0 = 0; m0 < g.M; m0 += mc) {
|
|
const long long mm = std::min<long long>(mc, g.M - m0);
|
|
HALO_TRY("gemm_w5a16_nt_m64",
|
|
halo_gemm_w5a16_nt_m64(
|
|
halo_addr(g.a) + (m0 * lda + p * K) * 2,
|
|
halo_addr(g.b) + n0 * K / 2, /* the nibble plane */
|
|
hi5 ? halo_addr(g.b) + Ntot * K / 2 + n0 * K / 8 : 0L, /* the fifth-bit plane */
|
|
halo_addr(g.bscale) + (n0 / 16) * nsz * 16 * 4,
|
|
halo_addr(g.y) + (m0 * Ntot + n0) * 2,
|
|
(int)mm, (int)K, (int)np, c.wv, c.sk, c.mb, c.npw, c.nt, (long)(uintptr_t)s,
|
|
(int)lda, (int)Ntot, hi5));
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
extern "C" int halo_rad_gemm_w5a16_m64(const RadArgs* a, RadStream s) { return w5_dispatch(a, s, true); }
|
|
/* ...and the plain 4-bit asymmetric grid (z-lab's int4), whose blob is the nibble plane alone. */
|
|
extern "C" int halo_rad_gemm_w4a16asym_m64(const RadArgs* a, RadStream s) { return w5_dispatch(a, s, false); }
|