Your code seems to work on this dummy example:
input_img = torch.zeros(5, 100, 100)
input_img[:, 25:75, 25:75] = 1.
input_img = input_img.unsqueeze(0)

a = torch.tensor([[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
                  [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
                  [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
                  [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]],
                  [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]]).float()

x = input_img
w = a
w = w.unsqueeze(1)
gx = F.conv2d(x, w, padding=1, groups=5)

f, axarr = plt.subplots(5)
for idx in range(5):
    axarr[idx].imshow(gx[0, idx].numpy())