Adapters
Adapter
¶
Bases: Generic[T]
Base class for adapters.
An Adapter modifies the structure of a Module
(typically by adding, removing or replacing layers), to adapt it to a new task.
eject
¶
Eject the adapter.
This method is the inverse of inject
,
and should leave the target in the same state as before the injection.
Source code in src/refiners/fluxion/adapters/adapter.py
inject
¶
inject(parent: Chain | None = None) -> TAdapter
Inject the adapter.
This method replaces the target of the adapter by the adapter inside the parent of the target.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parent
|
Chain | None
|
The parent to inject the adapter into, if the target doesn't have a parent. |
None
|
Source code in src/refiners/fluxion/adapters/adapter.py
setup_adapter
¶
setup_adapter(target: T) -> Iterator[None]
Setup the adapter.
This method should be called by the constructor of the adapter. It sets the target of the adapter and ensures that the adapter is not a submodule of the target.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target
|
T
|
The target of the adapter. |
required |
Source code in src/refiners/fluxion/adapters/adapter.py
Conv2dLora
¶
Conv2dLora(
name: str,
/,
in_channels: int,
out_channels: int,
rank: int = 16,
scale: float = 1.0,
kernel_size: tuple[int, int] = (1, 3),
stride: tuple[int, int] = (1, 1),
padding: tuple[int, int] = (0, 1),
device: device | str | None = None,
dtype: dtype | None = None,
)
Low-Rank Adaptation (LoRA) layer for 2D convolutional layers.
This layer uses two Conv2d
layers as its down and up layers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the LoRA. |
required |
in_channels
|
int
|
The number of input channels. |
required |
out_channels
|
int
|
The number of output channels. |
required |
rank
|
int
|
The rank of the LoRA. |
16
|
scale
|
float
|
The scale of the LoRA. |
1.0
|
kernel_size
|
tuple[int, int]
|
The kernel size of the LoRA. |
(1, 3)
|
stride
|
tuple[int, int]
|
The stride of the LoRA. |
(1, 1)
|
padding
|
tuple[int, int]
|
The padding of the LoRA. |
(0, 1)
|
device
|
device | str | None
|
The device of the LoRA weights. |
None
|
dtype
|
dtype | None
|
The dtype of the LoRA weights. |
None
|
Source code in src/refiners/fluxion/adapters/lora.py
LinearLora
¶
LinearLora(
name: str,
/,
in_features: int,
out_features: int,
rank: int = 16,
scale: float = 1.0,
device: device | str | None = None,
dtype: dtype | None = None,
)
Low-Rank Adaptation (LoRA) layer for linear layers.
This layer uses two Linear
layers as its down and up layers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the LoRA. |
required |
in_features
|
int
|
The number of input features. |
required |
out_features
|
int
|
The number of output features. |
required |
rank
|
int
|
The rank of the LoRA. |
16
|
scale
|
float
|
The scale of the LoRA. |
1.0
|
device
|
device | str | None
|
The device of the LoRA weights. |
None
|
dtype
|
dtype | None
|
The dtype of the LoRA weights. |
None
|
Source code in src/refiners/fluxion/adapters/lora.py
Lora
¶
Lora(
name: str,
/,
rank: int = 16,
scale: float = 1.0,
device: device | str | None = None,
dtype: dtype | None = None,
)
Low-Rank Adaptation (LoRA) layer.
This layer's purpose is to approximate a given layer by two smaller layers:
the down
layer (aka A) and the up
layer (aka B).
See [ arXiv:2106.09685] LoRA: Low-Rank Adaptation of Large Language Models for more details.
Note
This layer is not meant to be used directly. Instead, use one of its subclasses:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the LoRA. |
required |
rank
|
int
|
The rank of the LoRA. |
16
|
scale
|
float
|
The scale of the LoRA. |
1.0
|
device
|
device | str | None
|
The device of the LoRA weights. |
None
|
dtype
|
dtype | None
|
The dtype of the LoRA weights. |
None
|
Source code in src/refiners/fluxion/adapters/lora.py
from_dict
classmethod
¶
Create a dictionary of LoRA layers from a state dict.
Expects the state dict to be a succession of down and up weights.
Source code in src/refiners/fluxion/adapters/lora.py
load_weights
¶
Load the (pre-trained) weights of the LoRA.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
down_weight
|
Tensor
|
The down weight. |
required |
up_weight
|
Tensor
|
The up weight. |
required |
Source code in src/refiners/fluxion/adapters/lora.py
lora_layers
abstractmethod
¶
reset_parameters
¶
LoraAdapter
¶
LoraAdapter(target: WeightedModule, /, *loras: Lora[Any])
Bases: Sum
, Adapter[WeightedModule]
Adapter for LoRA layers.
This adapter simply sums the target layer with the given LoRA layers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target
|
WeightedModule
|
The target layer. |
required |
loras
|
Lora[Any]
|
The LoRA layers. |
()
|
Source code in src/refiners/fluxion/adapters/lora.py
add_lora
¶
Add a LoRA layer to the adapter.
Raises:
Type | Description |
---|---|
AssertionError
|
If the adapter already contains a LoRA layer with the same name. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lora
|
Lora[Any]
|
The LoRA layer to add. |
required |
Source code in src/refiners/fluxion/adapters/lora.py
remove_lora
¶
Remove a LoRA layer from the adapter.
Note
If the adapter doesn't contain a LoRA layer with the given name, nothing happens and None
is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the LoRA layer to remove. |
required |
Source code in src/refiners/fluxion/adapters/lora.py
auto_attach_loras
¶
auto_attach_loras(
loras: dict[str, Lora[Any]],
target: Chain,
/,
include: list[str] | None = None,
exclude: list[str] | None = None,
sanity_check: bool = True,
debug_map: list[tuple[str, str]] | None = None,
) -> list[str]
Auto-attach several LoRA layers to a Chain.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loras
|
dict[str, Lora[Any]]
|
A dictionary of LoRA layers associated to their respective key. The keys are typically
derived from the state dict and only used for |
required |
target
|
Chain
|
The target Chain. |
required |
include
|
list[str] | None
|
A list of layer names, only layers with such a layer in their ancestors will be considered. |
None
|
exclude
|
list[str] | None
|
A list of layer names, layers with such a layer in their ancestors will not be considered. |
None
|
sanity_check
|
bool
|
Check that LoRAs passed are correctly attached. |
True
|
debug_map
|
list[tuple[str, str]] | None
|
Pass a list to get a debug mapping of key - path pairs of attached points. |
None
|
Returns: A list of keys of LoRA layers which failed to attach.