{"record":{"id":"20a3d8d9cfff04d4","repo":"invoke-ai/InvokeAI","slug":"out-channels-must-be-divisible-by-groups","errorCode":null,"errorMessage":"out_channels must be divisible by groups","messagePattern":"out_channels must be divisible by groups","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/image_util/pidi/model.py","lineNumber":355,"sourceCode":"            weights = weights.view(shape[0], shape[1], -1)\n            buffer[:, :, [0, 2, 4, 10, 14, 20, 22, 24]] = weights[:, :, 1:]\n            buffer[:, :, [6, 7, 8, 11, 13, 16, 17, 18]] = -weights[:, :, 1:]\n            buffer[:, :, 12] = 0\n            buffer = buffer.view(shape[0], shape[1], 5, 5)\n            y = F.conv2d(x, buffer, bias, stride=stride, padding=padding, dilation=dilation, groups=groups)\n            return y\n        return func\n    else:\n        print('impossible to be here unless you force that')\n        return None\n\nclass Conv2d(nn.Module):\n    def __init__(self, pdc, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=False):\n        super(Conv2d, self).__init__()\n        if in_channels % groups != 0:\n            raise ValueError('in_channels must be divisible by groups')\n        if out_channels % groups != 0:\n            raise ValueError('out_channels must be divisible by groups')\n        self.in_channels = in_channels\n        self.out_channels = out_channels\n        self.kernel_size = kernel_size\n        self.stride = stride\n        self.padding = padding\n        self.dilation = dilation\n        self.groups = groups\n        self.weight = nn.Parameter(torch.Tensor(out_channels, in_channels // groups, kernel_size, kernel_size))\n        if bias:\n            self.bias = nn.Parameter(torch.Tensor(out_channels))\n        else:\n            self.register_parameter('bias', None)\n        self.reset_parameters()\n        self.pdc = pdc\n\n    def reset_parameters(self):\n        nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5))\n        if self.bias is not None:","sourceCodeStart":337,"sourceCodeEnd":373,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/image_util/pidi/model.py#L337-L373","documentation":"Same Conv2d wrapper in PidiNet's model.py also requires out_channels to be an integer multiple of groups, mirroring nn.Conv2d semantics. ValueError('out_channels must be divisible by groups') is raised in __init__ when out_channels % groups != 0, since each group must receive an equal share of output channels.","triggerScenarios":"Constructing Conv2d(pdc, in_channels, out_channels, kernel_size, ..., groups=g) where out_channels % g != 0 — e.g. out_channels=10 with groups=2 (10 % 2 == 0 is fine, but out_channels=10 with groups=4 fails), typically after hand-editing channel widths.","commonSituations":"Changing a layer's output width for a custom head while leaving grouped configuration intact; copying grouped-conv settings from another architecture; math errors when deriving group counts from channel counts.","solutions":["Set groups=1 unless grouped convolutions are required.","Round out_channels up to the nearest multiple of groups (e.g. ceil_div(out_channels, groups) * groups).","Reduce groups to a divisor of out_channels.","If both grouped constraints are hard, pick groups = gcd(in_channels, out_channels)."],"exampleFix":"// before\nconv = Conv2d(pdc, in_channels=32, out_channels=10, kernel_size=3, groups=4)  # 10 % 4 != 0\n// after\nconv = Conv2d(pdc, in_channels=32, out_channels=12, kernel_size=3, groups=4)","handlingStrategy":"validation","validationCode":"if groups > 1 and out_channels % groups != 0:\n    out_channels = ((out_channels + groups - 1) // groups) * groups  # round up to multiple","typeGuard":null,"tryCatchPattern":"try:\n    conv = Conv2d(pdc, in_channels, out_channels, kernel_size, groups=groups)\nexcept ValueError as e:\n    if 'divisible by groups' in str(e):\n        groups = 1\n        conv = Conv2d(pdc, in_channels, out_channels, kernel_size, groups=groups)\n    else:\n        raise","preventionTips":["Derive out_channels as a multiple of groups: (ceil(width/groups) * groups).","Use gcd(in_channels, out_channels) as the maximum safe group count.","Keep grouped-conv channel math in a shared helper, not per-layer literals.","Validate the whole architecture config before instantiating layers."],"tags":["python","pytorch","convolution","invalid-argument"],"backgroundTag":"out-channels-divisible-by-groups","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}