Skip to content

Commit 5978c4e

Browse files
author
rubik
committed
issue/467 [BUG]: enforce safetensors index-based weight loading for robustness
Replace fragile glob with model.safetensors.index.json lookup to handle non-standard names (e.g., Mistral-Large-Instruct). Includes glob fallback
1 parent 7a16c6d commit 5978c4e

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

‎python/infinilm/infer_engine.py‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,25 @@
1212
import json
1313
import os
1414

15+
_MODEL_DEFAULTS = {
16+
"gpt2": {"torch_dtype": "float32"},
17+
"mistral": {"torch_dtype": "bfloat16"},
18+
}
19+
20+
_GLOBAL_DEFAULTS = {
21+
"torch_dtype": "bfloat16"
22+
}
23+
24+
def _apply_torch_dtype_defaults(config: dict) -> dict:
25+
if config.get("torch_dtype") is None:
26+
config["torch_dtype"] = (
27+
config.get("dtype") or
28+
_MODEL_DEFAULTS.get(config.get("model_type"), {}).get("torch_dtype") or
29+
_GLOBAL_DEFAULTS["torch_dtype"]
30+
)
31+
return config
1532

16-
def normalize_hf_config_for_infinilm(config_dict):
33+
def _normalize_videonsa_config(config_dict):
1734
model_type = config_dict.get("model_type")
1835

1936
if model_type == "qwen2_5_vl" and config_dict.get("architectures") == [
@@ -36,24 +53,20 @@ def normalize_hf_config_for_infinilm(config_dict):
3653

3754
return config_dict
3855

39-
4056
def read_hf_config(model_path):
4157
config_path = os.path.join(model_path, "config.json")
4258
with open(config_path, "r") as f:
4359
config_dict = json.load(f)
4460

45-
if (
46-
config_dict.get("model_type") == "gpt2"
47-
and config_dict.get("torch_dtype") is None
48-
and config_dict.get("dtype") is None
49-
):
50-
config_dict["torch_dtype"] = "float32"
5161
if "model_type" not in config_dict:
5262
raise ValueError(
5363
f"`model_type` is not specified in the config file `{config_path}`."
5464
)
55-
return normalize_hf_config_for_infinilm(config_dict)
5665

66+
config_dict = _apply_torch_dtype_defaults(config_dict)
67+
config_dict = _normalize_videonsa_config(config_dict)
68+
69+
return config_dict
5770

5871
# config.json (required) defines model architecture, while generation_config.json
5972
# (optional) defines generation behavior. They are kept as separate readers

‎python/infinilm/modeling_utils.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,21 @@ def load_model_state_dict_by_file(
183183
already_loaded_keys = []
184184
embed_tokens_torch_unscaled = None
185185

186-
file_list = glob.glob(os.path.join(model_path, "*.safetensors"))
186+
index_file_path = os.path.join(model_path, "model.safetensors.index.json")
187+
if os.path.exists(index_file_path):
188+
# Priority 1: If the index file exists, strictly load exactly what it maps to.
189+
# This handles all standard sharded models perfectly, regardless of their actual prefix.
190+
print(f"Found index file: {index_file_path}. Loading shards by index.")
191+
with open(index_file_path, "r") as f:
192+
index_data = json.load(f)
193+
weight_map = index_data.get("weight_map", {})
194+
unique_filenames = set(weight_map.values())
195+
file_list = [os.path.join(model_path, fname) for fname in unique_filenames]
196+
else:
197+
# Priority 2: If no index file, scan all safetensors files.
198+
print("No index file found. Scanning all safetensors files...")
199+
file_list = glob.glob(os.path.join(model_path, "*.safetensors"))
200+
187201
if len(file_list) > 0:
188202
for file_path in tqdm(file_list, desc="Processing files"):
189203
tqdm.write(f"Processing: {os.path.basename(file_path)}")

0 commit comments

Comments
 (0)