Fix loftq None config for FastBaseModel (#2848)

add new validate_loftq_config to __all__
This commit is contained in:
DoubleMathew 2025-06-30 18:38:51 -05:00 committed by GitHub
commit bbdf55b40d
2 changed files with 63 additions and 0 deletions

View file

@ -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

View file

@ -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,