How can I set `device` globally instead of specify this every time when I create tensors?

GPU acceleration is great. However, when I want to use this feature, I have to specify this every time when I create tensors, like

dev = torch.device("cuda")

torch.ones(2, 3, device=dev)

How can I set device globally to make it the default value, so that I don’t need to specify device in the following codes ?

1 Like

This previous post might be relevant: How to create a tensor on GPU as default - PyTorch Forums

1 Like

Thank you! It’s very helpful.