You probably want to

use const torch::OptionalDeviceGuard device_guard(a.device()); or so.
This is similar to with torch.cuda.device(...) in Python 2.
Check that a, b, c actually are on the same device.

Best regards
Thomas
P.S.: If you take inspiration from PyTorch internals: The reason you don’t see this in aten/src/Aten/native/cuda/* is that there is a auto-generated wrapper doing it for you.
P.P.S.: The new torchvision C++ extension 19 does it right and is a great example of a model PyTorch extension.