-
Notifications
You must be signed in to change notification settings - Fork 0
02. Configuration
sd-optim uses Hydra to manage configuration through YAML files located in the conf/ directory. After following the 01. Getting Started guide, you should have copied the .tmpl.yaml files to .yaml files. This page explains the settings within those files.
This is the primary configuration file controlling the entire optimization run.
Sections:
-
defaults: Internal Hydra setting. Do not change. -
run_name: A name for your optimization run. Used to create the output directory name inlogs/. You can use variables like${merge_method}. -
hydra: Hydra-specific settings, mainly defining the output directory structure using therun_name.
Defines how sd-optim connects to your running Stable Diffusion WebUI's API.
-
webui_urls: A map of WebUI identifiers (keys) to their base URLs (values). -
webui: Select the identifier of the WebUI you are running (e.g.,forge,a1111,comfy,swarm). -
url: (Do not set manually) Automatically determined based on the selectedwebui.
# Example WebUI Connection
webui_urls:
a1111: http://localhost:7860
forge: http://localhost:7860
comfy: http://localhost:8188
webui: forge
url: "${webui_urls[${webui}]}" # This selects http://localhost:7860Specifies locations for necessary files.
-
configs_dir: Path to your customsd-mechablock definition.yamlfiles. -
conversion_dir: Path to your customsd-mechaconverter.pyscripts. -
wildcards_dir: Path to the directory containing your wildcard.txtfiles (used for prompt generation). -
scorer_model_dir: Path to the directory where aesthetic/quality scorer models will be downloaded and stored.
Defines the models to be merged.
-
model_paths: (Required) A list of strings, each pointing to the path of an input model file (.safetensors). At least one model is needed formergeorlayer_adjustmodes. Loras are detected automatically. -
base_model_index: (Required for some merge methods) The index (starting from 0) within themodel_pathslist corresponding to the model that should be used as the base when calculating deltas (e.g., foradd_difference).
Controls how the sd-mecha library performs the merge.
-
merge_method: (Required formergemode) The name (identifier string) of thesd-mechamerge method to use (e.g.,weighted_sum,ties_sum_extended). Can be a built-in method or a custom one (see 06. Merge Methods). -
device: The primary device (cudaorcpu) forsd-mechacalculations during the merge. -
threads: Number of threadssd-mechashould use for merging. -
merge_dtype: The precision used during the merge calculation (fp16,bf16,fp32,fp64). Usingfp32orfp64is recommended for stability, even if saving in lower precision. -
save_dtype: The precision used when saving the final merged model file (fp16,bf16,fp32,fp64). -
add_extra_keys: IfTrue, adds emptyv_predandztsnrkeys to the saved model. Useful for ensuring compatibility with v-prediction checkpoints.
General settings for the optimization run.
-
save_merge_artifacts: IfTrue, saves a self-contained, runnable.pyscript for each merge that can reproduce it exactly. Highly recommended for sharing and debugging. -
save_best: IfTrue, keeps a separate copy of the model file from the iteration that achieved the best score so far.
Selects the primary operation mode for the tool.
-
optimization_mode: Choose one:-
merge: Optimize hyperparameters for the specifiedmerge_methodapplied to themodel_paths. -
layer_adjust: Optimize layer adjustment parameters (brightness, contrast, etc.) applied to the first model inmodel_paths.. -
recipe: Optimize hyperparameters within an existing.mecharecipe file.
-
-
recipe_optimization: (Settings used only ifoptimization_mode: recipe)-
recipe_path: Path to the input.mechafile. -
target_nodes: A string or list of strings referencing the node(s) inside the recipe file (e.g.,'&3',['&3', '&4']) whose parameters should be optimized. -
target_params: A list of parameter names within the target node(s) to optimize (e.g.,[alpha, beta]).
-
Configures the chosen optimization algorithm.
-
Optimizer Choice: Set one of
bayesoroptunatoTrue.-
bayes: Uses the bayesian-optimization library. -
optuna: Uses the Optuna library.
-
-
Common Settings:
-
random_state: Seed for reproducibility (-1 for random). Affects both initial sampling and optimizer behavior. -
init_points: Number of initial exploration points/trials before exploitation begins. -
n_iters: Number of main exploitation iterations/trials. Total evaluations =init_points + n_iters. -
load_log_file: (Optional) Path to a previous run's log file (.jsonfor Bayes,.jsonlfor Optuna) to resume optimization. -
reset_log_file: IfTruewhen resuming, overwrites the log file; otherwise, appends.
-
-
Bayes-Specific Settings (
bayes: True): See 05. Optimization Strategies for details.-
sampler: Strategy forinit_points(random,latin_hypercube,sobol,halton). -
acquisition_function:kind(ucb,ei,poi) and related parameters (kappa,xi, etc.). -
bounds_transformer: Domain reduction settings (enabled,gamma_osc, etc.).
-
-
Optuna-Specific Settings (
optuna: True): See 05. Optimization Strategies for details.-
storage_dir: Folder where Optuna's study database files (.db) will be saved. -
resume_from_study: To resume a specific study, provide its name here (e.g.,"run_20250612_191003_tpe"). Set tonullfor new runs. -
fork_study: IfTruewhen resuming, creates a new study that starts by enqueueing all successful trials from the parent study. -
sampler: Nested dictionary defining the Optuna sampler (typeand specific options). -
use_pruning,pruner_type: Settings for trial pruning. -
early_stopping,patience,min_improvement: Settings for early stopping. -
n_jobs: Number of parallel trials (use carefully, experimental). -
launch_dashboard: IfTrue, automatically launches the Optuna dashboard in the background. -
dashboard_port: Port for the Optuna dashboard.
-
Settings controlling the image generation process performed by the WebUI.
-
batch_size: How many images to generate for each payload listed in yourcargo_*.yamlfile during each optimization iteration. Total images per iteration =batch_size * number_of_payloads. -
save_imgs: IfTrue, generated images are saved in thelogs/<run_folder>/imgs/directory. Automatically enabled formanualscoring. -
img_average_type: How scores for multiple images generated within the same iteration (due tobatch_size> 1 or multiple payloads) are averaged together before being reported to the optimizer (arithmetic,geometric,quadratic). -
generator_concurrency_limit: Max number of simultaneous API requests to the WebUI. -
generator_keepalive_interval: Interval (seconds) for sending TCP Keep-Alive probes to prevent connection drops. -
generator_total_timeout: Max total time (seconds) for a single API call. Set to0ornullto disable client timeout.
Settings controlling how generated images are evaluated. See 04. Scoring for details.
-
scorer_method: List of scorer identifiers (e.g.,cityaes,imagereward,manual). -
scorer_average_type: How scores from different scorers for the same image are averaged (arithmetic,geometric,quadratic). -
scorer_weight: (Optional) Weights for combining scores from different scorers. -
scorer_filters: (Optional) Exclude specific scorers from running on certain payloads. -
scorer_default_device: Default device (cpuorcuda) for running scorers. -
scorer_device: (Optional) Override the device for specific scorers. -
scorer_alt_location: (Optional) Specify custom paths for scorer models. -
scorer_print_individual: IfTrue, shows the score from each individual scorer. -
hpsv3_uncertainty_penalty: A multiplier for how much thehpsv3scorer's uncertainty (sigma) penalizes its final score. Default is0.5. - Undocumented scorer specific settings might be present at any time depending on what scorers are implemented, check their code specifically.
Controls the generation of plots summarizing the optimization run.
-
convergence_plot: IfTrue, saves a plot showing the best score found over iterations/trials. - (Other plot to be added in the future)
This file is the core of the optimization process, determining what to optimize and how. It uses a flexible component-and-strategy system to apply optimizable parameters from your merge method to different parts of the model.
If you are using a custom-defined set of blocks (e.g., sdxl-optim_blocks.yaml), specify its identifier here. The optimizer will use this for any component that is defined within it. For other components, it will fall back to the model's default key structure.
custom_block_config_id: "sdxl-optim_blocks"This is a list where each item defines the optimization plan for a model component (like unet, clip_l, etc.).
# Example guide.yaml structure
components:
- name: unet
optimize_params: [alpha, beta] # Optional: Default params for all strategies in this component.
strategies:
- type: group
target_type: block # Target block names from your custom_block_config_id
groups:
- name: unet_in
keys: ["UNET_IN*"] # Wildcard for all input blocks
- name: unet_mid
keys: ["UNET_MID*"]
- type: select
target_type: key # Target raw key names from the base model
optimize_params: [alpha] # Override component's default, only use alpha here.
keys: ["*.output_blocks.8.*"]Component Keys:
-
name: (Required) The name of the component (e.g.,unet,clip_l). Must match a component in yourcustom_block_config_idor the base model's config. -
optimize_params: (Optional) A list of parameter names from your merge method (e.g.,[alpha, rank_ratio]) to be applied by default to all strategies within this component. -
strategies: (Required) A list of strategy blocks that define how theoptimize_paramsare applied.
Strategy Block Keys:
-
type: (Required) The strategy to use.-
all: Create an independent parameter for every single key/block in the component. (e.g.,UNET_IN00_alpha,UNET_IN01_alpha, ...). Use with caution! -
select: Create an independent parameter for only the keys/blocks that match the wildcard patterns in thekeyslist. -
group: Create a single shared parameter for all keys/blocks matching the patterns within each defined group. (e.g., a singleunet_in_alphafor all input blocks). -
single: Create one single shared parameter for the entire component. -
none: Exclude this component/part from optimization.
-
-
target_type: (Optional) Specifies whether thekeysrefer toblocknames (from your custom config) orkeynames (from the base model). If omitted, it defaults toblockif the component is in the custom config, otherwisekey. -
optimize_params: (Optional) Override the component-leveloptimize_paramsfor just this strategy. -
keys: (Required forselect,group) A list of key/block names or wildcard patterns. -
groups: (Required forgroup) A list of group definitions, each with anameand a list ofkeys.
This optional dictionary allows you to override the default optimization bounds for any parameter generated by the strategies.
Parameter Naming Convention:
-
all/select:{key_or_block_name}_{param_name}(e.g.,UNET_IN04_alpha) -
group:{group_name}_{param_name}(e.g.,unet_in_alpha)
Bound Formats:
-
Continuous Range:
param_name: (0.1, 0.9)(using parentheses()). The optimizer can select any float value between 0.1 and 0.9. -
Categorical:
param_name: [8, 16, 32, 64](using square brackets[]). The optimizer will only choose from the specific values provided in the list. -
Fixed Value:
param_name: 0.75(This parameter will not be optimized).
Important
The use of parentheses () vs. square brackets [] is critical.
-
(min, max)defines a continuous range. -
[val1, val2, ...]defines a categorical choice. For a simple range,[0.1, 0.9]would be treated as a categorical choice between only 0.1 and 0.9, not the numbers in between.
-
Advanced (Optuna):
param_name: range: [0.01, 1.0] log: true # Use logarithmic scale step: 0.01 # Suggest values in increments
You can also set a global override for a base parameter name (e.g., alpha: [0.2, 0.8]), which will apply to all generated ..._alpha parameters that don't have a more specific override.
# Optional: Override default bounds (e.g., 0.0 to 1.0) for specific parameters
custom_bounds:
# Parameter names depend on strategy, names, keys, and merge method params (e.g., _alpha)
# See examples below and parameter naming conventions section.
# Examples:
# constraint: [0.01, 0.2] # Applies to all generated parameters named 'constraint'
# alpha: [0.1, 0.9] # Applies to all generated parameters ending in '_alpha'
# specific.key.name_alpha: [0.0, 0.5] # More specific override
# my_group_1_k: [0.1, 0.5] # Override for a group parameter
# unet_single_param_alpha: [0.2, 0.8] # Override for a single component parameter
# fixed_parameter_name: 0.75 # Fix a value (not optimized)
# categorical_parameter: [128, 256, 512] # Categorical choices
# binary_parameter: [0, 1] # Binary choice