Implementation:AUTOMATIC1111 Stable diffusion webui Merge output configuration
| Knowledge Sources | |
|---|---|
| Domains | Model Precision, Checkpoint Merging, Model Optimization |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for configuring precision, VAE baking, metadata, and weight filtering of merged model output provided by stable-diffusion-webui.
Description
The merge output configuration consists of several utility functions and code paths within modules/extras.py that control how merged model weights are post-processed before saving:
- to_half(tensor, enable): Conditionally converts a float32 tensor to float16. Returns the tensor unchanged if
enableis False or if the tensor is not float32 (e.g., integer tensors are never converted). - read_metadata(primary, secondary, tertiary): Collects metadata dictionaries from up to three source checkpoint models and merges them into a single JSON string. This is used to populate the metadata preview in the UI.
- checkpoint_dict_skip_on_merge: A list of state dict keys that are always skipped during merging (e.g.,
cond_stage_model.transformer.text_model.embeddings.position_ids) because they are not weight tensors and should not be interpolated. - discard_weights: A regex pattern applied post-merge to remove unwanted keys from the output state dictionary.
- bake_in_vae: Replaces VAE weights in the merged model with those from a specified external VAE file.
Usage
These configuration options are passed as parameters to run_modelmerger and applied during the merge pipeline. The to_half function is applied per-tensor during the merge loop, while VAE baking and weight discarding happen after all interpolation is complete.
Code Reference
Source Location
- Repository: stable-diffusion-webui
- File:
modules/extras.py - Lines: L65 (checkpoint_dict_skip_on_merge), L68-72 (to_half), L75-85 (read_metadata), L221-242 (VAE baking and weight discarding within run_modelmerger)
Signature
checkpoint_dict_skip_on_merge = ["cond_stage_model.transformer.text_model.embeddings.position_ids"]
def to_half(tensor, enable):
if enable and tensor.dtype == torch.float:
return tensor.half()
return tensor
def read_metadata(primary_model_name, secondary_model_name, tertiary_model_name):
metadata = {}
for checkpoint_name in [primary_model_name, secondary_model_name, tertiary_model_name]:
checkpoint_info = sd_models.checkpoints_list.get(checkpoint_name, None)
if checkpoint_info is None:
continue
metadata.update(checkpoint_info.metadata)
return json.dumps(metadata, indent=4, ensure_ascii=False)
Import
from modules.extras import to_half, read_metadata
I/O Contract
Inputs
to_half:
| Name | Type | Required | Description |
|---|---|---|---|
| tensor | torch.Tensor | Yes | The weight tensor to potentially convert |
| enable | bool | Yes | Whether to perform the FP16 conversion |
read_metadata:
| Name | Type | Required | Description |
|---|---|---|---|
| primary_model_name | str | Yes | Title/key of the primary model in checkpoints_list |
| secondary_model_name | str | Yes | Title/key of the secondary model (can be empty) |
| tertiary_model_name | str | Yes | Title/key of the tertiary model (can be empty) |
Outputs
to_half:
| Name | Type | Description |
|---|---|---|
| return | torch.Tensor | The tensor in FP16 if conversion was enabled and input was FP32; otherwise the original tensor unchanged |
read_metadata:
| Name | Type | Description |
|---|---|---|
| return | str | JSON string of merged metadata from all specified source models |
Configuration Options
The following options are parameters to run_modelmerger that control output configuration:
| Option | Type | Description |
|---|---|---|
| save_as_half | bool | Convert all float32 weight tensors to float16 before saving |
| checkpoint_format | str | Output file extension: "safetensors" or "ckpt"
|
| bake_in_vae | str | Name of a VAE to embed into the merged model (looked up from sd_vae.vae_dict)
|
| discard_weights | str | Regex pattern; any state dict key matching this pattern is removed from the output |
| save_metadata | bool | Whether to save metadata into the output file |
| add_merge_recipe | bool | Whether to include a machine-readable merge recipe in metadata |
| copy_metadata_fields | bool | Whether to copy metadata from source models |
| metadata_json | str | Additional custom JSON metadata to include |
Usage Examples
Basic Usage
import torch
from modules.extras import to_half, read_metadata
# Convert a tensor to half precision
tensor = torch.randn(512, 512, dtype=torch.float32)
half_tensor = to_half(tensor, enable=True)
print(half_tensor.dtype) # torch.float16
# No conversion when disabled
same_tensor = to_half(tensor, enable=False)
print(same_tensor.dtype) # torch.float32
# Read merged metadata from source models
metadata_json = read_metadata(
"v1-5-pruned-emaonly.safetensors [6ce0161689]",
"dreamshaper_8.safetensors [abcd1234ef]",
""
)
print(metadata_json) # JSON string of merged metadata