From bbdf55b40d39c6a4db4b34387bfe6a2425d2bf8c Mon Sep 17 00:00:00 2001 From: DoubleMathew Date: Mon, 30 Jun 2025 18:38:51 -0500 Subject: [PATCH] Fix loftq None config for FastBaseModel (#2848) add new validate_loftq_config to __all__ --- unsloth/models/_utils.py | 61 ++++++++++++++++++++++++++++++++++++++++ unsloth/models/vision.py | 2 ++ 2 files changed, 63 insertions(+) diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py index 154b437525..a71accd2f3 100644 --- a/unsloth/models/_utils.py +++ b/unsloth/models/_utils.py @@ -65,6 +65,7 @@ __all__ = [ "process_vision_info", "unsloth_compile_transformers", "patch_fast_lora", + "validate_loftq_config", ] import torch @@ -1307,3 +1308,63 @@ if USE_MODELSCOPE: raise ImportError(f'You are using the modelscope hub, please install modelscope by `pip install modelscope -U`') pass pass + + +def validate_loftq_config(loftq_config, lora_dropout, bias, init_lora_weights, model): + from peft import LoraConfig + + if loftq_config is None: loftq_config = {} + + signature = str(inspect.signature(LoraConfig)) + SUPPORTS_LOFTQ = "loftq_config" in signature + + if lora_dropout != 0: + logger.warning_once( + f"Unsloth: Dropout = 0 is supported for fast patching. You are using dropout = {lora_dropout}.\n"\ + f"Unsloth will patch all other layers, except LoRA matrices, causing a performance hit." + ) + pass + + if bias != "none": + logger.warning_once( + f"Unsloth: bias = `none` is supported for fast patching. You are using bias = {bias}.\n"\ + f"Unsloth will patch all other layers, except LoRA matrices, causing a performance hit." + ) + pass + + if not (type(init_lora_weights) is bool or \ + init_lora_weights == "gaussian" or init_lora_weights == "loftq"): + raise ValueError( + 'Unsloth: `init_lora_weights` must be either [True, False, "gaussian", "loftq"].' + ) + pass + + if init_lora_weights == "loftq": + + if not SUPPORTS_LOFTQ: + import peft + raise RuntimeError( + f"Unsloth: Your PEFT version of {peft.__version__} does not support LoftQ init.\n"\ + "Please install PEFT 0.7.2 or higher.\n"\ + "You can also install from source: `pip install git+https://github.com/huggingface/peft.git" + ) + pass + + if loftq_config == {}: + from peft import LoftQConfig + logger.warning_once( + "Unsloth: init_lora_weights = `loftq` is set, but `loftq_config` is None.\n"\ + "We shall use `loftq_config = LoftQConfig(loftq_bits = 4, loftq_iter = 1)`." + ) + loftq_config = LoftQConfig(loftq_bits = 4, loftq_iter = 1) + pass + + if hasattr(model.config, "quantization_config"): + raise ValueError( + "Unsloth: You are using `loftq` init, yet `load_in_4bit = True` was set.\n"\ + "Reload your model without any quantization by setting `load_in_4bit = False`." + ) + pass + pass + + return loftq_config \ No newline at end of file diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py index fb0a54489a..525e562abb 100644 --- a/unsloth/models/vision.py +++ b/unsloth/models/vision.py @@ -612,6 +612,8 @@ class FastBaseModel: torch.xpu.empty_cache() pass max_seq_length = model.max_seq_length + # if we pass loftq_config = None we will get an error + loftq_config = validate_loftq_config(loftq_config, lora_dropout, bias, init_lora_weights, model) lora_config = LoraConfig( r = r, lora_alpha = lora_alpha,