Add Triton recompile detection (#45631)

Signed-off-by: Thien Tran <gau.nernst@yahoo.com.sg>
This commit is contained in:
Thien Tran
2026-06-16 18:25:28 +08:00
committed by GitHub
parent 3f1ff1ff14
commit b2cfae777d
6 changed files with 81 additions and 15 deletions
+30 -6
View File
@@ -2,6 +2,7 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import os
import sys
from contextlib import contextmanager
from types import SimpleNamespace
from unittest import mock
@@ -14,8 +15,10 @@ from vllm.triton_utils import jit_monitor
def _reset_monitor():
"""Reset global monitor state between tests."""
jit_monitor._active = False
jit_monitor._verbose = False
yield
jit_monitor._active = False
jit_monitor._verbose = False
# ------------------------------------------------------------------
@@ -30,10 +33,15 @@ def _make_fake_knobs(*, autotuning_print=False, jit_hook=None):
return SimpleNamespace(autotuning=autotuning, runtime=runtime)
@contextmanager
def _patch_triton_knobs(fake_knobs):
"""Context manager that makes ``from triton import knobs`` return *fake_knobs*."""
fake_triton = SimpleNamespace(knobs=fake_knobs)
return mock.patch.dict(sys.modules, {"triton": fake_triton})
with (
mock.patch.dict(sys.modules, {"triton": fake_triton}),
mock.patch.object(jit_monitor, "HAS_TRITON", True),
):
yield
# ------------------------------------------------------------------
@@ -108,7 +116,10 @@ class TestJitHook:
hook = fake.runtime.jit_post_compile_hook
mock_fn = SimpleNamespace(name="test_kernel")
with mock.patch.object(jit_monitor.logger, "warning") as m:
with (
mock.patch.object(jit_monitor.logger, "warning_once") as m,
mock.patch.object(jit_monitor.logger, "warning") as warning,
):
hook(
key="some_key",
repr="some_repr",
@@ -119,6 +130,7 @@ class TestJitHook:
)
m.assert_called_once()
warning.assert_not_called()
msg = m.call_args[0][0] % m.call_args[0][1:]
assert "Triton kernel JIT compilation during inference" in msg
assert "test_kernel" in msg
@@ -206,9 +218,9 @@ if _HAS_TRITON:
tl.store(out_ptr + offs, x + y, mask=mask)
def _run_add_kernel(n: int, block: int = 256) -> None:
def _run_add_kernel(n: int, block: int = 256, offset: int = 0) -> None:
"""Launch ``_add_kernel`` with vectors of length *n*."""
x = torch.randn(n, device="cuda")
x = torch.randn(n + offset, device="cuda")[offset:] # affect alignment
y = torch.randn(n, device="cuda")
out = torch.empty(n, device="cuda")
grid = ((n + block - 1) // block,)
@@ -224,7 +236,7 @@ class TestTritonJitHookIntegration:
_run_add_kernel(1024)
jit_monitor.activate()
with mock.patch.object(jit_monitor.logger, "warning") as w:
with mock.patch.object(jit_monitor.logger, "warning_once") as w:
_run_add_kernel(1024)
w.assert_not_called()
@@ -232,9 +244,21 @@ class TestTritonJitHookIntegration:
_run_add_kernel(1024, block=256)
jit_monitor.activate()
with mock.patch.object(jit_monitor.logger, "warning") as w:
with mock.patch.object(jit_monitor.logger, "warning_once") as w:
# Different BLOCK (a tl.constexpr) forces recompilation.
_run_add_kernel(1024, block=512)
w.assert_called()
msg = w.call_args[0][0] % w.call_args[0][1:]
assert "_add_kernel" in msg
def test_verbose_warning_on_each_new_pointer_alignment(self):
_run_add_kernel(1024)
jit_monitor.activate(verbose=True)
with (
mock.patch.object(jit_monitor.logger, "warning") as w,
mock.patch.object(jit_monitor.logger, "warning_once") as w_once,
):
_run_add_kernel(1024, offset=1)
assert w.called
w_once.assert_not_called()