@@ -526,75 +526,17 @@ with open('${CONFIG_FILE}') as f:
526526 cfg = yaml.safe_load(f)
527527backend = cfg.get('backend', {})
528528sglang_cfg = backend.get('sglang_config', {})
529- # For profiling: enable layerwise NVTX markers and disable CUDA graphs
530- # (CUDA graphs hide individual kernel launches from the torch profiler)
529+ # Add --enable-layerwise-nvtx-marker to both prefill and decode configs
531530for mode in ['prefill', 'decode', 'aggregated']:
532531 mode_cfg = sglang_cfg.get(mode, {})
533532 mode_cfg['enable-layerwise-nvtx-marker'] = True
534- mode_cfg['disable-cuda-graph'] = True
535533 sglang_cfg[mode] = mode_cfg
536534backend['sglang_config'] = sglang_cfg
537535cfg['backend'] = backend
538536with open('${CONFIG_FILE} ', 'w') as f:
539537 yaml.dump(cfg, f, default_flow_style=False)
540- print('[profile] Enabled SGLang --enable-layerwise-nvtx-marker and --disable-cuda-graph')
541- " || echo " [profile] Warning: failed to inject SGLang config"
542-
543- # Patch SGLang's torch profiler to use CUDA sync events instead of CUPTI
544- # (CUPTI requires elevated container privileges not available on all clusters)
545- local configs_dir=" configs"
546- mkdir -p " $configs_dir "
547-
548- # Create setup script that patches SGLang source directly inside the container
549- cat > " $configs_dir /apply-sglang-profiling-patch.sh" << 'SETUP '
550- #!/bin/bash
551- echo "[profile-setup] Patching SGLang profiler for CUDA sync events..."
552-
553- # Guard: only patch once
554- if [[ -f /tmp/.sglang_profiling_patched ]]; then
555- echo "[profile-setup] Already patched, skipping"
556- exit 0
557- fi
558-
559- # Inject experimental_config=_ExperimentalConfig(enable_cuda_sync_events=True)
560- # into every torch.profiler.profile() call in SGLang's profiler code.
561- # Uses CUDA events for GPU kernel timing instead of CUPTI (which needs elevated privileges).
562-
563- patch_profiler_file() {
564- local file="$1"
565- local closing_paren_indent="$2" # indentation of the closing )
566- local kwarg_indent="${closing_paren_indent} " # one more level for the kwarg
567-
568- if [[ ! -f "$file" ]]; then
569- echo "[profile-setup] WARNING: $file not found"
570- return
571- fi
572- if grep -q "enable_cuda_sync_events" "$file"; then
573- echo "[profile-setup] $file already patched"
574- return
575- fi
576-
577- local exp_line="${kwarg_indent}experimental_config=torch.profiler._ExperimentalConfig(enable_cuda_sync_events=True),"
578- sed -i "/self\.torch_profiler = torch\.profiler\.profile/,/^${closing_paren_indent})/{
579- s|^${closing_paren_indent})|${exp_line}\n${closing_paren_indent})|
580- }" "$file"
581- echo "[profile-setup] Patched $file"
582- }
583-
584- # Patch 1: profile_utils.py (_ProfilerTorch.start - v2 path, 8-space indent)
585- PROFILE_UTILS=$(python3 -c "import sglang.srt.utils.profile_utils as m; print(m.__file__)" 2>/dev/null)
586- patch_profiler_file "$PROFILE_UTILS" " "
587-
588- # Patch 2: scheduler_profiler_mixin.py (legacy start_profile, 12-space indent)
589- SCHED_MIXIN=$(python3 -c "import sglang.srt.managers.scheduler_profiler_mixin as m; print(m.__file__)" 2>/dev/null)
590- patch_profiler_file "$SCHED_MIXIN" " "
591-
592- touch /tmp/.sglang_profiling_patched
593- echo "[profile-setup] SGLang CUDA sync events patch complete"
594- SETUP
595- chmod +x " $configs_dir /apply-sglang-profiling-patch.sh"
596- export PROFILING_SETUP_SCRIPT=" apply-sglang-profiling-patch.sh"
597- echo " [profile] Set PROFILING_SETUP_SCRIPT=$PROFILING_SETUP_SCRIPT (SGLang CUDA events)"
538+ print('[profile] Enabled SGLang --enable-layerwise-nvtx-marker')
539+ " || echo " [profile] Warning: failed to inject SGLang NVTX markers"
598540 fi
599541}
600542
0 commit comments