Fix weight tying (#51665)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
@@ -1112,7 +1112,9 @@ def try_get_safetensors_metadata(
|
||||
|
||||
try:
|
||||
return with_retry(
|
||||
get_safetensors_metadata_partial, "Error retrieving safetensors"
|
||||
get_safetensors_metadata_partial,
|
||||
"Error retrieving safetensors",
|
||||
fatal_errors=(huggingface_hub.errors.NotASafetensorsRepoError,),
|
||||
)
|
||||
except Exception:
|
||||
return None
|
||||
@@ -1218,6 +1220,24 @@ def get_safetensors_params_metadata(
|
||||
return _read_safetensors_metadata_in_dir(Path(local_dir))
|
||||
|
||||
|
||||
@cache
|
||||
def checkpoint_has_lm_head(model: str, *, revision: str | None = None) -> bool | None:
|
||||
"""Whether the checkpoint contains an `lm_head` tensor of its own.
|
||||
|
||||
Args:
|
||||
model: Name or path of the model repository.
|
||||
revision: The specific model version to use.
|
||||
|
||||
Returns:
|
||||
`None` if the checkpoint contents could not be determined, for example
|
||||
because it is not stored as safetensors.
|
||||
"""
|
||||
metadata = get_safetensors_params_metadata(model, revision=revision)
|
||||
if not metadata:
|
||||
return None
|
||||
return any(name.endswith("lm_head.weight") for name in metadata)
|
||||
|
||||
|
||||
def _download_mistral_config_file(model, revision) -> dict:
|
||||
config_file_name = "params.json"
|
||||
config_dict = get_hf_file_to_dict(config_file_name, model, revision)
|
||||
|
||||
@@ -95,10 +95,23 @@ def with_retry(
|
||||
log_msg: str,
|
||||
max_retries: int = 2,
|
||||
retry_delay: int = 2,
|
||||
fatal_errors: tuple[type[Exception], ...] = (),
|
||||
) -> _R:
|
||||
"""Call `func`, retrying transient failures.
|
||||
|
||||
Args:
|
||||
func: The call to make.
|
||||
log_msg: Prefix for the message logged when a call fails.
|
||||
max_retries: How many times to call `func` before giving up.
|
||||
retry_delay: Seconds to wait after the first failure, doubled each time.
|
||||
fatal_errors: Exceptions that are a definitive answer rather than a
|
||||
transient failure. These are raised immediately, without logging.
|
||||
"""
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
return func()
|
||||
except fatal_errors:
|
||||
raise
|
||||
except Exception as e:
|
||||
if attempt == max_retries - 1:
|
||||
logger.error("%s: %s", log_msg, e)
|
||||
|
||||
Reference in New Issue
Block a user