Consider
a = torch.randn(3,4, requires_grad=True)
b = a.median(1)
print("a =", a)
print("b = ",b)

giving
a = tensor([[-0.8903,  1.3827,  0.8102,  0.2972],
        [-0.5800, -0.3084, -0.6165, -0.5376],
        [-0.5144, -1.6414, -0.1507,  0.5274]], requires_grad=True)
b = torch.return_types.median(
values=tensor([ 0.2972, -0.5800, -0.5144], grad_fn=<MedianBackward1>),
indices=tensor([3, 0, 0]))

Then the backward will just be the grad_out scattered to the appropriate indices:
b.values.sum().backward()
print(a.grad)

gives
tensor([[0., 0., 0., 1.],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.]])

That also is why the indices are non-optional: They need to be computed for the backward, anyways.
Best regards
Thomas