-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[NPU] add device print #28126
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
Open
zhaozx-cn
wants to merge
6
commits into
sgl-project:main
Choose a base branch
from
zhaozx-cn:add_device_print
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[NPU] add device print #28126
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| import logging | ||
| import sys | ||
| from enum import IntEnum | ||
| from typing import TYPE_CHECKING, Callable | ||
| from typing import TYPE_CHECKING, Any, Callable | ||
|
|
||
| import torch | ||
|
|
||
|
|
@@ -16,6 +16,7 @@ | |
| _is_npu = is_npu() | ||
| indexer_weight_stream = None | ||
| gva_is_inited = False | ||
| device_print_registered = False | ||
|
|
||
|
|
||
| class NPUACLFormat(IntEnum): | ||
|
|
@@ -364,3 +365,87 @@ def process_routed_expert(hidden_states, topk_output, forward_func): | |
| with torch.get_device_module().stream(stream): | ||
| shared_output = forward_func(hidden_states, topk_output) | ||
| return shared_output | ||
|
|
||
|
|
||
| def _mark_op_side_effectful(op: Any) -> None: | ||
| torch.fx.node.has_side_effect(op) | ||
| default_overload = getattr(op, "default", None) | ||
| if default_overload is not None: | ||
| torch.fx.node.has_side_effect(default_overload) | ||
|
|
||
|
|
||
| def _ensure_device_print_registered() -> None: | ||
| global device_print_registered | ||
| if device_print_registered: | ||
| return | ||
| try: | ||
| # Mark device_print ops side-effectful so FX/Inductor does not DCE or reorder these debug callbacks. | ||
| import sgl_kernel_npu # noqa: F401 | ||
|
|
||
| _mark_op_side_effectful(torch.ops.npu.device_print) | ||
| _mark_op_side_effectful(torch.ops.npu.device_print_tensor) | ||
| device_print_registered = True | ||
| except AttributeError as exc: | ||
| raise RuntimeError( | ||
| "device_print ops are available in sgl_kernel_npu. " | ||
| "Please install the latest sgl_kernel_npu from source." | ||
| ) from exc | ||
|
|
||
|
|
||
| def device_print( | ||
| value: ( | ||
| torch.Tensor | ||
| | int | ||
| | float | ||
| | bool | ||
| | str | ||
| | torch.dtype | ||
| | torch.device | ||
| | torch.Size | ||
| ), | ||
| ) -> None: | ||
| """Print one value from a device callback. | ||
|
|
||
| This helper is intended for debugging. To stay replay-safe under | ||
| ``torch.npu.graph`` capture/replay, the underlying callback payloads are | ||
| retained instead of being reclaimed after the first host callback runs. | ||
| Avoid using it in hot paths or long-running high-frequency loops, otherwise | ||
| there may be memory issues due to too many retained payloads. | ||
|
|
||
| Supported usage: | ||
|
|
||
| >>> from sglang.srt.hardware_backend.npu.utils import device_print | ||
| >>> device_print(x) | ||
| >>> device_print("already formatted text") | ||
| >>> device_print(7) | ||
|
|
||
| Unsupported usage: | ||
|
|
||
| >>> device_print("x =", x) | ||
| >>> device_print("This is ", x, "and this is ", y) | ||
|
|
||
| If you need device-time tensor values, pass the tensor itself. If you need | ||
| text, pass one final string that is already formatted. | ||
|
|
||
| Tensor values are copied to host on the current stream before the callback | ||
| prints them, so printing remains ordered with respect to the surrounding | ||
| device work. | ||
|
|
||
| DO NOT FORMAT A DEVICE TENSOR INTO A STRING YOURSELF AND THEN PRINT, for example: | ||
|
|
||
| >>> device_print(f"x = {x}") | ||
| >>> device_print("x = " + str(x)) | ||
| """ | ||
| _ensure_device_print_registered() | ||
|
|
||
| if isinstance(value, torch.Tensor): | ||
| torch.ops.npu.device_print_tensor(value) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in sgl-kernel-npu?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sgl-project/sgl-kernel-npu#552 is required. |
||
| elif isinstance( | ||
| value, (str, int, float, bool, torch.dtype, torch.device, torch.Size) | ||
| ): | ||
| torch.ops.npu.device_print(str(value)) | ||
| else: | ||
| raise TypeError( | ||
| f"Unsupported device_print value type: {type(value)!r}. " | ||
| "Use exactly one argument: device_print(tensor), device_print('formatted text')." | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move import to top
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe need to lazy init here