{"record":{"id":"f6542cdb6d6d3e4b","repo":"sgl-project/sglang","slug":"duplicate-tensor-names-detected-across-safetensors","errorCode":null,"errorMessage":"Duplicate tensor names detected across safetensors files. Refusing to load because final weights would depend on file or streamer ordering. Found {len(duplicate_files_by_key)} duplicate tensor name(s). Examples: {examples}. This usually means multiple precision variants or consolidated+sharded checkpoints were passed together.","messagePattern":"Duplicate tensor names detected across safetensors files\\. Refusing to load because final weights would depend on file or streamer ordering\\. Found (.+?) duplicate tensor name\\(s\\)\\. Examples: (.+?)\\. This usually means multiple precision variants or consolidated\\+sharded checkpoints were passed together\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"python/sglang/multimodal_gen/runtime/loader/weight_utils.py","lineNumber":205,"sourceCode":"            corrupted_files.append(st_file)\n\n    return corrupted_files, duplicate_files_by_key\n\n\ndef _raise_if_duplicate_safetensors_keys(\n    duplicate_files_by_key: dict[str, set[str]],\n) -> None:\n    if not duplicate_files_by_key:\n        return\n\n    examples = []\n    for key in sorted(duplicate_files_by_key)[:8]:\n        files = \", \".join(\n            sorted(os.path.basename(p) for p in duplicate_files_by_key[key])\n        )\n        examples.append(f\"{key} [{files}]\")\n\n    raise ValueError(\n        \"Duplicate tensor names detected across safetensors files. Refusing to load \"\n        \"because final weights would depend on file or streamer ordering. \"\n        f\"Found {len(duplicate_files_by_key)} duplicate tensor name(s). \"\n        f\"Examples: {examples}. \"\n        \"This usually means multiple precision variants or consolidated+sharded \"\n        \"checkpoints were passed together.\"\n    )\n\n\ndef safetensors_weights_iterator(\n    hf_weights_files: list[str],\n    to_cpu: bool = True,\n    use_runai_model_streamer: bool | None = None,\n    key_filter: Callable[[str], bool] | None = None,\n    clone_streamed_tensors: bool = True,\n    weight_load_plan: WeightLoadPlan | None = None,\n) -> Generator[tuple[str, torch.Tensor], None, None]:\n    \"\"\"Iterate over the weights in the model safetensor files.\"\"\"","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/loader/weight_utils.py#L187-L223","documentation":"The same tensor name appears in more than one safetensors file. Loading would silently let file/streamer ordering decide the final value of those weights, so the iterator refuses. This typically happens when two precision variants or both consolidated and sharded copies of a checkpoint are present together.","triggerScenarios":"safetensors_weights_iterator scans all weight files and builds duplicate_files_by_key mapping a tensor key to multiple files with non-empty contents; any duplicate triggers the error, with up to 8 example keys listed.","commonSituations":"Downloading an fp8 variant into the same directory as the bf16 weights; a model dir containing both model.safetensors and model-0000X-of-000Y.safetensors; extra files like original/consolidated checkpoints copied alongside shards.","solutions":["Inspect the examples in the message to see which files collide, then remove or relocate the extra variant","Keep only one precision/variant of the checkpoint in the directory","Use --transformer-weights-path or a clean --model path pointing at a single consistent checkpoint"],"exampleFix":"# before\nmodels/my-model/ (model-00001.safetensors, model-00002.safetensors, model.safetensors)\n# after\nmodels/my-model/ (model-00001.safetensors, model-00002.safetensors, model.safetensors.index.json)","handlingStrategy":"validation","validationCode":"import os\nfrom safetensors import safe_open\n\ndef no_duplicate_keys(model_path) -> bool:\n    files = [f for f in os.listdir(model_path) if f.endswith(\".safetensors\")]\n    seen, dupes = set(), set()\n    for f in files:\n        with safe_open(os.path.join(model_path, f), framework=\"pt\") as fh:\n            for k in fh.keys():\n                (dupes if k in seen else seen).add(k)\n    return not dupes","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep exactly one precision/variant of a model per directory","Never mix consolidated single-file checkpoints into sharded checkpoint directories"],"tags":["safetensors","duplicate-keys","checkpoint","weights"],"backgroundTag":"duplicate-model-weights","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}