detach() detaches the output from  the computationnal graph. So no gradient will be backproped along this variable.
torch.no_grad says that no operation should build the graph.
The difference is that one refers to only a given variable on which it’s called. The other affects all operations taking place within the with statement.