llmcompressor.utils.transformers
Functions:
-
get_embeddings–Returns input and output embeddings of a model. If
get_input_embeddings/ -
targets_embeddings–Returns True if the given targets target the word embeddings of the model
-
untie_word_embeddings–Untie word embeddings, if possible. This function raises a warning if
-
warn_inference_mode_forwards–Warn if any submodule's forward method is decorated with
get_embeddings
Returns input and output embeddings of a model. If get_input_embeddings/
get_output_embeddings is not implemented on the model, then None will be returned
instead.
Parameters:
-
model(PreTrainedModel) –model to get embeddings from
Returns:
-
tuple[Module | None, Module | None]–tuple of containing embedding modules or none
Source code in src/llmcompressor/utils/transformers.py
targets_embeddings
targets_embeddings(
model: PreTrainedModel,
targets: NamedModules,
check_input: bool = True,
check_output: bool = True,
) -> bool
Returns True if the given targets target the word embeddings of the model
Parameters:
-
model(PreTrainedModel) –containing word embeddings
-
targets(NamedModules) –named modules to check
-
check_input(bool, default:True) –whether to check if input embeddings are targeted
-
check_output(bool, default:True) –whether to check if output embeddings are targeted
Returns:
-
bool–True if embeddings are targeted, False otherwise
Source code in src/llmcompressor/utils/transformers.py
untie_word_embeddings
Untie word embeddings, if possible. This function raises a warning if embeddings cannot be found in the model definition.
The model config will be updated to reflect that embeddings are now untied
Parameters:
-
model(PreTrainedModel) –transformers model containing word embeddings
Source code in src/llmcompressor/utils/transformers.py
warn_inference_mode_forwards
Warn if any submodule's forward method is decorated with @torch.inference_mode(). Any tensor produced during such a call stays tagged as an inference tensor even after the call returns. If a later stage of calibration, for example GPTQ's weight writeback or the offload cache, tries to update a tensor derived from that call in place, the update crashes with "Inplace update to inference tensor outside InferenceMode is not allowed", potentially after a long compression run has already made it through most of the model.
Parameters:
-
model(Module) –model to scan for inference_mode decorated forwards