Files
helexa/script/dump_qwen3_next_tiny.py
grenadeandClaude Fable 5 9bf13f09dd
CI / Format (push) Successful in 8s
CI / CUDA type-check (push) Successful in 1m38s
CI / Clippy (push) Successful in 2m17s
CI / Test (push) Successful in 5m17s
CI / Build cortex SRPM (push) Has been skipped
CI / Publish cortex to COPR (push) Has been skipped
CI / Build neuron SRPM (push) Has been skipped
CI / Publish neuron to COPR (push) Has been skipped
CI / Bump version in source (push) Has been skipped
CI / Format (pull_request) Successful in 7s
CI / CUDA type-check (pull_request) Successful in 1m38s
CI / Clippy (pull_request) Successful in 2m15s
CI / Test (pull_request) Successful in 6m54s
CI / Build cortex SRPM (pull_request) Has been skipped
CI / Build neuron SRPM (pull_request) Has been skipped
CI / Publish cortex to COPR (pull_request) Has been skipped
CI / Publish neuron to COPR (pull_request) Has been skipped
CI / Bump version in source (pull_request) Has been skipped
feat(neuron): qwen3_next MoE FFN block, single-GPU path + HF parity (#92)
F1 slice 2 — the MoE block itself, CPU/single-GPU:

- arch/qwen3_5/moe.rs: Qwen3_5MoeBlock — top-k router (upstream
  softmax-then-topk order, renorm iff norm_topk_prob), per-expert
  SwiGLU (reusing Qwen3_5MLP at moe_intermediate_size), and the
  always-on shared expert mixed via sigmoid(shared_expert_gate).
  Correctness-first host-side scatter dispatch; the fused grouped-GEMM
  path is slice 4 behind the same forward signature.
- decoder.rs: MlpKind { Dense, Moe } dispatch on layer_uses_moe,
  mirroring AttentionKind.
- linear_attn.rs: fused-checkpoint support — qwen3_next stores
  in_proj_qkvz / in_proj_ba interleaved per key-head group (upstream
  fix_query_key_value_ordering layout); split_fused_qkvz/ba
  de-interleave once at load into the contiguous [Q|K|V] + Z / B + A
  layout the forward path (incl. the conv channels) already uses.
  Auto-detected via contains_tensor, so Qwen3.6 checkpoints are
  untouched.
- mod.rs: text_weight_prefix() — qwen3_next checkpoints put the text
  core at `model.*`, Qwen3.6 at `model.language_model.*`; the slice-1
  single-GPU MoE guard is removed (TP guard stays until slice 3).

Validation:
- qwen3_next_parity integration test replays a committed tiny
  random-weight HF Qwen3NextForCausalLM checkpoint (generated by
  script/dump_qwen3_next_tiny.py on beast: transformers 5.9.0,
  torch 2.9.1) through neuron's full load path: max_abs 0.000000,
  cosine 1.00000000 at f32 — exact parity, pinning the config
  normalisation, weight prefix, qkvz/ba de-interleave, hybrid layer
  interleaving, and the whole MoE block against upstream.
- Unit tests: scatter forward vs per-token dense reference,
  no-shared-expert/no-renorm behaviour, fused-split round-trip, and a
  flat-layout end-to-end structural load.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TczcGF7JSjJs8r15RSSGpx
2026-07-02 00:16:19 +03:00

157 lines
5.2 KiB
Python

#!/usr/bin/env python3
"""Synthesize a tiny qwen3_next parity fixture (#92).
Unlike script/dump_reference.py (which replays a real HF snapshot and
therefore needs the weights on disk), this builds a TINY random-weight
`Qwen3NextForCausalLM` from scratch, saves the checkpoint INTO the
fixture directory, runs the reference forward on fixed token ids, and
dumps the final-position logits. The whole fixture (weights included)
is a few hundred KB, so it is committed and the companion Rust test
(crates/neuron/tests/qwen3_next_parity.rs) runs everywhere — no env
var, no snapshot, CI included.
What this pins, exactly: neuron's qwen3_next wiring against upstream —
flat config normalisation, the `model.*` weight prefix, the
per-key-head-group de-interleave of the fused `in_proj_qkvz` /
`in_proj_ba` projections, hybrid layer interleaving, and the MoE block
(softmax→top-k→renorm routing, per-expert SwiGLU, shared expert +
sigmoid gate). The full-size 80B checkpoint differs only in dimensions.
The config mirrors the real 80B's *shape decisions* at doll-house
scale: interval-4 hybrid, 8 layers (so two full-attention layers),
every layer MoE (decoder_sparse_step 1), 16 experts / top-4 + shared
expert, partial rotary 0.25, 2 KV heads.
Usage (host with torch + transformers>=4.57, e.g. beast):
python3 script/dump_qwen3_next_tiny.py \
--out crates/neuron/tests/fixtures/numerical/qwen3_next-tiny
Regenerate whenever the transformers reference implementation changes;
record the transformers version from the manifest in the commit
message.
"""
import argparse
import json
import os
import struct
# ---------------------------------------------------------------------------
# Compat shim (same as dump_reference.py): transformers 5.9 constructs
# kernels-hub repository objects at import time without the
# revision/version that kernels 0.15 requires. The hub kernels are
# never used here; the constructors just must not throw.
os.environ.setdefault("USE_HUB_KERNELS", "NO")
try:
import kernels.layer.func as _kf
import kernels.layer.layer as _kl
def _patch(cls):
orig = cls.__init__
def patched(self, *a, **kw):
if "revision" not in kw and "version" not in kw:
kw["revision"] = "main"
orig(self, *a, **kw)
cls.__init__ = patched
_patch(_kl.LayerRepository)
_patch(_kf.FuncRepository)
except Exception: # noqa: BLE001 — older/newer kernels may not need it
pass
# ---------------------------------------------------------------------------
import torch # noqa: E402
# Fixed input: 96 token ids (> 64 so neuron's chunked delta-rule
# prefill path is exercised), deterministic arithmetic sequence folded
# into the tiny vocab.
TOKEN_IDS = [(7 * i + 3) % 512 for i in range(96)]
SEED = 92
def tiny_config():
from transformers import Qwen3NextConfig
return Qwen3NextConfig(
vocab_size=512,
hidden_size=64,
intermediate_size=128,
num_hidden_layers=8,
num_attention_heads=4,
num_key_value_heads=2,
head_dim=32,
max_position_embeddings=512,
partial_rotary_factor=0.25,
rope_theta=10000000,
rms_norm_eps=1e-6,
tie_word_embeddings=False,
full_attention_interval=4,
linear_conv_kernel_dim=4,
linear_key_head_dim=16,
linear_num_key_heads=2,
linear_num_value_heads=4,
linear_value_head_dim=16,
decoder_sparse_step=1,
mlp_only_layers=[],
moe_intermediate_size=32,
norm_topk_prob=True,
num_experts=16,
num_experts_per_tok=4,
shared_expert_intermediate_size=32,
)
def write_f32(path, tensor):
data = tensor.detach().to(torch.float32).cpu().contiguous().reshape(-1)
with open(path, "wb") as f:
f.write(struct.pack(f"<{data.numel()}f", *data.tolist()))
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--out", required=True, help="fixture directory to write")
args = ap.parse_args()
import transformers
from transformers import Qwen3NextForCausalLM
os.makedirs(args.out, exist_ok=True)
torch.manual_seed(SEED)
cfg = tiny_config()
model = Qwen3NextForCausalLM(cfg).to(torch.float32).eval()
# Save the checkpoint into the fixture itself (config.json +
# model.safetensors) — the Rust test loads neuron's implementation
# from exactly these files.
model.save_pretrained(args.out, safe_serialization=True)
ids = torch.tensor([TOKEN_IDS], dtype=torch.long)
with torch.no_grad():
out = model(input_ids=ids)
logits = out.logits[0, -1] # final position, (vocab,)
write_f32(f"{args.out}/logits.f32", logits)
manifest = {
"case": "qwen3_next-tiny",
"seed": SEED,
"token_ids": TOKEN_IDS,
"files": {"logits": {"file": "logits.f32", "shape": [cfg.vocab_size]}},
"versions": {
"transformers": transformers.__version__,
"torch": torch.__version__,
},
}
with open(f"{args.out}/manifest.json", "w") as f:
json.dump(manifest, f, indent=2)
print(f"fixture written to {args.out}")
print(f" transformers {transformers.__version__}, torch {torch.__version__}")
print(f" logits[:4] = {logits[:4].tolist()}")
if __name__ == "__main__":
main()