invoke-ai/InvokeAI · error · ValueError
in_channels must be divisible by groups
Error message
in_channels must be divisible by groups
What it means
The PidiNet model's Conv2d wrapper validates group-convolution arguments like nn.Conv2d does: in_channels must be an integer multiple of groups. It raises ValueError('in_channels must be divisible by groups') in __init__ when that invariant is violated, before any weight tensors are created.
Source
Thrown at invokeai/backend/image_util/pidi/model.py:353
else:
buffer = torch.zeros(shape[0], shape[1], 5 * 5).to(weights.device)
weights = weights.view(shape[0], shape[1], -1)
buffer[:, :, [0, 2, 4, 10, 14, 20, 22, 24]] = weights[:, :, 1:]
buffer[:, :, [6, 7, 8, 11, 13, 16, 17, 18]] = -weights[:, :, 1:]
buffer[:, :, 12] = 0
buffer = buffer.view(shape[0], shape[1], 5, 5)
y = F.conv2d(x, buffer, bias, stride=stride, padding=padding, dilation=dilation, groups=groups)
return y
return func
else:
print('impossible to be here unless you force that')
return None
class Conv2d(nn.Module):
def __init__(self, pdc, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=False):
super(Conv2d, self).__init__()
if in_channels % groups != 0:
raise ValueError('in_channels must be divisible by groups')
if out_channels % groups != 0:
raise ValueError('out_channels must be divisible by groups')
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.dilation = dilation
self.groups = groups
self.weight = nn.Parameter(torch.Tensor(out_channels, in_channels // groups, kernel_size, kernel_size))
if bias:
self.bias = nn.Parameter(torch.Tensor(out_channels))
else:
self.register_parameter('bias', None)
self.reset_parameters()
self.pdc = pdc
def reset_parameters(self):View on GitHub (pinned to 0b6a024f2f)
Solutions
- Set groups=1 (the default) unless grouped convolutions are specifically required.
- For depthwise convolutions use groups=in_channels (which trivially divides itself).
- Adjust in_channels (or the preceding layer's out_channels) so it is an integer multiple of groups.
- Reduce groups to a divisor of in_channels, e.g. groups = gcd(in_channels, desired_groups).
Example fix
// before conv = Conv2d(pdc, in_channels=3, out_channels=32, kernel_size=3, groups=2) # 3 % 2 != 0 // after conv = Conv2d(pdc, in_channels=4, out_channels=32, kernel_size=3, groups=2) # or groups=1
Defensive patterns
Strategy: validation
Validate before calling
if groups > 1 and in_channels % groups != 0:
raise ValueError(f'in_channels={in_channels} not divisible by groups={groups}') Try / catch
try:
conv = Conv2d(pdc, in_channels, out_channels, kernel_size, groups=groups)
except ValueError as e:
if 'divisible by groups' in str(e):
groups = 1
conv = Conv2d(pdc, in_channels, out_channels, kernel_size, groups=groups)
else:
raise Prevention
- Default to groups=1; only opt into grouped convolutions deliberately.
- For depthwise layers, set groups=in_channels so the divisibility invariant holds by construction.
- When changing channel counts, recompute every dependent layer's groups in one place.
- Sanity-check configurations with a small forward pass at build time.
When it happens
Trigger: Constructing invokeai/backend/image_util/pidi/model.py Conv2d(pdc, in_channels, out_channels, kernel_size, ..., groups=g) where in_channels % g != 0 — e.g. in_channels=3 with groups=2, or any groups > 1 that does not evenly divide the input channel count.
Common situations: Hand-editing the network to add depthwise (groups=in_channels) or grouped convolutions and miscounting channels; changing the input channel count (e.g. grayscale or 4-channel input) without updating grouped layers; porting layers from another model with different channel widths.
Related errors
- out_channels must be divisible by groups
- Unknown model (%s)
- A submodel type (Tokenizer or TextEncoder) must be provided.
- A submodel type must be provided when loading main pipelines
- Unsupported mask shape: {mask.shape}. Expected (1, h, w) or
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/b89a2aab5de45133.
Report an issue: GitHub.