AUTOMATIC1111/stable-diffusion-webui · error · RuntimeError

When merging instruct-pix2pix model with a normal one, A mus

Error message

When merging instruct-pix2pix model with a normal one, A must be the instruct-pix2pix model.

What it means

Same merge routine as the inpainting case, but for Instruct-Pix2Pix models: they use 8 input channels (4 latent + 4 downsampled instruction-image). The code only supports A=instruct-pix2pix (8ch) merged with B=normal (4ch). If A has 4 channels and B has 8 for a matched layer, this RuntimeError is raised.

Source

Thrown at modules/extras.py:203

    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

    bake_in_vae_filename = sd_vae.vae_dict.get(bake_in_vae, None)

View on GitHub (pinned to 82a973c043)

Solutions

  1. Swap the model slots: A must be the instruct-pix2pix model, B the normal model.
  2. Check the first conv weight shape of each checkpoint: 8 channels in dim 1 identifies the instruct-pix2pix model.
  3. If neither model is IP2P, the shape mismatch has another cause — inspect the mismatching key named in the follow-up assert to find a corrupted or incompatible layer.

Example fix

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

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

Strategy: validation

Validate before calling

def channel_of(path, key_suffix='input_blocks.0.0.weight'):
    from safetensors import safe_open
    try:
        with safe_open(path, framework='pt') as f:
            for k in f.keys():
                if k.endswith(key_suffix):
                    return f.get_slice(k).get_shape()[1]
    except Exception:
        return None

a_ch, b_ch = channel_of(A), channel_of(B)
assert not (a_ch == 4 and b_ch == 8), 'A must be the instruct-pix2pix (8ch) model; swap A and B'

Try / catch

try:
    merged = merge(A, B, multiplier)
except RuntimeError as e:
    if 'instruct-pix2pix' in str(e):
        merged = merge(B, A, multiplier)
    else:
        raise

Prevention

When it happens

Trigger: Running the checkpoint merger with primary model A = a normal SD checkpoint and secondary model B = an Instruct-Pix2Pix checkpoint (layer shapes equal except dim 1 being 4 vs 8).

Common situations: Reversed A/B order in the Checkpoint Merger UI or in an API/script call when one of the models is instruct-pix2pix; confusing an IP2P model with an inpainting model.

Related errors


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