Strictly speaking this is no “detach” but it this way you can just set them to 0 with hooks:
import torch

def modify_grad(x, inds):
    x[inds] = 0 
    return x

a = torch.ones((5, 3)).requires_grad_(True)

inds = [2, 4]  # rows which will have zero gradients
b = 2 * a
b.register_hook(lambda x: modify_grad(x, inds))

loss = torch.sum(b**2)
loss.backward()

print('a.grad', a.grad)

will print
a.grad tensor([[8., 8., 8.],
        [8., 8., 8.],
        [0., 0., 0.],
        [8., 8., 8.],
        [0., 0., 0.]])