device_guard
- class paddle.device. device_guard ( device: PlaceLike ) [source]
-
Notes
This API only supports dynamic graph mode currently.
A context manager that specifies the current device context by the given device.
- Parameters
-
device (PlaceLike) – The specified device.
Examples
>>> >>> import paddle >>> # Set the global default device to CPU >>> paddle.set_device("cpu") >>> # Temporarily switch to GPU:0 using device_guard with string input >>> with paddle.device.device_guard("gpu:0"): ... x = paddle.randn([4, 4]) # Create a Tensor on GPU:0 ... x = x.tanh() * 2 # Perform computation on GPU:0 ... print(x.place) # Check the device of the Tensor Place(gpu:0) >>> # Set the global default device to GPU:0 >>> paddle.set_device("gpu:0") >>> # Temporarily switch to CPU using device_guard with Place object (CPUPlace) >>> cpu_place = paddle.CPUPlace() >>> with paddle.device.device_guard(cpu_place): ... x = paddle.randn([4, 4]) # Create a Tensor on CPU ... x = x.tanh() * 2 # Perform computation on CPU ... print(x.place) Place(cpu)