ParamWrapper
_WeightSpace[object]ParamWrapper(base_layer: nn.Module, adapter_name: str, parameter_name: str, r: int = 0, lora_alpha: float = 1, lora_dropout: float = 0.0, init_lora_weights: bool | str = True, use_rslora: bool = False, use_dora: bool = False, lora_bias: bool = False, arrow_config: ArrowConfig | None = None)A LoRA update on one nn.Parameter of a module, for target_parameters.
The wrapped module is any module that reads the parameter in its own
forward; the wrapper stands where the module was and calls it with
read in place of (see _WeightSpace).
Reading the parameter directly (wrapper.weight) gives the base
value, the update not applied. The module's other attributes read
through to it.
A parameter is (out, in) — one matrix — or (experts, out, in),
a stack with an adapter pair per expert. A stack whose module sets
is_transposed = True is read as (experts, in, out). Each
wrapper adapts one parameter; a module with several adapted parameters
is wrapped once per parameter, nested, the outermost standing in the
model. An adapter file keys the inner wrappers under
....base_layer.lora_A.weight.
Parameters
base_ layernn.Moduleadapter_ namestrlora_A / lora_B.parameter_ namestrrint= 0LoraLayer.update_layer takes
them; r must be positive. use_dora and lora_bias are
refused, as the reference does, and so is arrow_config: a router needs
a nn.Linear layer.lora_ alphaint= 0LoraLayer.update_layer takes
them; r must be positive. use_dora and lora_bias are
refused, as the reference does, and so is arrow_config: a router needs
a nn.Linear layer.init_ lora_ weightsint= 0LoraLayer.update_layer takes
them; r must be positive. use_dora and lora_bias are
refused, as the reference does, and so is arrow_config: a router needs
a nn.Linear layer.use_ rsloraint= 0LoraLayer.update_layer takes
them; r must be positive. use_dora and lora_bias are
refused, as the reference does, and so is arrow_config: a router needs
a nn.Linear layer.use_ doraint= 0LoraLayer.update_layer takes
them; r must be positive. use_dora and lora_bias are
refused, as the reference does, and so is arrow_config: a router needs
a nn.Linear layer.lora_ biasint= 0LoraLayer.update_layer takes
them; r must be positive. use_dora and lora_bias are
refused, as the reference does, and so is arrow_config: a router needs
a nn.Linear layer.arrow_ configint= 0LoraLayer.update_layer takes
them; r must be positive. use_dora and lora_bias are
refused, as the reference does, and so is arrow_config: a router needs
a nn.Linear layer.lora_ dropoutfloat= 0.0Raises
ValueErrorr is not positive, lora_dropout is not 0, the parameter is
missing or is not 2-D or 3-D.Examples
>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft.tuners.lora import ParamWrapper
>>> class Gate(nn.Module):
... def __init__(self) -> None:
... super().__init__()
... self.w = nn.Parameter(lucid.randn(4, 6))
... def forward(self, x):
... return x @ self.w.mT
>>> gate = Gate()
>>> layer = ParamWrapper(gate, "default", "w", r=2, lora_alpha=4)
>>> layer.get_base_layer() is gate, tuple(layer.lora_A["default"].weight.shape)
(True, (2, 6))
>>> x = lucid.randn(3, 6)
>>> bool((layer(x) == gate(x)).all().item()) # B starts at zero
TrueUsed by 2
Constructors
1__init__
→None__init__(base_layer: nn.Module, adapter_name: str, parameter_name: str, r: int = 0, lora_alpha: float = 1, lora_dropout: float = 0.0, init_lora_weights: bool | str = True, use_rslora: bool = False, use_dora: bool = False, lora_bias: bool = False, arrow_config: ArrowConfig | None = None)Wrap the module that holds parameter_name and adapt that parameter alone.
The other arguments are LoraLayer's.
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 parameter adapted, then what LoraLayer shows.
Run the module with each adapted weight shifted by the active adapters' update.
The base module alone when adapters are disabled, every active adapter is merged or none applies; a layer merged for some of its adapters shifts the weights that hold the merged ones by the others. The shifted weights are in force for the one call only.
Return the module holding the parameter: through every wrapper nested around it, down to the module itself.
The base class goes one step down (_root is the whole way); the wrappers of one
module are all this layer's own, so here the two agree.
Return for every expert, shaped like the parameter.
With experts the factors are the reference's: lora_A holds the
experts' stacked along its rows (rank-major within an
expert), lora_B holds the along its columns (expert
fastest).
Parameters
adapterstrReturns
TensorThe parameter's shape, 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.
supports_lora_conversion
→boolfrom _BaseTunerLayersupports_lora_conversion(adapter_name: str = 'default')Whether adapter_name on this layer can be converted to a plain LoRA update.
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