Skip to content
class

RankAllocator

RankAllocator(model: nn.Module, peft_config: AdaLoraConfig, adapter_name: str)

Decides, on a schedule, how many singular triplets each AdaLoRA layer keeps.

Reference: Zhang et al., 2023 (AdaLoRA). The allocator reads the sensitivity of the adapter's tensors after every backward pass (update_ipt), and on the steps the budget schedule marks, masks the least important triplets of the whole model down to the budget (mask_to_budget). update_and_allocate is the one call a training step makes, and AdaLoraModel.update_and_allocate makes it for the model.

The budget starts at the model's total number of triplets, stays there for tinit steps, falls on a cubic schedule to target_r triplets per layer at step total_step - tfinal and stays there.

Parameters

The model AdaLoRA was applied to.
peft_configAdaLoraConfig
The adapter's config: the schedule and the moving-average weights.
adapter_namestr
The adapter that trains.

Raises

ValueError
adapter_name is not held by any AdaLoRA layer of model.

Examples

>>> import lucid.nn as nn
>>> from lucid.peft.tuners.adalora import AdaLoraConfig, AdaLoraModel
>>> net = nn.Sequential(nn.Linear(6, 6), nn.Linear(6, 2))
>>> config = AdaLoraConfig(init_r=4, target_r=2, total_step=20, tfinal=5, target_modules=["0", "1"])
>>> allocator = AdaLoraModel(net, config, "default").rankallocator
>>> tuple(allocator.budget_schedule(0)), tuple(allocator.budget_schedule(19))
((8, False), (4, True))

Used by 2

Constructors

1
dunder

__init__

→None
__init__(model: nn.Module, peft_config: AdaLoraConfig, adapter_name: str)

Measure the adapted layers of model.

peft_config sets the budget schedule and adapter_name is the adapter whose triplets are ranked.

Properties

1
prop

final_step

→int
final_step: int

The step the budget reaches target_r and the final fine-tuning begins: total_step - tfinal.

Attributes

1
attr

init_bgt

init_bgt

Triplets the model holds before any is masked, and the total target_r per layer asks for.

Instance methods

7
fn

budget_schedule

→tuple
budget_schedule(step: int)

The triplet budget at step and whether this step masks down to it.

Parameters

stepint
The training step.

Returns

tuple

(triplets, mask): the budget, and whether to mask down to it now.

fn

mask_to_budget

→dict of str to list of bool
mask_to_budget(model: nn.Module, budget: int)

Zero the singular values of the least important triplets so that budget are left.

Every triplet scoring at or below the (initial - budget)-th lowest score is masked, so ties can leave fewer than budget.

Parameters

The model whose layers are masked.
budgetint
Triplets to keep, in the whole model.

Returns

dict of str to list of bool

The rank pattern: "<module>.lora_E" to one flag per triplet, True where it was kept.

fn

mask_using_rank_pattern

→None
mask_using_rank_pattern(model: nn.Module, rank_pattern: Mapping[str, list[bool]])

Zero the singular values the rank pattern dropped (the final phase, after the budget is reached).

A layer whose rank no longer matches its pattern was pruned already and is left alone.

Parameters

The model whose layers are masked.
rank_patterndict of str to list of bool
"<module>.lora_E" (the adapter's name may follow) to one flag per triplet.
fn

reset_ipt

→None
reset_ipt()

Forget the recorded sensitivities.

fn

set_total_step

→None
set_total_step(total_step: int)

Change the number of training steps the schedule spreads the budget over.

fn

update_and_allocate

→tuple
update_and_allocate(model: nn.Module, global_step: int, force_mask: bool = False)

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

Parameters

The model whose adapter trains.
global_stepint
The training step.
force_maskbool= False
Mask down to the budget even on a step the schedule skips.

Returns

tuple

The budget of this step and the rank pattern, None if nothing was masked.

fn

update_ipt

→None
update_ipt(model: nn.Module)

Record the sensitivity of every adapter tensor at its current value and gradient.