Modeling code: 6 issues on CPU / transformers >= 5.15 (with workaround patch)

#4
by Solshine - opened

Found while building a small CPU test fixture for llama.cpp's Kimi K3 support (ggml-org/llama.cpp#26185). The released modeling code can't run on CPU or on current transformers main as shipped. Six concrete issues, all in modeling_kimi_linear.py unless noted:

  1. The model constructor hard-forces flash_attention_2 and silently overrides whatever attn_implementation you request (the block around line 1114 logs a warning and replaces it). On a machine without flash-attn this makes eager/SDPA unusable without editing the file.

  2. from transformers.utils.generic import OutputRecorder fails on transformers >= 5.15.0.dev0. OutputRecorder moved to transformers.utils.output_capturing.

  3. _tied_weights_keys uses the old list-style API. transformers main expects a dict, so save_pretrained crashes with AttributeError: 'list' object has no attribute 'keys'. Not hit in normal from_pretrained use, but any fine-tune/save path hits it.

  4. create_causal_mask is called with input_embeds= (transformers main spells it inputs_embeds) and with cache_position=, which the current signature doesn't take. Both raise TypeError.

  5. Under from_config (fresh init, e.g. for fine-tuning from scratch or building test models), some custom parameters stay as uninitialized torch.empty memory because _init_weights doesn't cover them: self_attn.dt_bias and block_sparse_moe.gate.e_score_correction_bias. I observed absmax ~1.9e37 in dt_bias and the forward pass returns NaN logits. from_pretrained is unaffected since the checkpoint overwrites them.

  6. Minor, arguably by design: the fla KDA ops (chunk_kda, causal_conv1d, rms_norm_gated) are Triton-only, so there's no CPU path at all. I worked around it by rerouting to fla's own naive torch ops.

Workaround patch for 1-4 plus a re-init sweep for 5, and the CPU shim for 6: https://gist.github.com/SolshineCode/3115760b0c3b655563a3102ba897c426 (modeling_cpu.patch). With those applied, a 90M random-init structural clone of K3 runs on CPU and its greedy outputs match llama.cpp's implementation of the architecture token for token, so the patched path seems faithful.

Most of these probably apply to the upstream moonshotai repo too, since the modeling files look identical. Happy to split this into separate reports if that's easier to track.

AI usage disclosure: found and written up with Claude running on my machine; I approved and can rerun everything on request.

Sign up or log in to comment