Views
No views yet
transformers compatibility fixNanbeige/Nanbeige4.2-3B
(base revision 5d54321e9e01e0d026f8e371046678fc384dca39) with five bugs
fixed in its custom modeling code and baked into the checkpoint's weights.
Independent project — not affiliated with or endorsed by the Nanbeige
team. All credit for the architecture, training, and base weights belongs
to them; see their model card
and technical report.transformers
(trust_remote_code=True) crashes hard on Apple Silicon (MPS) a few
generation steps in, and — even patched around — produces structurally
incoherent output (looping </think> tokens, character-level word salad)
regardless of dtype, sampling settings, or enable_thinking. Five distinct
transformers-version compatibility bugs are responsible; none are security
issues, and none are the model's fault in any deep sense — its custom code
was written against an older transformers API surface that has since moved
on. Full root-cause writeup, exact evidence, and the elimination
methodology for each: MPS_FIX_NOTES.md.NanbeigeRotaryEmbedding's inv_freq buffer was being
silently zeroed on load (persistent=False + meta-device init means it's
never restored from the checkpoint) — meaning RoPE was contributing zero
positional information regardless of any other setting. That single bug
explains nearly all of the incoherence; the other four (a RoPE-config
dispatch KeyError, a Cache API sentinel mismatch, the position_ids
re-trim that caused the actual MPS crash, and a _tied_weights_keys format
issue that breaks save_pretrained) are all real but secondary.config.json disables every one of the model's more novel advertised
features (LoopSplit, manifold-constrained hyper-connections/mHC, depth
attention, n-gram embeddings) — what actually runs is standard GQA attention
plus a weight-shared loop over 22 layers, executed twice. Nanbeige's own
official Ollama/MLX serving path (Nanbeige/ollama, nanbeige42 branch)
independently confirms this: it implements only plain attention and the
loop-repeat, nothing else. Details in MPS_FIX_NOTES.md.harness/nanbeige_harness_server.py in the repo above.chat_template.jinja itself; this repo's copy is unmodified from the base
checkpoint. Fixed in the same harness above.transformers (below) with a system
message or a long context, you'll hit both. Use the harness in the GitHub repo, or
port the fix yourself — exact code and a full write-up are there.transformers version pin:1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "johnhalloran/Nanbeige4.2-3B-mps-fix"
5tok = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
6model = AutoModelForCausalLM.from_pretrained(
7 model_id, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True,
8)
9
10messages = [{"role": "user", "content": "Which number is bigger, 9.11 or 9.8?"}]
11text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
12inputs = tok([text], return_tensors="pt").to(model.device)
13out = model.generate(**inputs, max_new_tokens=256)
14print(tok.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))MPS_FIX_NOTES.md.jishnuvenugopal/nanbeige-mlx —
an independent MLX port whose documentation of the inv_freq buffer's
persistence semantics was the lead that found this repo's dominant bug.
Unaffiliated with Nanbeige or this repo.MPS_FIX_NOTES.md
per Apache-2.0 §4.