If you are using functions of other libraries, such as numpy or sklearn, you would have to implement the backward method manually, since Autograd cannot track these operations.
Wrapping the loss into a new tensor with requires_grad=True creates a new computation graph from this point, so that all preceding operations are detached (basically your model’s forward pass) and your model won’t get any valid gradients.
You could write a custom autograd.Function with the manual backward method as explained here 1 or you would have to use PyTorch methods only.