Helper variables and functions for integrating LLM Compressor with huggingface/transformers flows
Infer the recipe from the model_path.
Parameters:
Name | Type | Description | Default |
model_path | Union[str, Path] | The path to the model to load. It can be one of the following: - a path to the model directory - a path to the model file - Hugging face model ID | required |
Returns:
Type | Description |
Optional[str] | The path to the recipe file if found, None otherwise. |
Source code in src/llmcompressor/transformers/utils/helpers.py
| def infer_recipe_from_model_path(model_path: Union[str, Path]) -> Optional[str]:
"""
Infer the recipe from the model_path.
:param model_path: The path to the model to load. It can be one of the following:
- a path to the model directory
- a path to the model file
- 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
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 = (
os.path.dirname(model_path) if os.path.isfile(model_path) else model_path
)
recipe = os.path.join(model_path, RECIPE_FILE_NAME)
if os.path.isfile(recipe):
logger.info(f"Found recipe in the model_path: {recipe}")
return recipe
logger.debug(f"No recipe found in the model_path: {model_path}")
return None
# If the model path is a Hugging Face model ID
recipe = recipe_from_huggingface_model_id(hf_stub=model_path)
if recipe is None:
logger.debug("Failed to infer the recipe from the model_path")
return recipe
|
Determine if model from path is quantized based on the config
Parameters:
Name | Type | Description | Default |
path | str | path to the model or HF stub | required |
Returns:
Type | Description |
bool | True if config contains quantization_config from the given path |
Source code in src/llmcompressor/transformers/utils/helpers.py
| def is_model_ct_quantized_from_path(path: str) -> bool:
"""
Determine if model from path is quantized based
on the config
:param path: path to the model or HF stub
:return: True if config contains quantization_config from the given path
"""
config = AutoConfig.from_pretrained(path)
if config is not None:
if (
hasattr(config, "quantization_config")
and config.quantization_config["quant_method"] == "compressed-tensors"
):
return True
return False
|
Attempts to download the recipe from the Hugging Face model ID.
Parameters:
Name | Type | Description | Default |
hf_stub | str | Assumed to be the Hugging Face model ID. | required |
recipe_file_name | str | The name of the recipe file to download. Defaults to RECIPE_FILE_NAME. | RECIPE_FILE_NAME |
Returns:
Type | Description |
Optional[str] | A tuple: - The path to the recipe file if found, None otherwise. - True if hf_stub is a valid Hugging Face model ID, False otherwise. |
Source code in src/llmcompressor/transformers/utils/helpers.py
| def recipe_from_huggingface_model_id(
hf_stub: str, recipe_file_name: str = RECIPE_FILE_NAME
) -> Optional[str]:
"""
Attempts to download the recipe from the Hugging Face model ID.
:param hf_stub: Assumed to be the Hugging Face model ID.
:param recipe_file_name: The name of the recipe file to download.
Defaults to RECIPE_FILE_NAME.
:return: A tuple:
- The path to the recipe file if found, None otherwise.
- True if hf_stub is a valid Hugging Face model ID, False otherwise.
"""
model_id_url = os.path.join(HUGGINGFACE_CO_URL_HOME, hf_stub)
request = requests.head(model_id_url)
if request.status_code != 200:
logger.debug(
(
"hf_stub is not a valid Hugging Face model ID. ",
"Skipping recipe resolution.",
)
)
return None
try:
recipe = hf_hub_download(repo_id=hf_stub, filename=recipe_file_name)
logger.info(f"Found recipe: {recipe_file_name} for model ID: {hf_stub}.")
except Exception as e: # TODO: narrow acceptable exceptions
logger.debug(
(
f"Unable to find recipe {recipe_file_name} "
f"for model ID: {hf_stub}: {e}."
"Skipping recipe resolution."
)
)
recipe = None
return recipe
|