create_arrow_model(base_model: nn.Module, task_specific_adapter_paths: list[str], arrow_config: ArrowConfig, general_adapter_paths: list[str] | None = None, adapter_kwargs: Unpack[_LoaderOptions] = {})Load saved LoRA adapters into base_model and add an Arrow router over them.
Arrow (Ostapenko et al., 2024, arXiv:2405.11157) reuses a library of task-specific adapters:
at every adapted nn.Linear a token goes through the top_k adapters whose
prototype — the top singular vector of the adapter's update — it is most aligned with,
combined by a softmax with temperature arrow_config.router_temperature. With
arrow_config.use_gks the mean of the general-knowledge adapters' factors is first
subtracted from every task adapter's (GenKnowSub, ACL 2025).
The task adapters are loaded as task_0, task_1, … and the general ones as gks_0,
gks_1, …; the router is the adapter arrow_router, and it is the one in use. They
must target the same modules with the same rank. The model is changed in place (as
lucid.peft.PeftModel.from_pretrained changes it) and is undone, adapters included,
if anything fails.
Parameters
base_ modelnn.Moduletask_ specific_ adapter_ pathslist of stradapter_config.json and its weights), at least one.arrow_ configArrowConfigtask_adapter_names and gks_adapter_names are filled
in, as the reference does.general_ adapter_ pathslist of str= Nonearrow_config.use_gks.**adapter_ kwargsUnpack[_LoaderOptions]= {}is_trainable, subfolder, key_mapping,
convert …), handed to lucid.peft.PeftModel.from_pretrained for the first
adapter and to lucid.peft.PeftModel.load_adapter for the others. The reference
also forwards its hub options (revision, token …); here they are refused.Returns
PeftModelbase_model adapted, with the router active.
Raises
ValueErroruse_gks without general adapters, a directory without an
adapter_config.json, adapters that do not target the same modules with the same
rank, or one that is DoRA or has lora_bias.FileNotFoundErrorTypeErrornn.Linear.Examples
>>> import tempfile
>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft import ArrowConfig, LoraConfig, create_arrow_model, get_peft_model
>>> def base() -> nn.Module:
... lucid.manual_seed(1)
... return nn.Sequential(nn.Linear(6, 6))
>>> with tempfile.TemporaryDirectory() as d:
... for task in ("a", "b"):
... config = LoraConfig(r=2, target_modules=["0"], init_lora_weights=False)
... get_peft_model(base(), config).save_pretrained(f"{d}/{task}")
... model = create_arrow_model(
... base(), [f"{d}/a", f"{d}/b"], ArrowConfig(top_k=1, rng_seed=0)
... )
>>> model.active_adapter, sorted(model.peft_config)
('arrow_router', ['arrow_router', 'task_0', 'task_1'])
>>> tuple(model(lucid.randn(3, 6)).shape)
(3, 6)