MCPcopy
hub / github.com/huggingface/transformers / unwrap_peft_model

Function unwrap_peft_model

src/transformers/trainer_utils.py:75–99  ·  view source on GitHub ↗

Extract the base model from a PEFT-wrapped model. If the model is not a PEFT model, returns it unchanged. Otherwise, attempts to unwrap the base model using ``get_base_model()`` or the ``base_model.model`` attribute. Args: model: The model to unwrap. Returns:

(model)

Source from the content-addressed store, hash-verified

73
74
75def unwrap_peft_model(model):
76 """
77 Extract the base model from a PEFT-wrapped model.
78
79 If the model is not a PEFT model, returns it unchanged. Otherwise, attempts to
80 unwrap the base model using ``get_base_model()`` or the ``base_model.model`` attribute.
81
82 Args:
83 model: The model to unwrap.
84
85 Returns:
86 The unwrapped base model.
87
88 Raises:
89 AttributeError: If the model is a PEFT model but cannot be unwrapped safely.
90 """
91 if not _is_peft_model(model):
92 return model
93 if hasattr(model, "get_base_model"):
94 return model.get_base_model()
95 elif hasattr(model, "base_model") and hasattr(model.base_model, "model"):
96 # PeftMixedModel do not provide a `get_base_model` method
97 return model.base_model.model
98 else:
99 raise AttributeError("Cannot extract base model safely from this PEFT wrapper.")
100
101
102def validate_quantization_for_training(model):

Callers 2

__init__Method · 0.85
apply_liger_kernelFunction · 0.85

Calls 1

_is_peft_modelFunction · 0.85

Tested by

no test coverage detected