Skip to content

Commit 56ad84e

Browse files
committed
fix(ascend): patch accelerator cache cleanup
1 parent 5349f83 commit 56ad84e

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2026 BAAI. All rights reserved.
2+
3+
from types import SimpleNamespace
4+
5+
import torch
6+
7+
from vllm_fl.dispatch.backends.vendor.ascend.patch import (
8+
patch_accelerator_empty_cache,
9+
)
10+
11+
12+
def test_patch_accelerator_empty_cache(monkeypatch):
13+
replacement = lambda: None
14+
monkeypatch.setattr(
15+
torch, "npu", SimpleNamespace(empty_cache=replacement), raising=False
16+
)
17+
monkeypatch.setattr(torch.accelerator, "empty_cache", lambda: None)
18+
19+
patch_accelerator_empty_cache()
20+
21+
assert torch.accelerator.empty_cache is replacement

vllm_fl/dispatch/backends/vendor/ascend/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"""
66

77
from .ascend import AscendBackend
8-
from .patch import patch_mamba_config
8+
from .patch import patch_accelerator_empty_cache, patch_mamba_config
99

10+
patch_accelerator_empty_cache()
1011
patch_mamba_config()
1112

1213
__all__ = ["AscendBackend"]

vllm_fl/dispatch/backends/vendor/ascend/patch.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@
99
logger = logging.getLogger(__name__)
1010
_patches_applied = False
1111

12+
13+
def patch_accelerator_empty_cache():
14+
"""Redirect generic accelerator cache cleanup to torch-npu.
15+
16+
``torch.accelerator.empty_cache`` uses an incompatible allocator on NPU.
17+
Patch it during Ascend backend import so cleanup also works in the engine
18+
parent process, not only in worker processes.
19+
"""
20+
npu = getattr(torch, "npu", None)
21+
accelerator = getattr(torch, "accelerator", None)
22+
if npu is None or accelerator is None or not hasattr(npu, "empty_cache"):
23+
return
24+
25+
accelerator.empty_cache = npu.empty_cache
26+
logger.info("Patched torch.accelerator.empty_cache for Ascend NPU")
27+
28+
1229
def apply_ascend_patches():
1330
"""Apply all Ascend-specific patches."""
1431
global _patches_applied

0 commit comments

Comments
 (0)