Skip to content

Fix safetensors detection in try_collect_weight_map (missing dot in glob)#2463

Open
Osamaali313 wants to merge 1 commit into
huggingface:mainfrom
Osamaali313:fix/safetensors-glob-pattern
Open

Fix safetensors detection in try_collect_weight_map (missing dot in glob)#2463
Osamaali313 wants to merge 1 commit into
huggingface:mainfrom
Osamaali313:fix/safetensors-glob-pattern

Conversation

@Osamaali313

Copy link
Copy Markdown

Problem

try_collect_weight_map (optimum/fx/parallelization/utils.py) builds its weight-file glob patterns with the safetensors dot missing:

use_safetensors, weight_patterns = False, ["*safetensors", "*.bin"]
for pattern in weight_patterns:
    if len(glob.glob(os.path.join(folder_path, pattern))) > 0:
        use_safetensors = pattern == "*.safetensors"   # compares against the DOTTED form
        break

"*safetensors" still matches real files (glob("*safetensors") matches model.safetensors), so the loop breaks on it — but use_safetensors = ("*safetensors" == "*.safetensors") is always False, even for a safetensors-only model.

Impact

With use_safetensors stuck False, for a safetensors model:

  • index_path uses WEIGHTS_INDEX_NAME (pytorch_model.bin.index.json) instead of SAFE_WEIGHTS_INDEX_NAME → the real model.safetensors.index.json is never read.
  • weight_files globs *.bin → empty for a safetensors model.
  • convert_bin_to_safetensors runs on every call (acquires a file lock; no-op with no bin files).

try_collect_weight_map is called from optimum/fx/parallelization/api.py in the FX tensor-parallelism parallelize_model flow.

Evidence (siblings prove intent)

The three other references in the same function all use the dotted "*.safetensors":

  • use_safetensors = pattern == "*.safetensors"
  • glob.glob(os.path.join(folder_path, "*.safetensors" if use_safetensors else "*.bin"))
  • glob.glob(os.path.join(folder_path, "*.safetensors"))

The sibling "*.bin" entry in the same list has its dot; only the safetensors entry is missing it.

Reproduction

Synthetic sharded-safetensors dir (model-0000{1,2}-of-00002.safetensors + model.safetensors.index.json):

use_safetensors index used weight_files
before (*safetensors) False pytorch_model.bin.index.json []
after (*.safetensors) True model.safetensors.index.json both shards

Fix

use_safetensors, weight_patterns = False, ["*.safetensors", "*.bin"]

…lob)

`try_collect_weight_map` builds its weight-file patterns as
`["*safetensors", "*.bin"]` — the safetensors entry is missing its dot.
The pattern still matches real files (`glob("*safetensors")` matches
`model.safetensors`), so the loop breaks on it, but the next line sets

    use_safetensors = pattern == "*.safetensors"

which compares against the dotted form and is therefore always False, even
for a safetensors-only model.

With `use_safetensors` stuck False, the code reads `WEIGHTS_INDEX_NAME`
(`pytorch_model.bin.index.json`) instead of `SAFE_WEIGHTS_INDEX_NAME`,
globs `*.bin` (empty for a safetensors model), and runs
`convert_bin_to_safetensors` needlessly. The three sibling references in the
same function (the `== "*.safetensors"` comparison and the `*.safetensors`
globs) all use the dotted form, confirming the intended pattern. Add the
missing dot.
Copilot AI review requested due to automatic review settings July 17, 2026 19:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants