AdaLoraLayer
_ResidualUpdateAdaLoraLayer(base_layer: nn.Module, adapter_name: str, config: AdaLoraConfig, r: int = 0, lora_alpha: float = 1)Adapter bookkeeping shared by the AdaLoRA layers.
Holds the wrapped module as base_layer — the very module, so its
parameters stay the same Parameter objects — and, per adapter name,
the three tensors in lora_A / lora_B / lora_E and the
frozen ranknum (ParameterDicts, so a state dict reads
....lora_E.default and an adapter file ....lora_E). The forward
pass adds the update of every active adapter this layer holds.
Parameters
base_ layernn.Moduleadapter_ namestrupdate_layer).configAdaLoraConfiginit_lora_weights are read.rint= 0lora_ alphafloat= 1r.Raises
TypeErrorbase_layer is not the module type the layer class adapts.ValueErrorr is negative, or base_layer builds parameters on its first input.Used by 3
Constructors
1__init__
→None__init__(base_layer: nn.Module, adapter_name: str, config: AdaLoraConfig, r: int = 0, lora_alpha: float = 1)Wrap base_layer and build the adapter adapter_name on it.
The adapter starts at rank r and scale lora_alpha / ranknum; config supplies
the dropout and the initialisation.
Properties
5The adapters the forward pass applies, as a list.
The base layer's bias (a buffer on a quantized base), or None.
Inputs each output unit reads: the second dimension of lora_A.
Output units of the base layer: the first dimension of lora_B.
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
15Remove adapter_name: its triplets, its rank count, its dropout and its 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, whether it is merged and whether adapters are disabled.
The base layer's output plus every active adapter's contribution to it.
The base output alone when adapters are disabled or every active adapter is merged (a merged adapter is in the base weights already); the sum is cast back to the base output's dtype.
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 , shaped like the base weight.
Parameters
adapterstrReturns
TensorThe change merging adapter makes to the base weight, 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).NotImplementedErrorHow far and are from the identity, as two Frobenius norms.
What the penalty on the loss sums; it keeps the singular vectors
orthonormal, so that the values in lora_E are the update's real
singular values.
Parameters
adapterstrReturns
list of Tensor[||Q Q^T - I||_F, ||P^T P - I||_F], scalars that carry gradient.
Keep only the singular triplets at the indices kept (in order); the scale does not change.
ranknum stays what it was, so the update of the kept triplets is the
one they made before the others were dropped.
Parameters
adapterstrkeptlist of intRaises
ValueErrorIndexErrorGive adapter rank r: the first triplets stay, extra ones start at zero; the scale does not change.
Parameters
adapterstrrintRaises
ValueErrorset_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 _ResidualUpdatesupports_lora_conversion(adapter_name: str = 'default')Whether adapter_name on this additive linear layer can be converted to LoRA.
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
→Noneupdate_layer(adapter_name: str, r: int, lora_alpha: float, config: AdaLoraConfig)Create adapter adapter_name with r singular triplets.
The tensors live on the base weight's device, in its dtype — float32
for a half-precision base. With init_lora_weights the singular
values start at zero, so the adapted layer starts as the base. A
layer that holds an adapter already keeps applying the adapters it
applied: the new one is inactive until set_adapter names it.
Parameters
adapter_ namestrrintlora_ alphafloatr.configAdaLoraConfiglora_dropout and init_lora_weights.Raises
ValueErrorr is negative.