Skip to content
class

LNTuningLayer

extends_ConfigLayer
LNTuningLayer(base_layer: nn.Module, adapter_name: str, config: C)

A module beside a trainable copy of itself, one copy per adapter.

The layer applies the copy of the one active adapter it holds; with none it runs the base. It wraps any module that takes and returns one tensor.

Parameters

base_layernn.Module
The module to tune; kept as base_layer, its parameters shared.
adapter_namestr
Name of the adapter created at once.
The adapter's config.

Raises

ValueError
base_layer builds parameters on its first input.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft import LNTuningConfig
>>> from lucid.peft.tuners.ln_tuning import LNTuningLayer
>>> base = nn.LayerNorm(4)
>>> layer = LNTuningLayer(base, "default", LNTuningConfig(target_modules=["x"]))
>>> x = lucid.randn(3, 4)
>>> bool((layer(x) == base(x)).all().item())   # the copy starts as the base
True
>>> layer.ln_tuning_layers["default"].weight is base.weight
False

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 _ConfigLayer
delete_adapter(adapter_name: str)

Remove adapter_name: its 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 _ConfigLayer
extra_repr()

The active adapter, whether it is merged and whether adapters are disabled.

fn

forward

→Tensor
forward(x: Tensor)

Run the active adapter's trained copy of the layer.

The original layer when adapters are disabled, every active adapter is merged or none applies; more than one applied adapter raises ValueError, since LN tuning applies one copy at a time. The copy of an adapter does not hold what another's merge exchanged into the base, so it runs whether or not one 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

→Tensor
get_delta_weight(adapter: str)

Return the tuned weight minus the original one, shaped like the base weight.

Raises

AttributeError
The wrapped module has no weight.
fn

merge

→None
merge(safe_merge: bool = False, adapter_names: list[str] | None = None)

Make an adapter's copy the layer's values, in place.

One adapter can be merged at a time. Asking for another while one is merged unmerges the first, with a warning.

Parameters

safe_mergebool= False
Check the tuned values for NaN / inf first and leave the base untouched if there are any.
adapter_nameslist of str= None
The adapter to merge; the active one this layer holds by default.

Raises

ValueError
More than one adapter is asked for, or safe_merge found a non-finite value.
RuntimeError
The adapter is disabled.
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 _ConfigLayer
update_layer(adapter_name: str, config: C)

Create adapter adapter_name from config.

The adapter is built whole before the layer holds any of it. 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 of the layer's tables.
configC
The method's config.

Raises

ValueError
The layer already holds an adapter of this name, or config does not fit the layer.