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
modelnn.Modulepeft_ configAdaLoraConfigadapter_ namestrRaises
ValueErroradapter_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
1Properties
1Attributes
1Instance methods
7The triplet budget at step and whether this step masks down to it.
Parameters
stepintReturns
tuple(triplets, mask): the budget, and whether to mask down to it now.
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
modelnn.ModulebudgetintReturns
dict of str to list of boolThe rank pattern: "<module>.lora_E" to one flag per triplet, True where it was kept.
mask_using_rank_pattern
→Nonemask_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
modelnn.Modulerank_ patterndict of str to list of bool"<module>.lora_E" (the adapter's name may follow) to one flag per triplet.Forget the recorded sensitivities.
Change the number of training steps the schedule spreads the budget over.
update_and_allocate
→tupleupdate_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
modelnn.Moduleglobal_ stepintforce_ maskbool= FalseReturns
tupleThe budget of this step and the rank pattern, None if nothing was masked.
Record the sensitivity of every adapter tensor at its current value and gradient.