Model Converter
ConversionStage
¶
Bases: Enum
Represents the current stage of the conversion process.
Attributes:
Name | Type | Description |
---|---|---|
INIT |
The conversion process has not started. |
|
BASIC_LAYERS_MATCH |
The source and target models have the same number of basic layers. |
|
SHAPE_AND_LAYERS_MATCH |
The shape of both models agree. |
|
MODELS_OUTPUT_AGREE |
The source and target models agree. |
ModelConverter
¶
ModelConverter(
source_model: Module,
target_model: Module,
source_keys_to_skip: list[str] | None = None,
target_keys_to_skip: list[str] | None = None,
custom_layer_mapping: (
dict[type[Module], type[Module]] | None
) = None,
threshold: float = 1e-05,
skip_output_check: bool = False,
skip_init_check: bool = False,
verbose: bool = True,
)
Converts a model's state_dict to match another model's state_dict.
The conversion process consists of three stages
- Verify that the source and target models have the same number of basic layers.
- Find matching shapes and layers between the source and target models.
- Convert the source model's state_dict to match the target model's state_dict.
- Compare the outputs of the source and target models.
The conversion process can be run multiple times, and will resume from the last stage.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_model |
Module
|
The model to convert from. |
required |
target_model |
Module
|
The model to convert to. |
required |
source_keys_to_skip |
list[str] | None
|
A list of keys to skip when tracing the source model. |
None
|
target_keys_to_skip |
list[str] | None
|
A list of keys to skip when tracing the target model. |
None
|
custom_layer_mapping |
dict[type[Module], type[Module]] | None
|
A dictionary mapping custom layer types between the source and target models. |
None
|
threshold |
float
|
The threshold for comparing outputs between the source and target models. |
1e-05
|
skip_output_check |
bool
|
Whether to skip comparing the outputs of the source and target models. |
False
|
skip_init_check |
bool
|
Whether to skip checking that the source and target models have the same number of basic layers. |
False
|
verbose |
bool
|
Whether to print messages during the conversion process. |
True
|
Source code in src/refiners/fluxion/model_converter.py
compare_models
¶
compare_models(
source_args: ModuleArgs,
target_args: ModuleArgs | None = None,
threshold: float = 1e-05,
) -> bool
Compare the outputs of the source and target models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_args |
ModuleArgs
|
The arguments to pass to the source model it can be either a tuple of positional arguments,
a dictionary of keyword arguments, or a dictionary with |
required |
target_args |
ModuleArgs | None
|
The arguments to pass to the target model it can be either a tuple of positional arguments,
a dictionary of keyword arguments, or a dictionary with |
None
|
threshold |
float
|
The threshold for comparing outputs between the source and target models. |
1e-05
|
Returns:
Type | Description |
---|---|
bool
|
True if the outputs of the source and target models agree. |
Source code in src/refiners/fluxion/model_converter.py
get_mapping
¶
Get the mapping between the source and target models' state_dicts.
Source code in src/refiners/fluxion/model_converter.py
get_module_signature
¶
Get the signature of a module.
Source code in src/refiners/fluxion/model_converter.py
get_state_dict
¶
Get the converted state_dict.
map_state_dicts
¶
map_state_dicts(
source_args: ModuleArgs,
target_args: ModuleArgs | None = None,
) -> dict[str, str] | None
Find a mapping between the source and target models' state_dicts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_args |
ModuleArgs
|
The arguments to pass to the source model it can be either a tuple of positional arguments,
a dictionary of keyword arguments, or a dictionary with |
required |
target_args |
ModuleArgs | None
|
The arguments to pass to the target model it can be either a tuple of positional arguments,
a dictionary of keyword arguments, or a dictionary with |
None
|
Returns:
Type | Description |
---|---|
dict[str, str] | None
|
A dictionary mapping keys in the target model's state_dict to keys in the source model's state_dict. |
Source code in src/refiners/fluxion/model_converter.py
run
¶
run(
source_args: ModuleArgs,
target_args: ModuleArgs | None = None,
) -> bool
Run the conversion process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_args |
ModuleArgs
|
The arguments to pass to the source model it can be either a tuple of positional arguments,
a dictionary of keyword arguments, or a dictionary with |
required |
target_args |
ModuleArgs | None
|
The arguments to pass to the target model it can be either a tuple of positional arguments,
a dictionary of keyword arguments, or a dictionary with |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if the conversion process is done and the models agree. |
Source code in src/refiners/fluxion/model_converter.py
save_to_safetensors
¶
save_to_safetensors(
path: Path | str,
metadata: dict[str, str] | None = None,
half: bool = False,
) -> None
Save the converted model to a SafeTensors file.
Warning
This method can only be called after the conversion process is done.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path | str
|
The path to save the converted model to. |
required |
metadata |
dict[str, str] | None
|
Metadata to save with the converted model. |
None
|
half |
bool
|
Whether to save the converted model as half precision. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
If the conversion process is not done yet. Run |