core/format, tools: GGUF Q8_0, Q4_K and Q6_K tensors import as their planes -- rad-convert writes them without a recipe

A GGUF tensor of block type Q8_0, Q4_K or Q6_K could only be dequantised, so rad-convert reached it
through a recipe rule and re-quantised what was already quantised. ckpt_resolve now states the
planes libquant's ggml quantiser writes for the same blocks and marks the weight imported, and
ckpt_plane_rows splits each row's blocks into them bit for bit. The split is
core/format/ggml_planes.h, the reader's copy of lq_ggml's unpack functions, since the core cannot
call libquant; a type there is one table row, its planes and its split. Pieces of one block type
joined along rows import, an expert's slices of a stacked tensor included; mixed types and column
joins keep a GGUF reason.

The weight is not trivial, so the loader refuses it with the hint to serve the container
rad-convert writes, and ckpt_rows_f32 still dequantises it for a recipe. A rule that declines such
a weight is an error in rad-convert, since keeping its planes would hide that the rule did not
apply (spec.md §4.4).

Tests: quant_ggml_test holds every type of the reader's table to the quantiser's encoding,
checkpoint_test decodes the planes of Q8_0, Q4_K (one-block rows and a row join) and Q6_K against
the block definition and holds an expert's gate|up, two slices of stacked Q4_K tensors, to the same
blocks stored on their own, and convert_test runs a declining rule over a Q8_0 tensor, with the
GGUF writer moved to tests/gguf_writer.h for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Opus 5.5 authored and StillDeadcode committed 2026-10-28 12:48:13 +01:00
1 parent 6f034d989f
commit 385318f81a
10 files changed
+602 -57

No files matched your search

+46
View File
@@ -1,5 +1,6 @@
/* checkpoint.cpp -- see checkpoint.h. */
#include "checkpoint.h"
#include "ggml_planes.h"
#include "rad_plugin.h"
#include <nlohmann/json.hpp>
@@ -2499,6 +2500,26 @@ int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, b
if (dt == RAD_DT_INVALID) {
out->why = std::string("'") + p0.name + "' is a GGUF " +
(gguf_dtype(p0.ggml) ? gguf_dtype(p0.ggml)->name : "block") + " tensor";
/* Q8_0, Q4_K and Q6_K become their planes when every piece is of that type and they join
* along rows, so each row of the weight is one source row of blocks; the loader still
* refuses them, since the blocks interleave what the planes hold apart. */
bool one_type = true;
for (const SrcTensor* pk : out->pieces) one_type = one_type && pk->ggml == p0.ggml;
RadEncoding enc;
if (!one_type) {
out->why = "its GGUF pieces are of different block types";
} else if (ggml_plane_encoding(p0.ggml, &enc)) {
if (out->pieces.size() > 1 && (d != 0 || out->rank < 2)) {
out->why = "its GGUF pieces are joined along a column, across their blocks";
} else {
out->imported = true;
out->ggml_blocks = true;
out->enc = enc;
out->why = std::string("it is a GGUF ") + gguf_dtype(p0.ggml)->name +
" tensor, which rad-convert imports as its planes -- serve the "
"container it writes";
}
}
} else if (!same) {
out->why = "its pieces are stored in different dtypes";
} else if (p0.pbits) {
@@ -3395,6 +3416,31 @@ int ckpt_plane_rows(const CkptWeight& w, int k, int64_t r0, int64_t nr, void* ds
}
return RAD_OK;
}
if (w.ggml_blocks) {
/* GGUF blocks, split one row at a time into every plane's row: a call pays the whole row
* for the one plane it keeps, and rad-convert asks plane by plane. */
int64_t at[RAD_ENC_MAX_PLANES + 1] = { 0 };
for (int i = 0; i < w.enc.n_planes; ++i) {
int64_t qr = 0, qc = 0;
rad_enc_plane_dims(&w.enc.plane[i], 1, w.cols, &qr, &qc);
at[i + 1] = at[i] + rad_enc_row_bytes(w.enc.plane[i].dtype, qc);
}
std::vector<uint8_t> split((size_t)at[w.enc.n_planes]);
uint8_t* row[RAD_ENC_MAX_PLANES] = { nullptr };
for (int i = 0; i < w.enc.n_planes; ++i) row[i] = split.data() + at[i];
int64_t base = 0;
for (const SrcTensor* piece : w.pieces) {
const int64_t prows = piece->numel() / w.cols, rb = piece->bytes / prows;
for (int64_t r = std::max(r0, base); r < std::min(r0 + nr, base + prows); ++r) {
ggml_plane_row(piece->ggml, (const uint8_t*)piece->data + (r - base) * rb, w.cols,
row);
std::memcpy(o, row[k], (size_t)(at[k + 1] - at[k]));
o += at[k + 1] - at[k];
}
base += prows;
}
return RAD_OK;
}
if (k == 0) {
/* Codes: the pieces joined, bytes as stored. Sub-byte codes (MXFP4) join along rows of even
* width, so every run is byte-aligned. */