alf.pretrained_models.model_adapters#
alf.pretrained_models.model_adapters.lora#
- class Conv2dAdapter(m, rank=16, weight=1.0, name='LoRA')[source]#
Bases:
alf.pretrained_models.model_adapters.lora.LoRAAdapter for Conv2d layers.
The most natural way of a LoRA decomposition for Conv2d is
(rank, kernel_size[0] * kernel_size[1] * in_channels) x (out_channels, rank)
However, since
out_channelsis usually small, this decomposition is not low-rank actually and it won’t save much memory.We can first reinterpret the weight matrix as a shape of
(out_channels * kernel_size[0], in_channels * kernel_size[1])
to make the in- and out-dimensions balanced, and then decompose.
- Parameters
m (
Module) – the module to be adaptedrank (
int) – the low rankweight (
float) – weight for the low-rank weight matrix
- classmethod can_adapt(m)[source]#
Check if the adapter class can adapt a given module.
- Return type
bool
- forward(input)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool#
- class EmbeddingAdapter(m, rank=16, weight=1.0, name='LoRA')[source]#
Bases:
alf.pretrained_models.model_adapters.lora.LoRAAdapter for embedding layers.
- Parameters
m (
Module) – the module to be adaptedrank (
int) – the low rankweight (
float) – weight for the low-rank weight matrix
- classmethod can_adapt(m)[source]#
Check if the adapter class can adapt a given module.
- Return type
bool
- forward(input)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool#
- class LinearAdapter(m, rank=16, weight=1.0, name='LoRA')[source]#
Bases:
alf.pretrained_models.model_adapters.lora.LoRAAdapter for linear layers.
- Parameters
m (
Module) – the module to be adaptedrank (
int) – the low rankweight (
float) – weight for the low-rank weight matrix
- classmethod can_adapt(m)[source]#
Check if the adapter class can adapt a given module.
- Return type
bool
- forward(input)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool#
- class LoRA(m, rank=16, weight=1.0, name='LoRA')[source]#
Bases:
torch.nn.modules.module.ModuleBase class for LoRA (Low-Rank Adaptation).
For any model layer expressed as a matrix multiplication of the form \(h=W_0x\), it performs a reparameterization such that:
\[h = W_0x + \frac{\alpha}{r} BAx\]where \(A\in\mathbb{R}^{r\times k}\) and \(B\in\mathbb{R}^{d\times r}\) are the decomposition matrices and \(r\) is the low-dim rank of the decomposition.
The original LoRA paper doesn’t adapt biases: “We leave the empirical investigation of adapting the MLP layers, LayerNorm layers, and biases to a future work.”
If a module has a weight matrix with dimensionality greater than 2 (e.g., conv), we might need to first reinterpret it as a 2D matrix
W_0. This reinterpretation will differ for different modules.- Parameters
m (
Module) – the module to be adaptedrank (
int) – the low rankweight (
float) – weight for the low-rank weight matrix
- classmethod can_adapt(m)[source]#
Check if the adapter class can adapt a given module.
- Return type
bool
- detach()[source]#
Detach from and recover the base module.
Note that this operation is irreversible. Once detached, there is no way to add the adapter back to the module.
We still keep the adapter weights after detachment.
- forward(input)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- merge()[source]#
Merge the adapter and base module weights.
This operation will change the base module weight in place and restore its original
forward(). It should only be called in the inference mode.
- property scaling: float#
- Return type
float
- training: bool#