unfold is differentiable so won’t detach the tensor from the computation graph.
To make sure that’s indeed the case, you could check the .grad_fn of the output tensor of any operation and see if it points to a valid function (returning None would mean the tensor is not attached to a computation graph).
To check the correctness of the gradient calculation you could use gradcheck.