My code snippet should yield valid gradients for all tensors in xi.
Based on your initial code snippet it seems you want to get gradients for all subtensors, as you were initializing it with requires_grad=True.
If you want to keep a portion of a tensor constant without updating it, your new approach seems to be the right way to do so.