-
Notifications
You must be signed in to change notification settings - Fork 0
08. Advanced Usage
This page covers advanced features for users who are comfortable with the basic workflow of sd-optim.
For complex merges, you can optimize parameters directly within an existing .mecha recipe file instead of using a single merge method.
Configuration (config.yaml):
- Set
optimization_mode: recipe. - Configure the
recipe_optimizationblock:-
recipe_path: The full path to your.mechafile. -
target_nodes: The reference to the node(s) you want to optimize (e.g.,'&8'or['&8', '&12']). -
target_params: A list of the parameter names within that node to optimize (e.g.,[alpha, beta]).
-
The optimizer will then vary the specified parameters within the target node(s) of your recipe to find the highest scoring parameter values.
sd-optim, thanks to sd-mecha, is designed to be extensible. You can add your own block definitions, conversion scripts, and even new merge methods.
You can define custom groupings of model keys (layers) to target them specifically during optimization.
-
Create a Block YAML file: Create a
.yamlfile following thesd-mechaModelConfigformat. Give it a uniqueidentifier. For a complete example, seesd_optim/model_configs/sdxl-optim_blocks.yaml. -
Place the file: Put this file in the directory specified by
configs_dirin yourconfig.yaml. -
Use in Guide: In your
optimization_guide.yaml, set thecustom_block_config_idto your new identifier and usetarget_type: blockin your strategies to reference the block names.
Conversion scripts are a special type of merge method that translates between different ModelConfig formats. They are essential for using custom block definitions.
-
Create a Python script: Create a
.pyfile containing your conversion function. The function must be decorated with@sd_mecha.merge_method(..., is_conversion=True). For a complete example, seesd_optim/model_configs/convert_sdxl_optim_blocks.py. -
Place the file: Put this script in the directory specified by
conversion_dirin yourconfig.yaml. The script will be loaded automatically.
You can add your own merge algorithms to the optimizer.
-
Edit
merge_methods.py: Open thesd_optim/merge_methods.pyfile. -
Add your function: Add your new merge method as a static method inside the
MergeMethodsclass. It must be decorated with@merge_methodand have the correctParameterandReturntype hints fromsd-mecha. -
Use in Config: Set the
merge_methodinconfig.yamlto theidentifieryou gave your function in the decorator.
# Example of a custom merge method in sd_optim/merge_methods.py
class MergeMethods:
@merge_method
def pop_lora(
a: Parameter(Tensor, "weight"),
b: Parameter(Tensor, "weight"),
*,
alpha: Parameter(Tensor) = 0.5,
rank_ratio: Parameter(float) = 0.25,
# ... other parameters
) -> Return(Tensor, "weight"):
"""
Pivoted Orthogonal Projection
Merges tensors 'a' and 'b' using pivoted QR, projection, and low-rank.
"""
# ... implementation ...
return merged_tensorTo ensure you can perfectly reproduce a specific merge from an optimization run, sd-optim can generate a self-contained Python script for each iteration.
Note: This might exhibit yet undiscovered errors as it's complex code. (Perhaps needlessly so)
-
Enable: Set
save_merge_artifacts: Truein yourconfig.yaml. -
Output: For each iteration, a
_run.pyscript will be saved in thelogs/RUN_FOLDER/merge_artifacts/directory. -
Usage: This script contains the exact recipe, custom code, and settings used for that merge. You can run it with
python FILENAME_run.pyto generate the exact same model file again, independent ofsd-optim.
For long-running experiments, you can resume or fork previous Optuna studies. Studies are stored as .db files in the storage_dir (default: optuna_db/).
-
Resuming a Study (
resume_from_study):- To continue a previous run, set
resume_from_studyinconfig.yamlto the name of the study you want to resume (e.g.,"run_20250612_191003_tpe"). - The optimizer will load the existing
.dbfile and continue adding trials. -
Important: You cannot change the
scorer_methodwhen resuming a study directly.
- To continue a previous run, set
-
Forking a Study (
fork_study: True):- Use this if you want to start a new experiment based on a previous one (e.g., with different scorers).
- Set
resume_from_studyto the parent study name and setfork_study: True. - A new study and
.dbfile will be created. All successful trials from the parent study will be enqueued at the start of the new run, giving the optimizer a warm start before it begins generating new trials.