sgl-project/sglang · critical · ValueError

Duplicate tensor names detected across safetensors files. Re

Error message

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.

What it means

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.

Source

Thrown at python/sglang/multimodal_gen/runtime/loader/weight_utils.py:205

            corrupted_files.append(st_file)

    return corrupted_files, duplicate_files_by_key


def _raise_if_duplicate_safetensors_keys(
    duplicate_files_by_key: dict[str, set[str]],
) -> None:
    if not duplicate_files_by_key:
        return

    examples = []
    for key in sorted(duplicate_files_by_key)[:8]:
        files = ", ".join(
            sorted(os.path.basename(p) for p in duplicate_files_by_key[key])
        )
        examples.append(f"{key} [{files}]")

    raise ValueError(
        "Duplicate tensor names detected across safetensors files. Refusing to load "
        "because final weights would depend on file or streamer ordering. "
        f"Found {len(duplicate_files_by_key)} duplicate tensor name(s). "
        f"Examples: {examples}. "
        "This usually means multiple precision variants or consolidated+sharded "
        "checkpoints were passed together."
    )


def safetensors_weights_iterator(
    hf_weights_files: list[str],
    to_cpu: bool = True,
    use_runai_model_streamer: bool | None = None,
    key_filter: Callable[[str], bool] | None = None,
    clone_streamed_tensors: bool = True,
    weight_load_plan: WeightLoadPlan | None = None,
) -> Generator[tuple[str, torch.Tensor], None, None]:
    """Iterate over the weights in the model safetensor files."""

View on GitHub (pinned to 0132848349)

Solutions

  1. Inspect the examples in the message to see which files collide, then remove or relocate the extra variant
  2. Keep only one precision/variant of the checkpoint in the directory
  3. Use --transformer-weights-path or a clean --model path pointing at a single consistent checkpoint

Example fix

# before
models/my-model/ (model-00001.safetensors, model-00002.safetensors, model.safetensors)
# after
models/my-model/ (model-00001.safetensors, model-00002.safetensors, model.safetensors.index.json)
Defensive patterns

Strategy: validation

Validate before calling

import os
from safetensors import safe_open

def no_duplicate_keys(model_path) -> bool:
    files = [f for f in os.listdir(model_path) if f.endswith(".safetensors")]
    seen, dupes = set(), set()
    for f in files:
        with safe_open(os.path.join(model_path, f), framework="pt") as fh:
            for k in fh.keys():
                (dupes if k in seen else seen).add(k)
    return not dupes

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/f6542cdb6d6d3e4b. Report an issue: GitHub.