Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Reject Activated LoRA adapters instead of training them as plain LoRA (#13089)

Daniel Han5333c969f08a
diff --git a/tests/test_reject_alora.py b/tests/test_reject_alora.py
new file mode 100644
index 00000000000..b858be132b6
--- /dev/null
+++ b/tests/test_reject_alora.py
@@ -0,0 +1,89 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
+
+from types import SimpleNamespace
+
+import pytest
+
+import unsloth # noqa: F401
+from peft import LoraConfig
+from unsloth.models._utils import reject_alora
+from unsloth.models.llama import FastLlamaModel
+from unsloth.models.vision import FastBaseModel
+
+
+def _model(**extra):
+ config = LoraConfig(r = 8, target_modules = ["q_proj"], task_type = "CAUSAL_LM", **extra)
+ return SimpleNamespace(peft_config = {"default": config})
+
+
+ALORA = dict(alora_invocation_tokens = [151644, 77091, 198])
+
+
+def test_alora_is_rejected():
+ with pytest.raises(NotImplementedError, match = "alora_invocation_tokens"):
+ reject_alora(_model(**ALORA))
+
+
+def test_alora_in_any_adapter_is_rejected():
+ model = _model()
+ model.peft_config["second"] = _model(**ALORA).peft_config["default"]
+ with pytest.raises(NotImplementedError):
+ reject_alora(model)
+
+
+ "model", [_model(), _model(use_dora = True), SimpleNamespace(), SimpleNamespace(peft_config = None)]
+)
+def test_other_models_pass(model):
+ reject_alora(model)
+
+
+ "patch",
+ [FastLlamaModel.patch_peft_model, FastBaseModel.post_patch_model],
+ ids = ["patch_peft_model", "post_patch_model"],
+)
+def test_patch_entry_points_reject_alora(patch):
+ with pytest.raises(NotImplementedError, match = "alora_invocation_tokens"):
+ patch(_model(**ALORA))
+
+
+def _tiny_llama():
+ from transformers import LlamaConfig, LlamaForCausalLM
+ config = LlamaConfig(
+ vocab_size = 64,
+ hidden_size = 16,
+ intermediate_size = 32,
+ num_hidden_layers = 1,
+ num_attention_heads = 2,
+ num_key_value_heads = 2,
+ )
+ return LlamaForCausalLM(config)
+
+
+def test_requested_alora_is_rejected():
+ with pytest.raises(NotImplementedError, match = "alora_invocation_tokens"):
+ reject_alora(_model(), ALORA["alora_invocation_tokens"])
+
+
+ "stored, requested", [(ALORA, {}), ({}, ALORA)], ids = ["stored", "requested"]
+)
+def test_prewrapped_peft_model_is_rejected(stored, requested):
+ from peft import get_peft_model
+ model = get_peft_model(_tiny_llama(), _model(**stored).peft_config["default"])
+ with pytest.raises(NotImplementedError, match = "alora_invocation_tokens"):
+ FastLlamaModel.get_peft_model(model, r = 8, target_modules = ["q_proj"], **requested)
+
+
+ "get_peft_model",
+ [FastLlamaModel.get_peft_model, FastBaseModel.get_peft_model],
+ ids = ["llama", "base"],
+)
+def test_requested_alora_rejected_before_wrapping(get_peft_model):
+ model = _tiny_llama()
+ with pytest.raises(NotImplementedError, match = "alora_invocation_tokens"):
+ get_peft_model(model, r = 8, target_modules = ["q_proj"], **ALORA)
+ assert not hasattr(model, "peft_config")
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index b17ef82ce4d..9b76c8e5caf 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -77,6 +77,7 @@ __all__ = [
"validate_loftq_config",
"validate_init_lora_weights",
"validate_init_target_parameters",
+ "reject_alora",
"RESIDUAL_INIT_LORA_WEIGHTS",
"snapshot_residual_lora_init",
"lora_relative_to_original_base",
@@ -5958,6 +5959,19 @@ def validate_init_target_parameters(init_lora_weights, target_parameters):
)
+def reject_alora(model, requested = None):
+ # Unsloth's LoRA forwards never apply aLoRA's invocation offsets, so the adapter would fire on every token (#2471).
+ configs = (getattr(model, "peft_config", None) or {}).values()
+ if requested is not None or any(
+ getattr(c, "alora_invocation_tokens", None) is not None for c in configs
+ ):
+ raise NotImplementedError(
+ "Unsloth: Activated LoRA (`alora_invocation_tokens`) is not supported yet. "
+ "Unsloth would apply the adapter to every token, not only after the invocation tokens.\n"
+ "Use plain `transformers` + `peft` for aLoRA, or drop `alora_invocation_tokens` for a normal LoRA."
+ )
+
+
def validate_init_lora_weights(
init_lora_weights,
model,
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 87e9595b67b..1df32c272dd 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -3664,6 +3664,8 @@ class FastLlamaModel:
):
offload_layers = legacy_offload_layers(kwargs, offload_layers)
prefetch_depth = prefetch_depth_arg(kwargs)
+ # A pre-wrapped model returns early below, before patch_peft_model; reject before PEFT injects layers.
+ reject_alora(model, kwargs.get("alora_invocation_tokens"))
# The flag reflects the LAST load, not this model.
_text_seq2seq = _is_text_seq2seq_config(getattr(model, "config", None))
if os.environ.get("UNSLOTH_USE_NEW_MODEL", "0") == "1" or _text_seq2seq:
@@ -4252,6 +4254,7 @@ class FastLlamaModel:
# module flags every GRPO step, and TrainingArguments defaults it to False, which would silently
# disable it at train time (#4735). Recorded here so loader.py's from_pretrained path is covered.
model._unsloth_gradient_checkpointing = use_gradient_checkpointing
+ reject_alora(model)
if os.environ.get("UNSLOTH_USE_NEW_MODEL", "0") == "1" or _is_text_seq2seq_config(
getattr(model, "config", None)
):
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index 01f70591e96..612aa602a7b 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -4174,6 +4174,7 @@ class FastBaseModel:
):
offload_layers = legacy_offload_layers(kwargs, offload_layers)
prefetch_depth = prefetch_depth_arg(kwargs)
+ reject_alora(model, kwargs.get("alora_invocation_tokens"))
if os.environ.get("UNSLOTH_ENABLE_FULL_FINETUNING", "0") == "1":
print("Unsloth: Full finetuning is enabled, so .get_peft_model has no effect")
# Full finetuning still compiles, so a stray pre-train forward can poison the cache; install the detector here too (idempotent).
@@ -4588,6 +4589,7 @@ class FastBaseModel:
tokenizer = None,
float32_mixed_precision = None,
):
+ reject_alora(model)
full_finetuning = os.environ.get("UNSLOTH_ENABLE_FULL_FINETUNING", "0") == "1"
if type(float32_mixed_precision) is bool: