Thanks ! this worked.
We could simplify it a bit, since we don’t want to compute gradients, but the outputs look great 
#Black and white input image x, 1x1xHxW
a = torch.Tensor([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
a = a.view((1,1,3,3))
G_x = F.conv2d(x, a)
b = torch.Tensor([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
b = b.view((1,1,3,3))
G_y = F.conv2d(x, b)
G = torch.sqrt(torch.pow(G_x,2)+ torch.pow(G_y,2))
res = P(G)