{"record":{"id":"70581882b18f3f2c","repo":"Stability-AI/generative-models","slug":"provide-num-res-blocks-either-as-an-int-globally","errorCode":null,"errorMessage":"provide num_res_blocks either as an int (globally constant) or as a list/tuple (per-level) with the same length as channel_mult","messagePattern":"provide num_res_blocks either as an int \\(globally constant\\) or as a list/tuple \\(per-level\\) with the same length as channel_mult","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sgm/modules/diffusionmodules/openaimodel.py","lineNumber":606,"sourceCode":"            ), \"Either num_heads or num_head_channels has to be set\"\n\n        if num_head_channels == -1:\n            assert (\n                num_heads != -1\n            ), \"Either num_heads or num_head_channels has to be set\"\n\n        self.in_channels = in_channels\n        self.model_channels = model_channels\n        self.out_channels = out_channels\n        if isinstance(transformer_depth, int):\n            transformer_depth = len(channel_mult) * [transformer_depth]\n        transformer_depth_middle = transformer_depth[-1]\n\n        if isinstance(num_res_blocks, int):\n            self.num_res_blocks = len(channel_mult) * [num_res_blocks]\n        else:\n            if len(num_res_blocks) != len(channel_mult):\n                raise ValueError(\n                    \"provide num_res_blocks either as an int (globally constant) or \"\n                    \"as a list/tuple (per-level) with the same length as channel_mult\"\n                )\n            self.num_res_blocks = num_res_blocks\n\n        if disable_self_attentions is not None:\n            assert len(disable_self_attentions) == len(channel_mult)\n        if num_attention_blocks is not None:\n            assert len(num_attention_blocks) == len(self.num_res_blocks)\n            assert all(\n                map(\n                    lambda i: self.num_res_blocks[i] >= num_attention_blocks[i],\n                    range(len(num_attention_blocks)),\n                )\n            )\n            logpy.info(\n                f\"Constructor of UNetModel received num_attention_blocks={num_attention_blocks}. \"\n                f\"This option has LESS priority than attention_resolutions {attention_resolutions}, \"","sourceCodeStart":588,"sourceCodeEnd":624,"githubUrl":"https://github.com/Stability-AI/generative-models/blob/e8cd657656fa5d61688191730d0e03242bf4ed44/sgm/modules/diffusionmodules/openaimodel.py#L588-L624","documentation":"UNetModel validates that num_res_blocks is either a single int (broadcast to all levels) or a list/tuple whose length equals len(channel_mult). A mismatched list length raises ValueError during model construction.","triggerScenarios":"Passing num_res_blocks as a list of length != len(channel_mult), e.g. [2,2,2] with channel_mult [1,2,4,4] in the UNet config.","commonSituations":"Hand-edited diffusion model YAMLs where resolution/channel_mult was changed but num_res_blocks list not updated; porting configs from UNet variants with different level counts.","solutions":["Make the num_res_blocks list the same length as channel_mult","Replace the list with a single int to broadcast it to every level","Regenerate the config from the reference model definition"],"exampleFix":"// before (yaml)\nchannel_mult: [1, 2, 4, 4]\nnum_res_blocks: [2, 2, 2]\n// after (yaml)\nchannel_mult: [1, 2, 4, 4]\nnum_res_blocks: [2, 2, 2, 2]  # or just 2","handlingStrategy":"validation","validationCode":"if not isinstance(num_res_blocks, int) and len(num_res_blocks) != len(channel_mult):\n    raise ValueError(\n        f\"num_res_blocks has length {len(num_res_blocks)} but channel_mult has {len(channel_mult)} levels\"\n    )","typeGuard":"def num_res_blocks_ok(nrb, channel_mult) -> bool:\n    return isinstance(nrb, int) or (isinstance(nrb, (list, tuple)) and len(nrb) == len(channel_mult))","tryCatchPattern":"try:\n    model = UNetModel(**unet_config)\nexcept ValueError as e:\n    if \"num_res_blocks\" in str(e):\n        unet_config[\"num_res_blocks\"] = unet_config[\"channel_mult\"].__len__() * [2]\n        model = UNetModel(**unet_config)\n    else:\n        raise","preventionTips":["Always change channel_mult and num_res_blocks lists together","Prefer a scalar num_res_blocks unless per-level control is needed","Schema-validate UNet configs (length constraints) before instantiation"],"tags":["python","config","shape-mismatch","unet"],"backgroundTag":"config-length-mismatch","analyzedSha":"e8cd657656fa5d61688191730d0e03242bf4ed44","analyzedAt":"2026-08-29T11:23:43.234Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}