In the code you posted the result of out.detach() is not stored or used, so has no effect.
For info, there is an in-place version of detach out.detach_() that tells pytorch not to try to backpropagate furthur.