[Bugfix][Quant] Read safetensors headers of checkpoints with other file names (#60411)
Signed-off-by: Stefan Koncarevic <Stefan.Koncarevic@amd.com> Co-authored-by: Andreas Karatzas <akaratza@amd.com>
This commit is contained in:
co-authored by
Andreas Karatzas
parent
ee406adf0c
commit
814301b268
@@ -1355,6 +1355,30 @@ def _read_safetensors_metadata_in_dir(local_dir: Path) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _read_safetensors_metadata_in_repo(
|
||||
model: str,
|
||||
*,
|
||||
revision: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
api = hf_api()
|
||||
try:
|
||||
filenames = [
|
||||
filename
|
||||
for filename in api.list_repo_files(model, revision=revision)
|
||||
if filename.endswith(".safetensors")
|
||||
]
|
||||
return {
|
||||
param_name: asdict(info)
|
||||
for filename in filenames
|
||||
for param_name, info in api.parse_safetensors_file_metadata(
|
||||
model, filename, revision=revision
|
||||
).tensors.items()
|
||||
}
|
||||
except Exception as e:
|
||||
logger.debug("Could not read safetensors headers for %s: %s", model, e)
|
||||
return {}
|
||||
|
||||
|
||||
def get_safetensors_params_metadata(
|
||||
model: str,
|
||||
*,
|
||||
@@ -1372,6 +1396,12 @@ def get_safetensors_params_metadata(
|
||||
for param_name, info in file_mt.tensors.items()
|
||||
}
|
||||
|
||||
# `get_safetensors_metadata` only looks for `model.safetensors` and its
|
||||
# index, so older checkpoints naming their weights differently land here
|
||||
# even though their headers are perfectly readable.
|
||||
if metadata := _read_safetensors_metadata_in_repo(model, revision=revision):
|
||||
return metadata
|
||||
|
||||
# Hub fetch failed (e.g. 429, network unreachable). Fall back to the
|
||||
# local HF cache: weights may already be cached from a prior run, and
|
||||
# weight loading itself uses the same cache.
|
||||
@@ -1390,7 +1420,15 @@ def get_safetensors_params_metadata(
|
||||
str(e),
|
||||
)
|
||||
return {}
|
||||
return _read_safetensors_metadata_in_dir(Path(local_dir))
|
||||
|
||||
metadata = _read_safetensors_metadata_in_dir(Path(local_dir))
|
||||
if not metadata:
|
||||
logger.warning_once(
|
||||
"Could not retrieve safetensors metadata for %s "
|
||||
"(Hub fetch failed and the cached snapshot holds no weights yet).",
|
||||
model,
|
||||
)
|
||||
return metadata
|
||||
|
||||
|
||||
@cache
|
||||
|
||||
Reference in New Issue
Block a user