Skip to content
class

LoKrLayer

extends_LycorisLayer
LoKrLayer(base_layer: nn.Module, adapter_name: str, config: _LycorisConfig, r: int = 0, alpha: float = 0.0)

Adapter bookkeeping and the update shared by the LoKr layers.

Per adapter, W1W_1 is lokr_w1 ((a, c)) or lokr_w1_a / lokr_w1_b ((a, r), (r, c)); W2W_2 is lokr_w2 ((b, d) plus the kernel for a convolution), lokr_w2_a / lokr_w2_b ((b, r), (r, d·kernel)), or with a spatial core lokr_t2 ((r, r, *kernel)) and (r, b), (r, d). Here dout=abd_{out} = ab and din=cdd_{in} = cd by factorization.

Parameters

base_layernn.Module
The module to adapt; kept as base_layer, its parameters shared.
adapter_namestr
Name of the adapter, the key in every table.
The adapter's config (dropouts, init_weights, decompose_both, decompose_factor, use_effective_conv2d).
rint= 0
Rank. The default is the reference's and is refused: pass a positive rank.
alphafloat= 0.0
Numerator of the scale.

Used by 2

Properties

3
prop

active_adapters

→list[str]from _BaseTunerLayer
active_adapters: list[str]

The adapters the forward pass applies, as a list.

prop

bias

→Tensor | Nonefrom _BaseTunerLayer
bias: Tensor | None

The base layer's bias (a buffer on a quantized base), or None.

prop

weight

→Parameterfrom _BaseTunerLayer
weight: Parameter

The base layer's weight (unchanged unless an adapter is merged into it).

Attributes

3
attr

active_adapter

from _BaseTunerLayer
active_adapter: str | list[str]

Name (or names) of the adapter the forward pass applies.

attr

disable_adapters

from _BaseTunerLayer
disable_adapters

Whether the forward pass runs the base layer alone.

attr

merged

from _BaseTunerLayer
merged

Whether an adapter is folded into the base weight (merged_adapters non-empty).

Instance methods

12
fn

delete_adapter

→Nonefrom _LycorisLayer
delete_adapter(adapter_name: str)

Remove adapter_name: its factor tables, through the base, and its recorded settings.

fn

enable_adapters

→Nonefrom _BaseTunerLayer
enable_adapters(enabled: bool)

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

enabledbool
Whether the adapter takes part. Gradient flags are left as they are: a frozen adapter stays frozen when it is re-enabled.
fn

extra_repr

→strfrom _LycorisLayer
extra_repr()

The active adapter, its rank, and whether the layer is merged or its adapters disabled.

fn

forward

→Tensorfrom _LycorisLayer
forward(x: Tensor)

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.

fn

get_base_layer

→nn.Modulefrom _BaseTunerLayer
get_base_layer()

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).

fn

get_delta_weight

→Tensorfrom _LycorisLayer
get_delta_weight(adapter: str, apply_rank_dropout: bool = False)

Return s ΔWs\,\Delta W, the update adapter adds to the base weight.

Parameters

adapterstr
The adapter's name.
apply_rank_dropoutbool= False
Zero a random subset of output rows first, as training does.

Returns

Tensor

Shaped like the base weight, in the factors' dtype.

fn

merge

→Nonefrom _BaseTunerLayer
merge(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= False
Check the merged weights for NaN / inf first and leave the base untouched if there are any.
adapter_nameslist of str= None
Adapters to merge; the active ones this layer holds by default.

Raises

ValueError
An adapter name this layer does not hold, or safe_merge found a non-finite merged weight.
RuntimeError
The adapter is disabled, or the layer's own rules refuse the merge (lora_bias on a base with no bias).
NotImplementedError
This layer's update cannot be written into its base weights.
fn

set_adapter

→Nonefrom _BaseTunerLayer
set_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 str
The adapter, or adapters, to apply.
inference_modebool= False
Freeze the activated adapters as well.
fn

set_requires_grad

→Nonefrom _BaseTunerLayer
set_requires_grad(adapter_names: str | Sequence[str], requires_grad: bool = True)

Set requires_grad on the parameters of the named adapters this layer holds.

fn

supports_lora_conversion

→boolfrom _BaseTunerLayer
supports_lora_conversion(adapter_name: str = 'default')

Whether adapter_name on this layer can be converted to a plain LoRA update.

fn

unmerge

→Nonefrom _BaseTunerLayer
unmerge()

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.

fn

update_layer

→Nonefrom _LycorisLayer
update_layer(adapter_name: str, r: int, alpha: float, config: _LycorisConfig)

Create adapter adapter_name: its factors, initialised, and its scale α/r\alpha / r.

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_namestr
Key of the adapter in each table.
rint
Rank; positive.
alphafloat
Numerator of the scale.
config_LycorisConfig
The method's config: dropouts, init_weights, use_effective_conv2d and whatever the method's factorisation reads.

Raises

ValueError
The layer already holds an adapter of this name, or r is not positive.