Skip to content
fn

create_arrow_model

→PeftModel
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.Module
The model the adapters were trained on.
task_specific_adapter_pathslist of str
Local adapter directories (an adapter_config.json and its weights), at least one.
arrow_configArrowConfig
The router's options. Its task_adapter_names and gks_adapter_names are filled in, as the reference does.
general_adapter_pathslist of str= None
The general-knowledge adapters; required with arrow_config.use_gks.
**adapter_kwargsUnpack[_LoaderOptions]= {}
The options the two loaders share (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

PeftModel

base_model adapted, with the router active.

Raises

ValueError
No task adapter, use_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.
FileNotFoundError
A path is not a directory (hub ids are not resolved).
TypeError
An adapter targets a layer that is not nn.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)

Used by 1