I’m not aware of such a context manager, so would you mind creating a feature request on GitHub to discuss this proposal?
Meanwhile, you could copy this util function 24, which is used in tests and use it in combination with with torch.cuda.device(id).