LoHaLayer
_LycorisLayerLoHaLayer(base_layer: nn.Module, adapter_name: str, config: _LycorisConfig, r: int = 0, alpha: float = 0.0)Adapter bookkeeping and the update shared by the LoHa layers.
Per adapter: hada_w1_a / hada_w1_b / hada_w2_a / hada_w2_b
— for a linear layer (or a flattened kernel) (out, r), (r, in),
(out, r), (r, in) — and, with a spatial core, hada_t1 /
hada_t2 of (r, r, *kernel) with the matrices (r, out) and
(r, in). Rank dropout always rescales the rows it keeps (the
reference's LoHa does), so rank_dropout_scale reads True.
Parameters
base_ layernn.Modulebase_layer, its parameters shared.adapter_ namestrconfigLoHaConfiginit_weights, use_effective_conv2d).rint= 0alphafloat= 0.0Used by 2
Properties
3The adapters the forward pass applies, as a list.
The base layer's bias (a buffer on a quantized base), or None.
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
12Remove adapter_name: its factor tables, through the base, 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 whether the layer is merged or its adapters disabled.
The base output plus each active adapter's weight update, applied like the base weight.
The update is get_delta_weight (with rank dropout while training) run as a linear map
or a convolution of x; the base output alone when adapters are disabled or every active
adapter is merged.
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).
get_delta_weight(adapter: str, apply_rank_dropout: bool = False)Return , the update adapter adds to the base weight.
Parameters
adapterstrapply_ rank_ dropoutbool= FalseReturns
TensorShaped like the base weight, in the factors' 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).NotImplementedErrorset_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 _LycorisLayerupdate_layer(adapter_name: str, r: int, alpha: float, config: _LycorisConfig)Create adapter adapter_name: its factors, initialised, and its scale .
The factors are on the base weight's device and in its dtype, widened to float32
when that is half precision. 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.
Parameters
adapter_ namestrrintalphafloatconfig_LycorisConfiginit_weights, use_effective_conv2d
and whatever the method's factorisation reads.Raises
ValueErrorr is not positive.