mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
19da3da3d3
The fix-cpp-build branch is a major restructuring that: - Drops Exhale (which auto-generated API docs but caused OOM in CI) in favor of hand-written RST pages using Breathe directives - Reorganizes notes into a hierarchical API reference with proper categories - Adds new content: XPU docs, stable ABI docs, DeviceGuard/Stream docs - Always runs Doxygen instead of gating on an env var Pull Request resolved: https://github.com/pytorch/pytorch/pull/174096 Approved by: https://github.com/janeyx99, https://github.com/seemethere, https://github.com/albanD
53 lines
1.1 KiB
Markdown
53 lines
1.1 KiB
Markdown
---
|
|
myst:
|
|
html_meta:
|
|
description: Device and stream guards in PyTorch C++ — RAII guards for managing current device and stream context.
|
|
keywords: PyTorch, C++, DeviceGuard, StreamGuard, RAII, device management
|
|
---
|
|
|
|
# Device Guards
|
|
|
|
C10 provides device-agnostic RAII guards for managing the current device
|
|
context. These guards work across all backends (CUDA, XPU, etc.) and
|
|
automatically restore the previous device when they go out of scope.
|
|
|
|
For backend-specific guards, see {doc}`../cuda/guards` and {doc}`../xpu/index`.
|
|
|
|
## DeviceGuard
|
|
|
|
```{doxygenclass} c10::DeviceGuard
|
|
:members:
|
|
:undoc-members:
|
|
```
|
|
|
|
**Example:**
|
|
|
|
```cpp
|
|
#include <c10/core/DeviceGuard.h>
|
|
|
|
{
|
|
c10::DeviceGuard guard(c10::Device(c10::kCUDA, 1));
|
|
// All operations here run on CUDA device 1
|
|
}
|
|
// Previous device is restored
|
|
```
|
|
|
|
## OptionalDeviceGuard
|
|
|
|
```{doxygenclass} c10::OptionalDeviceGuard
|
|
:members:
|
|
:undoc-members:
|
|
```
|
|
|
|
**Example:**
|
|
|
|
```cpp
|
|
#include <c10/core/DeviceGuard.h>
|
|
|
|
c10::OptionalDeviceGuard guard;
|
|
if (use_gpu) {
|
|
guard.reset_device(c10::Device(c10::kCUDA, 0));
|
|
}
|
|
// Guard only restores device if it was set
|
|
```
|