Skip to content

Commit 0bb9b5b

Browse files
[release/2.13] Revert "dynamo: round-trip torch.cuda.stream ctx mgr across graph breaks (#184487)" (#188197)
Revert "dynamo: round-trip torch.cuda.stream ctx mgr across graph breaks (#184487)" This reverts commit b0a67c7. Reverted #184487 on behalf of https://github.com/williamwen42 due to Breaks internal builds ([comment](#184487 (comment))) (cherry picked from commit 697514a) Co-authored-by: PyTorch MergeBot <[email protected]>
1 parent aaac2bf commit 0bb9b5b

2 files changed

Lines changed: 8 additions & 129 deletions

File tree

‎test/dynamo/test_streams.py‎

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import re
44
import unittest
55
import weakref
6-
from typing import Any
76
from unittest.mock import patch
87

98
import torch
@@ -182,6 +181,7 @@ def forward(self, arg0_1: "f32[2, 2]", arg1_1: "f32[2, 2]"):
182181
)
183182

184183
@requires_cuda
184+
@unittest.skip("Needs graph break support with annotation context")
185185
def test_stream_context_graph_break(self):
186186
def fn(x, y):
187187
s2 = torch.Stream()
@@ -206,85 +206,8 @@ def fn(x, y):
206206
) = extract_graph(fn, *inp)
207207
self.assertEqual(expected, actual)
208208
self.assertEqual(len(fw_graphs), 2)
209-
self.assertExpectedInline(
210-
print_graph(fw_graphs[0]),
211-
"""\
212-
class <lambda>(torch.nn.Module):
213-
def forward(self, arg0_1: "f32[2, 2]", arg1_1: "f32[2, 2]"):
214-
# Annotation: {'stream': 2}
215-
add: "f32[2, 2]" = torch.ops.aten.add.Tensor(arg0_1, arg1_1)
216-
217-
# Annotation: {'stream': 1}
218-
add_1: "f32[2, 2]" = torch.ops.aten.add.Tensor(arg0_1, arg1_1); arg0_1 = arg1_1 = None
219-
220-
# Annotation: {'stream': 1}
221-
add_2: "f32[2, 2]" = torch.ops.aten.add.Tensor(add_1, 2); add_1 = None
222-
add_3: "f32[2, 2]" = torch.ops.aten.add.Tensor(add_2, add); add_2 = add = None
223-
return (add_3,)
224-
""",
225-
)
226-
self.assertExpectedInline(
227-
print_graph(fw_graphs[1]),
228-
"""\
229-
class <lambda>(torch.nn.Module):
230-
def forward(self, arg0_1: "f32[2, 2]"):
231-
# Annotation: {'stream': 1}
232-
add: "f32[2, 2]" = torch.ops.aten.add.Tensor(arg0_1, 1); arg0_1 = None
233-
return (add,)
234-
""",
235-
)
236-
237-
@requires_cuda
238-
def test_target_values_dict_round_trips_through_graph_break(self):
239-
# Regression test for a dict-vs-tuple issue interpreting
240-
# `target_values`. This was initially a tuple and got repurposed as a
241-
# dict, but some oft-unexecuted paths still assumed it was a tuple.
242-
# Here we ensure that an in-use stream gets properly reconstructed on
243-
# the other side of a graph break.
244-
def fn(x):
245-
s = torch.cuda.Stream()
246-
with torch.cuda.stream(s):
247-
z = x + 1
248-
torch._dynamo.graph_break()
249-
z = z + 2
250-
return z
251-
252-
inp = (torch.ones(2, 2, device="cuda"),)
253-
expected = fn(*inp)
254-
actual, _, fw_graphs, _ = extract_graph(fn, *inp)
255-
self.assertEqual(expected, actual)
256-
# A graceful graph break inside with torch.cuda.stream(s):
257-
# should split into two graph fragments. Without the fix the
258-
# post-break code is dropped and only the pre-break fragment is
259-
# captured (or the whole frame falls back to eager and zero are
260-
# captured).
261-
self.assertEqual(
262-
len(fw_graphs),
263-
2,
264-
f"expected 2 graph fragments after a graceful graph break "
265-
f"inside 'with torch.cuda.stream(s):', got {len(fw_graphs)} "
266-
f"-- the post-break body was likely dropped",
267-
)
268-
269-
# Both halves of the round-trip should carry a stream annotation
270-
# referring to the one and only stream `s` entered via
271-
# `with torch.cuda.stream(s):`. We do not pin the exact index, only
272-
# that both adds are annotated with the same stream (i.e. `s`).
273-
def add_node_stream(gm) -> int:
274-
adds: list[torch.fx.Node] = [
275-
n
276-
for n in gm.graph.nodes
277-
if n.op == "call_function" and n.target is torch.ops.aten.add.Tensor
278-
]
279-
self.assertEqual(len(adds), 1)
280-
custom: dict[str, Any] = adds[0].meta.get("custom", {})
281-
self.assertIn("stream", custom)
282-
return custom["stream"]
283-
284-
pre_break_stream = add_node_stream(fw_graphs[0])
285-
post_break_stream = add_node_stream(fw_graphs[1])
286-
# The same stream `s` annotates the add on both sides of the break.
287-
self.assertEqual(pre_break_stream, post_break_stream)
209+
self.assertExpectedInline(print_graph(fw_graphs[0]), """""")
210+
self.assertExpectedInline(print_graph(fw_graphs[1]), """""")
288211

289212
@requires_cuda
290213
def test_stream_input(self):

‎torch/_dynamo/variables/streams.py‎

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import collections
2-
import contextlib
3-
import functools
42
from collections.abc import Callable
53
from typing import Any, Optional
64

@@ -22,7 +20,7 @@
2220
from ..source import CurrentStreamSource
2321
from .base import VariableTracker
2422
from .constant import ConstantVariable
25-
from .ctx_manager import ContextWrappingVariable
23+
from .ctx_manager import FxTracebackAnnotateVariable
2624
from .lazy import LazyVariableTracker
2725

2826

@@ -321,7 +319,7 @@ def cur_stream_id(self) -> int:
321319
return id(stream.value)
322320

323321

324-
class StreamContextVariable(ContextWrappingVariable):
322+
class StreamContextVariable(FxTracebackAnnotateVariable):
325323
"""This represents torch.cuda.StreamContext"""
326324

327325
@staticmethod
@@ -338,30 +336,18 @@ def create(
338336
def __init__(self, stream: Optional["StreamVariable"], **kwargs: Any) -> None:
339337
self.stream = stream
340338
super().__init__(
341-
target_values=(),
339+
target_values={"stream": self.get_stream().user_object_index},
342340
initial_values=None,
343341
**kwargs,
344342
)
345343

346344
def enter(
347345
self, tx: "InstructionTranslatorBase", *args: VariableTracker
348346
) -> VariableTracker:
349-
strm: StreamVariable = self.get_stream()
350347
# to stream, from stream is the order of the arguments
351348
# we are entering the target, and leaving the initial stream
352-
tx.symbolic_stream_state.enter_stream(strm)
353-
354-
# annotate + preserve_node_meta: this ensures the captured
355-
# nodes have appropriate node.meta["custom"]["stream"] annotations.
356-
if strm.user_object_index is None:
357-
raise AssertionError("stream not registered")
358-
stream_idx: int = strm.user_object_index
359-
annotation: dict[str, Any] = {"stream": stream_idx}
360-
stack = contextlib.ExitStack()
361-
stack.enter_context(torch.fx.traceback.annotate(annotation))
362-
stack.enter_context(torch.fx.traceback.preserve_node_meta())
363-
self.set_cleanup_hook(tx, lambda: stack.close())
364-
return ConstantVariable.create(None)
349+
tx.symbolic_stream_state.enter_stream(self.get_stream())
350+
return super().enter(tx)
365351

366352
def exit(
367353
self, tx: "InstructionTranslatorBase", *args: VariableTracker
@@ -377,36 +363,6 @@ def python_type(self) -> type:
377363
def supports_graph_breaks(self) -> bool:
378364
return True
379365

380-
def reconstruct_type(self, codegen: "PyCodegen") -> None:
381-
# Override our parent's reconstruct_type, as that implementation tries
382-
# to access "torch._C.fn_name()" which does not exist. Unfortunately we
383-
# cannot just directly reference the stream in the generated code.
384-
#
385-
# Instead, we push a 0-arg callable that returns the ctx mgr value
386-
# at runtime. The partial is installed as a global on the compiled
387-
# function so the resume prologue can LOAD_GLOBAL it.
388-
389-
# self.stream is None when we're really a StreamVariable
390-
# entering itself as a ctx mgr (with foo: where foo is a
391-
# bare Stream), in which case self IS the stream and
392-
# self.value holds it. Otherwise we're a true
393-
# StreamContextVariable wrapping someone else's StreamVariable
394-
# and the value lives on the wrapped stream.
395-
if self.stream is not None:
396-
stream_value = self.stream.value
397-
else:
398-
stream_value = getattr(self, "value", None)
399-
if stream_value is None:
400-
raise AssertionError(
401-
"StreamContextVariable.reconstruct_type called without a "
402-
"stream value to rebuild; this should be impossible for "
403-
"a properly-constructed ctx manager."
404-
)
405-
406-
thunk = functools.partial(torch.cuda.stream, stream_value)
407-
name = codegen.tx.output.install_global_by_id("_stream_ctx_thunk", thunk)
408-
codegen.append_output(codegen.create_load_global(name, add=True))
409-
410366
def get_stream(self) -> "StreamVariable":
411367
if not self.stream:
412368
raise AssertionError("Stream context should have a separate stream")

0 commit comments

Comments
 (0)