Skip to content

Add DeviceAllocator as the base device allocator #138222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 95 commits into from

Conversation

guangyey
Copy link
Collaborator

@guangyey guangyey commented Oct 17, 2024

Stack from ghstack (oldest at bottom):

Motivation

In line with [RFC] A device-agnostic Python device memory related API design for stream-based accelerators, some memory-related APIs are widely used in popular repositories, such as HuggingFace so many if-else conditional code. We would like to introduce a generic API set under torch.accelerator namespace to generalize these user cases.

Device-specific memory APIs torch.xxx.foo Device-agnostic memory APIs torch.accelerator.foo
torch.xxx.empty_cache
torch.accelerator.empty_cache
torch.xxx.reset_peak_memory_stats
torch.accelerator.reset_peak_memory_stats
torch.xxx.reset_accumulated_memory_stats
torch.accelerator.reset_accumulated_memory_stats
torch.xxx.memory_stats
torch.accelerator.memory_stats
torch.xxx.memory_allocated
torch.accelerator.memory_allocated
torch.xxx.max_memory_allocated
torch.accelerator.max_memory_allocated
torch.xxx.memory_reserved
torch.accelerator.memory_reserved
torch.xxx.max_memory_reserved
torch.accelerator.max_memory_reserved

Solution

This design follows a similar pattern to HostAllocator. We're introducing a base class DeviceAllocator, from which CUDAAllocator and XPUAllocator will inherit. This allows us to provide a unified call path like: torch.accelerator.empty_cache() -> GetDeviceAllocator(allocator)->empty_cache().

cc @albanD @EikanWang

Copy link

pytorch-bot bot commented Oct 17, 2024

🔗 Helpful Links

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

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

⏳ 5 Pending, 2 Unrelated Failures

As of commit 5521326 with merge base 178515d (image):

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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

guangyey added a commit that referenced this pull request Oct 17, 2024
@guangyey guangyey marked this pull request as draft October 17, 2024 15:06
@guangyey guangyey changed the title Add CachingDeviceAllocatorInterface as the base device allocator [WIP] Add CachingDeviceAllocatorInterface as the base device allocator Oct 17, 2024
[ghstack-poisoned]
Copy link
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions bot added the Stale label Dec 16, 2024
guangyey added a commit that referenced this pull request Mar 4, 2025
[ghstack-poisoned]
@guangyey guangyey added topic: improvements topic category topic: not user facing topic category labels Mar 18, 2025
guangyey added a commit that referenced this pull request Mar 18, 2025
guangyey added a commit that referenced this pull request Mar 18, 2025
guangyey added a commit that referenced this pull request Mar 18, 2025
guangyey added a commit that referenced this pull request Mar 18, 2025
guangyey added a commit that referenced this pull request Mar 18, 2025
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
guangyey added a commit that referenced this pull request Mar 20, 2025
guangyey added a commit that referenced this pull request Mar 20, 2025
[ghstack-poisoned]
[ghstack-poisoned]
guangyey added a commit that referenced this pull request Mar 21, 2025
@guangyey
Copy link
Collaborator Author

guangyey commented Aug 6, 2025

@pytorchbot merge -i

@pytorchmergebot
Copy link
Collaborator

[ghstack-poisoned]
pytorchmergebot pushed a commit that referenced this pull request Aug 6, 2025
# Motivation
The following API will be put under torch.accelerator
- empty_cache
- max_memory_allocated
- max_memory_reserved
- memory_allocated
- memory_reserved
- memory_stats
- reset_accumulated_memory_stats
- reset_peak_memory_stats

Pull Request resolved: #152932
Approved by: https://github.com/albanD
ghstack dependencies: #138222
pytorchmergebot pushed a commit that referenced this pull request Aug 6, 2025
@jithunnair-amd
Copy link
Collaborator

@pytorchbot revert -c nosignal -m "Broke ROCm periodic runs on MI300 e.g. https://github.com/pytorch/pytorch/actions/runs/16764977800/job/47470050573"

cc @guangyey If this revert doesn't go through because it's part of a stack, please forward fix the issue.

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

pytorchmergebot added a commit that referenced this pull request Aug 7, 2025
pytorchmergebot added a commit that referenced this pull request Aug 7, 2025
pytorchmergebot added a commit that referenced this pull request Aug 7, 2025
@pytorchmergebot
Copy link
Collaborator

@guangyey your PR has been successfully reverted.

@guangyey guangyey added ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR ciflow/periodic-rocm-mi300 Trigger "distributed" config CI on ROCm MI300 labels Aug 8, 2025
@guangyey
Copy link
Collaborator Author

guangyey commented Aug 8, 2025

@jithunnair-amd I think the failure is not introduced by this PR, I see the same failure pynvml.NVMLError_LibraryNotFound: NVML Shared Library Not Found in File "/var/lib/jenkins/pytorch/test/distributed/test_c10d_nccl.py", line 689, in test_extra_cuda_context in the previous commit.
Anyway, let's add the label ciflow/periodic-rocm-mi300 to see what CI says. And I will reland this PR once this CI pass.

@guangyey guangyey removed the ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR label Aug 8, 2025
@guangyey
Copy link
Collaborator Author

guangyey commented Aug 8, 2025

Hi @jithunnair-amd , the ciflow/periodic-rocm-mi300 has passed on this PR, see https://github.com/pytorch/pytorch/actions/runs/16825034603/job/47659415983?pr=138222
I would like to reland this PR again.

[ghstack-poisoned]
@pytorchmergebot
Copy link
Collaborator

Starting merge as part of PR stack under #155200

pytorchmergebot pushed a commit that referenced this pull request Aug 8, 2025
# Motivation
The following API will be put under torch.accelerator
- empty_cache
- max_memory_allocated
- max_memory_reserved
- memory_allocated
- memory_reserved
- memory_stats
- reset_accumulated_memory_stats
- reset_peak_memory_stats

Pull Request resolved: #152932
Approved by: https://github.com/albanD
ghstack dependencies: #138222
pytorchmergebot pushed a commit that referenced this pull request Aug 8, 2025
hinriksnaer pushed a commit to hinriksnaer/pytorch that referenced this pull request Aug 8, 2025
# Motivation
In line with [RFC] [A device-agnostic Python device memory related API design for stream-based accelerators](pytorch#134978), some memory-related APIs are widely used in popular repositories, such as HuggingFace [so many if-else conditional code](https://github.com/search?q=repo%3Ahuggingface%2Faccelerate%20torch.cuda.empty_cache&type=code). We would like to introduce a generic API set under torch.accelerator namespace to generalize these user cases.

<div align="center">
<table>
<tr>
<td> Device-specific memory APIs torch.xxx.foo</td> <td> Device-agnostic memory APIs torch.accelerator.foo</td>
</tr>
<tr>
<td>

```python
torch.xxx.empty_cache
```

</td>
<td>

```python
torch.accelerator.empty_cache
```

</td>
</tr>

<tr>
<td>

```python
torch.xxx.reset_peak_memory_stats
```

</td>
<td>

```python
torch.accelerator.reset_peak_memory_stats
```

</td>
</tr>

<tr>
<td>

```python
torch.xxx.reset_accumulated_memory_stats
```

</td>
<td>

```python
torch.accelerator.reset_accumulated_memory_stats
```

</td>
</tr>

<tr>
<td>

```python
torch.xxx.memory_stats
```

</td>
<td>

```python
torch.accelerator.memory_stats
```

</td>
</tr>

<tr>
<td>

```python
torch.xxx.memory_allocated
```

</td>
<td>

```python
torch.accelerator.memory_allocated
```

</td>
</tr>

<tr>
<td>

```python
torch.xxx.max_memory_allocated
```

</td>
<td>

```python
torch.accelerator.max_memory_allocated
```

</td>
</tr>

<tr>
<td>

```python
torch.xxx.memory_reserved
```

</td>
<td>

```python
torch.accelerator.memory_reserved
```

</td>
</tr>

<tr>
<td>

```python
torch.xxx.max_memory_reserved
```

</td>
<td>

```python
torch.accelerator.max_memory_reserved
```

</td>
</tr>

</table>
</div>

# Solution
This design follows a similar pattern to `HostAllocator`. We're introducing a base class `DeviceAllocator`, from which `CUDAAllocator` and `XPUAllocator` will inherit. This allows us to provide a unified call path like: `torch.accelerator.empty_cache()` -> `GetDeviceAllocator(allocator)->empty_cache()`.

Pull Request resolved: pytorch#138222
Approved by: https://github.com/albanD, https://github.com/Camyll
hinriksnaer pushed a commit to hinriksnaer/pytorch that referenced this pull request Aug 8, 2025
# Motivation
The following API will be put under torch.accelerator
- empty_cache
- max_memory_allocated
- max_memory_reserved
- memory_allocated
- memory_reserved
- memory_stats
- reset_accumulated_memory_stats
- reset_peak_memory_stats

Pull Request resolved: pytorch#152932
Approved by: https://github.com/albanD
ghstack dependencies: pytorch#138222
hinriksnaer pushed a commit to hinriksnaer/pytorch that referenced this pull request Aug 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci-no-td Do not run TD on this PR ciflow/periodic-rocm-mi300 Trigger "distributed" config CI on ROCm MI300 ciflow/rocm Trigger "default" config CI on ROCm ciflow/trunk Trigger trunk jobs on your pull request ciflow/xpu Run XPU CI tasks Merged module: accelerator Issues related to the shared accelerator API no-stale open source Reverted Stale topic: improvements topic category topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants