AUTOMATIC1111/stable-diffusion-webui · error · RuntimeError

When merging inpainting model with a normal one, A must be t

Error message

When merging inpainting model with a normal one, A must be the inpainting model.

What it means

Thrown by add_extra_paste_field / model-merging code in modules/extras.py when checkpoint A (theta_0) has a conv layer with 4 input channels while checkpoint B (theta_1) has 9 for the same key, with all other dimensions equal. The 9-channel model is an inpainting model (4 latent + 4 masked-image latent + 1 mask), so the merge only supports A=inpainting (9ch) and B=normal (4ch). Passing them in the opposite order raises this RuntimeError.

Source

Thrown at modules/extras.py:201

    print("Merging...")
    shared.state.textinfo = 'Merging A and B'
    shared.state.sampling_steps = len(theta_0.keys())
    for key in tqdm.tqdm(theta_0.keys()):
        if theta_1 and 'model' in key and key in theta_1:

            if key in checkpoint_dict_skip_on_merge:
                continue

            a = theta_0[key]
            b = theta_1[key]

            # this enables merging an inpainting model (A) with another one (B);
            # where normal model would have 4 channels, for latenst space, inpainting model would
            # have another 4 channels for unmasked picture's latent space, plus one channel for mask, for a total of 9
            if a.shape != b.shape and a.shape[0:1] + a.shape[2:] == b.shape[0:1] + b.shape[2:]:
                if a.shape[1] == 4 and b.shape[1] == 9:
                    raise RuntimeError("When merging inpainting model with a normal one, A must be the inpainting model.")
                if a.shape[1] == 4 and b.shape[1] == 8:
                    raise RuntimeError("When merging instruct-pix2pix model with a normal one, A must be the instruct-pix2pix model.")

                if a.shape[1] == 8 and b.shape[1] == 4:#If we have an Instruct-Pix2Pix model...
                    theta_0[key][:, 0:4, :, :] = theta_func2(a[:, 0:4, :, :], b, multiplier)#Merge only the vectors the models have in common.  Otherwise we get an error due to dimension mismatch.
                    result_is_instruct_pix2pix_model = True
                else:
                    assert a.shape[1] == 9 and b.shape[1] == 4, f"Bad dimensions for merged layer {key}: A={a.shape}, B={b.shape}"
                    theta_0[key][:, 0:4, :, :] = theta_func2(a[:, 0:4, :, :], b, multiplier)
                    result_is_inpainting_model = True
            else:
                theta_0[key] = theta_func2(a, b, multiplier)

            theta_0[key] = to_half(theta_0[key], save_as_half)

        shared.state.sampling_step += 1

    del theta_1

View on GitHub (pinned to 82a973c043)

Solutions

  1. Swap the two checkpoints: put the inpainting model in slot A (primary) and the normal model in slot B (secondary), then merge again.
  2. Verify which model is the inpainting one by inspecting model.model.diffusion_model.input_blocks.0.0.weight shape in the checkpoint — it should have 9 channels.
  3. If you intentionally want the 4-channel model dominant, be aware the code only supports A=inpainting; merge B into A's first 4 channels is not implemented — instead merge with A as inpainting and use an appropriate multiplier.

Example fix

# before
merge(checkpoint_A=normal_model, checkpoint_B=inpainting_model, ...)  # RuntimeError

# after
merge(checkpoint_A=inpainting_model, checkpoint_B=normal_model, ...)
Defensive patterns

Strategy: validation

Validate before calling

import torch

def first_conv_channels(ckpt):
    sd = torch.load(ckpt, map_location='meta') if False else None
    # lightweight: use safetensors when possible
    from safetensors import safe_open
    with safe_open(ckpt, framework='pt') as f:
        for k in f.keys():
            if 'input_blocks.0.0' in k and 'weight' in k:
                return f.get_slice(k).get_shape()[1]
    return None

def is_inpainting(path):
    return first_conv_channels(path) == 9

assert is_inpainting(model_a_path), 'A must be the inpainting (9ch) model'

Try / catch

try:
    merged = merge(A, B, multiplier)
except RuntimeError as e:
    if 'A must be the inpainting model' in str(e):
        merged = merge(B, A, multiplier)  # swap and retry
    else:
        raise

Prevention

When it happens

Trigger: Calling the checkpoint merger (extras tab / modelmerger API) with primary model A = a normal SD checkpoint and secondary model B = an inpainting checkpoint (or any pair where the matched layer has a.shape[1]==4 and b.shape[1]==9 while a.shape != b.shape otherwise matching on dims 0 and 2+).

Common situations: User swaps the two model fields in the Checkpoint Merger UI, or a script calls the merge API with the argument order reversed; also when merging SD 2.0 512-inpainting (9ch) with a base model in the wrong slot.

Related errors


AI-assisted analysis of AUTOMATIC1111/stable-diffusion-webui@82a973c043 (2026-08-14). Data as JSON: /api/errors/fdb0e79dc87ae3b1. Report an issue: GitHub.