{"record":{"id":"492195c787de316d","repo":"sgl-project/sglang","slug":"invalid-kimi-k3-attention-residual-target-target","errorCode":null,"errorMessage":"invalid Kimi-K3 attention-residual target {target_index}","messagePattern":"invalid Kimi-K3 attention-residual target (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/model_loader/kimi_k3_gguf.py","lineNumber":126,"sourceCode":"    return (prefix + target,)\n\n\ndef _runtime_name(checkpoint_name: str, quantized: bool) -> str:\n    if not quantized or not checkpoint_name.endswith(\".weight\"):\n        return checkpoint_name\n    return checkpoint_name.removesuffix(\"weight\") + \"qweight\"\n\n\ndef _residual_target_value(raw: torch.Tensor, target_index: int) -> torch.Tensor:\n    if raw.ndim != 1:\n        raise ValueError(\n            f\"Kimi-K3 attention-residual score must be a vector, got {tuple(raw.shape)}\"\n        )\n    if target_index == 0:\n        return raw.unsqueeze(0)\n    if target_index == 1:\n        return torch.ones_like(raw)\n    raise ValueError(f\"invalid Kimi-K3 attention-residual target {target_index}\")\n\n\ndef _kda_a_log_target_value(raw: torch.Tensor) -> torch.Tensor:\n    \"\"\"Undo llama.cpp's GGUF-time ``A_log -> -exp(A_log)`` transform.\"\"\"\n    if not raw.is_floating_point() or not torch.isfinite(raw).all():\n        raise ValueError(\"Kimi-K3 GGUF ssm_a must contain finite floating values\")\n    if not torch.all(raw < 0):\n        raise ValueError(\"Kimi-K3 GGUF ssm_a must contain only -exp(A_log) values\")\n    return torch.log(-raw)\n\n\ndef kimi_k3_nonexpert_weights_iterator(\n    manifest_path: str | os.PathLike[str],\n) -> Generator[tuple[str, torch.Tensor], None, None]:\n    \"\"\"Stream non-routed tensors shard by shard without reading routed payloads.\"\"\"\n\n    import gguf\n","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/model_loader/kimi_k3_gguf.py#L108-L144","documentation":"Raised by _residual_target_value in Kimi-K3 GGUF weight conversion when the attention-residual target index is neither 0 nor 1. The target index determines whether the raw vector or a ones-like tensor is emitted for the dual residual parameters, so any other value is a programming error in the target mapping.","triggerScenarios":"Calling _residual_target_value(raw, target_index) with target_index not in {0,1}; indirectly via kimi_k3_nonexpert_weights_iterator when kimi_k3_checkpoint_targets maps a checkpoint tensor to exactly 2 targets but the iteration index goes out of range.","commonSituations":"Editing the residual target mapping (kimi_k3_checkpoint_targets) and adding a third target name, or reordering targets so the enumerate index no longer matches 0/1; bugs in custom fork of the loader.","solutions":["Check kimi_k3_checkpoint_targets: any tensor resolving to exactly 2 targets must keep target index 0 or 1; remove extra targets.","If you added a new residual parameter, extend _residual_target_value with an explicit branch for the new index instead of relying on the ValueError.","Run the unit tests test_residual_score_preserves_exact_combined_weight / test_residual_score_rejects_non_vector_source to verify the mapping."],"exampleFix":"// before\ntargets = kimi_k3_checkpoint_targets(name)  # returns 3 targets\n...\nvalue = _residual_target_value(raw, target_index)  # target_index == 2 -> raises\n\n// after\ntargets = kimi_k3_checkpoint_targets(name)  # keep exactly [score, ones] targets\nassert target_index in (0, 1)\nvalue = _residual_target_value(raw, target_index)","handlingStrategy":"validation","validationCode":"targets = kimi_k3_checkpoint_targets(name)\nif len(targets) == 2:\n    assert all(i in (0, 1) for i in range(len(targets))), f\"bad targets for {name}\"","typeGuard":"def is_valid_residual_index(i: int) -> bool:\n    return i in (0, 1)","tryCatchPattern":null,"preventionTips":["Keep residual target lists exactly length 2 and covered by unit tests.","Never reorder target names without re-running the residual tests."],"tags":["kimi-k3","gguf","weight-conversion","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}