-
Notifications
You must be signed in to change notification settings - Fork 78
feat(observability): NVTX kernel tracing + Prometheus metrics endpoint #250
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
Billy1900
wants to merge
2
commits into
RL-Align:main
Choose a base branch
from
Billy1900:feat/observability-nvtx-prometheus
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
2 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
Some comments aren't visible on the classic Files Changed page.
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,62 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) 2026 RL-Kernel Contributors | ||
|
|
||
| #pragma once | ||
|
|
||
| // NVTX ranges are only meaningful -- and only safe to include -- on CUDA | ||
| // builds. csrc/ops.cpp also compiles under the ROCm/HIP build (its | ||
| // unconditional `fused_logp` binding has no #if guard), so this header must | ||
| // degrade to a true no-op there rather than failing to find <nvToolsExt.h>. | ||
| // ROCm/roctx tracing is explicit future work, not in scope here. | ||
| #if defined(__CUDACC__) || defined(KERNEL_ALIGN_WITH_CUDA) || defined(KERNEL_ALIGN_WITH_SM90) | ||
|
|
||
| #include <nvToolsExt.h> | ||
|
|
||
| #include <utility> | ||
|
|
||
| namespace rl_kernel { | ||
|
|
||
| // RAII scoped NVTX range. Uses the classic <nvToolsExt.h> API, which links | ||
| // against libnvToolsExt (see the `-lnvToolsExt` link flag added in setup.py) | ||
| // rather than nvtx3's dlopen-based injection layer. Calls are a cheap no-op | ||
| // when no profiler (nsys/ncu) is attached to the process. | ||
| class NvtxRange { | ||
| public: | ||
| explicit NvtxRange(const char* name) { nvtxRangePushA(name); } | ||
| ~NvtxRange() { nvtxRangePop(); } | ||
| NvtxRange(const NvtxRange&) = delete; | ||
| NvtxRange& operator=(const NvtxRange&) = delete; | ||
| }; | ||
|
|
||
| // Wraps a free-function pointer so pybind11 can bind the wrapper in place of | ||
| // the raw pointer; each call is bracketed by an NVTX range named `name`, so | ||
| // nsys shows one labeled block per RL-Kernel op regardless of how many CUDA | ||
| // kernels the op launches internally. | ||
| template <typename Ret, typename... Args> | ||
| auto traced(const char* name, Ret (*fn)(Args...)) { | ||
| return [name, fn](Args... args) -> Ret { | ||
| NvtxRange range(name); | ||
| return fn(std::forward<Args>(args)...); | ||
| }; | ||
| } | ||
|
|
||
| } // namespace rl_kernel | ||
|
|
||
| #define RL_KERNEL_NVTX_RANGE(name) ::rl_kernel::NvtxRange _rl_kernel_nvtx_range(name) | ||
|
|
||
| #else // Not a CUDA build (e.g. ROCm-only): compile out entirely. | ||
|
|
||
| namespace rl_kernel { | ||
|
|
||
| template <typename Ret, typename... Args> | ||
| auto traced(const char* /*name*/, Ret (*fn)(Args...)) { | ||
| return fn; | ||
| } | ||
|
|
||
| } // namespace rl_kernel | ||
|
|
||
| #define RL_KERNEL_NVTX_RANGE(name) \ | ||
| do { \ | ||
| } while (0) | ||
|
|
||
| #endif |
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,97 @@ | ||
| # Metrics & Dashboards Guide | ||
|
|
||
| This guide explains how to expose RL-Kernel's live Prometheus `/metrics` endpoint and load the | ||
| sample Grafana dashboard, for cluster-level monitoring of kernel throughput, backend-fallback | ||
| rate, and KV-cache fragmentation across a training/rollout deployment. | ||
|
|
||
| For per-op kernel-launch tracing inside a single process (an `nsys` timeline), see the | ||
| [NVTX & Nsight Profiling Guide](nsys-profiling.md) instead — that is a micro-level, offline | ||
| trace; this page covers the macro-level, always-on metrics surface. | ||
|
|
||
| ## 1. Install | ||
|
|
||
| Prometheus support is an optional dependency: | ||
|
|
||
| ```bash | ||
| pip install -e .[observability] | ||
| ``` | ||
|
|
||
| Without it, every metrics function in `rl_engine.observability.metrics` degrades to a no-op and | ||
| logs a one-time warning — no other RL-Kernel functionality is affected. | ||
|
|
||
| ## 2. Environment Variables | ||
|
|
||
| | Variable | Default | Purpose | | ||
| | --- | --- | --- | | ||
| | `RL_KERNEL_ENABLE_OP_METRICS` | off | Opt-in: wrap `KernelRegistry.get_op(...)` results to record per-op call count and latency. Off by default because several tests assert on the concrete op class returned by `get_op(...)`. | | ||
| | `RL_KERNEL_ENABLE_METRICS_SERVER` | off | Opt-in: auto-start the `/metrics` HTTP endpoint from `RolloutExecutor` on kernel init. | | ||
| | `RL_KERNEL_METRICS_PORT` | `9400` | Base port for the `/metrics` endpoint. The actual bind port is `RL_KERNEL_METRICS_PORT + RANK` (falls back to `LOCAL_RANK`, then `0`), so multiple ranks on one node do not collide. | | ||
|
|
||
| Backend-fallback and KV-cache-fragmentation recording require no opt-in beyond having | ||
| `prometheus_client` installed — they never change any function's return type, so they are | ||
| always active once the dependency is present. | ||
|
|
||
| ## 3. Start a Worker and Scrape It | ||
|
|
||
| ```bash | ||
| RL_KERNEL_ENABLE_METRICS_SERVER=1 RL_KERNEL_ENABLE_OP_METRICS=1 \ | ||
| python examples/grpo_single_gpu.py --device cuda --steps 2 \ | ||
| --num-prompts 1 --samples-per-prompt 2 --prompt-len 2 --completion-len 3 \ | ||
| --vocab-size 16 --hidden-dim 8 | ||
| ``` | ||
|
|
||
| In another shell: | ||
|
|
||
| ```bash | ||
| curl http://localhost:9400/metrics | ||
| ``` | ||
|
|
||
| Confirm the response contains: | ||
|
|
||
| - `rlkernel_op_calls_total` | ||
| - `rlkernel_op_latency_seconds_bucket` | ||
| - `rlkernel_op_fallbacks_total` | ||
| - `rlkernel_kv_cache_fragmentation_ratio` | ||
|
|
||
| You can also start the server directly from Python without any environment variable, for | ||
| notebooks or ad hoc scripts: | ||
|
|
||
| ```python | ||
| from rl_engine.observability.metrics import start_metrics_server | ||
|
|
||
| start_metrics_server(port=9400) | ||
| ``` | ||
|
|
||
| ## 4. Point Prometheus at It | ||
|
|
||
| ```yaml | ||
| scrape_configs: | ||
| - job_name: rl-kernel | ||
| static_configs: | ||
| - targets: ["localhost:9400"] | ||
| ``` | ||
|
|
||
| For a multi-rank node, add one target per rank's resolved port | ||
| (`RL_KERNEL_METRICS_PORT + rank`). | ||
|
|
||
| ## 5. Load the Sample Dashboard | ||
|
|
||
| Import `examples/grafana/rl_kernel_dashboard.json` into Grafana (**Dashboards → New → Import**), | ||
| and select your Prometheus datasource when prompted. It ships five panels: | ||
|
|
||
| - Scrape Target Up | ||
| - KV-Cache Fragmentation | ||
| - Op Throughput (calls/sec) | ||
| - Op Fallback Rate | ||
| - Op Latency p50 / p95 / p99 | ||
|
|
||
| ## Reporting Guidance | ||
|
|
||
| When sharing a dashboard screenshot or a metrics snapshot, include: | ||
|
|
||
| - The RL-Kernel commit and the exact command used to start the worker. | ||
| - Whether `RL_KERNEL_ENABLE_OP_METRICS` was set (call-count/latency panels are empty otherwise). | ||
| - The number of ranks/workers scraped and their resolved ports. | ||
|
|
||
| Keep committed docs focused on process and configuration. Point-in-time metrics snapshots and | ||
| dashboard screenshots should stay outside the repository. |
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.
The instrumentation is still enabled when we disable profiling. This might cause some performance loss.