-
Notifications
You must be signed in to change notification settings - Fork 25
Kernel shape profiler registry #1024
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
mohbasit
wants to merge
12
commits into
main
Choose a base branch
from
kernel-shape-profiler-registry
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
93691f4
Add launcher-wrapping kernel shape profiler (registry approach)
mohbasit 7aacf63
Add sglang.kernels.ops. to auto-discovery prefixes
mohbasit 9a1f47e
kernel_shape_tool: use active default-device override for state prese…
mohbasit b438244
Merge remote-tracking branch 'origin/main' into kernel-shape-profiler…
mohbasit 6c2a761
patchless shape discovery
mohbasit 05127cb
move location
mohbasit 5c5b136
Fix black formatting in kernel_shape_profiler
mohbasit 8db9973
Use black suggestion for _built_wrappers formatting
mohbasit 0b89eb0
added tests
mohbasit ff879d1
fix tests
mohbasit 3115425
Merge branches 'main' and 'kernel-shape-profiler-registry' of github.…
mohbasit d5fdb7d
skip torch tests
mohbasit 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
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <!-- | ||
| Copyright (c) 2025-2026 Advanced Micro Devices, Inc. All rights reserved. | ||
|
|
||
| See LICENSE for license information. | ||
| --> | ||
|
|
||
| # Kernel shape profiler | ||
|
|
||
| Adds `Input Dims` / `Input type` / `Input Strides` to PyTorch profiler traces | ||
| for GPU kernels (Triton / aiter / FlashInfer) that would otherwise appear with | ||
| no operand metadata, so they can be rooflined. | ||
|
|
||
| It works by wrapping **kernel launcher functions** — Python entry points like | ||
| `gemm_a8w8_blockscale`, `invoke_fused_moe_kernel` or `rmsnorm` — as | ||
| `torch.library` custom ops. Each wrapped launcher then shows up in the trace as | ||
| a `cpu_op` named after itself, carrying its tensor operands: | ||
|
|
||
| ``` | ||
| sglang_profiler::fp8_utils_gemm_a8w8_blockscale_12 <- cpu_op, named after the launcher | ||
| Input Dims: [[1025, 7168], [7168, 2112], [1025, 56], [56, 17]] | ||
| Input Strides: [[7168, 1], [1, 7168], [56, 1], [1, 56]] | ||
| Input type: ['c10::Float8_e4m3fnuz', 'c10::Float8_e4m3fnuz', 'float', 'float'] | ||
| _gemm_a8w8_blockscale_kernel_GROUP_K_128_... <- the real GPU kernel | ||
| ``` | ||
|
|
||
| Launchers come from an explicit registry plus auto-discovery, so no | ||
| serving-framework source is patched. | ||
|
|
||
| ## Files | ||
|
|
||
| | File | Role | | ||
| |------|------| | ||
| | `kernel_shape_profiler.py` | Wraps launcher functions as custom ops and rebinds every module-level reference to them. | | ||
| | `sitecustomize.py` | Auto-loaded shim that drives `enable()` / `disable()` from the torch-profiler window so nothing is wrapped outside a profiling run. | | ||
|
|
||
| ## Activation | ||
|
|
||
| Put this directory on `PYTHONPATH` and set the flag: | ||
|
|
||
| ```bash | ||
| export PYTHONPATH=/path/to/kernel_shape_tool:$PYTHONPATH | ||
| export TRACELENS_SHAPE_DISCOVERY=1 | ||
| ``` | ||
|
|
||
| CPython auto-imports `sitecustomize` at interpreter startup for every process, | ||
| so the server and all TP workers pick it up. When `TRACELENS_SHAPE_DISCOVERY` | ||
| is unset or `0`, every hook short-circuits, so it is safe to leave the directory | ||
| on `PYTHONPATH` permanently. | ||
|
|
||
| ### Optional knobs | ||
|
|
||
| | Env var | Default | Meaning | | ||
| |---------|---------|---------| | ||
| | `TRACELENS_SHAPE_DISCOVERY` | `0` | Master switch | | ||
| | `TRACELENS_SHAPE_FORCE_RECORD_SHAPES` | `1` | Force `record_shapes=True` on the profiler (`Input Dims` only surface when shapes are recorded). Set `0` to respect the server's own setting. | | ||
|
|
||
| > Note on CUDA graphs: kernels that only run inside replayed graphs execute no | ||
| > Python, so their shapes are recorded at graph **capture** time. A decode-path | ||
| > analysis therefore needs the graph-capture trace, not just the serving-window | ||
| > trace. | ||
Oops, something went wrong.
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.
Is the module autoloaded for all torch-profile instances or only when shape recording is enabled in PyTorch profiler?
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.
it is only loaded when the Pythonpath is set,
export PYTHONPATH=/path/to/kernel_shape_tool:$PYTHONPATH
export TRACELENS_SHAPE_DISCOVERY=1
both these env variables need to be set, only then the module is loaded