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)
| 73 | |
| 74 | |
| 75 | def 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 | |
| 102 | def validate_quantization_for_training(model): |
no test coverage detected