Skip to content

[API Compatibility] Add torch-compatible NVTX range context manager#79251

Merged
SigureMo merged 1 commit into
PaddlePaddle:developfrom
ShigureNyako:feature/torch-cuda-nvtx-range
Jun 5, 2026
Merged

[API Compatibility] Add torch-compatible NVTX range context manager#79251
SigureMo merged 1 commit into
PaddlePaddle:developfrom
ShigureNyako:feature/torch-cuda-nvtx-range

Conversation

@ShigureNyako

Copy link
Copy Markdown
Contributor

PR Category

User Experience

PR Types

Improvements

Description

This PR adds a torch-compatible NVTX range context manager to Paddle's existing NVTX namespace:

  • paddle.cuda.nvtx.range(msg, *args, **kwargs)
  • paddle.device.nvtx.range(msg, *args, **kwargs)

The wrapper follows torch.cuda.nvtx.range behavior: it formats msg, calls the existing range_push, yields as a context manager/decorator, and always calls range_pop in finally. It does not introduce a torch dependency and preserves Paddle's current CUDA/NVTX availability behavior by reusing the existing range_push / range_pop APIs.

Background: in NVIDIA/cudnn-frontend's Paddle adaptation, with torch.cuda.nvtx.range(...) call sites had to be replaced by a local context manager wrapping paddle.cuda.nvtx.range_push/pop. Adding this compatibility wrapper lets such external projects keep the torch-style call shape when using import paddle as torch.

Validation:

  • pre-commit run --files python/paddle/cuda/__init__.py python/paddle/device/__init__.py test/legacy_test/test_cuda_unittest.py
  • git diff --check
  • python3 - <<'PY' ... compile edited files ... PY
  • Targeted TestNvtx runtime was not runnable in this source checkout because there is no built libpaddle; CUDA/NVTX runtime behavior remains covered by the existing guarded CUDA test path plus the new monkeypatched no-CUDA behavior test.

是否引起精度变化

@ShigureNyako ShigureNyako left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review:

  • Diff is limited to paddle.cuda.nvtx.range, mirrored paddle.device.nvtx.range, and focused TestNvtx coverage.
  • The wrapper matches torch.cuda.nvtx.range call shape (msg.format(*args, **kwargs)) and guarantees range_pop() in finally for normal and exceptional exits.
  • No torch dependency is introduced; no stream/current_stream code is touched.

@SigureMo 请帮忙 review,谢谢。

@SigureMo SigureMo changed the title [Compat] Add torch-compatible NVTX range context manager [API Compatibility] Add torch-compatible NVTX range context manager Jun 4, 2026
Comment thread python/paddle/device/__init__.py Outdated

@staticmethod
@contextmanager
def range(msg: str, *args, **kwargs):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def range(msg: str, *args, **kwargs):
def range(msg: str, *args: Any, **kwargs: Any) -> generator 的返回值咋标来着太久了我忘了你自己查去:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,已在 bedbde59a5daedb2e00463161c8fc9e44db9bb5b 中补上类型标注:

  • *args: Any
  • **kwargs: Any
  • -> Generator[None, None, None]

Generator 按 Paddle 现有 contextmanager 写法放在 TYPE_CHECKING 下从 collections.abc 引入,并同步更新了 paddle.cuda.nvtx.range / paddle.device.nvtx.range

验证:pre-commit run --files python/paddle/cuda/__init__.py python/paddle/device/__init__.py test/legacy_test/test_cuda_unittest.py 通过。

@ShigureNyako ShigureNyako force-pushed the feature/torch-cuda-nvtx-range branch from 4841b38 to bedbde5 Compare June 4, 2026 15:37

@SigureMo SigureMo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@paddle-bot paddle-bot Bot added the contributor External developers label Jun 4, 2026
@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@8df3d8a). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #79251   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         2           
  Lines              ?        18           
  Branches           ?         0           
===========================================
  Hits               ?        18           
  Misses             ?         0           
  Partials           ?         0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

2 similar comments
@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@SigureMo

SigureMo commented Jun 4, 2026

Copy link
Copy Markdown
Member

停停停,先别 rerun 了,得等 #79240 修好了我给你 rerun

@ShigureNyako

Copy link
Copy Markdown
Contributor Author

收到,先不继续 rerun 了,等 #79240 修好后再处理。

@ShigureNyako

Copy link
Copy Markdown
Contributor Author

CI 已全部通过,PR 现在是 approved / clean。我这边没有 merge 权限(MergePullRequest 权限不足),麻烦 @SigureMo 合并一下,谢谢。

@SigureMo SigureMo merged commit 29e37f9 into PaddlePaddle:develop Jun 5, 2026
159 of 172 checks passed
@SigureMo SigureMo deleted the feature/torch-cuda-nvtx-range branch June 5, 2026 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants