mirror of
https://github.com/kvcache-ai/ktransformers.git
synced 2026-11-05 01:07:28 +00:00
[GH-ISSUE #2151] GeneralMoEWrapper.load_weights() cpu_save path reads nonexistent dict keys, guaranteed KeyError #1294
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
kvcache-ai/ktransformers#1294
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @AmirF194 on GitHub (Aug 9, 2026).
Original GitHub issue: https://github.com/kvcache-ai/ktransformers/issues/2151
Reminder
System Info
Not hardware-dependent: the crash happens in pure Python before any native kt_kernel_ext call, so it reproduces on any platform with the package importable (kt-kernel Python layer, any CPU variant).
Reproduction
GeneralMoEWrapper.load_weights()(kt-kernel/python/utils/moe_kernel.py) has two branches that callself.safetensor_loader.load_experts(base_key). The first, in theif self.load_merged_weight:block, reads the returned dict correctly:The second, in the
if self.cpu_save:block about 80 lines later, reads different keys thatload_experts()never returns:SafeTensorLoader.load_experts()(kt-kernel/python/utils/loader.py) returns a dict with keysup,gate,down,up_scale,gate_scale,down_scale(no_weightsuffix), soload_weights()raisesKeyError: 'gate_weight'whenever it is called withcpu_save=Trueand merged safetensor weights are present. Minimal repro against currentmain(constructingGeneralMoEWrapperwith a mocked loader returning that exact key set, since the crash happens before any native call is reached):Both keys trace back to the same introducing commit (
9bc00e5,#1587) and have never been touched since; the correctly-keyed branch 80 lines earlier in the same function was never updated to match.An identical, byte-for-byte copy of the same wrong keys exists in
kt-kernel/python/utils/amx.py(AMXMoEWrapper, lines ~514-516), also from9bc00e5. I'm leaving that one alone sinceamx.pyis currently touched by open PR #2111.Others
I have a fix ready (correct key names plus converting the loader's per-expert numpy arrays into the contiguous
[expert_num, intermediate, hidden]tensor the native kernel expects, matchingload_weights_from_tensors()'s existingtorch.stackpattern) and will open a PR referencing this issue.@Anai-Guo commented on GitHub (Aug 18, 2026):
Thanks for flagging the identical copy in
amx.pyhere. I've opened #2161 to fix theAMXMoEWrapper.load_weights()cpu_savebranch the same way #2152 handlesGeneralMoEWrapper— switching the key toblk.{layer_idx}(themodel.layers.prefix and its fallback bothValueErrorinSafeTensorLoader.load_experts()) and stacking the per-expert numpy arrays fromw["gate"|"up"|"down"][0]instead of the nonexistent*_weightkeys. It only touches those lines, so it's orthogonal to #2111.