Skip to content

Conversation

@kyleliang-nv
Copy link
Contributor

@kyleliang-nv kyleliang-nv commented Oct 20, 2025

Motivation

Enable auto-inserting NVTX markers that contains layer name and input tensor shapes.

Modifications

  • Add pytorch_hooks.py which will add pre/post-foward hooks with pytorch.
  • Add enable-layerwise-nvtx-marker server arg. This will register the loaded model with pytorch_hooks.py
  • Add unittest for /start_profile + nsys profile
  • Move Prefill log from before to after run_batch, to be consistent with Decode log
  • Add documents to explain how to use start_profile with start_steps and num_steps
  • Add document to explain how to use enable-layerwise-nvtx-marker with /start_profile endpoint

Accuracy Tests

Benchmarking and Profiling

With enable-layerwise-nvtx-marker, the nsys output file will now contain NVTX which corresponds to the pytorch nn.module path and input tensor shapes.
Screenshot 2025-10-20 at 12 00 50 PM

Checklist

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @kyleliang-nv, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances SGLang's profiling capabilities by integrating layer-wise NVTX support for PyTorch models. This allows developers to gain granular insights into the performance of individual model layers using tools like Nsight Systems, facilitating bottleneck identification and optimization. The changes include a new server argument to enable this feature, a dedicated utility for PyTorch hooks, and updated documentation to guide users through the new profiling workflows, alongside minor improvements to logging for better step tracking.

Highlights

  • Layer-wise NVTX Profiling: Introduced a new feature to automatically insert NVTX markers for each layer in a PyTorch model, including layer names and input tensor shapes, for detailed profiling with Nsight Systems.
  • New Server Argument: Added --enable-layerwise-nvtx to activate this new profiling capability.
  • Enhanced Profiling Documentation: Updated the benchmark_and_profiling.md documentation with comprehensive guides on using the new layer-wise NVTX profiling and the existing /start_profile and /end_profile HTTP API endpoints, including num_steps and start_step parameters.
  • Logging Improvements: Incorporated step # into prefill and decode log messages for better tracking of model execution steps.
  • PyTorch Hooks Utility: Added a new utility file (pytorch_hooks.py) to manage PyTorch forward and pre-forward hooks for NVTX annotation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces layer-wise NVTX profiling support, a valuable feature for performance analysis. The changes include adding a PytHooks module for PyTorch hooks, a new server argument to enable this functionality, and comprehensive documentation updates. The implementation is well-done, but I have a few suggestions for the new pytorch_hooks.py file to improve code style and fix a minor bug in debug code. The documentation is thorough, though some minor formatting adjustments would enhance consistency.

@kyleliang-nv kyleliang-nv force-pushed the feature/layerwise_nvtx branch from fa618d5 to f3d023e Compare November 14, 2025 05:30
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Nov 14, 2025
Copy link
Collaborator

@Fridge003 Fridge003 left a comment

Choose a reason for hiding this comment

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

Nice job

@Fridge003 Fridge003 merged commit 597d416 into sgl-project:main Nov 16, 2025
131 of 164 checks passed
@kyleliang-nv
Copy link
Contributor Author

Nice job

Thanks for reviewing it and merging it in @Fridge003 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation run-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants