Pl. refer to documentation of conv2d’s groups parameter here.
According to this, weight parameter shall have the dimension of out_channels  x (in_channels / groups) x h x w.
In your example above,
input size = 2 x 4 x 2 x 2 (in_channels = 4)
weight size = 2 x 4 x 2 x 2 (out_channels = 2, in_channels / groups = 4)
When in_channels=4, groups=2, weight should have size of out_channels x 2 x h x w (i.e., in_channels / groups = 4 / 2 = 2).
import torch
from torch import nn
import torch.nn.functional as F

input = torch.ones((2,4,2,2))
weight = torch.ones((2,2,2,2))

input[0][1] = input[0][1]*2
input[1][0] = input[1][0]*3
input[1][1] = input[1][1]*4

weight[0][1] = weight[0][1]*2
weight[1][0] = weight[1][0]*3
weight[1][1] = weight[1][1]*4

result = F.conv2d(input, weight, padding=1, stride=1, groups=2)

print(input)
print(weight)
print(result)
print(result.shape)