{"record":{"id":"9435a92050adb818","repo":"sgl-project/sglang","slug":"kimi-k3-gguf-ssm-a-must-contain-only-exp-a-log-v","errorCode":null,"errorMessage":"Kimi-K3 GGUF ssm_a must contain only -exp(A_log) values","messagePattern":"Kimi-K3 GGUF ssm_a must contain only -exp\\(A_log\\) values","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/model_loader/kimi_k3_gguf.py","lineNumber":134,"sourceCode":"\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\n    manifest_file = Path(manifest_path).resolve()\n    manifest = json.loads(manifest_file.read_text(encoding=\"utf-8\"))\n    if manifest.get(\"format\") != \"SGLANG-KIMI-GGMLMOEPACK-ADAPTER-v1\":\n        raise ValueError(\"Kimi-K3 manifest format is unsupported\")\n    if not manifest.get(\"complete\"):\n        raise ValueError(\"Kimi-K3 manifest is incomplete\")\n\n    records_by_shard: dict[int, list[dict]] = defaultdict(list)","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/model_loader/kimi_k3_gguf.py#L116-L152","documentation":"Raised by _kda_a_log_target_value when the ssm_a tensor contains non-negative values. Since the loader inverts A_log -> -exp(A_log) (which is always strictly negative), any value >= 0 means the tensor was not transformed the way llama.cpp transforms it, and taking torch.log(-raw) would be invalid.","triggerScenarios":"The GGUF ssm_a tensor contains zero or positive entries — e.g. it was written from raw A_log values instead of -exp(A_log), or mixed-up tensor names map the wrong data into ssm_a.","commonSituations":"Using a GGUF produced by a converter that does not apply the llama.cpp A_log transform; tensor renamed/mis-mapped so another weight lands in the ssm_a slot; hand-edited GGUF.","solutions":["Regenerate the GGUF with a converter that applies A_log -> -exp(A_log) (llama.cpp-compatible export).","Inspect the tensor: if all values look like logs (mixed sign, small magnitude) it is raw A_log — re-export with the transform.","Check tensor naming in the manifest/records matches the converter's scheme."],"exampleFix":"# before: writer.add_tensor(\"ssm_a\", torch.log(a))           # wrong: raw A_log\n# after:  writer.add_tensor(\"ssm_a\", -torch.exp(a_log))        # llama.cpp transform","handlingStrategy":"validation","validationCode":"if not bool((raw < 0).all()):\n    raise SystemExit(\"ssm_a not in -exp(A_log) form; re-export the GGUF\")","typeGuard":"def is_neg_exp_form(t: torch.Tensor) -> bool:\n    return bool((t < 0).all())","tryCatchPattern":null,"preventionTips":["Use a llama.cpp-compatible converter that applies A_log -> -exp(A_log).","Spot-check ssm_a: all values must be strictly negative."],"tags":["kimi-k3","gguf","ssm","a-log"],"backgroundTag":"tensor-data-validation","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}