{"record":{"id":"fdb0e79dc87ae3b1","repo":"AUTOMATIC1111/stable-diffusion-webui","slug":"when-merging-inpainting-model-with-a-normal-one-a","errorCode":null,"errorMessage":"When merging inpainting model with a normal one, A must be the inpainting model.","messagePattern":"When merging inpainting model with a normal one, A must be the inpainting model\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"modules/extras.py","lineNumber":201,"sourceCode":"\r\n    print(\"Merging...\")\r\n    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","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/82a973c04367123ae98bd9abdf80d9eda9b910e2/modules/extras.py#L183-L219","documentation":"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.","triggerScenarios":"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+).","commonSituations":"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.","solutions":["Swap the two checkpoints: put the inpainting model in slot A (primary) and the normal model in slot B (secondary), then merge again.","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.","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."],"exampleFix":"# before\nmerge(checkpoint_A=normal_model, checkpoint_B=inpainting_model, ...)  # RuntimeError\n\n# after\nmerge(checkpoint_A=inpainting_model, checkpoint_B=normal_model, ...)","handlingStrategy":"validation","validationCode":"import torch\n\ndef first_conv_channels(ckpt):\n    sd = torch.load(ckpt, map_location='meta') if False else None\n    # lightweight: use safetensors when possible\n    from safetensors import safe_open\n    with safe_open(ckpt, framework='pt') as f:\n        for k in f.keys():\n            if 'input_blocks.0.0' in k and 'weight' in k:\n                return f.get_slice(k).get_shape()[1]\n    return None\n\ndef is_inpainting(path):\n    return first_conv_channels(path) == 9\n\nassert is_inpainting(model_a_path), 'A must be the inpainting (9ch) model'","typeGuard":null,"tryCatchPattern":"try:\n    merged = merge(A, B, multiplier)\nexcept RuntimeError as e:\n    if 'A must be the inpainting model' in str(e):\n        merged = merge(B, A, multiplier)  # swap and retry\n    else:\n        raise","preventionTips":["Label checkpoints as inpainting/pix2pix/base in filenames so slot order is obvious.","Scripted merges should assert channel counts of the first conv before calling the merger."],"tags":["model-merge","inpainting","checkpoint","stable-diffusion"],"backgroundTag":null,"analyzedSha":"82a973c04367123ae98bd9abdf80d9eda9b910e2","analyzedAt":"2026-08-14T16:46:43.225Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}