Oh, right! I just tested with a new tensor that has unique values in all rows and columns, and the gradients are the same! Thanks a lot. It appears as the max and topk functions were returning different indices because of the same values, just as you suspected. Here are some results for reference:
tens = torch.Tensor([[[ 2.,  8., 10., 20.,  7.],
                      [15.,  3.,  2.,  4.,  9.],
                      [10., 17.,  6.,  7.,  8.],
                      [ 1.,  7., 12.,  6.,  2.],
                      [ 8.,  1.,  7.,  2., 10.]]])

a.grad
tensor([[[ 0., -1., -1., -4.,  0.],
         [-4.,  0.,  0.,  0., -1.],
         [-1., -4.,  0., -1.,  0.],
         [ 0.,  0., -4.,  0.,  0.],
         [ 0.,  0.,  0.,  0., -4.]]])

b.grad
tensor([[[ 0., -1., -1., -4.,  0.],
         [-4.,  0.,  0.,  0., -1.],
         [-1., -4.,  0., -1.,  0.],
         [ 0.,  0., -4.,  0.,  0.],
         [ 0.,  0.,  0.,  0., -4.]]])