Archived
M2XFP4 (MXFP4v2): quantiser, gfx1201 WMMA kernels, host rows, df2 wiring, .rad packer (Import::ParoM2), stability fixes (counters-in-scratch x3, aql publish fences), kbench rows, runbook (REMAINING.md)
This commit is contained in:
1 parent
9eaf10da6e
commit
ea0908596b
60 files changed
+4619
-193
No files matched your search
+32
-3
@@ -598,6 +598,8 @@ struct Stream {
|
||||
bool agent_only = false; /* rad_xp_stream_create: never a system-scope acquire */
|
||||
std::atomic<int> gates{0}; /* closed gates (rad_stream_gate) on this queue */
|
||||
std::atomic<int> fault{0};
|
||||
int unordered_run = 0; /* played packets in a row without the barrier bit */
|
||||
bool unordered_warned = false;
|
||||
char* bounce = nullptr; /* pinned, for pageable copies */
|
||||
std::mutex bounce_mu;
|
||||
/* Chained launches (RadKernelChainFn): the completion counter every block of a chained kernel
|
||||
@@ -933,12 +935,18 @@ inline int reserve(Stream* s, uint64_t* out) {
|
||||
|
||||
inline void publish(Stream* s, uint64_t idx, uint16_t header, uint16_t setup) {
|
||||
s->wr = idx + 1;
|
||||
hsa_queue_store_write_index_relaxed(s->q, idx + 1);
|
||||
_mm_sfence(); /* the argument slot, written before the header */
|
||||
s->unordered_run = 0; /* an offered packet: every reclamation proof holds */
|
||||
_mm_sfence(); /* the packet body and the argument slot are write-combined
|
||||
VRAM: globally visible before the index below exposes
|
||||
the packet to the GPU (a torn kernarg read faults on the
|
||||
device with a stripped high pointer bit) */
|
||||
__atomic_store_n(reinterpret_cast<uint32_t*>(
|
||||
static_cast<hsa_kernel_dispatch_packet_t*>(s->q->base_address) +
|
||||
(idx & s->mask)),
|
||||
(uint32_t)header | ((uint32_t)setup << 16), __ATOMIC_RELEASE);
|
||||
/* The index is what exposes the packet to the GPU: only after the fence and the
|
||||
* header, or the packet processor can read a half-written argument block. */
|
||||
hsa_queue_store_write_index_relaxed(s->q, idx + 1);
|
||||
hsa_signal_store_relaxed(s->q->doorbell_signal, (hsa_signal_value_t)idx);
|
||||
}
|
||||
|
||||
@@ -1163,6 +1171,17 @@ struct PlayRun {
|
||||
uint64_t next = 0; /* the write index after the last packet written */
|
||||
int unrung = 0;
|
||||
bool foreign = true; /* the next packet's queue has packets this thread did not play */
|
||||
uint32_t seen[4] = {UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX};
|
||||
int n_seen = 0;
|
||||
/* True for the first dispatch this play writes on each queue, whatever its record says.
|
||||
* reserve()'s argument-slot proof needs an ordered packet after any the reuse may race:
|
||||
* a slot is reused once the read index passes the packet that held it, and only an
|
||||
* ordered packet after that one makes the index mean "it completed". */
|
||||
bool first_on(const Stream* q) {
|
||||
for (int i = 0; i < n_seen; ++i) if (seen[i] == q->id) return false;
|
||||
if (n_seen < 4) seen[n_seen++] = q->id;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ring() {
|
||||
if (!unrung) return;
|
||||
@@ -1235,12 +1254,22 @@ struct PlayRun {
|
||||
/* A dispatch carrying a record keeps the bit: its completion must cover everything before
|
||||
* it on the queue, which an unordered dispatch's does not. A chained kernel behind another chained one
|
||||
* on this queue waits in its prologue instead. */
|
||||
bool ordered = foreign || !r.overlap || done.handle;
|
||||
bool ordered = first_on(s) || foreign || !r.overlap || done.handle;
|
||||
/* Not while a plain packet may still run: a played plain kernel the overlap analysis let
|
||||
* start early (it touched nothing in flight) adds nothing to the counter, so a chained
|
||||
* kernel that reads its output and sits behind a chained one (a cache read beside a
|
||||
* collective, say) would wait for the chained blocks only. */
|
||||
if (chain && s->prev_chained && !s->plain_inflight && !foreign && !done.handle) ordered = false;
|
||||
/* The bound the slot proof rests on. Say so once if it is ever crossed, rather than tear
|
||||
* an argument block silently. */
|
||||
if (ordered) {
|
||||
s->unordered_run = 0;
|
||||
} else if (++s->unordered_run > kTapeOverlapRun && !s->unordered_warned) {
|
||||
s->unordered_warned = true;
|
||||
RAD_WARN("queue %u: %d played packets in a row carry no barrier bit, past the %d the "
|
||||
"argument-slot proof tolerates; a kernarg block may be reused while a kernel "
|
||||
"still reads it", s->id, s->unordered_run, kTapeOverlapRun);
|
||||
}
|
||||
const uint16_t h = header(HSA_PACKET_TYPE_KERNEL_DISPATCH, acq, rel, ordered);
|
||||
__atomic_store_n(reinterpret_cast<uint32_t*>(p), (uint32_t)h | ((uint32_t)r.pkt.setup << 16),
|
||||
__ATOMIC_RELEASE);
|
||||
|
||||
@@ -1111,6 +1111,17 @@ void Checkpoint::detect_import() {
|
||||
else
|
||||
import_why_ = "ParoQuant's MXFP4 here is not format mxfp4 with 32-element blocks and "
|
||||
"rotation groups of 128, which is the one this reads";
|
||||
} else if (qm == "paroquant_m2xfp4") {
|
||||
/* The same rotated planes on libquant's `m2xfp4` grid: e2m1 codes, an e8m0 exponent per 32
|
||||
* of K, and one metadata byte a group whose four 2-bit subgroup multipliers refine that
|
||||
* exponent to (1 + k/4) . 2^E. `mx_block` states the group the planes are cut in and
|
||||
* `subgroup` the columns one multiplier covers. */
|
||||
if (qc("format") == "m2xfp4" && qc("mx_block") == "32" && qc("subgroup") == "8" &&
|
||||
qc("group_size") == "128")
|
||||
import_ = Import::ParoM2;
|
||||
else
|
||||
import_why_ = "ParoQuant's M2XFP4 here is not format m2xfp4 with 32-element groups, "
|
||||
"8-element subgroups and rotation groups of 128, which is the one this reads";
|
||||
}
|
||||
/* Rename the tool's quantization_config: plugins read that key as the served format
|
||||
* (qwen35_fp8 checks quant_method is "fp8"), while an imported checkpoint states each weight's
|
||||
@@ -1175,10 +1186,13 @@ int Checkpoint::import_piece(const std::string& name, const SrcTensor** codes,
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
if (import_ == Import::QuarkMx || import_ == Import::ParoMx) {
|
||||
if (import_ == Import::QuarkMx || import_ == Import::ParoMx || import_ == Import::ParoM2) {
|
||||
/* Quark's MXFP4 is `fp4_e2m1*e8m0[1x32]` as stored: two codes per byte (element 2j in the
|
||||
* low nibble), one biased exponent per 32 of K. The view only renames dtypes and the
|
||||
* logical width. Excluded layers are plain tensors. */
|
||||
* logical width. Excluded layers are plain tensors. ParoQuant's M2XFP4 grid carries one
|
||||
* more plane (`weight_meta`, [N, K/32] u8) that is handed to the weight through
|
||||
* import_meta: its bytes are the encoding's third plane as they ship. */
|
||||
const bool m2 = import_ == Import::ParoM2;
|
||||
const SrcTensor* w = find(name);
|
||||
const SrcTensor* e = w ? find(name + "_scale") : nullptr;
|
||||
if (!w || !e || w->rad_dtype != RAD_U8) return RAD_E_NOTFOUND;
|
||||
@@ -1186,6 +1200,11 @@ int Checkpoint::import_piece(const std::string& name, const SrcTensor** codes,
|
||||
e->shape[0] != w->shape[0] || e->shape[1] * 32 != w->shape[1] * 2)
|
||||
return refuse(RAD_E_SHAPE, "'" + e->name + "' is not the [N, K/32] exponent plane of '" +
|
||||
w->name + "'");
|
||||
const SrcTensor* mt = m2 ? find(name + "_meta") : nullptr;
|
||||
if (m2 && (!mt || mt->rad_dtype != RAD_U8 || mt->shape != e->shape))
|
||||
return refuse(RAD_E_FORMAT, "'" + w->name + "' has no [N, K/32] u8 '" + name +
|
||||
"_meta' beside its exponent plane, which the checkpoint's "
|
||||
"m2xfp4 grid stores the subgroup multipliers in");
|
||||
SrcTensor c = *w;
|
||||
c.rad_dtype = RAD_FP4E2M1;
|
||||
c.shape = { w->shape[0], w->shape[1] * 2 };
|
||||
@@ -1196,6 +1215,7 @@ int Checkpoint::import_piece(const std::string& name, const SrcTensor** codes,
|
||||
sliced_.push_back(std::move(x));
|
||||
*scale = &sliced_.back();
|
||||
imported_[name] = { *codes, *scale };
|
||||
if (mt) imported_meta_[name] = mt;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -1472,6 +1492,11 @@ int Checkpoint::import_piece(const std::string& name, const SrcTensor** codes,
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
const SrcTensor* Checkpoint::import_meta(const std::string& name) const {
|
||||
auto it = imported_meta_.find(name);
|
||||
return it == imported_meta_.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
int Checkpoint::open(const std::string& path) {
|
||||
path_ = path;
|
||||
const bool gguf = path.size() > 5 && path.compare(path.size() - 5, 5, ".gguf") == 0;
|
||||
@@ -1594,7 +1619,8 @@ std::string ckpt_tensor_of(const SrcTensor& t) {
|
||||
bool ckpt_reads_any(const CkptWeight& w, const std::unordered_set<std::string>& names,
|
||||
std::unordered_set<std::string>* hit) {
|
||||
bool any = false;
|
||||
for (const auto* list : { &w.pieces, &w.scales, &w.pq_pairs, &w.pq_theta, &w.pq_scale })
|
||||
for (const auto* list : { &w.pieces, &w.scales, &w.pq_pairs, &w.pq_theta, &w.pq_scale,
|
||||
&w.pq_meta })
|
||||
for (const SrcTensor* t : *list) {
|
||||
if (!t) continue;
|
||||
const std::string n = ckpt_tensor_of(*t);
|
||||
@@ -2115,6 +2141,34 @@ int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, b
|
||||
rad_enc_add_plane(&out->enc, "codes", RAD_FP4E2M1, 1, 1);
|
||||
rad_enc_add_plane(&out->enc, "scale", RAD_F32, 1, 16);
|
||||
}
|
||||
} else if (dt == RAD_FP4E2M1 && scaled && ck.imports() == Checkpoint::Import::ParoM2) {
|
||||
/* ParoQuant's M2XFP4: the MXFP4 planes byte for byte -- e2m1 codes, an e8m0 exponent per
|
||||
* 32 of K -- plus the scheme's metadata byte a group (one per 32, four 2-bit subgroup
|
||||
* multipliers), in the rotated basis the checkpoint trained, with the rotation beside them
|
||||
* (paro_attach). Joined only along rows: every plane follows the pieces row by row. */
|
||||
bool fits = d == 0 || out->pieces.size() == 1;
|
||||
for (size_t k = 0; k < out->pieces.size() && fits; ++k) {
|
||||
const SrcTensor* mt = ck.import_meta(out->pieces[k]->name);
|
||||
fits = mt && out->scales[k] && out->scales[k]->rad_dtype == RAD_E8M0;
|
||||
if (fits) out->pq_meta.push_back(mt);
|
||||
}
|
||||
if (!fits) {
|
||||
out->pq_meta.clear();
|
||||
out->why = "its M2XFP4 pieces are joined along a column, or a piece has no metadata "
|
||||
"plane beside its exponents";
|
||||
} else {
|
||||
out->imported = true;
|
||||
out->trivial = false;
|
||||
rad_enc_clear(&out->enc);
|
||||
rad_enc_copy_str(out->enc.scheme, "m2xfp4");
|
||||
rad_enc_add_plane(&out->enc, "codes", RAD_FP4E2M1, 1, 1);
|
||||
rad_enc_add_plane(&out->enc, "scale", RAD_E8M0, 1, 32);
|
||||
rad_enc_add_plane(&out->enc, "meta", RAD_U8, 1, 32);
|
||||
const int rc = paro_attach(ck, out, why);
|
||||
if (rc != RAD_OK) return rc;
|
||||
out->why = "it is ParoQuant M2XFP4, which rad-convert imports with its rotation tables "
|
||||
"-- serve the container it writes";
|
||||
}
|
||||
} else if (dt == RAD_FP4E2M1 && scaled) {
|
||||
/* Quark's MXFP4 byte for byte: codes and exponents are the planes of
|
||||
* `fp4_e2m1*e8m0[1x32]`. Joined only along rows (gate and up). */
|
||||
@@ -2741,6 +2795,31 @@ int ckpt_plane_rows(const CkptWeight& w, int k, int64_t r0, int64_t nr, void* ds
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
if (w.paro && std::strncmp(w.enc.plane[k].role, "meta", RAD_ENC_STR) == 0) {
|
||||
/* The M2XFP4 grid's metadata byte a group, as stored: [N][K/32], one byte a group, the
|
||||
* subgroup multipliers packed in it (subgroup s in bits [2s, 2s+2)). The checkpoint packs
|
||||
* along N the way the plane does, so a row is a memcpy. */
|
||||
int64_t base = 0;
|
||||
for (size_t p = 0; p < w.pieces.size() && p < w.pq_meta.size(); ++p) {
|
||||
const SrcTensor& m = *w.pq_meta[p];
|
||||
const int64_t Np = w.pieces[p]->shape[0];
|
||||
/* The pieces view is the logical [N, K] codes; one metadata byte covers 32 of K. */
|
||||
const int64_t ng = w.pieces[p]->shape[1] / 32;
|
||||
if (m.shape.size() != 2 || m.shape[0] != Np || m.shape[1] != ng)
|
||||
return refuse(RAD_E_SHAPE, "'" + m.name + "' is not the [" + std::to_string(Np) +
|
||||
", " + std::to_string(ng) + "] metadata plane of '" +
|
||||
w.pieces[p]->name + "'; a stacked form it cannot be "
|
||||
"sliced out of is refused rather than packed");
|
||||
const int64_t lo = std::max(r0, base), hi = std::min(r0 + nr, base + Np);
|
||||
if (hi > lo) {
|
||||
std::memcpy(o, (const uint8_t*)m.data + (lo - base) * ng,
|
||||
(size_t)((hi - lo) * ng));
|
||||
o += (hi - lo) * ng;
|
||||
}
|
||||
base += Np;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
if (w.paro && std::strncmp(w.enc.plane[k].role, "t.", 2) == 0) {
|
||||
/* A rotation table, built a row at a time: pairs converted from group-local to whole-row
|
||||
* columns, angles widened, the channel scale inverted (the checkpoint multiplies the
|
||||
|
||||
@@ -156,7 +156,8 @@ public:
|
||||
* quantization_config at open. Gptq: AutoRound's `auto_round:auto_gptq` export and GPTQ v1
|
||||
* (zero point stored less one). QuarkMx: Quark's MXFP4 (fp4 weights, per_group 32, e8m0
|
||||
* scales, no per-layer overrides). ParoMx: ParoQuant's `paroquant_mxfp4` (Quark's codes and
|
||||
* exponents in a rotated basis, rotations beside them). CtInt4, Nvfp4, CtFp8:
|
||||
* exponents in a rotated basis, rotations beside them). ParoM2: ParoQuant's
|
||||
* `paroquant_m2xfp4` (those planes plus the scheme's metadata byte a group). CtInt4, Nvfp4, CtFp8:
|
||||
* compressed-tensors' pack-quantized int4 (symmetric, or asymmetric with a zero point), a
|
||||
* release with an nvfp4-pack-quantized group, and one whose groups are all float-quantized FP8
|
||||
* (detect_import). ModelOpt: NVIDIA ModelOpt's NVFP4, FP8, or both per layer
|
||||
@@ -165,7 +166,7 @@ public:
|
||||
* An unreadable form of these stays None, with the reason in import_refusal(). The
|
||||
* quantization_config keys are then renamed `imported_quantization_config.*`, since they
|
||||
* describe the tool, not a served format. */
|
||||
enum class Import { None, Gptq, QuarkMx, CtInt4, ParoMx, ParoI5, ParoI4, Nvfp4, CtFp8,
|
||||
enum class Import { None, Gptq, QuarkMx, CtInt4, ParoMx, ParoM2, ParoI5, ParoI4, Nvfp4, CtFp8,
|
||||
ModelOpt };
|
||||
Import imports() const { return import_; }
|
||||
const std::string& import_refusal() const { return import_why_; }
|
||||
@@ -178,6 +179,10 @@ public:
|
||||
int import_piece(const std::string& name, const SrcTensor** codes, const SrcTensor** scale,
|
||||
std::string* why);
|
||||
|
||||
/* The metadata plane an imported `name` carries, when its form has one (ParoM2): [N, K/32] u8,
|
||||
* one byte a group, as `weight_meta` ships it. Null for every other form. */
|
||||
const SrcTensor* import_meta(const std::string& name) const;
|
||||
|
||||
private:
|
||||
/* `owner` is the index's weight_map, if any: a tensor it places in another shard is skipped
|
||||
* here. */
|
||||
@@ -209,6 +214,8 @@ private:
|
||||
bool ct_asym_ = false; /* CtInt4: the config states asymmetric weights */
|
||||
/* import_piece's answers by name, so a weight resolved twice reuses its views. */
|
||||
std::unordered_map<std::string, std::pair<const SrcTensor*, const SrcTensor*>> imported_;
|
||||
/* The metadata plane of an imported form that has one (ParoM2), by name. */
|
||||
std::unordered_map<std::string, const SrcTensor*> imported_meta_;
|
||||
};
|
||||
|
||||
/* ================================================================== logical weights */
|
||||
@@ -250,9 +257,10 @@ struct CkptWeight {
|
||||
/* ParoQuant MXFP4 (Import::ParoMx), also `imported`: written plane by plane through
|
||||
* ckpt_plane_rows. Planes 0 and 1 are Quark's codes and exponents byte for byte; the rest are
|
||||
* the "givens" transform's tables, built from `pq_pairs`/`pq_theta`/`pq_scale`, one set per
|
||||
* piece (a row part: gate and up, or q, k and v, each rotated separately). */
|
||||
* piece (a row part: gate and up, or q, k and v, each rotated separately). Import::ParoM2 is
|
||||
* the same plus plane 3, the scheme's metadata byte a group, from `pq_meta`. */
|
||||
bool paro = false;
|
||||
std::vector<const SrcTensor*> pq_pairs, pq_theta, pq_scale;
|
||||
std::vector<const SrcTensor*> pq_pairs, pq_theta, pq_scale, pq_meta;
|
||||
/* A weight-normalised tensor (RAD_MAP_WNORM): `pieces` is its direction v alone, and every
|
||||
* element of v with first index i reads times wnorm_scale[i] = g_i / ||v_i||. Not trivial
|
||||
* (the values change), so a recipe rule stores it. */
|
||||
|
||||
@@ -434,6 +434,40 @@ int enc_selection_logical(const RadEncoding& e, const int* sel, int n_sel, const
|
||||
return RAD_OK;
|
||||
}
|
||||
}
|
||||
/* Codes with the metadata planes of a quantiser's own scheme, without the scale: the
|
||||
* selected planes' canonical bytes concatenated, as stored. Read by a kernel that decodes
|
||||
* the scheme itself beside the scale stream another operand carries (an m2xfp4 weight's
|
||||
* codes+meta view, whose E8M0 exponent plane is the b_scale operand); there is nothing to
|
||||
* decode here, because the scale is what the selection leaves out. */
|
||||
if (!rad_enc_is(&e, "plain") && !rad_enc_is(&e, "affine") && dt == RAD_U8) {
|
||||
int codes = -1;
|
||||
bool meta = true;
|
||||
for (int k = 0; k < n_sel && meta; ++k) {
|
||||
const RadEncPlane& p = e.plane[sel[k]];
|
||||
if (p.kind != RAD_PLANE_TILED || p.block[0] != 1 || p.block[1] < 1 ||
|
||||
rad_enc_role_in_chain(p.role, "scale"))
|
||||
meta = false;
|
||||
else if (!std::strcmp(p.role, "codes")) {
|
||||
if (codes >= 0 || p.block[1] != 1) meta = false; /* one code a value */
|
||||
else codes = k;
|
||||
}
|
||||
}
|
||||
if (meta && codes >= 0) {
|
||||
int64_t bytes = 0;
|
||||
for (int k = 0; k < n_sel; ++k)
|
||||
bytes += rows[k] * rad_enc_row_bytes(e.plane[sel[k]].dtype, cols[k]);
|
||||
if (bytes != n)
|
||||
return refuse("its planes hold " + std::to_string(bytes) +
|
||||
" bytes and the reader wants " + std::to_string(n));
|
||||
uint8_t* dst = (uint8_t*)out;
|
||||
for (int k = 0; k < n_sel; ++k) {
|
||||
const int64_t rb = rad_enc_row_bytes(e.plane[sel[k]].dtype, cols[k]);
|
||||
std::memcpy(dst, data[k], (size_t)(rows[k] * rb));
|
||||
dst += rows[k] * rb;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
}
|
||||
if (n_sel != e.n_planes)
|
||||
return refuse("it takes " + std::to_string(n_sel) + " of its encoding's " +
|
||||
std::to_string(e.n_planes) + " planes, which decode to nothing on their own");
|
||||
|
||||
Reference in new issue
Block a user