checkpoint: nomic config key aliases, RAD_MAP_TRANSPOSE honouring src_index

meta_from_config_json reads the nomic spellings (n_layer, n_embd, n_head, n_inner, n_positions, moe_top_k, layer_norm_epsilon, rotary_emb_base) as fallbacks in the same chain as the usual keys; existing checkpoints parse as before. RAD_MAP_TRANSPOSE can first take one expert's sub-tensor of a stacked table: src_index[0] picks the dim-0 sub-tensor of a rank-3 stack, or, with src_rows[0] > 0, the row block of a rank-2 stack flattened over its rows. map_transpose_slice() declares it; map_slice() takes the same optional sub_rows. Tests in checkpoint_test cover the aliases, the [8,4,3] case, the flattened case and an out-of-range index.
This commit is contained in:
Boris Manojlovic authored and StillDeadcode committed 2026-10-27 20:58:47 +01:00
1 parent 157450a82b
commit 4853db97db
4 files changed
+182 -16

No files matched your search

+7 -2
View File
@@ -597,7 +597,10 @@ typedef struct RadNameMap {
/* RAD_MAP_ROWS only: piece i is rows [src_index[i], src_index[i] + src_rows[i]) of the 2-D (or
* higher, rows along dim 0) tensor src[i]; the pieces join along dim 0. RAD_MAP_COLS: columns
* [src_index[0], src_index[0] + src_rows[0]) of src[0]'s last dimension. */
* [src_index[0], src_index[0] + src_rows[0]) of src[0]'s last dimension. RAD_MAP_TRANSPOSE with
* src_index[0] >= 0: rows [src_index[0] * src_rows[0], (src_index[0] + 1) * src_rows[0]) when
* src[0] is rank 2 and src_rows[0] > 0 (a stack flattened over rows); otherwise src_index[0] is
* the dim-0 sub-tensor of a higher-rank stack. */
int64_t src_rows[8];
} RadNameMap;
@@ -624,7 +627,9 @@ typedef struct RadNameMap {
* columns it is fed.
*
* RAD_MAP_TRANSPOSE swaps the last two dimensions of src[0] in each leading slice, [.., a, b] to
* [.., b, a]: a linear a checkpoint keeps as (in, out), where the GEMMs read (out, in).
* [.., b, a]: a linear a checkpoint keeps as (in, out), where the GEMMs read (out, in). With
* src_index[0], it first takes one expert's sub-tensor (see src_rows): a stacked expert table kept
* [n_expert, in, out] or flattened [n_expert * in, out].
*
* RAD_MAP_EMA is an exponential-moving-average codebook as EnCodec-style quantisers keep it: src[0]
* the embedding sums [bins, ...], src[1] the cluster usage [bins], and the codebook is each row
+23 -2
View File
@@ -513,12 +513,33 @@ inline int map_copy_alt(RadBuilder* b, const char* declared, const char* a, cons
/* One expert's slice of a stacked checkpoint tensor. GGUF stores a MoE layer's experts as one
* ffn_gate_exps [.., n_expert], while placement moves one expert at a time and so needs one
* declared weight per expert (spec §4.2). src_index carries the expert index explicitly. */
inline int map_slice(RadBuilder* b, const char* declared, const char* src, int index) {
* declared weight per expert (spec §4.2). src_index carries the expert index explicitly.
*
* `sub_rows` is for a stack flattened into the rows of a rank-2 tensor (Nomic's
* experts.mlp.w1 is [n_expert * n_ff, n_embd]): it is the sub-tensor's row count, so the slice is
* rows [index * sub_rows, (index + 1) * sub_rows). Left 0, a rank-3 stack takes index as its
* dim-0 sub-tensor. */
inline int map_slice(RadBuilder* b, const char* declared, const char* src, int index,
int64_t sub_rows = 0) {
RadNameMap m = map_base(declared, RAD_MAP_COPY);
m.n_src = 1;
m.src[0] = src;
m.src_index[0]= index;
m.src_rows[0] = sub_rows;
return rad_decl_name_map(b, &m);
}
/* One expert's slice of a stacked tensor, transposed: the per-expert table a grouped GEMM reads
* when the checkpoint keeps it [in, out]. A rank-3 stack takes index as its dim-0 sub-tensor. A
* rank-2 stack flattened over its rows takes `sub_rows` as the sub-tensor's row count, as
* map_slice's. */
inline int map_transpose_slice(RadBuilder* b, const char* declared, const char* src, int index,
int64_t sub_rows = 0) {
RadNameMap m = map_base(declared, RAD_MAP_TRANSPOSE);
m.n_src = 1;
m.src[0] = src;
m.src_index[0]= index;
m.src_rows[0] = sub_rows;
return rad_decl_name_map(b, &m);
}
+34 -12
View File
@@ -235,28 +235,33 @@ int meta_from_config_json(const std::string& dir, MetaOwn* out) {
if (const char* nm = gs("_name_or_path")) out->name = nm;
RadModelMeta& m = out->m;
m.n_layers = gi("num_hidden_layers", 0);
m.n_embd = gi("hidden_size", 0);
m.n_head = gi("num_attention_heads", 0);
/* Nomic BERT states its geometry with n_layer, n_embd, n_head, n_inner, n_positions and
* moe_top_k. They are fallbacks in the same chain, so a config carrying both spellings keeps
* the HuggingFace one and every existing checkpoint reads byte for byte as before. */
m.n_layers = gi("num_hidden_layers", gi("n_layer", 0));
m.n_embd = gi("hidden_size", gi("n_embd", 0));
m.n_head = gi("num_attention_heads", gi("n_head", 0));
m.n_head_kv = gi("num_key_value_heads", m.n_head);
/* Read head_dim; derive it only as a fallback. On qwen35 it is 256 while
* hidden_size/num_attention_heads is 213. */
m.head_dim = gi("head_dim", m.n_head ? m.n_embd / m.n_head : 0);
/* A routed config has no `intermediate_size`, only `moe_intermediate_size` (one expert's
* width, which is what a GEMM sees and what n_ff means) and
* `shared_expert_intermediate_size` (read by the plugin from the free-form table). */
m.n_ff = gi("intermediate_size", gi("moe_intermediate_size", 0));
* `shared_expert_intermediate_size` (read by the plugin from the free-form table). Nomic
* spells the same width n_inner. */
m.n_ff = gi("intermediate_size", gi("moe_intermediate_size", gi("n_inner", 0)));
m.n_vocab = gi("vocab_size", 0);
m.n_ctx_train = gi("max_position_embeddings", 0);
m.n_ctx_train = gi("max_position_embeddings", gi("n_positions", 0));
/* The DeepSeek spelling: Unlimited-OCR's decoder is a DeepSeek-V2 MoE and states its expert
* count as `n_routed_experts`; a routed config names the count under one of the three and
* never under more than one, so the chain is a rename, not a guess. */
m.n_expert = gi("num_experts", gi("num_local_experts", gi("n_routed_experts", 0)));
m.n_expert_used = gi("num_experts_per_tok", 0);
m.n_expert_used = gi("num_experts_per_tok", gi("moe_top_k", 0));
m.n_expert_shared = gi("n_shared_experts", gi("num_shared_experts",
gi("shared_expert_intermediate_size", 0) ? 1 : 0));
m.rms_eps = (float)gf("rms_norm_eps", gf("norm_eps", 1e-6)); /* LFM2: norm_eps */
m.rope_theta = (float)gf("rope_parameters.rope_theta", gf("rope_theta", 10000.0));
m.rms_eps = (float)gf("rms_norm_eps", gf("norm_eps", gf("layer_norm_epsilon", 1e-6)));
m.rope_theta = (float)gf("rope_parameters.rope_theta",
gf("rope_theta", gf("rotary_emb_base", 10000.0)));
/* Rotary scaling stays in the free-form keys with its type (arch/common/rad_arch.h,
* yarn_from); a bare factor here would wrongly mean linear scaling. */
m.rope_scale = 1.0f;
@@ -2262,6 +2267,19 @@ static int resolve_derived(Checkpoint& ck, const std::vector<const RadNameMap*>&
"'") +
", not all in " + ck.path());
}
/* A transposed expert table is one sub-tensor of a stack, transposed. A stack kept as a
* rank-3 tensor takes src_index[0] as its dim-0 index. A stack flattened into the rows of a
* rank-2 tensor takes src_rows[0] as the sub-tensor's row count, with src_index[0] selecting
* the block: Nomic's experts.mlp.w2 is [n_expert * n_ff, n_embd]. */
if (m.mode == RAD_MAP_TRANSPOSE && m.src_index[0] >= 0) {
if (m.src_rows[0] > 0 && a->shape.size() == 2) {
const int64_t r0 = (int64_t)m.src_index[0] * m.src_rows[0];
a = ck.rows(*a, r0, m.src_rows[0], out->name.c_str());
} else {
a = ck.slice(*a, m.src_index[0], out->name.c_str());
}
if (!a) return refuse(RAD_E_SHAPE, "a sub-tensor it names is not there");
}
const size_t rank = a->shape.size();
if (rank < 2 || rank > RAD_MAX_RANK)
return refuse(RAD_E_SHAPE, "'" + a->name + "' has rank " + std::to_string(rank) +
@@ -2373,7 +2391,7 @@ int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, b
const bool row_pieces = maps.front()->mode == RAD_MAP_ROWS;
for (const RadNameMap* pm : maps)
for (int k = 0; k < pm->n_src; ++k)
srcs.push_back({ pm->src[k], pm->src_index[k], row_pieces ? pm->src_rows[k] : 0 });
srcs.push_back({ pm->src[k], pm->src_index[k], pm->src_rows[k] });
out->concat_dim = row_pieces ? 0 : maps.front()->concat_dim;
/* The mode decides what several sources mean:
@@ -2416,9 +2434,13 @@ int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, b
if (!st) return refuse(RAD_E_SHAPE, "the rows it names are not there");
}
/* A sub-tensor index is always applied (ignoring it would give every expert the whole
* stack). Its scale plane is sliced at the same index. */
* stack). Its scale plane is sliced at the same index. A stack flattened into the rows of
* a rank-2 tensor takes src_rows as the sub-tensor's row count. */
else if (st && s1.index >= 0) {
st = ck.slice(*st, s1.index, name.c_str());
if (s1.rows > 0 && st->shape.size() == 2)
st = ck.rows(*st, (int64_t)s1.index * s1.rows, s1.rows, name.c_str());
else
st = ck.slice(*st, s1.index, name.c_str());
if (!st) return refuse(RAD_E_SHAPE, "a sub-tensor it names is not there");
if (sc && !(sc = ck.slice(*sc, s1.index, name.c_str())))
return refuse(RAD_E_SHAPE, "its scale plane cannot be sliced at the same index");
+118
View File
@@ -1972,6 +1972,38 @@ TEST(config_json_fills_the_model_meta_as_each_family_spells_it) {
}
}
/* Nomic BERT names its dimensions n_layer, n_embd, n_head, n_inner and n_positions, its expert
* count num_experts and its top-k moe_top_k, and its norm epsilon layer_norm_epsilon. Without the
* fallbacks the engine refuses the checkpoint at startup and rad-convert sizes nothing. */
TEST(a_nomic_config_fills_the_meta_from_its_own_spellings) {
const char* cfg =
R"({"model_type":"nomic_bert","n_layer":12,"n_embd":768,"n_head":12,
"n_inner":3072,"n_positions":2048,"vocab_size":250048,
"layer_norm_epsilon":1e-05,"num_experts":8,"moe_top_k":2,
"moe_every_n_layers":2,"rotary_emb_base":10000,"prenorm":false})";
const std::string dir = write_checkpoint({ bf16("x", { 2, 2 }, 0) }, cfg);
Checkpoint ck;
REQUIRE_EQ(ck.open(dir), RAD_OK);
const MetaOwn& m = ck.meta();
CHECK_EQ(m.arch, std::string("nomicbert"));
CHECK_EQ(m.m.n_layers, (int64_t)12);
CHECK_EQ(m.m.n_embd, (int64_t)768);
CHECK_EQ(m.m.n_head, (int64_t)12);
CHECK_EQ(m.m.n_head_kv, (int64_t)12);
CHECK_EQ(m.m.head_dim, (int64_t)64);
CHECK_EQ(m.m.n_ff, (int64_t)3072);
CHECK_EQ(m.m.n_vocab, (int64_t)250048);
CHECK_EQ(m.m.n_ctx_train, (int64_t)2048);
CHECK_EQ(m.m.n_expert, (int64_t)8);
CHECK_EQ(m.m.n_expert_used, (int64_t)2);
CHECK_EQ(m.m.rms_eps, 1e-5f);
CHECK_EQ(m.m.rope_theta, 1e4f);
CHECK_EQ(m.get("moe_every_n_layers"), std::string("2"));
CHECK_EQ(m.get("num_experts"), std::string("8"));
CHECK_EQ(m.get("rotary_emb_base"), std::string("10000"));
CHECK_EQ(m.get("prenorm"), std::string("0"));
}
/* ================================================================== a table declared in pieces
*
* A table shipped as many shards is declared as several CONCAT maps under one name (each map holds
@@ -3039,6 +3071,92 @@ TEST(a_transposed_weight_swaps_its_last_two_dimensions_in_each_slice) {
CHECK_EQ(f[(size_t)(s * 12 + j * 3 + i)], (float)(s * 12 + i * 4 + j));
}
/* An expert's w2 is a dim-0 sub-tensor of a stacked [n_expert, in, out] tensor, transposed into
* the [out, in] table a grouped GEMM reads. Ignoring src_index would give every expert the whole
* stack, and transposing before slicing would mix experts. */
TEST(a_transposed_expert_slice_takes_its_dim0_sub_tensor_then_transposes) {
std::vector<float> v(8 * 4 * 3);
for (size_t i = 0; i < v.size(); ++i) v[i] = (float)i;
const std::string dir = write_checkpoint({ f32("w2", { 8, 4, 3 }, v) }, kConfig);
Checkpoint ck;
REQUIRE_EQ(ck.open(dir), RAD_OK);
Program prog;
std::vector<std::string> names;
names.reserve(8);
for (int e = 0; e < 8; ++e) names.push_back("w2." + std::to_string(e));
for (int e = 0; e < 8; ++e)
prog.name_map.push_back(map(names[(size_t)e].c_str(), RAD_MAP_TRANSPOSE, { "w2" }, { e }));
prog.name_map.push_back(map("w2.bad", RAD_MAP_TRANSPOSE, { "w2" }, { 8 }));
CkptWeight w;
std::string why;
for (int e = 0; e < 8; ++e) {
const std::string& name = names[(size_t)e];
REQUIRE_EQ(ckpt_resolve(ck, prog, name, false, &w, &why), RAD_OK);
CHECK(!w.trivial);
REQUIRE_EQ(w.rank, (uint32_t)2);
CHECK_EQ(w.shape[0], (int64_t)3);
CHECK_EQ(w.shape[1], (int64_t)4);
std::vector<float> f(3 * 4);
REQUIRE_EQ(ckpt_rows_f32(w, 0, 3, f.data(), &why), RAD_OK);
/* Element (j, i) is the source's (e, i, j). */
for (int j = 0; j < 3; ++j)
for (int i = 0; i < 4; ++i)
CHECK_EQ(f[(size_t)(j * 4 + i)], (float)(e * 12 + i * 3 + j));
}
CHECK(ckpt_resolve(ck, prog, "w2.bad", false, &w, &why) < 0);
}
/* A stack flattened over the rows of a rank-2 tensor (Nomic's experts.mlp.w1 and w2 are
* [n_expert * n_ff, n_embd]) takes src_rows as one expert's row count. w1 is a copy of the block,
* w2 the same block transposed. */
TEST(a_flat_expert_stack_is_one_blocks_rows_before_transposing) {
std::vector<float> v(8 * 4 * 3);
for (size_t i = 0; i < v.size(); ++i) v[i] = (float)i;
const std::string dir = write_checkpoint({ f32("w", { 8 * 4, 3 }, v) }, kConfig);
Checkpoint ck;
REQUIRE_EQ(ck.open(dir), RAD_OK);
Program prog;
std::vector<std::string> names;
names.reserve(16);
for (int e = 0; e < 8; ++e) names.push_back("w1." + std::to_string(e));
for (int e = 0; e < 8; ++e) names.push_back("w2." + std::to_string(e));
for (int e = 0; e < 8; ++e) {
RadNameMap a = map(names[(size_t)e].c_str(), RAD_MAP_COPY, { "w" }, { e });
a.src_rows[0] = 4;
prog.name_map.push_back(a);
RadNameMap t = map(names[(size_t)(8 + e)].c_str(), RAD_MAP_TRANSPOSE, { "w" }, { e });
t.src_rows[0] = 4;
prog.name_map.push_back(t);
}
CkptWeight w1, w2;
std::string why;
for (int e = 0; e < 8; ++e) {
REQUIRE_EQ(ckpt_resolve(ck, prog, names[(size_t)e], false, &w1, &why), RAD_OK);
REQUIRE_EQ(w1.rank, (uint32_t)2);
CHECK_EQ(w1.shape[0], (int64_t)4);
CHECK_EQ(w1.shape[1], (int64_t)3);
std::vector<float> f1(4 * 3);
REQUIRE_EQ(ckpt_rows_f32(w1, 0, 4, f1.data(), &why), RAD_OK);
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 3; ++j)
CHECK_EQ(f1[(size_t)(i * 3 + j)], (float)(e * 12 + i * 3 + j));
REQUIRE_EQ(ckpt_resolve(ck, prog, names[(size_t)(8 + e)], false, &w2, &why), RAD_OK);
REQUIRE_EQ(w2.rank, (uint32_t)2);
CHECK_EQ(w2.shape[0], (int64_t)3);
CHECK_EQ(w2.shape[1], (int64_t)4);
std::vector<float> f2(3 * 4);
REQUIRE_EQ(ckpt_rows_f32(w2, 0, 3, f2.data(), &why), RAD_OK);
for (int j = 0; j < 3; ++j)
for (int i = 0; i < 4; ++i)
CHECK_EQ(f2[(size_t)(j * 4 + i)], (float)(e * 12 + i * 3 + j));
}
RadNameMap bad = map("w1.bad", RAD_MAP_COPY, { "w" }, { 8 });
bad.src_rows[0] = 4;
prog.name_map.push_back(bad);
CHECK(ckpt_resolve(ck, prog, "w1.bad", false, &w1, &why) < 0);
}
/* An EMA codebook is its sums over max(usage, 1e-5), row by row; codebooks declared in pieces
* stack along dim 0. */
TEST(an_ema_codebook_divides_its_sums_by_their_usage_and_stacks) {