Skip to content

Commit

Permalink
dynamo_timed: Add a log_waitcounter option. (#141402)
Browse files Browse the repository at this point in the history
Summary:
This logs a waitcounter of the name pytorch.dynamo_timed.{key}.

Primarily sending this now to make sure everyone likes the API, then
I'll add tests, and migrate one dynamo_timed to use it. (likely starting
with
pytorch/pytorch#141379).

Testing is a bit harder, since we don't normally have any way to read
_WaitCounter state AFAICT. I want to poke around and see if I can figure
out a way to read the state, otherwise I'll just mock it to at least
make sure it's mostly working.

X-link: pytorch/pytorch#141402
Approved by: https://github.com/jamesjwu, https://github.com/masnesral

Reviewed By: atalman

Differential Revision: D66729166

Pulled By: c00w

fbshipit-source-id: cec031ead8e83e0cc7dc31435b9cc3c69744a29c
  • Loading branch information
c00w authored and facebook-github-bot committed Dec 4, 2024
1 parent a86ec7f commit 505e74e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion userbenchmark/dynamo/dynamobench/_dynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
signpost_event,
)
from torch.fx._utils import _format_graph_code, lazy_format_graph_code
from torch.monitor import _WaitCounter
from torch.nn.modules.lazy import LazyModuleMixin
from torch.utils._triton import has_triton, has_triton_package
from torch.utils.hooks import RemovableHandle
Expand Down Expand Up @@ -301,6 +302,7 @@ def dynamo_timed(
log_pt2_compile_event: bool = False,
metadata: Optional[Dict[str, object]] = None,
dynamo_compile_column_us: Optional[str] = None,
log_waitcounter: bool = False,
) -> Generator[Any, None, None]:
"""
dynamo_timed is a context manager
Expand Down Expand Up @@ -334,6 +336,7 @@ def _foo(...):
- dynamo_compile_column_us: If provided, updates the specified CompilationMetrics
field to be logged to dyname_compile column. We expect all columns to be _us;
therefore, the field name must end with "_us".
- log_waitcounter: If set, we'll log a waitcounter of the form "pytorch.dynamo_timed.{key}"
"""
# We're standardizing on microseconds for dynamo_compile timings.
if dynamo_compile_column_us is not None:
Expand Down Expand Up @@ -363,7 +366,11 @@ def _foo(...):

try:
with torch.profiler.record_function(f"{key} (dynamo_timed)"):
yield
if log_waitcounter:
with _WaitCounter(f"pytorch.dynamo_timed.{key}").guard():
yield
else:
yield
finally:
end_ns = time.time_ns()
time_spent_ns = end_ns - start_ns
Expand Down

0 comments on commit 505e74e

Please sign in to comment.