[bug fix] has_fp8_weights_in_checkpoint: handle HF repo IDs, not just local paths (#23542)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
9b2f7f8a91
commit
579bd0b152
@@ -2712,33 +2712,52 @@ def has_fp8_weights_in_checkpoint(model_path: str) -> bool:
|
|||||||
experts without declaring it in quantization_config, while other models
|
experts without declaring it in quantization_config, while other models
|
||||||
sharing the same architecture (e.g. Moonlight) are purely BF16.
|
sharing the same architecture (e.g. Moonlight) are purely BF16.
|
||||||
|
|
||||||
Only reads the safetensors header (a few KB of JSON), not the actual weights.
|
Accepts a local directory or a HuggingFace repo ID. For remote repos, only
|
||||||
|
safetensors headers (a few KB) are fetched via byte-range reads; full
|
||||||
|
shards are never downloaded.
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
try:
|
try:
|
||||||
index_path = os.path.join(model_path, "model.safetensors.index.json")
|
if os.path.isdir(model_path):
|
||||||
if os.path.exists(index_path):
|
|
||||||
with open(index_path) as f:
|
def _open(name):
|
||||||
index = json.load(f)
|
return open(os.path.join(model_path, name), "rb")
|
||||||
weight_map = index.get("weight_map", {})
|
|
||||||
expert_files = {
|
def _exists(name):
|
||||||
v for k, v in weight_map.items() if "experts" in k and "weight" in k
|
return os.path.exists(os.path.join(model_path, name))
|
||||||
}
|
|
||||||
shard_file = next(iter(expert_files), None) or next(
|
else:
|
||||||
iter(set(weight_map.values())), None
|
from huggingface_hub import HfFileSystem
|
||||||
|
|
||||||
|
fs = HfFileSystem()
|
||||||
|
|
||||||
|
def _open(name):
|
||||||
|
return fs.open(f"{model_path}/{name}", "rb")
|
||||||
|
|
||||||
|
def _exists(name):
|
||||||
|
return fs.exists(f"{model_path}/{name}")
|
||||||
|
|
||||||
|
if _exists("model.safetensors.index.json"):
|
||||||
|
with _open("model.safetensors.index.json") as f:
|
||||||
|
weight_map = json.loads(f.read()).get("weight_map", {})
|
||||||
|
expert_files = sorted(
|
||||||
|
{v for k, v in weight_map.items() if "experts" in k and "weight" in k}
|
||||||
|
)
|
||||||
|
shard_file = (
|
||||||
|
expert_files[0]
|
||||||
|
if expert_files
|
||||||
|
else next(iter(sorted(set(weight_map.values()))), None)
|
||||||
)
|
)
|
||||||
if shard_file is None:
|
if shard_file is None:
|
||||||
return False
|
return False
|
||||||
shard_path = os.path.join(model_path, shard_file)
|
elif _exists("model.safetensors"):
|
||||||
|
shard_file = "model.safetensors"
|
||||||
else:
|
else:
|
||||||
shard_path = os.path.join(model_path, "model.safetensors")
|
|
||||||
|
|
||||||
if not os.path.exists(shard_path):
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
with open(shard_path, "rb") as f:
|
with _open(shard_file) as f:
|
||||||
header_len = struct.unpack("<Q", f.read(8))[0]
|
header_len = struct.unpack("<Q", f.read(8))[0]
|
||||||
header = json.loads(f.read(header_len))
|
header = json.loads(f.read(header_len))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user