Yes, if you are using numpy operations, Autograd won’t be able to track these operations and you would thus detach the computation graph. You could write a custom autograd.Function and define your backward method there as described in this tutorial 6.
However, based on your code snippet you could also replace the numpy functions (np.unique, sum(), np.log2) with the PyTorch equivalents.