Archived
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
253 lines
14 KiB
C++
253 lines
14 KiB
C++
/* checkpoint.h -- a safetensors or GGUF checkpoint as a source of logical weights.
|
|
*
|
|
* Read by rad-convert (to write a container) and by the loader (to serve a checkpoint with
|
|
* trivial encodings directly, spec §4.3). Both need:
|
|
*
|
|
* - the tensors, as one row-major namespace across shards and drafters;
|
|
* - the metadata, from config.json and its siblings or the GGUF header;
|
|
* - the logical weight for a declared name: the tensors the plugin's name map resolves it to
|
|
* (concatenations, alternatives, expert slices), with their encoding when trivial, and its rows
|
|
* on demand.
|
|
*
|
|
* Trivial (spec §4.3): plain f32, f16 and bf16 (GGUF's too); FP8 with a block or per-row scale
|
|
* plane (`X.weight_scale_inv`, or `X.weight_scale`); and MXFP4 as AMD Quark ships it (`X.weight`
|
|
* U8 codes, `X.weight_scale` U8 exponents), byte for byte `fp4_e2m1*e8m0[1x32]`. Anything else
|
|
* needs a recipe ahead of time, or for a published release in a form listed under
|
|
* Checkpoint::Import an import: the shipped codes and scales re-laid, or decoded for a recipe, at
|
|
* convert time.
|
|
*
|
|
* An imported quantisation's descriptor (`auto-round`, `gptq`, `quark`, `compressed-tensors`,
|
|
* `paroquant`) names the tool, not a served form, so no plugin claims it; Checkpoint::imports()
|
|
* describes how the plugin is chosen.
|
|
*/
|
|
#pragma once
|
|
#include "../rad_internal.h"
|
|
#include "../rad_core.h"
|
|
#include "safetensors.h"
|
|
#include "../text/gguf.h"
|
|
|
|
#include <deque>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <unordered_set>
|
|
#include <vector>
|
|
|
|
namespace rad {
|
|
|
|
class Program;
|
|
|
|
/* ================================================================== tensors */
|
|
/* Shapes are row-major, shape[0] slowest. GGUF stores ne[] fastest-first, so Checkpoint::open
|
|
* reverses it, once; getting it wrong transposes every matrix. */
|
|
struct SrcTensor {
|
|
std::string name;
|
|
uint32_t ggml = 0;
|
|
uint32_t rad_dtype = RAD_DT_INVALID; /* INVALID: a GGUF block type */
|
|
std::vector<int64_t> shape; /* row-major */
|
|
const void* data = nullptr;
|
|
int64_t bytes = 0;
|
|
/* GPTQ-packed (auto_gptq's layout, shared by AutoRound's `auto_round:auto_gptq` export):
|
|
* `data` is the qweight's int32 words [K * bits / 32, N], each packing 32 / bits codes along K
|
|
* from the low bits up, unsigned with zero point 2^(bits-1). The tensor described is the
|
|
* logical [N, K] code matrix, `rad_dtype` RAD_I4 or RAD_I8; `gptq_group` is the K columns one
|
|
* scale row covers. 0 for other tensors. */
|
|
int32_t gptq_bits = 0;
|
|
int64_t gptq_group = 0;
|
|
/* CT-INT4 (compressed-tensors' symmetric pack-quantized int4): `data` is X.weight_packed's
|
|
* int32 words [N, K / 8], eight codes per word along K from the low bits up, unsigned with zero
|
|
* point 8. The tensor described is the logical [N, K] code matrix, `rad_dtype` RAD_I4;
|
|
* `ct_group` is the K columns one scale of X.weight_scale [N, K / group] covers. 0 for other
|
|
* tensors. */
|
|
int64_t ct_group = 0;
|
|
/* ParoQuant's int5-bitplane (Import::ParoI5): `data` is the AWQ-packed low nibbles qweight
|
|
* [K, N/8] and the tensor described is the logical [N, K] u5 code matrix; `aux` are the fifth-bit
|
|
* plane qweight_hi [K, N/32], and the zero points qzeros [G, N/8] and qzeros_hi [G, N/32].
|
|
* z-lab's int4 (Import::ParoI4) is the same without the fifth-bit planes (aux[0], aux[2] null). */
|
|
int pbits = 0; /* 5 for int5-bitplane, 4 for z-lab's plain AWQ-packed int4; 0 otherwise */
|
|
const SrcTensor* aux[3] = { nullptr, nullptr, nullptr };
|
|
|
|
int64_t numel() const {
|
|
int64_t n = 1;
|
|
for (int64_t d : shape) n *= d;
|
|
return shape.empty() ? 0 : n;
|
|
}
|
|
};
|
|
|
|
/* ================================================================== metadata */
|
|
/* The model's metadata as RadModelMeta, with the strings it points at owned here. */
|
|
struct MetaOwn {
|
|
std::string arch, name, quant;
|
|
std::vector<std::string> keys, vals;
|
|
std::vector<const char*> kp, vp;
|
|
RadModelMeta m{};
|
|
|
|
void kv(const std::string& k, const std::string& v) { keys.push_back(k); vals.push_back(v); }
|
|
/* Set a key that must win over an existing one: lookups take the first row with a key, so
|
|
* kv() would leave the old value in effect. */
|
|
void set(const std::string& k, const std::string& v);
|
|
bool has(const std::string& k) const;
|
|
const char* get(const std::string& k) const; /* null when absent */
|
|
/* Republish RadModelMeta's pointers. kv()/set() may move the strings, so call this after the
|
|
* last of them and before reading `m`. */
|
|
void finish();
|
|
};
|
|
|
|
int meta_from_config_json(const std::string& dir, MetaOwn* out);
|
|
/* quantization_config's quant_method and fmt as the plugin descriptor ("fp8_e4m3"), "" without. */
|
|
std::string quant_descriptor(const MetaOwn& m);
|
|
int meta_merge_config_json(const std::string& dir, const std::string& prefix, MetaOwn* out);
|
|
int meta_merge_generation_config(const std::string& dir, MetaOwn* out);
|
|
int meta_merge_preprocessor_configs(const std::string& dir, MetaOwn* out);
|
|
int meta_from_gguf(GgufFile& g, MetaOwn* out);
|
|
|
|
/* ================================================================== the checkpoint */
|
|
class Checkpoint {
|
|
public:
|
|
Checkpoint() = default;
|
|
Checkpoint(const Checkpoint&) = delete;
|
|
Checkpoint& operator=(const Checkpoint&) = delete;
|
|
|
|
/* A .gguf, a .safetensors, a model.safetensors.index.json, or the directory holding one,
|
|
* plus the metadata beside it. */
|
|
int open(const std::string& path);
|
|
/* Merge a second checkpoint under `prefix`: a drafter shipped as its own repository (DFlash2)
|
|
* borrows the target's embedding and head, so it goes into the target's container with its
|
|
* tensors and config.json keys under `draft.`. */
|
|
int add_draft(const std::string& dir, const std::string& prefix);
|
|
|
|
const SrcTensor* find(const std::string& n) const;
|
|
const std::vector<SrcTensor>& tensors() const { return tensors_; }
|
|
bool is_gguf() const { return kind_ == Gguf; }
|
|
GgufFile& gguf() { return gguf_; }
|
|
const std::string& path() const { return path_; }
|
|
const std::string& dir() const { return dir_; } /* where config.json and tokenizer.json sit */
|
|
/* Where tokenizer.json, tokenizer_config.json and the chat template sit: dir() for a model
|
|
* repository, the `processor/` (or `tokenizer/`) folder of a diffusers pipeline. */
|
|
const std::string& tok_dir() const { return tok_dir_.empty() ? dir_ : tok_dir_; }
|
|
MetaOwn& meta() { return meta_; }
|
|
const MetaOwn& meta() const { return meta_; }
|
|
|
|
/* Sub-tensor `index` along dim 0 of a stacked (rank >= 2) tensor, as a view owned here and
|
|
* valid for the checkpoint's life. Null, with the reason logged, if there is no such slice. */
|
|
const SrcTensor* slice(const SrcTensor& st, int index, const char* declared);
|
|
|
|
/* FP8's block, from quantization_config.weight_block_size; 128x128 when it says nothing. */
|
|
int64_t fp8_block_rows() const { return fp8_br_; }
|
|
int64_t fp8_block_cols() const { return fp8_bc_; }
|
|
|
|
/* The checkpoint's quantisation imported weight by weight, or None; read from
|
|
* 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:
|
|
* compressed-tensors' pack-quantized int4, a release with an nvfp4-pack-quantized group, and
|
|
* one whose groups are all float-quantized FP8 (detect_import). In an Nvfp4 or CtFp8 release
|
|
* every weight without the NVFP4 tensors reads stock, so FP8 and bf16 linears beside the
|
|
* NVFP4 ones read by their own form. 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 };
|
|
Import imports() const { return import_; }
|
|
const std::string& import_refusal() const { return import_why_; }
|
|
|
|
/* The pieces an imported `name` (a `.weight`) is read from, as views owned here: GPTQ-packed
|
|
* codes and their scales, Quark's codes and exponents as RAD dtypes, or NVFP4's codes as fp4
|
|
* beside an f32 scale plane derived from the e4m3 scales and the global scale. RAD_E_NOTFOUND
|
|
* when `name` is an ordinary tensor or absent; another error, with `why`, for an unreadable
|
|
* imported form. */
|
|
int import_piece(const std::string& name, const SrcTensor** codes, const SrcTensor** scale,
|
|
std::string* why);
|
|
|
|
private:
|
|
/* `owner` is the index's weight_map, if any: a tensor it places in another shard is skipped
|
|
* here. */
|
|
int add_shard(const std::string& p, const std::string& prefix = "",
|
|
const std::unordered_map<std::string, std::string>* owner = nullptr);
|
|
int open_safetensors(const std::string& path);
|
|
int open_diffusers();
|
|
int open_gguf(const std::string& path);
|
|
void detect_import();
|
|
|
|
enum Kind { None, Gguf, SafeT } kind_ = None;
|
|
std::string path_, dir_, tok_dir_;
|
|
GgufFile gguf_;
|
|
MetaOwn meta_;
|
|
std::vector<std::unique_ptr<SafeTensorsFile>> shards_;
|
|
std::vector<SrcTensor> tensors_;
|
|
std::unordered_map<std::string, size_t> index_;
|
|
std::unordered_map<std::string, size_t> index_shard_; /* name -> shards_ position */
|
|
std::deque<SrcTensor> sliced_; /* views; a deque never moves what it holds */
|
|
/* Bytes a view's data points at when they are derived, not mapped (NVFP4's f32 scale
|
|
* plane); they live as long as the views. */
|
|
std::vector<std::unique_ptr<uint8_t[]>> owned_;
|
|
int64_t fp8_br_ = 128, fp8_bc_ = 128;
|
|
Import import_ = Import::None;
|
|
std::string import_why_;
|
|
int64_t ct_group_ = 0; /* CtInt4: the group every weight's config states */
|
|
/* 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_;
|
|
};
|
|
|
|
/* ================================================================== logical weights */
|
|
/* A declared weight's source, as the name map resolves it: pieces joined along `concat_dim`, and
|
|
* for block-scaled FP8 each piece's scale plane beside it. */
|
|
struct CkptWeight {
|
|
std::string name; /* the logical (declared) name */
|
|
std::vector<const SrcTensor*> pieces; /* the codes, in concatenation order */
|
|
std::vector<const SrcTensor*> scales; /* FP8 only: one a piece */
|
|
int concat_dim = 0;
|
|
uint32_t rank = 0;
|
|
int64_t shape[RAD_MAX_RANK] = {0}; /* the logical shape */
|
|
int64_t rows = 0, cols = 0; /* its [rows, cols] view (rad_enc_view) */
|
|
/* `enc` is the pieces' encoding when trivial; otherwise `why` says what is not. */
|
|
bool trivial = false;
|
|
/* Imported pieces, read into `enc` by ckpt_plane_rows and ckpt_rows_f32. GPTQ-packed: `i4` or
|
|
* `i8` codes transposed and offset to signed, f16 scales rounded to bf16 per 128 of K.
|
|
* NVFP4: fp4 codes beside the f32 scale e4m3 / global per 16 of K. Not trivial (the scales
|
|
* are changed or derived), so the loader refuses it; rad-convert writes it as `enc` or
|
|
* quantises it by a recipe rule. A non-trivial FP8 weight may also state `enc` when it
|
|
* still reads piece by piece (a row join ending inside a scale block); one that cannot be
|
|
* read leaves `enc` unstated, and ckpt_rows_f32 refuses it with `why`. */
|
|
bool imported = false;
|
|
/* 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). */
|
|
bool paro = false;
|
|
std::vector<const SrcTensor*> pq_pairs, pq_theta, pq_scale;
|
|
RadEncoding enc{};
|
|
std::string why;
|
|
};
|
|
|
|
/* Resolve `name` through `prog`'s name map. RAD_OK; RAD_E_NOTFOUND when the name map has no entry
|
|
* for it yet (declare may add one later) or, for an optional weight, none of its tensors is
|
|
* there; anything else with the reason in `why`. */
|
|
int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, bool optional,
|
|
CkptWeight* out, std::string* why);
|
|
|
|
/* The checkpoint tensor a piece or scale reads: its name without the "[index]" a slice of a
|
|
* stacked tensor carries, so an expert reads the stack it was cut from. */
|
|
std::string ckpt_tensor_of(const SrcTensor& t);
|
|
|
|
/* Whether `w` reads any tensor named in `names` (checkpoint names, as ckpt_tensor_of gives them),
|
|
* through a piece or a scale. Each name it reads is added to `hit` when that is given. */
|
|
bool ckpt_reads_any(const CkptWeight& w, const std::unordered_set<std::string>& names,
|
|
std::unordered_set<std::string>* hit = nullptr);
|
|
|
|
/* One (part, group) of a ParoQuant rotation as a dense matrix: `S` stages of 64 pairs, each with
|
|
* group-local columns `ia`/`ib` and angle `th`, stage-major; out[j * 128 + i] (f16) is output j for
|
|
* unit vector e_i. This is the "givens" encoding's "t.dense" table. */
|
|
void paro_compose_dense(int64_t S, const int32_t* ia, const int32_t* ib, const float* th, uint16_t* out);
|
|
|
|
/* Logical rows [r0, r0 + nr) of `w` as f32 [nr, cols], whatever the pieces are stored as: a
|
|
* plain float widened, FP8 times its block scale, a GGUF block type dequantised. */
|
|
int ckpt_rows_f32(const CkptWeight& w, int64_t r0, int64_t nr, float* out, std::string* why);
|
|
|
|
/* Rows [r0, r0 + nr) of plane `k` of a trivial or imported `w`, in the plane's own elements, dense
|
|
* into `dst`: the bytes a container entry holds. */
|
|
int ckpt_plane_rows(const CkptWeight& w, int k, int64_t r0, int64_t nr, void* dst,
|
|
std::string* why);
|
|
|
|
} /* namespace rad */
|