Linear
Linear]Linear(base_layer: nn.Module, adapter_name: str, r: int = 0, lora_alpha: float = 1, lora_dropout: float = 0.0, fan_in_fan_out: bool = False, is_target_conv_1d_layer: bool = False, init_lora_weights: bool | str = True, use_rslora: bool = False, use_dora: bool = False, lora_bias: bool = False, arrow_config: ArrowConfig | None = None)Implementing kernel
C++ engine symbols that back this Python API.An nn.Linear with a LoRA update: .
The base layer runs as it is and stays frozen; the update is computed
in the adapter's dtype and added in the wider of the two dtypes before
the result is cast back to the base output's dtype. merge folds
into in place, so a merged layer costs what
the base costs; unmerge takes it out again.
Stackable: the update is added beside whatever layer it stands on, which the layer calls once.
QuantizedLinear is not (it is built on the quantized buffers themselves).
Parameters
base_ layernn.Linearbase_layer, its parameters shared.adapter_ namestrlora_A / lora_B.rint= 0lora_ alphafloat= 1lora_ dropoutfloat= 0.0fan_ in_ fan_ outbool= FalseFalse: nn.Linear stores its weight (out, in).is_ target_ conv_ 1d_ layerbool= FalseFalse; kept so positional calls line up with the
reference, whose fused-QKV Conv1D base v1 does not have.init_ lora_ weightsbool or str= TrueLoraConfig; "pissa" and "olora" also rewrite the
base weight (the residual).use_ rslorabool= Falseuse_ dorabool= Falselora_magnitude_vector) beside the factors, so the layer computes
row by row.lora_ biasbool= Falselora_B a bias (lora_B.<adapter>.bias); merging folds it,
scaled, into the base bias.lora_arrow): each token goes through the top_k of them its
prototype is nearest to, and the router cannot be merged. Its own lora_A /
lora_B exist, as in the reference, and are not used.Raises
ValueErrorr is not positive, fan_in_fan_out or
is_target_conv_1d_layer is True, init_lora_weights
asks for more rank than the weight has, or arrow_config is given
with use_dora, to a layer holding no plain adapter to route, or with
a top_k larger than their number.Examples
>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft.tuners.lora import Linear
>>> base = nn.Linear(8, 4)
>>> layer = Linear(base, "default", r=2, lora_alpha=4)
>>> layer.base_layer is base, layer.scaling["default"]
(True, 2.0)
>>> x = lucid.randn(3, 8)
>>> bool((layer(x) == base(x)).all().item()) # B starts at zero
TrueUsed by 4
Constructors
1__init__
→None__init__(base_layer: nn.Module, adapter_name: str, r: int = 0, lora_alpha: float = 1, lora_dropout: float = 0.0, fan_in_fan_out: bool = False, is_target_conv_1d_layer: bool = False, init_lora_weights: bool | str = True, use_rslora: bool = False, use_dora: bool = False, lora_bias: bool = False, arrow_config: ArrowConfig | None = None)Wrap an nn.Linear as LoraLayer does.
fan_in_fan_out and is_target_conv_1d_layer are accepted for the reference
library's signature and must stay False: nn.Linear stores (out, in).
Properties
8The adapters the forward pass applies, as a list.
The base layer's bias (a buffer on a quantized base), or None.
of each adapter, by name (read-only).
Whether each adapter's second factor carries a bias, by name (read-only).
Rank of each adapter, by name (read-only).
The scale each adapter's update is multiplied by, by name (read-only).
Whether each adapter is a DoRA adapter, by name (read-only).
The base layer's weight (unchanged unless an adapter is merged into it).
Attributes
3Name (or names) of the adapter the forward pass applies.
Whether the forward pass runs the base layer alone.
Whether an adapter is folded into the base weight (merged_adapters non-empty).
Instance methods
13Remove adapter_name: its factors, its dropout and its recorded settings.
Apply the adapter in the forward pass (True) or run the base alone (False).
Disabling a merged layer unmerges it first, here rather than on the next call, so the forward pass never writes a weight — a compiled forward must not replay that write.
Parameters
enabledboolThe active adapter, its rank and lora_alpha, and whether it is merged or disabled.
The base output plus each applied adapter's low-rank update.
The base output alone when adapters are disabled or every active adapter is merged; a layer merged for some of its adapters adds the others' updates to the weights that hold the merged ones.
Return the wrapped module, whose parameters are the base model's own.
One step down: where another tuner layer stands between, that layer, not the user's
module (_root goes all the way).
Return , the update adapter adds to the weight.
Parameters
adapterstrReturns
Tensor(out_features, in_features), in the adapter's dtype.
merge
→Nonefrom _BaseTunerLayermerge(safe_merge: bool = False, adapter_names: list[str] | None = None)Fold the update into the base weights, in place.
The weights keep their identity — they are written, not replaced —
so whatever holds them (an optimizer, a tied embedding) sees the
merged values. That is also why a weight shared with another
module must not be merged into; the tuner's merge_adapter and
merge_and_unload refuse it, as this layer alone cannot see the
rest of the model.
Merging some of the active adapters leaves the rest applying: the layer's
forward pass runs the folded weights and then the active adapters that are
not folded, so it computes what it did unmerged. The reference runs a
merged layer's base alone, which drops the others — a deliberate departure
(LCD-971). A method whose adapters would not compute the same function
that way (rotations that must apply in order, DoRA reading the weight)
refuses the split in the tuner's merge_adapter, before it writes
anything; this method, called on a layer directly, does not check it.
Merging an adapter that is not active adds it: the layer then runs the weights
with it folded in and the active adapters it has not folded (the base with that
adapter alone, in the reference).
Parameters
safe_ mergebool= Falseadapter_ nameslist of str= NoneRaises
ValueErrorsafe_merge
found a non-finite merged weight.RuntimeErrorlora_bias on a base with no bias).NotImplementedErrorreset_lora_parameters
→Nonefrom LoraLayerreset_lora_parameters(adapter_name: str, init_lora_weights: bool | str)Initialise adapter adapter_name the way init_lora_weights asks.
True: every Kaiming-uniform with a = sqrt(5) (what
nn.Linear uses) and every , so the update starts at
zero. "gaussian": ,
. False: leave both as the layers made them.
"pissa", "pissa_niter_<k>" and "olora" factorise the
base weight and write the residual back into it, in place.
Parameters
adapter_ namestrinit_ lora_ weightsbool or strupdate_layer takes it.set_adapter
→Nonefrom _BaseTunerLayerset_adapter(adapter_names: str | list[str], inference_mode: bool = False)Apply the named adapters, and train exactly those unless inference_mode.
The names this layer holds nothing for are skipped by the forward pass. A merged layer is unmerged first, since the active adapters are no longer the merged ones.
Parameters
adapter_ namesstr or list of strinference_ modebool= Falseset_requires_grad
→Nonefrom _BaseTunerLayerset_requires_grad(adapter_names: str | Sequence[str], requires_grad: bool = True)Set requires_grad on the parameters of the named adapters this layer holds.
Whether adapter_name on this LoRA linear layer can be converted to LoRA (a layer that holds none can; an Arrow router cannot).
Take every merged update back out of the base weights, in place.
The result equals the original weights up to the rounding of the round trip.
update_layer
→Nonefrom LoraLayerupdate_layer(adapter_name: str, r: int, lora_alpha: float, lora_dropout: float, init_lora_weights: bool | str, use_rslora: bool, use_dora: bool = False, lora_bias: bool = False, arrow_config: ArrowConfig | None = None)Create adapter adapter_name: its factors, dropout and scale.
The adapter lives on the base weight's device. Its dtype is the
base's, except that a float16 / bfloat16 base gets a float32
adapter — the reference's default, which keeps the low-rank update
from underflowing in training. A layer that holds an adapter already
keeps applying the adapters it applied: the new one is inactive until
set_adapter names it. A layer with none starts applying this one.
Everything the layer cannot do is refused before anything is allocated or any base weight is touched.
Parameters
adapter_ namestrrintlora_ alphafloatlora_ dropoutfloat[0, 1).init_ lora_ weightsbool or strTrue, False, "gaussian", "pissa",
"pissa_niter_<k>" or "olora" (see LoraConfig).
PiSSA and OLoRA rewrite the base weight (the residual), in place.use_ rslorabooluse_ dorabool= Falselora_ biasbool= Falsenn.Linear layers only; not with use_dora). Its own factors are never
used.Raises
NotImplementedErrorValueError