From e7608e26ea5278f9c7f4a9b5b28045360b3cc869 Mon Sep 17 00:00:00 2001 From: HDCharles Date: Thu, 20 Nov 2025 20:04:43 +0000 Subject: [PATCH 1/6] [bugfix] saving model without model.name_or_path Summary: the way models were saved assumed that model.name_or_path was populated but this is not always the case. This PR relaxes this assumption so that the model can be saved. Signed-off-by: HDCharles --- src/llmcompressor/pytorch/model_load/helpers.py | 3 ++- src/llmcompressor/transformers/utils/helpers.py | 15 ++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/llmcompressor/pytorch/model_load/helpers.py b/src/llmcompressor/pytorch/model_load/helpers.py index b4390afc72..3399506808 100644 --- a/src/llmcompressor/pytorch/model_load/helpers.py +++ b/src/llmcompressor/pytorch/model_load/helpers.py @@ -144,7 +144,8 @@ def load_safetensors_state_dict(file_path: str) -> Dict[str, torch.Tensor]: def copy_python_files_from_model_cache(model, save_path: str): config = model.config cache_path = None - if hasattr(config, "_name_or_path"): + print("GGG", config._name_or_path) + if hasattr(config, "_name_or_path") and len(config._name_or_path)>0: import os import shutil diff --git a/src/llmcompressor/transformers/utils/helpers.py b/src/llmcompressor/transformers/utils/helpers.py index 1834c19b00..558b8649db 100644 --- a/src/llmcompressor/transformers/utils/helpers.py +++ b/src/llmcompressor/transformers/utils/helpers.py @@ -14,6 +14,7 @@ hf_hub_download, try_to_load_from_cache, ) +from huggingface_hub.errors import HFValidationError from loguru import logger from transformers import AutoConfig @@ -58,7 +59,7 @@ def infer_recipe_from_model_path(model_path: Union[str, Path]) -> Optional[str]: :return: The path to the recipe file if found, None otherwise. """ model_path = model_path.as_posix() if isinstance(model_path, Path) else model_path - + print("DD", model_path, os.path.isdir(model_path) , os.path.isfile(model_path)) if os.path.isdir(model_path) or os.path.isfile(model_path): # Model path is a local path to the model directory or file model_path = ( @@ -73,10 +74,14 @@ def infer_recipe_from_model_path(model_path: Union[str, Path]) -> Optional[str]: return None # Try to resolve HF model ID to cached location first - cached_recipe = try_to_load_from_cache( - repo_id=model_path, - filename=RECIPE_FILE_NAME, - ) + cached_recipe = None + try: + cached_recipe = try_to_load_from_cache( + repo_id=model_path, + filename=RECIPE_FILE_NAME, + ) + except HFValidationError as e: + logger.info(f"unable to get recipe from hf_hub, raised:\n{e}") if cached_recipe and cached_recipe is not _CACHED_NO_EXIST: # Recipe found in cached model From a460f02c64f9656ff18a89918bd58b68e860a124 Mon Sep 17 00:00:00 2001 From: HDCharles Date: Thu, 20 Nov 2025 20:08:48 +0000 Subject: [PATCH 2/6] formatting Summary Signed-off-by: HDCharles --- src/llmcompressor/pytorch/model_load/helpers.py | 3 +-- src/llmcompressor/transformers/utils/helpers.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/llmcompressor/pytorch/model_load/helpers.py b/src/llmcompressor/pytorch/model_load/helpers.py index 3399506808..127a61230b 100644 --- a/src/llmcompressor/pytorch/model_load/helpers.py +++ b/src/llmcompressor/pytorch/model_load/helpers.py @@ -144,8 +144,7 @@ def load_safetensors_state_dict(file_path: str) -> Dict[str, torch.Tensor]: def copy_python_files_from_model_cache(model, save_path: str): config = model.config cache_path = None - print("GGG", config._name_or_path) - if hasattr(config, "_name_or_path") and len(config._name_or_path)>0: + if hasattr(config, "_name_or_path") and len(config._name_or_path) > 0: import os import shutil diff --git a/src/llmcompressor/transformers/utils/helpers.py b/src/llmcompressor/transformers/utils/helpers.py index 558b8649db..8181dced3b 100644 --- a/src/llmcompressor/transformers/utils/helpers.py +++ b/src/llmcompressor/transformers/utils/helpers.py @@ -59,7 +59,7 @@ def infer_recipe_from_model_path(model_path: Union[str, Path]) -> Optional[str]: :return: The path to the recipe file if found, None otherwise. """ model_path = model_path.as_posix() if isinstance(model_path, Path) else model_path - print("DD", model_path, os.path.isdir(model_path) , os.path.isfile(model_path)) + if os.path.isdir(model_path) or os.path.isfile(model_path): # Model path is a local path to the model directory or file model_path = ( From 91ee4ba01a4068dbdc6e56ef2d68c004e73ee7c6 Mon Sep 17 00:00:00 2001 From: HDCharles Date: Thu, 20 Nov 2025 20:10:39 +0000 Subject: [PATCH 3/6] logger debug Summary Signed-off-by: HDCharles --- src/llmcompressor/transformers/utils/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llmcompressor/transformers/utils/helpers.py b/src/llmcompressor/transformers/utils/helpers.py index 8181dced3b..45508d93f7 100644 --- a/src/llmcompressor/transformers/utils/helpers.py +++ b/src/llmcompressor/transformers/utils/helpers.py @@ -81,7 +81,7 @@ def infer_recipe_from_model_path(model_path: Union[str, Path]) -> Optional[str]: filename=RECIPE_FILE_NAME, ) except HFValidationError as e: - logger.info(f"unable to get recipe from hf_hub, raised:\n{e}") + logger.debug(f"unable to get recipe from hf_hub, raised:\n{e}") if cached_recipe and cached_recipe is not _CACHED_NO_EXIST: # Recipe found in cached model From 790413727bc9161fb35145b59806e0f396571aa0 Mon Sep 17 00:00:00 2001 From: HDCharles Date: Thu, 20 Nov 2025 21:37:05 +0000 Subject: [PATCH 4/6] change to exit early Summary Signed-off-by: HDCharles --- src/llmcompressor/pytorch/model_load/helpers.py | 2 +- src/llmcompressor/transformers/utils/helpers.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/llmcompressor/pytorch/model_load/helpers.py b/src/llmcompressor/pytorch/model_load/helpers.py index 127a61230b..80aeeea7b8 100644 --- a/src/llmcompressor/pytorch/model_load/helpers.py +++ b/src/llmcompressor/pytorch/model_load/helpers.py @@ -144,7 +144,7 @@ def load_safetensors_state_dict(file_path: str) -> Dict[str, torch.Tensor]: def copy_python_files_from_model_cache(model, save_path: str): config = model.config cache_path = None - if hasattr(config, "_name_or_path") and len(config._name_or_path) > 0: + if hasattr(config, "_name_or_path") and len(config._name_or_path.strip()) > 0: import os import shutil diff --git a/src/llmcompressor/transformers/utils/helpers.py b/src/llmcompressor/transformers/utils/helpers.py index 45508d93f7..371261e38a 100644 --- a/src/llmcompressor/transformers/utils/helpers.py +++ b/src/llmcompressor/transformers/utils/helpers.py @@ -58,7 +58,12 @@ def infer_recipe_from_model_path(model_path: Union[str, Path]) -> Optional[str]: - Hugging face model ID :return: The path to the recipe file if found, None otherwise. """ - model_path = model_path.as_posix() if isinstance(model_path, Path) else model_path + model_path = ( + model_path.as_posix() if isinstance(model_path, Path) else model_path.strip() + ) + if model_path == "": + logger.debug("got path_or_name=" "unable to find recipe") + return None if os.path.isdir(model_path) or os.path.isfile(model_path): # Model path is a local path to the model directory or file From 2e073603acca106803a26a985a57805be5e0964d Mon Sep 17 00:00:00 2001 From: HDCharles Date: Thu, 20 Nov 2025 21:37:57 +0000 Subject: [PATCH 5/6] remove old soln Summary Signed-off-by: HDCharles --- src/llmcompressor/transformers/utils/helpers.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/llmcompressor/transformers/utils/helpers.py b/src/llmcompressor/transformers/utils/helpers.py index 371261e38a..5749e48c2c 100644 --- a/src/llmcompressor/transformers/utils/helpers.py +++ b/src/llmcompressor/transformers/utils/helpers.py @@ -79,14 +79,10 @@ def infer_recipe_from_model_path(model_path: Union[str, Path]) -> Optional[str]: return None # Try to resolve HF model ID to cached location first - cached_recipe = None - try: - cached_recipe = try_to_load_from_cache( - repo_id=model_path, - filename=RECIPE_FILE_NAME, - ) - except HFValidationError as e: - logger.debug(f"unable to get recipe from hf_hub, raised:\n{e}") + cached_recipe = try_to_load_from_cache( + repo_id=model_path, + filename=RECIPE_FILE_NAME, + ) if cached_recipe and cached_recipe is not _CACHED_NO_EXIST: # Recipe found in cached model From 393fed0e5bca08a39e275be476e33f6b54f2e756 Mon Sep 17 00:00:00 2001 From: HDCharles Date: Thu, 20 Nov 2025 21:39:54 +0000 Subject: [PATCH 6/6] remove unneeded import Summary Signed-off-by: HDCharles --- src/llmcompressor/transformers/utils/helpers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/llmcompressor/transformers/utils/helpers.py b/src/llmcompressor/transformers/utils/helpers.py index 5749e48c2c..9a3b9059d3 100644 --- a/src/llmcompressor/transformers/utils/helpers.py +++ b/src/llmcompressor/transformers/utils/helpers.py @@ -14,7 +14,6 @@ hf_hub_download, try_to_load_from_cache, ) -from huggingface_hub.errors import HFValidationError from loguru import logger from transformers import AutoConfig