{"record":{"id":"ac75f2e2b1169624","repo":"huggingface/pytorch-image-models","slug":"num-branches-num-blocks","errorCode":null,"errorMessage":"num_branches({}) <> num_blocks({})","messagePattern":"num_branches\\((.+?)\\) <> num_blocks\\((.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"timm/models/hrnet.py","lineNumber":406,"sourceCode":"            block_types,\n            num_blocks,\n            num_channels,\n            **dd,\n        )\n        self.fuse_layers = self._make_fuse_layers(**dd)\n        self.fuse_act = nn.ReLU(False)\n\n    def _check_branches(self, num_branches, block_types, num_blocks, num_in_chs, num_channels):\n        error_msg = ''\n        if num_branches != len(num_blocks):\n            error_msg = 'num_branches({}) <> num_blocks({})'.format(num_branches, len(num_blocks))\n        elif num_branches != len(num_channels):\n            error_msg = 'num_branches({}) <> num_channels({})'.format(num_branches, len(num_channels))\n        elif num_branches != len(num_in_chs):\n            error_msg = 'num_branches({}) <> num_in_chs({})'.format(num_branches, len(num_in_chs))\n        if error_msg:\n            _logger.error(error_msg)\n            raise ValueError(error_msg)\n\n    def _make_one_branch(self, branch_index, block_type, num_blocks, num_channels, stride=1, device=None, dtype=None):\n        dd = {'device': device, 'dtype': dtype}\n        downsample = None\n        if stride != 1 or self.num_in_chs[branch_index] != num_channels[branch_index] * block_type.expansion:\n            downsample = nn.Sequential(\n                nn.Conv2d(\n                    self.num_in_chs[branch_index],\n                    num_channels[branch_index] * block_type.expansion,\n                    kernel_size=1,\n                    stride=stride,\n                    bias=False,\n                    **dd,\n                ),\n                nn.BatchNorm2d(num_channels[branch_index] * block_type.expansion, momentum=_BN_MOMENTUM, **dd),\n            )\n\n        layers = [block_type(self.num_in_chs[branch_index], num_channels[branch_index], stride, downsample, **dd)]","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/models/hrnet.py#L388-L424","documentation":"HRNet's high-resolution module validates that num_branches matches the lengths of num_blocks, num_channels, and num_in_chs. The message shows the specific mismatch (here num_branches vs len(num_blocks)); any inconsistency raises ValueError after logging the error.","triggerScenarios":"Constructing HighResolutionModule directly (or a custom HRNet config) where e.g. num_branches=3 but num_blocks=[4,4,4,4] has 4 entries; usually when hand-modifying stage definitions.","commonSituations":"Customizing HRNet stage depths/widths for experiments and forgetting to update one of the parallel lists; merging HRNet configs from different variants (hrnet_w32 vs hrnet_w48); yaml-driven model builders that populate the lists independently.","solutions":["Make len(num_blocks), len(num_channels), len(num_in_chs) all equal num_branches","Prefer timm's hrnet_w32/hrnet_w48 factories, which carry consistent stage configs","Add an assert in your config loader: all(num_branches == len(l) for l in (num_blocks, num_channels, num_in_chs))"],"exampleFix":"# before\nHighResolutionModule(num_branches=3, num_blocks=[4,4,4,4], num_channels=[32,64,128], num_in_chs=[32,64,128])\n# after\nHighResolutionModule(num_branches=3, num_blocks=[4,4,4], num_channels=[32,64,128], num_in_chs=[32,64,128])","handlingStrategy":"validation","validationCode":"lens = {len(num_blocks), len(num_channels), len(num_in_chs)}\nassert len(lens) == 1 and num_branches == len(num_blocks), \\\n    f'inconsistent HRNet config: branches={num_branches}, blocks={len(num_blocks)}, chs={len(num_channels)}'\nmod = HighResolutionModule(num_branches, num_blocks, num_channels, num_in_chs, ...)","typeGuard":"def is_consistent_hrnet_cfg(num_branches, num_blocks, num_channels, num_in_chs) -> bool:\n    return num_branches == len(num_blocks) == len(num_channels) == len(num_in_chs)","tryCatchPattern":null,"preventionTips":["Derive num_branches from len(num_channels) instead of stating it separately","Validate stage config lists in a single config-loading function","Use timm's hrnet factories for standard widths"],"tags":["timm","hrnet","architecture","config-mismatch"],"backgroundTag":"inconsistent-architecture-config","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}