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.

Build multi-LoRA expert groups when the batch is set (#16651)

Daniel Han3de707fc815b
diff --git a/tests/test_multi_lora_cold_compile_cpu.py b/tests/test_multi_lora_cold_compile_cpu.py
index a00d5e64f26..d6e2cdf04ba 100644
--- a/tests/test_multi_lora_cold_compile_cpu.py
+++ b/tests/test_multi_lora_cold_compile_cpu.py
@@ -29,6 +29,9 @@ def _load_experts():
"_grouped_disabled",
"_warn_once",
"_is_quantized",
+ "MultiLoraLinear",
+ "_layers",
+ "set_multi_lora_batch",
}
tree = ast.parse(path.read_text(encoding = "utf-8"), filename = str(path))
tree.body = [
@@ -102,10 +105,8 @@ def test_cold_fullgraph_does_not_run_grouped_probes(monkeypatch, dtype, probe):
for A, B in zip(adapter.lora_A, adapter.lora_B):
A.fill_(0.01)
B.fill_(0.02)
- batch = ml.MultiLoraBatch.from_adapter_ids(torch.tensor([0, 1, 0, 1]), 2)
- for adapter in experts.adapters.values():
- adapter.batch = batch
- ml._adapter_groups(batch)
+ batch = ml.set_multi_lora_batch(experts, torch.tensor([0, 1, 0, 1]))
+ assert all(adapter.batch is batch for adapter in experts.adapters.values())
hidden = torch.full((4, 8), 0.1, dtype = dtype, requires_grad = True)
index = torch.zeros(4, 1, dtype = torch.long)
weights = torch.ones(4, 1, dtype = dtype, requires_grad = True)
diff --git a/unsloth/kernels/multi_lora.py b/unsloth/kernels/multi_lora.py
index 80c3803502f..856c9291daa 100644
--- a/unsloth/kernels/multi_lora.py
+++ b/unsloth/kernels/multi_lora.py
@@ -1507,6 +1507,9 @@ def set_multi_lora_batch(
seg = layer.batch.seg_start
present = (seg[1:] - seg[:-1]) > 0
layer.seen = present if layer.seen is None else layer.seen | present
+ if isinstance(layer, MultiLoraExpertParam):
+ # Build groups eagerly: a data-dependent .tolist() inside forward breaks fullgraph.
+ _adapter_groups(layer.batch)
return next(iter(by_device.values()))