xnn.common.finetune.lora.inject_lora#
- xnn.common.finetune.lora.inject_lora(model, rank=4, alpha=1.0, init_std=0.001, *, exclude=(), freeze=True, trainable=())[source]#
Replace every eligible linear layer of
modelby a LoRA adapter, in place.Eligible layers are
torch.nn.Linear,e3nn.o3.Linear, the layers ofe3nn.nn.FullyConnectedNetand every class registered withregister_lora_target(). The adapted model computes exactly whatmodeldid until the adapters are trained.- Parameters:
model (torch.nn.Module) – The model (bare, multi-head or wrapped).
rank (int, optional) – Rank of every update, by default 4.
alpha (float, optional) – Scaling numerator, by default 1.0.
init_std (float, optional) – Standard deviation of the Gaussian factor at initialization.
exclude (sequence of str, optional) –
fnmatchpatterns of module names to leave alone (["readouts*"]keeps the readouts frozen and unadapted).freeze (bool, optional) – Freeze every parameter that is not a LoRA factor (the usual LoRA setting), by default True.
trainable (sequence of str, optional) – Parameter-name patterns kept trainable besides the LoRA factors (
["*atom_ref*"]lets the reference energies move).
- Returns:
model.- Return type:
- Raises:
ValueError – If no layer was eligible.