My test:
x = torch.rand(256, 2, requires_grad=True)
y = torch.randint(0, 10, (256, ), requires_grad=True)
custom_op = nn.Linear(2, 10)
res = torch.autograd.gradcheck(custom_op, (x, ))
print(res)
My result:
RuntimeError: Jacobian mismatch for output 0 with respect to input 0,
numerical:tensor([[-0.1639, -0.4768, 0.3874, ..., 0.0000, 0.0000, 0.0000],
[ 0.6892, -0.0894, -0.0894, ..., 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0000, 0.0000],
...,
[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, ..., 0.0894, -0.3278, 0.4321],
[ 0.0000, 0.0000, 0.0000, ..., 0.3278, 0.1788, -0.2086]])
analytical:tensor([[-0.1609, -0.4752, 0.3847, ..., 0.0000, 0.0000, 0.0000],
[ 0.6913, -0.0883, -0.0926, ..., 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0000, 0.0000],
...,
[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, ..., 0.1128, -0.3249, 0.4321],
[ 0.0000, 0.0000, 0.0000, ..., 0.3346, 0.1746, -0.2108]])