Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/184487
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit e1dfc67 with merge base 404390f ( BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
One or more co-authors of this pull request were not found. You must specify co-authors in commit message trailer via: Supported
Alternatively, if the co-author should not be included, remove the Please update your commit message(s) by doing |
|
|
|
cc: @riccardofelluga for review |
riccardofelluga
left a comment
There was a problem hiding this comment.
@tfogal Thanks! It mostly looks bullet proof to me, a couple of nits with the test and the inheritance but overall I like this!
williamwen42
left a comment
There was a problem hiding this comment.
Approach looks good to me - address lint failures and comments.
96ccdeb to
3993fad
Compare
|
I don't think I have any means to fix the CI issue: Infra problem? Who can we ping to investigate? |
|
@pytorchmergebot rebase |
1 similar comment
|
@pytorchmergebot rebase |
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
|
Successfully rebased |
a69bf56 to
f1d262d
Compare
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
|
Looks like the issue is @pytorchmergebot rebase |
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
Upstream pytorch#166472 (mlazos, '[user-streams] Switch to fx annotations at trace time') changed StreamContextVariable.target_values from a positional list [stream_var] to a dict {"stream": idx} so that StreamContextVariable.enter() (inherited from FxTracebackAnnotateVariable.enter) could feed it to torch.fx.traceback.annotate. That broke three other consumers of target_values -- output_graph.py's stack_ctx_args / locals_ctx_args population, the base ContextWrappingVariable.reconstruct, and BlockStackEntry.resume_fn -> ReenterWith.try_finally -- all of which iterate it as a positional tuple. tuple({"stream": 3}) flattens to ("stream",): the int index is silently dropped and the rebuild call ends up as e.g. torch.cuda.stream("stream"), which fails with a confusing device-string parse error. The breakage has been latent since pytorch#166472 because FxTracebackAnnotateVariable.reconstruct_type raises Unsupported("torch.fx.traceback.annotate escaped from compiled region") unconditionally, short-circuiting the rebuild before the dict-vs-positional mismatch can surface. The visible symptom is a silent ungraceful graph break that drops the post-break body when a graph break occurs inside with torch.cuda.stream(s):. This fix structurally separates the two purposes of target_values: * self.annotation = {"stream": idx} is a new dedicated field that enter() passes to torch.fx.traceback.annotate. * target_values = () keeps the positional rebuild contract happy. Then StreamContextVariable.reconstruct_type is overridden to push a 0-arg callable -- functools.partial(torch.cuda.stream, <Stream>) -- installed as a global on the compiled function via OutputGraph.install_global_by_id. The base ContextWrappingVariable.reconstruct appends CALL 0 (target_values is empty), invoking the partial to produce a fresh StreamContext just in time for the resume prologue's BEFORE_WITH. functools.partial (rather than a freshly-defined closure) is critical: dynamo retraces the resume prologue, and a closure defined inside torch/_dynamo would be classified as MOD_SKIPLIST'd and refuse-to-trace, breaking the prologue. functools.partial is a builtin that dynamo handles natively, and the wrapped target (torch.cuda.stream) is plainly traceable. enter() is also inlined (rather than inherited from FxTracebackAnnotateVariable.enter) because the parent passes self.target_values to annotate, but target_values is now the positional rebuild-args tuple; the annotation dict lives on self.annotation. reconstruct_type is overridden alongside to bypass FxTracebackAnnotateVariable.reconstruct_type's unconditional Unsupported raise. Regression test added at test/dynamo/test_streams.py::test_target_values_dict_round_trips_through_graph_break: graph break inside with torch.cuda.stream(s):, asserts two graph fragments are captured (rather than the post-break body being silently dropped to eager). Whole test_streams.py suite: 68 pass, 7 platform-skipped, no regressions. Authored with Claude. Signed-off-by: Tom Fogal <[email protected]> Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
instead of having a member variable that's only used in 1 place.
instead of FxTracebackAnnotateVariable. Makes the hierarchy directly mirror the actual types, and importantly deriving from FxTracebackAnnotateVariable wasn't important anymore as we do the annotation ourselves manually. From review comment. Authored with Claude.
Kshiteej asked for a follow-up to pytorch#184487 (review r3274071294): the base ctx-manager treats target_values as the positional rebuild-args contract (iterated as a tuple by reconstruct / resume_fn / _call_func), so it must be None or a Sequence. A dict slips through tuple(target_values) as just its keys -- the pytorch#184487 footgun -- so raise TypeError on misuse. This surfaced FxTracebackAnnotateVariable, which passed the torch.fx.traceback.annotate() dict straight into target_values (safe today only because its reconstruct_type raises Unsupported, so the positional path is never hit). Give it a dedicated self.annotation attribute and pass target_values=(), mirroring StreamContextVariable from pytorch#184487. Similarly surfaced the same issue with NullContextVariable; same fix. Authored with Claude.
|
@pytorchbot revert -m="Breaks internal builds" -c=ghfirst |
|
@pytorchbot successfully started a revert job. Check the current status here. |
…aks (#184487)" This reverts commit b0a67c7. Reverted #184487 on behalf of https://github.com/williamwen42 due to Breaks internal builds ([comment](#184487 (comment)))
|
@tfogal your PR has been successfully reverted. |
This PR was reopened (likely due to being reverted), so your approval was removed. Please request another review.
|
Streams were not being reconstructed across graph breaks correctly, e.g. @requires_cuda
def test_stream_value_live_across_graph_break_in_stream_context(self):
# Regression test: a plain torch.Stream value that is live across a
# graph break occurring inside `with torch.cuda.stream(side):` must be
# reconstructed as a Stream, not rebuilt as a StreamContext.
#
# StreamVariable subclasses StreamContextVariable for code reuse, so
# OutputGraph._get_stack_values_to_restore used to misclassify the live
# stream `s` as an inactive context manager and rebuild it in the resume
# function via torch.cuda.stream(s) -> a StreamContext. The eager
# record_stream() call below then died with "unknown parameter type".
@torch._dynamo.disable
def eager_record_stream(t, stream):
assert isinstance(stream, torch.Stream), type(stream)
t.record_stream(stream)
def fn(x):
s = torch.cuda.current_stream()
side = torch.cuda.Stream()
with torch.cuda.stream(side):
y = x + 1
torch._dynamo.graph_break()
out = y + 1
del y
eager_record_stream(out, s)
return out
x = torch.ones(3, device="cuda")
out = torch.compile(fn, backend="eager")(x)
self.assertEqual(out, torch.full((3,), 3.0, device="cuda"))I have an internal diff (trying to export) that fixes this issue - we should include the fixes into this commit before relanding |
…cross 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]>
Kshiteej asked for a follow-up to pytorch#184487 (review r3274071294): the base ctx-manager treats target_values as the positional rebuild-args contract (iterated as a tuple by reconstruct / resume_fn / _call_func), so it must be None or a Sequence. A dict slips through tuple(target_values) as just its keys -- the pytorch#184487 footgun -- so raise TypeError on misuse. This surfaced FxTracebackAnnotateVariable, which passed the torch.fx.traceback.annotate() dict straight into target_values (safe today only because its reconstruct_type raises Unsupported, so the positional path is never hit). Give it a dedicated self.annotation attribute and pass target_values=(), mirroring StreamContextVariable from pytorch#184487. Similarly surfaced the same issue with NullContextVariable; same fix. Authored with Claude.
…orch#184487) Upstream pytorch#166472 ('[user-streams] Switch to fx annotations at trace time') changed `StreamContextVariable.target_values` from a positional list [stream_var] to a dict {"stream": idx} so that StreamContextVariable.enter() (inherited from FxTracebackAnnotateVariable.enter) could feed it to torch.fx.traceback.annotate. That broke three other consumers of target_values -- output_graph.py's stack_ctx_args / locals_ctx_args population, the base ContextWrappingVariable.reconstruct, and BlockStackEntry.resume_fn -> ReenterWith.try_finally -- all of which iterate it as a positional tuple. tuple({"stream": 3}) flattens to ("stream",): the int index is silently dropped and the rebuild call ends up as e.g. torch.cuda.stream("stream"), which fails with a confusing device-string parse error. The breakage has been latent since pytorch#166472 because FxTracebackAnnotateVariable.reconstruct_type raises Unsupported("torch.fx.traceback.annotate escaped from compiled region") unconditionally, short-circuiting the rebuild before the dict-vs-positional mismatch can surface. The visible symptom is a silent ungraceful graph break that drops the post-break body when a graph break occurs inside with torch.cuda.stream(s):. This fix structurally separates the two purposes of target_values: * self.annotation = {"stream": idx} is a new dedicated field that enter() passes to torch.fx.traceback.annotate. * target_values = () keeps the positional rebuild contract happy. Then StreamContextVariable.reconstruct_type is overridden to push a 0-arg callable -- functools.partial(torch.cuda.stream, <Stream>) -- installed as a global on the compiled function via OutputGraph.install_global_by_id. The base ContextWrappingVariable.reconstruct appends CALL 0 (target_values is empty), invoking the partial to produce a fresh StreamContext just in time for the resume prologue's BEFORE_WITH. functools.partial (rather than a freshly-defined closure) is critical: dynamo retraces the resume prologue, and a closure defined inside torch/_dynamo would be classified as MOD_SKIPLIST'd and refuse-to-trace, breaking the prologue. functools.partial is a builtin that dynamo handles natively, and the wrapped target (torch.cuda.stream) is plainly traceable. enter() is also inlined (rather than inherited from FxTracebackAnnotateVariable.enter) because the parent passes self.target_values to annotate, but target_values is now the positional rebuild-args tuple; the annotation dict lives on self.annotation. reconstruct_type is overridden alongside to bypass FxTracebackAnnotateVariable.reconstruct_type's unconditional Unsupported raise. Regression test added at test/dynamo/test_streams.py::test_target_values_dict_round_trips_through_graph_break: graph break inside with torch.cuda.stream(s):, asserts two graph fragments are captured (rather than the post-break body being silently dropped to eager). Whole test_streams.py suite: 68 pass, 7 platform-skipped, no regressions. Authored with Claude. Pull Request resolved: pytorch#184487 Approved by: https://github.com/williamwen42, https://github.com/mlazos Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
…aks (pytorch#184487)" This reverts commit b0a67c7. Reverted pytorch#184487 on behalf of https://github.com/williamwen42 due to Breaks internal builds ([comment](pytorch#184487 (comment)))
Kshiteej asked for a follow-up to pytorch#184487 (review r3274071294): the base ctx-manager treats target_values as the positional rebuild-args contract (iterated as a tuple by reconstruct / resume_fn / _call_func), so it must be None or a Sequence. A dict slips through tuple(target_values) as just its keys -- the pytorch#184487 footgun -- so raise TypeError on misuse. This surfaced FxTracebackAnnotateVariable, which passed the torch.fx.traceback.annotate() dict straight into target_values (safe today only because its reconstruct_type raises Unsupported, so the positional path is never hit). Give it a dedicated self.annotation attribute and pass target_values=(), mirroring StreamContextVariable from pytorch#184487. Similarly surfaced the same issue with NullContextVariable; same fix. Authored with Claude.
Kshiteej asked for a follow-up to pytorch#184487 (review r3274071294): the base ctx-manager treats target_values as the positional rebuild-args contract (iterated as a tuple by reconstruct / resume_fn / _call_func), so it must be None or a Sequence. A dict slips through tuple(target_values) as just its keys -- the pytorch#184487 footgun -- so raise TypeError on misuse. This surfaced FxTracebackAnnotateVariable, which passed the torch.fx.traceback.annotate() dict straight into target_values (safe today only because its reconstruct_type raises Unsupported, so the positional path is never hit). Give it a dedicated self.annotation attribute and pass target_values=(), mirroring StreamContextVariable from pytorch#184487. Similarly surfaced the same issue with NullContextVariable; same fix. Authored with Claude.
…orch#184487) Upstream pytorch#166472 ('[user-streams] Switch to fx annotations at trace time') changed `StreamContextVariable.target_values` from a positional list [stream_var] to a dict {"stream": idx} so that StreamContextVariable.enter() (inherited from FxTracebackAnnotateVariable.enter) could feed it to torch.fx.traceback.annotate. That broke three other consumers of target_values -- output_graph.py's stack_ctx_args / locals_ctx_args population, the base ContextWrappingVariable.reconstruct, and BlockStackEntry.resume_fn -> ReenterWith.try_finally -- all of which iterate it as a positional tuple. tuple({"stream": 3}) flattens to ("stream",): the int index is silently dropped and the rebuild call ends up as e.g. torch.cuda.stream("stream"), which fails with a confusing device-string parse error. The breakage has been latent since pytorch#166472 because FxTracebackAnnotateVariable.reconstruct_type raises Unsupported("torch.fx.traceback.annotate escaped from compiled region") unconditionally, short-circuiting the rebuild before the dict-vs-positional mismatch can surface. The visible symptom is a silent ungraceful graph break that drops the post-break body when a graph break occurs inside with torch.cuda.stream(s):. This fix structurally separates the two purposes of target_values: * self.annotation = {"stream": idx} is a new dedicated field that enter() passes to torch.fx.traceback.annotate. * target_values = () keeps the positional rebuild contract happy. Then StreamContextVariable.reconstruct_type is overridden to push a 0-arg callable -- functools.partial(torch.cuda.stream, <Stream>) -- installed as a global on the compiled function via OutputGraph.install_global_by_id. The base ContextWrappingVariable.reconstruct appends CALL 0 (target_values is empty), invoking the partial to produce a fresh StreamContext just in time for the resume prologue's BEFORE_WITH. functools.partial (rather than a freshly-defined closure) is critical: dynamo retraces the resume prologue, and a closure defined inside torch/_dynamo would be classified as MOD_SKIPLIST'd and refuse-to-trace, breaking the prologue. functools.partial is a builtin that dynamo handles natively, and the wrapped target (torch.cuda.stream) is plainly traceable. enter() is also inlined (rather than inherited from FxTracebackAnnotateVariable.enter) because the parent passes self.target_values to annotate, but target_values is now the positional rebuild-args tuple; the annotation dict lives on self.annotation. reconstruct_type is overridden alongside to bypass FxTracebackAnnotateVariable.reconstruct_type's unconditional Unsupported raise. Regression test added at test/dynamo/test_streams.py::test_target_values_dict_round_trips_through_graph_break: graph break inside with torch.cuda.stream(s):, asserts two graph fragments are captured (rather than the post-break body being silently dropped to eager). Whole test_streams.py suite: 68 pass, 7 platform-skipped, no regressions. Authored with Claude. Pull Request resolved: pytorch#184487 Approved by: https://github.com/williamwen42, https://github.com/mlazos Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
…aks (pytorch#184487)" This reverts commit b0a67c7. Reverted pytorch#184487 on behalf of https://github.com/williamwen42 due to Breaks internal builds ([comment](pytorch#184487 (comment)))
In #184487, @kshitij12345 recommended we validate that target_values is actually what it should be. This does that validation---and as Kshiteej probably suspected, it indeed flags a bug (also fixed). This bug was: FxTracebackAnnotateVariable passed the torch.fx.traceback.annotate() dict straight into target_values (safe today only because its reconstruct_type raises Unsupported, so the positional path is never hit). Give it a dedicated self.annotation attribute and pass target_values=(), mirroring StreamContextVariable from #184487. Authored with Claude. Pull Request resolved: #186968 Approved by: https://github.com/kshitij12345, https://github.com/jansel
In pytorch#184487, @kshitij12345 recommended we validate that target_values is actually what it should be. This does that validation---and as Kshiteej probably suspected, it indeed flags a bug (also fixed). This bug was: FxTracebackAnnotateVariable passed the torch.fx.traceback.annotate() dict straight into target_values (safe today only because its reconstruct_type raises Unsupported, so the positional path is never hit). Give it a dedicated self.annotation attribute and pass target_values=(), mirroring StreamContextVariable from pytorch#184487. Authored with Claude. Pull Request resolved: pytorch#186968 Approved by: https://github.com/kshitij12345, https://github.com/jansel
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Upstream #166472 ('[user-streams] Switch to fx annotations at trace time') changed
StreamContextVariable.target_valuesfrom a positional list [stream_var] to a dict {"stream": idx} so that StreamContextVariable.enter() (inherited from FxTracebackAnnotateVariable.enter) could feed it to torch.fx.traceback.annotate. That broke three other consumers of target_values -- output_graph.py's stack_ctx_args / locals_ctx_args population, the baseContextWrappingVariable.reconstruct, and
BlockStackEntry.resume_fn -> ReenterWith.try_finally -- all of which iterate it as a positional tuple. tuple({"stream": 3}) flattens to ("stream",): the int index is silently dropped and the rebuild call ends up as e.g. torch.cuda.stream("stream"), which fails with a confusing device-string parse error.
The breakage has been latent since #166472 because FxTracebackAnnotateVariable.reconstruct_type raises Unsupported("torch.fx.traceback.annotate escaped from compiled region") unconditionally, short-circuiting the rebuild before the dict-vs-positional mismatch can surface. The visible symptom is a silent ungraceful graph break that drops the post-break body when a graph break occurs inside with torch.cuda.stream(s):.
This fix structurally separates the two purposes of target_values:
Then StreamContextVariable.reconstruct_type is overridden to push a 0-arg callable -- functools.partial(torch.cuda.stream, ) -- installed as a global on the compiled function via OutputGraph.install_global_by_id. The base
ContextWrappingVariable.reconstruct appends CALL 0 (target_values is empty), invoking the partial to produce a fresh StreamContext just in time for the resume prologue's BEFORE_WITH.
functools.partial (rather than a freshly-defined closure) is critical: dynamo retraces the resume prologue, and a closure defined inside torch/_dynamo would be classified as MOD_SKIPLIST'd and refuse-to-trace, breaking the prologue. functools.partial is a builtin that dynamo handles natively, and the wrapped target (torch.cuda.stream) is plainly traceable.
enter() is also inlined (rather than inherited from FxTracebackAnnotateVariable.enter) because the parent passes self.target_values to annotate, but target_values is now the positional rebuild-args tuple; the annotation dict lives on self.annotation. reconstruct_type is overridden alongside to bypass FxTracebackAnnotateVariable.reconstruct_type's unconditional Unsupported raise.
Regression test added at
test/dynamo/test_streams.py::test_target_values_dict_round_trips_through_graph_break: graph break inside with torch.cuda.stream(s):, asserts two graph fragments are captured (rather than the post-break body being silently dropped to eager). Whole test_streams.py suite: 68 pass, 7 platform-skipped, no regressions.
Authored with Claude.
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98