Skip to content
fn

find_kappa_target_modules

→dict of str to list of str or None
find_kappa_target_modules(model: nn.Module, top_p: float = 0.2, max_dim_size_to_analyze: int = 16384, moe_param_suffixes: tuple[str, ...] | None = None, show_progress: bool = True)

Choose what to adapt by the condition number of the weights (KappaTune).

The condition number of a weight matrix is its largest singular value over its smallest. The top_p fraction of nn.Linear modules with the lowest numbers are returned as target_modules, and the same fraction of the fused mixture-of-experts weights (three-dimensional parameters named gate_up_proj, down_proj and the like) as target_parameters, a pair ready to give to lucid.peft.LoraConfig. An expert weight's number is the mean over its experts. Every weight is decomposed, so a large model takes a while.

Parameters

The base model whose weights are analysed.
top_pfloat= 0.2
The fraction, between 0 and 1, of candidates to keep; at least one is kept when there are any.
max_dim_size_to_analyzeint= 16384
A weight with a dimension larger than this is skipped.
moe_param_suffixestuple of str= None
The endings of the names of fused expert parameters; .gate_up_proj, .down_proj, .gate_proj and .up_proj when None.
show_progressbool= True
Accepted for the reference's signature; no progress bar is drawn.

Returns

dict of str to list of str or None

{"target_modules": [...], "target_parameters": [...] or None}; the second is None when the model has no fused expert weights.

Raises

ValueError
top_p is outside [0, 1].

Examples

>>> import lucid.nn as nn
>>> from lucid.peft import find_kappa_target_modules
>>> net = nn.Sequential(nn.Linear(8, 8), nn.Linear(8, 8), nn.Linear(8, 8))
>>> picked = find_kappa_target_modules(net, top_p=0.4)
>>> len(picked["target_modules"]), picked["target_parameters"]
(1, None)

Used by 1