{"record":{"id":"ebd8d5f75f9b105f","repo":"AUTOMATIC1111/stable-diffusion-webui","slug":"when-merging-instruct-pix2pix-model-with-a-normal","errorCode":null,"errorMessage":"When merging instruct-pix2pix model with a normal one, A must be the instruct-pix2pix model.","messagePattern":"When merging instruct-pix2pix model with a normal one, A must be the instruct-pix2pix model\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"modules/extras.py","lineNumber":203,"sourceCode":"    shared.state.textinfo = 'Merging A and B'\r\n    shared.state.sampling_steps = len(theta_0.keys())\r\n    for key in tqdm.tqdm(theta_0.keys()):\r\n        if theta_1 and 'model' in key and key in theta_1:\r\n\r\n            if key in checkpoint_dict_skip_on_merge:\r\n                continue\r\n\r\n            a = theta_0[key]\r\n            b = theta_1[key]\r\n\r\n            # this enables merging an inpainting model (A) with another one (B);\r\n            # where normal model would have 4 channels, for latenst space, inpainting model would\r\n            # have another 4 channels for unmasked picture's latent space, plus one channel for mask, for a total of 9\r\n            if a.shape != b.shape and a.shape[0:1] + a.shape[2:] == b.shape[0:1] + b.shape[2:]:\r\n                if a.shape[1] == 4 and b.shape[1] == 9:\r\n                    raise RuntimeError(\"When merging inpainting model with a normal one, A must be the inpainting model.\")\r\n                if a.shape[1] == 4 and b.shape[1] == 8:\r\n                    raise RuntimeError(\"When merging instruct-pix2pix model with a normal one, A must be the instruct-pix2pix model.\")\r\n\r\n                if a.shape[1] == 8 and b.shape[1] == 4:#If we have an Instruct-Pix2Pix model...\r\n                    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.\r\n                    result_is_instruct_pix2pix_model = True\r\n                else:\r\n                    assert a.shape[1] == 9 and b.shape[1] == 4, f\"Bad dimensions for merged layer {key}: A={a.shape}, B={b.shape}\"\r\n                    theta_0[key][:, 0:4, :, :] = theta_func2(a[:, 0:4, :, :], b, multiplier)\r\n                    result_is_inpainting_model = True\r\n            else:\r\n                theta_0[key] = theta_func2(a, b, multiplier)\r\n\r\n            theta_0[key] = to_half(theta_0[key], save_as_half)\r\n\r\n        shared.state.sampling_step += 1\r\n\r\n    del theta_1\r\n\r\n    bake_in_vae_filename = sd_vae.vae_dict.get(bake_in_vae, None)\r","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/82a973c04367123ae98bd9abdf80d9eda9b910e2/modules/extras.py#L185-L221","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Swap the model slots: A must be the instruct-pix2pix model, B the normal model.","Check the first conv weight shape of each checkpoint: 8 channels in dim 1 identifies the instruct-pix2pix model.","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."],"exampleFix":"# before\nmerge(checkpoint_A=normal_model, checkpoint_B=pix2pix_model, ...)  # RuntimeError\n\n# after\nmerge(checkpoint_A=pix2pix_model, checkpoint_B=normal_model, ...)","handlingStrategy":"validation","validationCode":"def channel_of(path, key_suffix='input_blocks.0.0.weight'):\n    from safetensors import safe_open\n    try:\n        with safe_open(path, framework='pt') as f:\n            for k in f.keys():\n                if k.endswith(key_suffix):\n                    return f.get_slice(k).get_shape()[1]\n    except Exception:\n        return None\n\na_ch, b_ch = channel_of(A), channel_of(B)\nassert not (a_ch == 4 and b_ch == 8), 'A must be the instruct-pix2pix (8ch) model; swap A and B'","typeGuard":null,"tryCatchPattern":"try:\n    merged = merge(A, B, multiplier)\nexcept RuntimeError as e:\n    if 'instruct-pix2pix' in str(e):\n        merged = merge(B, A, multiplier)\n    else:\n        raise","preventionTips":["Check first-conv channel counts (8 = pix2pix, 9 = inpainting, 4 = base) before merging.","Keep a manifest of which checkpoints are special-architecture models."],"tags":["model-merge","instruct-pix2pix","checkpoint","stable-diffusion"],"backgroundTag":null,"analyzedSha":"82a973c04367123ae98bd9abdf80d9eda9b910e2","analyzedAt":"2026-08-14T16:46:43.225Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}