Skip to content

Commit

Permalink
[Safetensors] Relax missing metadata constraint (#1151)
Browse files Browse the repository at this point in the history
* [Safetensors] Relax missing metadata constraint

* correcct

* char limit
  • Loading branch information
patrickvonplaten authored Mar 6, 2023
1 parent 0aae1e9 commit 9dc5b34
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/accelerate/utils/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import gc
import json
import logging
import os
import re
import shutil
Expand All @@ -35,6 +36,9 @@
WEIGHTS_INDEX_NAME = "pytorch_model.bin.index.json"


logger = logging.getLogger(__name__)


def convert_file_size_to_int(size: Union[int, str]):
"""
Converts a size expressed as a string with digits an unit (like `"5MB"`) to an integer (in bytes).
Expand Down Expand Up @@ -667,6 +671,14 @@ def load_state_dict(checkpoint_file, device_map=None):
with safe_open(checkpoint_file, framework="pt") as f:
metadata = f.metadata()
weight_names = f.keys()

if metadata is None:
logger.warn(
f"The safetensors archive passed at {checkpoint_file} does not contain metadata. "
"Make sure to save your model with the `save_pretrained` method. Defaulting to 'pt' metadata."
)
metadata = {"format": "pt"}

if metadata.get("format") not in ["pt", "tf", "flax"]:
raise OSError(
f"The safetensors archive passed at {checkpoint_file} does not contain the valid metadata. Make sure "
Expand Down

0 comments on commit 9dc5b34

Please sign in to comment.