Copy
tensorplay.set_default_device
- tensorplay.set_default_device(device: Device) None[source]
Sets the default
tensorplay.Tensorto be allocated ondevice. This does not affect factory function calls which are called with an explicitdeviceargument. Factory calls will be performed as if they were passeddeviceas an argument.To only temporarily change the default device instead of setting it globally, use
with tensorplay.device(device):instead.The default device is initially
cpu. If you set the default tensor device to another device (e.g.,cuda) without a device index, tensors will be allocated on whatever the current device for the device type, even aftertensorplay.cuda.set_device()is called.Warning
This function imposes a slight performance cost on every Python call to the tensorplay API (not just factory functions).
Note
This doesn’t affect functions that create tensors that share the same memory as the input, like:
tensorplay.from_numpy()andtensorplay.frombuffer()- Parameters:
device (
tensorplay.device, str, int, or None) – the device to set as default, orNoneto clear the override. An integer is interpreted as an index for the current accelerator.
Example:
>>> # xdoctest: +SKIP("requires cuda, changes global state") >>> tensorplay.get_default_device() device(type='cpu') >>> tensorplay.set_default_device('cuda') # current device is 0 >>> tensorplay.get_default_device() device(type='cuda', index=0) >>> tensorplay.set_default_device('cuda') >>> tensorplay.cuda.set_device('cuda:1') # current device is 1 >>> tensorplay.get_default_device() device(type='cuda', index=1) >>> tensorplay.set_default_device('cuda:1') >>> tensorplay.get_default_device() device(type='cuda', index=1)
Help improve this page
Found an error, an unclear step, or a missing example?
