Skip to content
class

SubspaceAllocator

SubspaceAllocator(model: nn.Module, config: AdamssConfig, adapter_name: str)

Decides, on a schedule, which subspaces of an AdaMSS model stay trainable.

Call update_and_allocate once per training step, after loss.backward() and before the gradients are cleared; AdamssModel.update_and_allocate does so for the adapters that asked for ASA. Importances are gathered every step from init_warmup to final_warmup; every mask_interval steps in that window the subspaces are ranked across the whole model, all but the best frozen, and the importances restarted. The number to keep falls from the model's total number of subspaces to asa_target_subspaces as target+(total−target) (1−p)ρ\text{target} + (\text{total} - \text{target})\,(1 - p)^{\rho} truncated, with pp the progress through the window and ρ\rho asa_schedule_exponent.

Parameters

The model AdaMSS was applied to.
The adapter's config: the window, the interval, the averages' weights and the target.
adapter_namestr
The adapter that trains.

Raises

ValueError
No AdaMSS layer of model holds adapter_name.

Examples

>>> import lucid.nn as nn
>>> from lucid.peft import AdamssConfig, AdamssModel
>>> net = nn.Sequential(nn.Linear(6, 6), nn.Linear(6, 2))
>>> config = AdamssConfig(r=4, num_subspaces=3, use_asa=True, asa_target_subspaces=2,
...                       init_warmup=1, final_warmup=5, mask_interval=1, target_modules=["0", "1"])
>>> allocator = AdamssModel(net, config, "default").allocator("default")
>>> allocator.total, [allocator.target(step) for step in range(1, 6)]
(5, [5, 3, 2, 2, 2])

Used by 2

Constructors

1
dunder

__init__

→None
__init__(model: nn.Module, config: AdamssConfig, adapter_name: str)

Find the adapter's layers in model; config sets the schedule.

Properties

1
prop

total

→int
total: int

The subspaces of the whole model, whether or not they train.

Instance methods

5
fn

mask_to_target

→None
mask_to_target(target: int)

Freeze every recorded subspace whose score is not strictly above the target-th highest.

Nothing is frozen while target is at least the number of subspaces recorded. A frozen factor loses its gradient and its requires_grad; a kept one is trainable.

Parameters

targetint
The number of subspaces to keep, in the whole model.
fn

reset_importance

→None
reset_importance()

Forget the recorded importances.

fn

target

→int or None
target(step: int)

How many subspaces step allows to stay trainable, or None before the window opens.

Parameters

stepint
The training step.

Returns

int or None

The truncated schedule value; asa_target_subspaces from final_warmup on.

fn

update_and_allocate

→None
update_and_allocate(global_step: int)

One training step's worth of allocation: record importances, then freeze if the schedule says so.

Nothing happens outside the window init_warmup <= step <= final_warmup. Inside it the importances are recorded every step; on a step that is a multiple of mask_interval the subspaces are masked to the schedule's target and the importances restart.

Parameters

global_stepint
The training step.
fn

update_importance

→None
update_importance()

Record the importance of every factor that has a gradient, at its current value.