|
5 | 5 | import os |
6 | 6 | import random |
7 | 7 | import statistics |
8 | | -import warnings |
9 | 8 | from contextlib import contextmanager, nullcontext |
10 | 9 | from dataclasses import dataclass, field |
11 | 10 | from pathlib import Path |
|
20 | 19 | from torch.cuda._memory_viz import profile_plot # type: ignore |
21 | 20 | from torch.profiler import profile, ProfilerActivity, record_function, schedule |
22 | 21 |
|
23 | | -warnings.filterwarnings("ignore", message=".*SyncActivityProfilerHandler.*") |
24 | | -warnings.filterwarnings("ignore", message=".*profiler_start.*") |
25 | | -warnings.filterwarnings("ignore", message=".*profiler_stop.*") |
26 | | - |
27 | 22 | logger = logging.getLogger(__name__) |
28 | 23 | logger.addHandler(logging.NullHandler()) |
29 | 24 |
|
| 25 | +_KINETO_LOG_LEVEL_ENV = "KINETO_LOG_LEVEL" |
| 26 | +_KINETO_SUPPRESS_ALL_LOGS_LEVEL = "6" |
| 27 | +_KEEP_KINETO_LOG_LEVEL_ENV = "TRANSFORMER_NUGGETS_KEEP_KINETO_LOG_LEVEL" |
| 28 | + |
| 29 | + |
| 30 | +@contextmanager |
| 31 | +def _suppress_sync_activity_profiler_logs(): |
| 32 | + """Suppress noisy Kineto profiler start/stop logs during benchmark timing. |
| 33 | +
|
| 34 | + Recent PyTorch nightlies can print lines like:: |
| 35 | +
|
| 36 | + USDT:... SyncActivityProfilerHandler.cpp:52] profiler_start |
| 37 | + USDT:... SyncActivityProfilerHandler.cpp:59] profiler_stop |
| 38 | +
|
| 39 | + These are emitted by Kineto native logging, not Python ``warnings``. |
| 40 | + ``KINETO_LOG_LEVEL=6`` disables these logs and is read dynamically by |
| 41 | + Kineto, so setting it only around the profiler-backed benchmark call avoids |
| 42 | + subprocesses and fd-level stderr redirection. |
| 43 | + """ |
| 44 | + if os.environ.get(_KEEP_KINETO_LOG_LEVEL_ENV): |
| 45 | + yield |
| 46 | + return |
| 47 | + |
| 48 | + previous_level = os.environ.get(_KINETO_LOG_LEVEL_ENV) |
| 49 | + os.environ[_KINETO_LOG_LEVEL_ENV] = _KINETO_SUPPRESS_ALL_LOGS_LEVEL |
| 50 | + try: |
| 51 | + yield |
| 52 | + finally: |
| 53 | + if previous_level is None: |
| 54 | + os.environ.pop(_KINETO_LOG_LEVEL_ENV, None) |
| 55 | + else: |
| 56 | + os.environ[_KINETO_LOG_LEVEL_ENV] = previous_level |
| 57 | + |
30 | 58 |
|
31 | 59 | def _nvml(): |
32 | 60 | library_path = ctypes.util.find_library("nvidia-ml") or "libnvidia-ml.so.1" |
@@ -267,7 +295,8 @@ def _call_do_bench_using_profiling( |
267 | 295 | call_kwargs = {"rep": rep} |
268 | 296 | if "is_vetted_benchmarking" in params: |
269 | 297 | call_kwargs["is_vetted_benchmarking"] = is_vetted_benchmarking |
270 | | - return do_bench_using_profiling(fn, **call_kwargs) |
| 298 | + with _suppress_sync_activity_profiler_logs(): |
| 299 | + return do_bench_using_profiling(fn, **call_kwargs) |
271 | 300 |
|
272 | 301 |
|
273 | 302 | def _benchmark_cuda_graph_replay_samples_us( |
@@ -373,13 +402,14 @@ def benchmark_cuda_function_stats(func: Callable, *args, **kwargs) -> CudaBenchm |
373 | 402 | no_args = lambda: func(*args, **kwargs) |
374 | 403 | from torch._inductor.runtime.benchmarking import benchmarker |
375 | 404 |
|
376 | | - samples_ms = benchmarker.benchmark_gpu( |
377 | | - no_args, |
378 | | - benchmark_iters=num_iters, |
379 | | - memory_warmup_iters=memory_warmup_iters, |
380 | | - return_mode="all", |
381 | | - is_vetted_benchmarking=is_vetted_benchmarking, |
382 | | - ) |
| 405 | + with _suppress_sync_activity_profiler_logs(): |
| 406 | + samples_ms = benchmarker.benchmark_gpu( |
| 407 | + no_args, |
| 408 | + benchmark_iters=num_iters, |
| 409 | + memory_warmup_iters=memory_warmup_iters, |
| 410 | + return_mode="all", |
| 411 | + is_vetted_benchmarking=is_vetted_benchmarking, |
| 412 | + ) |
383 | 413 | return CudaBenchmarkStats.from_samples( |
384 | 414 | (float(sample) * 1e3 for sample in samples_ms), |
385 | 415 | confidence=confidence, |
|
0 commit comments