Skip to content

Commit 764d9f7

Browse files
committed
[Refactor] Add compile-aware timing and profiling helpers
Add _maybe_timeit, _maybe_record_function, and _maybe_record_function_decorator that return nullcontext when inside a torch.compile region. These helpers prevent graph breaks from timing/profiling code during compilation. ghstack-source-id: 33dfc09 Pull-Request: #3297
1 parent d3aba7d commit 764d9f7

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

torchrl/_utils.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,47 @@ def reset(cls):
333333
cls.erase()
334334

335335

336+
def _maybe_timeit(name):
337+
"""Return timeit context if not compiling, nullcontext otherwise.
338+
339+
torch.compiler.is_compiling() returns True when inside a compiled region,
340+
and timeit uses time.time() which dynamo cannot trace.
341+
"""
342+
if is_compiling():
343+
return nullcontext()
344+
return timeit(name)
345+
346+
347+
def _maybe_record_function(name):
348+
"""Return record_function context if not compiling, nullcontext otherwise.
349+
350+
torch.autograd.profiler.record_function cannot be used inside compiled regions.
351+
"""
352+
from torch.autograd.profiler import record_function
353+
354+
if is_compiling():
355+
return nullcontext()
356+
return record_function(name)
357+
358+
359+
def _maybe_record_function_decorator(name: str) -> Callable[[Callable], Callable]:
360+
"""Decorator version of :func:`_maybe_record_function`.
361+
362+
This is preferred over sprinkling many context managers in hot code paths,
363+
as it reduces Python overhead while keeping a useful profiler structure.
364+
"""
365+
366+
def decorator(fn: Callable) -> Callable:
367+
@wraps(fn)
368+
def wrapped(*args, **kwargs):
369+
with _maybe_record_function(name):
370+
return fn(*args, **kwargs)
371+
372+
return wrapped
373+
374+
return decorator
375+
376+
336377
def _check_for_faulty_process(processes):
337378
terminate = False
338379
for p in processes:

0 commit comments

Comments
 (0)