Skip to content

Conversation

@pytorchbot
Copy link
Collaborator

This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #16772 by @Gasoonjia
^ Please use this as the source of truth for the PR details, comments, and reviews
ghstack PR base: https://github.com/pytorch/executorch/tree/gh/gasoonjia/112/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/112/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/gasoonjia/111/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/112/orig
Differential Revision: D91202898
@diff-train-skip-merge

Pull Request resolved: #16769

This diff adds CUDA storage infrastructure to SlimTensor, enabling GPU memory allocation and management.

**Key changes:**

1. **`cuda/Guard.h`** - CUDAGuard RAII class:
   - Saves current CUDA device on construction, restores on destruction
   - Exception-safe device context switching
   - Constructors accept device index or Device object

2. **`core/Storage.h`** - Extended for CUDA support:
   - Added `DeviceTraits<DeviceType::CUDA>` specialization with:
     - `allocate()` - Uses cudaMalloc with CUDAGuard for device selection
     - `free()` - Uses cudaFree with warning on error
     - `memcpy()` - Supports Host↔Device and Device↔Device copies
   - Added `DEFAULT_CUDA_DEVICE` constant
   - Updated `MaybeOwningStorage` constructor to handle CUDA devices
   - Stub implementation when `CUDA_AVAILABLE` is not defined (throws error)
ghstack-source-id: 335102161
@exported-using-ghexport

Differential Revision: [D91202899](https://our.internmc.facebook.com/intern/diff/D91202899/)
Pull Request resolved: #16770

This diff enables CUDA tensor creation with basic tensor functionality and factory function support

**Key changes:*

1. **`core/SlimTensor.h`** - Extended for CUDA support:
   - Added `is_cuda()` method to check if tensor is on CUDA device

2. **`factory/Empty.h`** - Supports CUDA:
   - `empty_strided()` and `empty()` work with CUDA device via `new_storage()`
   - Device routing is handled by `MaybeOwningStorage` constructor
ghstack-source-id: 335102160
@exported-using-ghexport

Differential Revision: [D91202897](https://our.internmc.facebook.com/intern/diff/D91202897/)
Pull Request resolved: #16771

This diff enables CUDA tensor copy operations in SlimTensor.

**Key changes:**

**`core/SlimTensor.h`** - Extended for CUDA support:
   - Updated `copy_()` to handle cross-device copies:
     - CPU→CUDA (cudaMemcpyHostToDevice)
     - CUDA→CPU (cudaMemcpyDeviceToHost)
     - CUDA→CUDA (cudaMemcpyDeviceToDevice, same device)
   - Cross-device copies require contiguous tensors
   - CPU-to-CPU copies continue to support non-contiguous (strided) tensors
ghstack-source-id: 335102159
@exported-using-ghexport

Differential Revision: [D91202900](https://our.internmc.facebook.com/intern/diff/D91202900/)
Pull Request resolved: #16772

Add non-owning constructor to MaybeOwningStorage that allows wrapping
external memory without taking ownership. This is needed for from_blob()
which creates tensors from user-provided data pointers.

Changes:
- Add MaybeOwningStorage(device, data, nbytes) non-owning constructor
- Add is_owning() method to query ownership mode
- Non-owning storage uses no-op deleter so external memory is not freed
ghstack-source-id: 335102163
@exported-using-ghexport

Differential Revision: [D91202898](https://our.internmc.facebook.com/intern/diff/D91202898/)
@pytorch-bot
Copy link

pytorch-bot bot commented Jan 22, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16801

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 1 Cancelled Job, 34 Pending

As of commit 4a28e6e with merge base 37aa87e (image):

NEW FAILURE - The following job has failed:

CANCELLED JOB - The following job was cancelled. Please retry:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 22, 2026
Base automatically changed from gh/gasoonjia/111/orig to main January 22, 2026 22:32
@github-actions
Copy link

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@Gasoonjia Gasoonjia merged commit f8eead2 into main Jan 22, 2026
155 of 157 checks passed
@Gasoonjia Gasoonjia deleted the gh/gasoonjia/112/orig branch January 22, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants