Archived
New models:
- Hunyuan3D-2 (hunyuan3d2): an untextured 3-D mesh from one picture of an object. DINOv2-giant
over the picture, a FLUX-shaped flow-matching DiT (16 double-stream and 32 single-stream blocks
of 1024) over 3072 latents with both guidance branches in one step, and the shape VAE's
geometry decoder. Converted from tencent/Hunyuan3D-2's hunyuan3d-dit-v2-0 folder with
data/recipes/hunyuan3d-2-bf16.recipe (fp16 cast to bf16, 4.4 GiB, one card). The licence
(Tencent Hunyuan 3D 2.0 Community License) does not cover the EU, the UK or South Korea
- Served on Hunyuan3D-2's own API server routes, so its Blender add-on and scripts work
unchanged: POST /generate answers with the mesh, POST /send runs it in the background and
GET /status/{uid} reports steps_done and then the file. GLB, OBJ, PLY or STL; the field is
sampled coarse to fine as FlashVDM's hierarchical decoding does, or densely with
"volume_decoding": "dense"; seed, steps, guidance and octree_resolution as the reference.
Texturing (Hunyuan3D-Paint) is refused for now (docs/GUIDE.md §5.17)
Merged:
- libr3d rewritten for RDNA3's wave matrix (@RikshaDriver): bf16, f16, int8-activation and vocab
GEMMs retiled to 16x128 blocks with a double-buffered k-walk; fp8, mxfp4a8, w2a8, w4a16, w5a16
and w8a8 quantised GEMMs and the gated form; grouped MoE GEMMs (bf16, fp8a8f32, mxfp4a8); the
norms and quantisers, the QSA state ops and the MoE router, scatter and gather. Brought onto the
op contract before release (13 schemas, the MoE GEMMs on weight tables with a_order and bf16
output, moe_gather's gate pitch, moe_scatter above 1024 slots, the mxfp4 m16 dispatch) and its
gated-delta-net kernels replaced by libhalo's; every op it serves now passes rad-kbench against
libref on a gfx1151, and r3d_selftest checks its MoE GEMMs by value
- Gemma 4 31B: asymmetric int5 linears (w5a8i8 above 64 rows, w5a16 up to 64, a scale per 128),
the norm, GELU and two-rank all-reduce junctions at quant_act_i8g's grid (rmsnorm_quant_i8g,
gelu_mul_quant_i8g, ar_rmsnorm_quant_i8g) so the prefill overlap runs on the i8a8, a4 and w5
trunks, and compose files for the 31B i8g, heretic-ara-i8g and w5 on two cards (@keptn42)
- The norms' lane sum reads eight at a time: TranslateGemma 27B decode is back from 225 to 239
tok/s a stream at four streams, bit for bit the same output (@mici81)
- The head-256 prefill softmax rescales each row by its own maximum, not by a vote of the wave, so
a row's rounding no longer depends on which rows share its launch (@gostar)
Anthropic Messages API:
- /v1/messages takes min_p, presence_penalty, frequency_penalty, repetition_penalty and
repeat_last_n as top-level fields with the chat path's names, ranges and meaning, so clients that
speak only the Anthropic format (Claude Code) can set a model card's sampling, such as Qwen's
presence_penalty 1.5. The penalties count the last repeat_last_n tokens of prompt and reply (64
by default, --repeat-last-n), as llama.cpp does
Plugin ABI 33:
- Shape generators: rad_declare_shape_generator, RadShapeGenBatch (RadBatch::sgen_b) and
RadArchProbe::gen_shape, with the SCOND, SSTEP, SLATENT and SQUERY passes. The layout
fingerprint table is regenerated; out-of-tree plugins rebuild against 33
- New op point_fourier ([x | sin(x 2^j) | cos(x 2^j)], times pi on request) in libref, libavx,
libr4d and libhalo
- A checkpoint for a model that reads no text gets a one-token vocabulary
More cards from the one image:
- stilldeadcode/radiance now carries libr3d (RDNA3: gfx11-generic, every gfx11 chip) and libhalo
(Strix Halo: gfx1151, preferred there over libr3d) beside libr4d (gfx1201). docker/build.sh and
the Dockerfile default to gfx1201;gfx11-generic;gfx1151
- libr3d loads only where the KFD topology lists a gfx11 card and libhalo only where it lists a
gfx1151; a process without /dev/kfd sees neither. On an RDNA4 card, or with no card at all,
the image therefore behaves exactly as before, and rad-convert lays weights out as before
- libhalo and libr3d now declare gemm_nt_q's w_rb, and libhalo gram_accum's upper, as libr4d
does. Before, any build carrying them refused both at load ("9 parameters vs 8"), so a build
for Strix Halo or RDNA3 had no device kernel library
- libhalo does not yet have every op libr4d added after it was ported (the IQR rows, Pixtral and
Ministral rope, the tree-attention and DFlash-tree rows, among others); halo_parity lists them
and is left out of the image's test stage until they are ported
Fixed -- container and checkpoint parsing:
- A .rad expert-1 entry inside or before expert 0 is refused; a vocab section whose count names
an array at offset 0 is refused rather than read from the file header; offset checks no longer
wrap; header tables must sit on their element alignment
- An imatrix array of arrays (unbounded recursion) and a safetensors header naming a tensor twice
are refused; a failed RadWriter undo says the container is damaged; a tune cache measured on
another machine is refused, as its format contract says
Fixed -- plugins:
- Two plugins declaring one op with different parameter roles are refused; before, the core
applied the first one's chunk/capacity roles to the other's rows
- Null tables behind nonzero counts, unknown roles, keyless parameters or constraints and more
operands than an issue carries are refused by name at load; on the host backend device rows are
dropped even when no fat binary was registered; rad-schemas checks the ABI layout fingerprint as
the engine does
Fixed -- device:
- Stream slots are recycled under a new generation: before, 1024 stream create/destroy cycles
(LoRA loads, lanes, uplinks) made every later rad_stream_create fail
- A stream whose drain fails keeps its queue and kernarg ring instead of freeing them under the
card; the kernarg ring no longer leaks when its access grant fails; a block over 1024 work-items
is refused
- rad_dev_free takes the allocation off the ledger the pin-cap check reads; the host backend
charges pages to the card the range was reserved for
Fixed -- engine and server:
- Two NDEBUG-only asserts (runtime-window totals, ring blocks per group) are refusals; a
plugin-declared lcm past int64 saturates so the quantum check refuses it; every share view rect
and part span is bounded by its entry
- Video: the timestep regions hold the refine rungs a schedule appends, a zero-frame encode is
refused, /v1/videos bounds each side before the area product, and a realtime session needs a
clip length to cut a turn at
Fixed -- kernels and quantisation:
- A trellis run shorter than a state's codes (tcq_rows=1, or N % tcq_rows == 1) read its seam
out of bounds; the encoder now refuses it as the decoder does, and tcq_rows below one state is
refused at parse
- qsa_tail_store bounds its slot index by the state plane (libr4d and libhalo); paged attention
refuses kv_heads <= 0; libavx row_amax skips a NaN as libref does
Fixed -- tools:
- pad_tp.py no longer runs cp through a shell with shard names from the checkpoint's index
(quote injection, path traversal): names must be bare file names
- ops_doc_check.sh runs under mawk (Ubuntu's awk); rad-kbench --max-cases/--max-bytes are checked
integers; oot_plugins fails unless the installed replay passed a case and failed none
Thanks to @RikshaDriver, @keptn42, @mici81 and @gostar.
360 lines
18 KiB
C++
360 lines
18 KiB
C++
/* tokenizer.h -- the tokeniser, driven by the step chain baked into the .rad.
|
|
*
|
|
* spec §12. llama.cpp identifies a pre-tokeniser at runtime by hashing tokenizer.json against a
|
|
* hardcoded enum, so an unrecognised model silently gets the default split: plausible text with
|
|
* wrong token boundaries. Here the chain is declared instead. rad-convert reads tokenizer.json's
|
|
* normalizer / pre_tokenizer / decoder sections and emits them as RadVocabStep rows, which this
|
|
* file executes in order. A chain that cannot be expressed fails at convert time with the step
|
|
* named.
|
|
*
|
|
* A `Split` step's regex is compiled at load (tokenizer_regex.h) into a DFA that reproduces what
|
|
* HuggingFace tokenizers' Oniguruma reports, over Oniguruma's own Unicode tables. A pattern
|
|
* outside that dialect does not load; the refusal names the construct and there is no fallback
|
|
* matcher.
|
|
*
|
|
* Lifted from llama.cpp (06938ac12, MIT -- see vendor/UPSTREAM):
|
|
* - the byte-level alphabet and the unicode category tables the non-regex steps read
|
|
* (vendor/llama/src/unicode*)
|
|
* - the shape of the BPE bigram queue and the UGM Viterbi (src/llama-vocab.cpp)
|
|
* The latter is re-expressed rather than vendored: llama-vocab.cpp depends on
|
|
* llama_model_loader, GGML_ASSERT and llama_vocab, and spec §12 puts a heavily modified file in
|
|
* core/ under our own name.
|
|
*/
|
|
#pragma once
|
|
#include "rad_internal.h"
|
|
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
#include <unordered_set>
|
|
#include <vector>
|
|
|
|
namespace rad {
|
|
|
|
class GgufFile;
|
|
struct TokEncoder;
|
|
|
|
/* ================================================================== the mutable vocab */
|
|
|
|
/* One row of the declared chain, in the form rad-convert builds and the .rad bakes.
|
|
*
|
|
* `kind` is a RAD_NORM_* / RAD_PRE_* / RAD_DEC_* value from rad_format.h; its numeric range says
|
|
* which of the three chains the row belongs to, so all three are one list in declared order.
|
|
* The per-kind meaning of flags/args is documented at `step_describe()` in tokenizer.cpp, the
|
|
* contract rad-convert writes against. */
|
|
struct VocabStep {
|
|
uint32_t kind = 0;
|
|
uint32_t flags = 0;
|
|
std::string arg0, arg1;
|
|
int64_t iarg = 0;
|
|
};
|
|
|
|
/* RAD_PRE_WHITESPACE's flags: which of HuggingFace's whitespace pre-tokenisers the step is.
|
|
* Whitespace is 0, so a container written before the other two had flags reads as what it ran. */
|
|
enum : uint32_t {
|
|
kPreWhitespace = 0, /* the matches of \w+|[^\w\s]+, the rest removed */
|
|
kPreWhitespaceSplit = 1, /* split on whitespace, removed */
|
|
kPreBert = 2 /* WhitespaceSplit, then every punctuation character isolated */
|
|
};
|
|
|
|
/* RAD_DEC_REPLACE's flags: 0 is a plain Replace of arg0 by arg1 in every token. The others are the
|
|
* HuggingFace decoders converted to it, which also read the token's place in the list. */
|
|
enum : uint32_t {
|
|
kDecWordPiece = 1, /* arg0 is the continuation prefix; other words join with a space */
|
|
kDecWordPieceCleanup = 2, /* with kDecWordPiece: WordPiece's `cleanup` of spaced punctuation */
|
|
kDecBpeSuffix = 4 /* arg0 is the end-of-word suffix: a space, none after the last */
|
|
};
|
|
|
|
/* Kind ranges, matching the enum blocks in rad_format.h. */
|
|
inline bool step_is_norm(uint32_t k) { return k >= 1 && k < 32; }
|
|
inline bool step_is_pre (uint32_t k) { return k >= 32 && k < 64; }
|
|
inline bool step_is_dec (uint32_t k) { return k >= 64 && k < 96; }
|
|
|
|
/* Everything the tokeniser needs, in a form that can be built, inspected, written and read.
|
|
*
|
|
* The engine copies this out of the mmapped .rad instead of binding to it, so rad-convert and the
|
|
* server share one construction path and run byte-for-byte the same tokeniser. The copy costs a
|
|
* few megabytes and a few tens of milliseconds at load. */
|
|
struct VocabBuild {
|
|
uint32_t kind = RAD_TOK_BPE;
|
|
bool ignore_merges = false; /* BPE: a whole pre-token in the vocab is not merged */
|
|
|
|
std::vector<std::string> tokens; /* id -> text, in the vocab's own encoding */
|
|
std::vector<float> scores; /* unigram only; empty otherwise */
|
|
std::vector<uint8_t> types; /* RAD_TT_*; empty means all RAD_TT_NORMAL */
|
|
std::vector<std::pair<uint32_t, uint32_t>> merges; /* BPE, in rank order */
|
|
|
|
std::vector<VocabStep> steps; /* the whole declared chain, in order */
|
|
|
|
int32_t bos = -1, eos = -1, eot = -1, pad = -1, unk = -1, sep = -1;
|
|
bool add_bos = false, add_eos = false;
|
|
|
|
std::string chat_template;
|
|
|
|
/* Diagnostics only: what rad-convert thought it was reading. Not baked. */
|
|
std::string source;
|
|
|
|
/* generation_config.json's eos_token_id ids, carried as container metadata
|
|
* (`generation.eos_token_id`) rather than in the vocab section. The engine fills them before
|
|
* load(); not baked. */
|
|
std::vector<int32_t> eos_ids;
|
|
};
|
|
|
|
/* ================================================================== the loaded vocab */
|
|
|
|
class Vocab {
|
|
public:
|
|
int load(VocabBuild b); /* takes ownership; RAD_OK or negative */
|
|
bool ready() const { return ready_; }
|
|
|
|
uint32_t kind() const { return b_.kind; }
|
|
int32_t n_tokens() const { return (int32_t)b_.tokens.size(); }
|
|
|
|
const std::string& text(int32_t id) const;
|
|
uint8_t type(int32_t id) const;
|
|
float score(int32_t id) const;
|
|
bool is_eog(int32_t id) const; /* eos, eot, or a declared extra */
|
|
/* All end-of-generation ids. The server needs their spellings: a chat format can fold the
|
|
* turn terminator into its last marker, so the decoder renders it for the reply parser, and
|
|
* whatever the parser does not consume is stripped from the content. */
|
|
const std::vector<int32_t>& eog() const { return eog_; }
|
|
|
|
/* -1 when the exact text is not a token. */
|
|
int32_t find(std::string_view text) const;
|
|
/* BPE merge rank of the pair, or -1. */
|
|
int32_t merge_rank(std::string_view left, std::string_view right) const;
|
|
|
|
int32_t bos() const { return b_.bos; }
|
|
int32_t eos() const { return b_.eos; }
|
|
int32_t eot() const { return b_.eot; }
|
|
int32_t unk() const { return b_.unk; }
|
|
int32_t pad() const { return b_.pad; }
|
|
bool add_bos() const { return b_.add_bos; }
|
|
bool add_eos() const { return b_.add_eos; }
|
|
const std::string& chat_template() const { return b_.chat_template; }
|
|
|
|
const std::vector<VocabStep>& steps() const { return b_.steps; }
|
|
const std::vector<int32_t>& special_ids() const { return special_; }
|
|
const VocabBuild& build() const { return b_; }
|
|
|
|
/* The byte a RAD_TT_BYTE token stands for, or -1. Both spellings are recognised: SPM's
|
|
* "<0xF0>" and byte-level BPE's single mapped codepoint. Under a ByteFallback decoder a NORMAL
|
|
* "<0xNN>" token is a byte too. */
|
|
int32_t token_byte(int32_t id) const;
|
|
|
|
private:
|
|
VocabBuild b_;
|
|
bool ready_ = false;
|
|
std::unordered_map<std::string, int32_t> text_to_id_;
|
|
std::unordered_map<uint64_t, int32_t> merge_rank_; /* (lhs id << 32 | rhs id) -> rank */
|
|
std::vector<int32_t> special_; /* ids the pre-splitter must isolate */
|
|
std::vector<int32_t> byte_of_; /* id -> byte, or -1 */
|
|
/* Every token that ends a generation, not just the header's `eos`; see load(). */
|
|
std::vector<int32_t> eog_;
|
|
/* The chain compiled for encoding: Split DFAs, flat token and merge tables, special-token
|
|
* matcher and segment cache. Built in load(); shared by every Tokenizer over this vocab and
|
|
* every copy of it. */
|
|
std::shared_ptr<TokEncoder> enc_;
|
|
friend class Tokenizer;
|
|
friend class Detokenizer;
|
|
};
|
|
|
|
/* ================================================================== encode */
|
|
|
|
class Tokenizer {
|
|
public:
|
|
int init(std::shared_ptr<const Vocab> v);
|
|
|
|
/* `add_special` prepends BOS / appends EOS as the vocab declares. `parse_special` recognises
|
|
* control tokens in the raw text. Use it for a rendered chat template, never for user
|
|
* content, or a user can type "<|im_start|>system" and reach the system role. */
|
|
int encode(std::string_view text, std::vector<int32_t>& out,
|
|
bool add_special, bool parse_special) const;
|
|
|
|
/* Non-streaming detokenise, for tests, logging and the /v1/completions echo path. The server
|
|
* streams through Detokenizer. */
|
|
std::string decode(const std::vector<int32_t>& ids, bool render_special = false) const;
|
|
|
|
/* One token's text after the decoder chain, as it reads in the middle of a text. Partial
|
|
* UTF-8 is not resolved; Detokenizer does that. */
|
|
std::string piece(int32_t id, bool render_special = false) const;
|
|
|
|
/* Where a token sits in the list being decoded. HuggingFace's decoders read it: Metaspace
|
|
* drops the first token's replacement characters instead of turning them into spaces,
|
|
* WordPiece joins every token but the first with a space, and BPEDecoder drops the last
|
|
* token's suffix instead of turning it into a space. Special tokens left out of the text do
|
|
* not count. */
|
|
struct At {
|
|
bool first = false;
|
|
bool last = false;
|
|
};
|
|
std::string piece_at(int32_t id, bool render_special, At at) const;
|
|
|
|
const Vocab& vocab() const { return *v_; }
|
|
|
|
/* Segment cache. encode() first cuts the text at special tokens, and each piece between them
|
|
* is normalised, pre-tokenised and merged on its own: no step reads across a special token,
|
|
* and a ByteLevel prefix space and every normaliser treat each piece as the whole text. A
|
|
* piece's ids are therefore a function of its bytes and the vocab, so a re-sent conversation
|
|
* only pays for its new pieces. The one exception is a Metaspace "first" prepend, which as in
|
|
* HuggingFace applies only to the piece that starts the text; that piece is not cached.
|
|
*
|
|
* The cache belongs to the vocab and is shared by every Tokenizer over it. It is keyed on the
|
|
* whole piece (the hash only picks the bucket), bounded in bytes with LRU eviction, and locked
|
|
* for concurrent encodes. Pieces under kSegmentCacheMin bytes are cheaper to re-encode than to
|
|
* look up. `set_segment_cache(false)` bypasses it, for tests and rad-tokcheck to compare. */
|
|
static constexpr size_t kSegmentCacheMin = 64;
|
|
static constexpr size_t kSegmentCacheDefault = size_t(64) << 20;
|
|
void set_segment_cache(bool on) { use_cache_ = on; }
|
|
struct CacheStats {
|
|
uint64_t hits = 0, misses = 0;
|
|
size_t entries = 0, bytes = 0, limit = 0;
|
|
};
|
|
CacheStats cache_stats() const;
|
|
void set_cache_limit(size_t bytes) const;
|
|
void clear_cache() const;
|
|
|
|
private:
|
|
std::shared_ptr<const Vocab> v_;
|
|
bool use_cache_ = true;
|
|
};
|
|
|
|
/* ================================================================== streaming decode */
|
|
|
|
/* Replace every byte that is not part of a well-formed UTF-8 sequence with U+FFFD; well-formed
|
|
* input is returned unchanged. A byte-fallback vocab can emit a byte no continuation completes
|
|
* (`<0xC1>` only starts overlong, never-legal encodings), and nlohmann::json::dump throws
|
|
* type_error.316 on it, failing the whole response. Such bytes cannot be held back, and dropping
|
|
* them would hide that the model produced something undecodable. */
|
|
std::string utf8_sanitize(std::string_view s);
|
|
/* Length of the well-formed UTF-8 prefix of `s`, by the same rules: how much of a byte-level
|
|
* token that ends inside a character a JSON string can carry. */
|
|
size_t utf8_valid_prefix(std::string_view s);
|
|
|
|
|
|
/* Streaming detokeniser (spec §12). Byte-level BPE emits partial UTF-8 (one token can be one byte
|
|
* of a three-byte codepoint), and a stop string can straddle a chunk boundary; both are buffered
|
|
* until they resolve, so the stream never shows a stray U+FFFD or leaks "</to" of "</tool_call>".
|
|
*
|
|
* Contract: push() returns final text. It is never retracted, always well-formed UTF-8, and never
|
|
* a prefix of a stop string. Ambiguous text stays inside until a later push resolves it or
|
|
* flush() gives up on it. A sequence that is malformed rather than unfinished leaves as U+FFFD
|
|
* (utf8_sanitize), since raw invalid bytes cannot go into a JSON string. */
|
|
class Detokenizer {
|
|
public:
|
|
Detokenizer() = default;
|
|
/* `preserved`: control tokens rendered as text anyway. Some tool-call formats are built from
|
|
* control tokens (MiniCPM5: `<function`, `<param`, `</param>`, `</function>`); suppressing
|
|
* them would strip the call's structure and hand the reply parser prose. The chat template
|
|
* names them (common_chat_params::preserved_tokens); nothing else is unsuppressed. */
|
|
Detokenizer(std::shared_ptr<const Vocab> v, std::vector<std::string> stops = {},
|
|
bool render_special = false, const std::vector<std::string>& preserved = {});
|
|
|
|
std::string push(int32_t token);
|
|
std::string flush();
|
|
|
|
/* True once a stop string has been seen. The text before it has already been returned; the
|
|
* stop string itself and anything after it never is. */
|
|
bool stopped() const { return stopped_; }
|
|
const std::string& stop_hit() const { return stop_hit_; }
|
|
|
|
/* Stop strings apply to the answer, not the reasoning. For a generation that starts inside its
|
|
* reasoning block, stops are matched only in the text after `marker` (the block's closer).
|
|
* Otherwise a stop string written while thinking (e.g. a few-shot "Q:") ends the request with
|
|
* no answer. "" matches from the first token. */
|
|
void defer_stops_until(std::string marker) {
|
|
arm_after_ = std::move(marker);
|
|
armed_ = arm_after_.empty();
|
|
arm_scanned_ = 0;
|
|
}
|
|
|
|
/* Everything pushed so far, stop string included; the scheduler uses it to tell a stop from
|
|
* a length limit. */
|
|
const std::string& text() const { return all_; }
|
|
|
|
private:
|
|
std::shared_ptr<const Vocab> v_;
|
|
std::vector<std::string> stops_;
|
|
bool render_special_ = false;
|
|
std::unordered_set<int32_t> preserved_; /* control ids this stream renders anyway */
|
|
std::string pending_; /* decoded but not yet safe to emit */
|
|
std::string all_; /* everything, for the caller's record */
|
|
bool stopped_ = false;
|
|
std::string stop_hit_;
|
|
size_t max_stop_ = 0;
|
|
bool first_ = true; /* no token has counted as the first yet */
|
|
/* A Strip after Fuse in the decoder chain strips the text as a whole: up to `strip_left_`
|
|
* copies of `strip_` at its start. */
|
|
std::string strip_;
|
|
int strip_left_ = 0;
|
|
/* BPEDecoder turns a suffix into a space except on the last token, which a stream cannot
|
|
* know, so a trailing space is held until the next token and dropped by flush(). */
|
|
bool hold_space_ = false;
|
|
std::string arm_after_; /* see defer_stops_until */
|
|
bool armed_ = true;
|
|
size_t arm_scanned_ = 0; /* how far into all_ the search for arm_after_ has reached */
|
|
Tokenizer tok_; /* over v_, set up by the first push */
|
|
bool tok_ready_ = false;
|
|
|
|
size_t safe_prefix() const; /* how much of pending_ can be released */
|
|
};
|
|
|
|
/* ================================================================== sources and sinks */
|
|
|
|
/* rad-convert owns the .rad's single string blob; the vocab section only interns into it.
|
|
* Passing the interner rather than a blob keeps the two writers from disagreeing about who owns
|
|
* offset 0. */
|
|
struct StringSink {
|
|
virtual ~StringSink() = default;
|
|
virtual rad_stroff intern(std::string_view s) = 0;
|
|
};
|
|
|
|
/* Build a VocabBuild from a HuggingFace tokenizer.json.
|
|
*
|
|
* Interprets the declared normalizer / pre_tokenizer / decoder chain (spec §12). Returns RAD_OK
|
|
* or a negative RAD_E_*, notably RAD_E_UNSUPPORTED, with the component named in the log, for a
|
|
* component that has no RadVocabStep form. Such a chain is refused, never approximated. */
|
|
int parse_tokenizer_json(const char* path, VocabBuild& out);
|
|
/* Same, from an in-memory buffer, for a tokenizer.json that arrived some other way. */
|
|
int parse_tokenizer_json_buf(std::string_view json, const char* origin, VocabBuild& out);
|
|
|
|
/* The vocabulary of a model that reads no text (container metadata `radiance.text` = none: a
|
|
* picture-to-shape generator): one control token, which no request reaches, since such a program
|
|
* serves no text endpoint. rad-convert bakes it and the engine builds it for a checkpoint. */
|
|
void vocab_textless(VocabBuild& out);
|
|
inline bool meta_textless(const char* v) { return v && std::string_view(v) == "none"; }
|
|
|
|
/* Read a standalone chat template file into out.chat_template, trailing newlines stripped.
|
|
* A release that ships no template (DeepSeek's does not) needs one from elsewhere; the caller
|
|
* passes the file. An unreadable or empty file is an error, never a silent skip. Returns
|
|
* RAD_OK, or RAD_E_IO when the file cannot be read. */
|
|
int read_chat_template_path(const char* path, VocabBuild& out);
|
|
|
|
/* Build a VocabBuild from a GGUF's tokenizer.ggml.* metadata.
|
|
*
|
|
* GGUF has no declared chain, only `tokenizer.ggml.pre`, a name llama.cpp derived by hashing the
|
|
* original tokenizer.json. The chain is reconstructed from a table of known names; an unknown
|
|
* name returns RAD_E_UNSUPPORTED instead of a default split. Converting from tokenizer.json is
|
|
* the preferred path. */
|
|
int vocab_from_gguf(const GgufFile& g, VocabBuild& out);
|
|
|
|
/* Serialise into the .rad's vocab section. `section_file_off` is where rad-convert will place
|
|
* the section; every offset in the RadVocabHeader is absolute from the start of the file, per
|
|
* rad_format.h. */
|
|
int vocab_serialize(const VocabBuild& in, StringSink& strings, uint64_t section_file_off,
|
|
std::vector<uint8_t>& section_out);
|
|
/* Read it back. `file_base` is the mmapped .rad; `h` points inside it. */
|
|
int vocab_deserialize(const void* file_base, uint64_t file_bytes,
|
|
const RadVocabHeader* h, VocabBuild& out);
|
|
|
|
/* `text` through the normaliser steps of `steps` (the others are skipped), as the encoder runs
|
|
* them. RAD_E_UNSUPPORTED when a Replace pattern does not compile. */
|
|
int vocab_normalize(const std::vector<VocabStep>& steps, std::string* text);
|
|
|
|
/* Human-readable one-liner for a step, for --debug-graph and for the convert log. */
|
|
std::string step_describe(const VocabStep& s);
|
|
|
|
} /* namespace rad */
|