[Feature] Add Layer-wise NVTX Support (#29990)

Signed-off-by: Max Hu <hyoung2991@gmail.com>
Signed-off-by: Max Hu <maxhu@nvidia.com>
Co-authored-by: Max Hu <maxhu@nvidia.com>
This commit is contained in:
Max Hu
2025-12-05 11:20:07 +00:00
committed by GitHub
co-authored by Max Hu
parent 3628bcaaf2
commit c2894d3883
5 changed files with 375 additions and 3 deletions
+49
View File
@@ -88,6 +88,7 @@ from vllm.utils.jsontree import json_map_leaves
from vllm.utils.math_utils import cdiv, round_up
from vllm.utils.mem_constants import GiB_bytes
from vllm.utils.mem_utils import DeviceMemoryProfiler
from vllm.utils.nvtx_pytorch_hooks import PytHooks
from vllm.utils.platform_utils import is_pin_memory_available
from vllm.utils.torch_utils import (
get_dtype_size,
@@ -599,6 +600,7 @@ class GPUModelRunner(
# Ephemeral state transferred between execute_model() and sample_tokens().
self.execute_model_state: ExecuteModelState | None = None
self.kv_connector_output: KVConnectorOutput | None = None
self.layerwise_nvtx_hooks_registered = False
def reset_mm_cache(self) -> None:
if self.mm_budget:
@@ -2828,6 +2830,42 @@ class GPUModelRunner(
cudagraph_stats,
)
def _register_layerwise_nvtx_hooks(self) -> None:
"""
Register layerwise NVTX hooks if --enable-layerwise-nvtx-tracing is enabled
to trace detailed information of each layer or module in the model.
"""
if (
self.vllm_config.observability_config.enable_layerwise_nvtx_tracing
and not self.layerwise_nvtx_hooks_registered
):
if self.compilation_config.cudagraph_mode != CUDAGraphMode.NONE:
logger.debug_once(
"layerwise NVTX tracing is not supported when CUDA graph is "
"turned off; you may observe part or all of the model "
"missing NVTX markers"
)
# In STOCK_TORCH_COMPILE mode, after registering hooks here,
# the __call__ function of nn.module will be recompiled with
# fullgraph=True. Since nvtx.range_push/pop are not traceable
# by torch dynamo, we can't register hook functions here
# because hook functions will also be traced by torch dynamo.
if (
self.vllm_config.compilation_config.mode
== CompilationMode.STOCK_TORCH_COMPILE
):
logger.debug_once(
"layerwise NVTX tracing is not supported when "
"CompilationMode is STOCK_TORCH_COMPILE, skipping "
"function hooks registration"
)
else:
pyt_hooks = PytHooks()
pyt_hooks.register_hooks(self.model, self.model.__class__.__name__)
self.layerwise_nvtx_hooks_registered = True
@torch.inference_mode()
def execute_model(
self,
@@ -4122,6 +4160,17 @@ class GPUModelRunner(
is_graph_capturing=is_graph_capturing,
)
# We register layerwise NVTX hooks here after the first dynamo tracing is
# done to avoid nvtx operations in hook functions being traced by
# torch dynamo and causing graph breaks.
# Note that for DYNAMO_ONCE and VLLM_COMPILE mode,
# compiled model's dynamo tracing is only done once and the compiled model's
# __call__ function is replaced by calling the compiled function.
# So it's safe to register hooks here. Hooks will be registered to
# both compiled and uncompiled models but they will never
# be called on the compiled model execution path.
self._register_layerwise_nvtx_hooks()
# This is necessary to avoid blocking DP.
# For dummy runs, we typically skip EPLB since we don't have any real
# requests to process.