Repository navigation
[precompile] Refuse recompiles in an installed artifact without the global stance - #198557
bobrenjc93 wants to merge 11 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/198557
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 e3b2f5e with merge base a4849a9 ( 🟡 GREEN LIGHT - PR requires human review:
reason: 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. |
|
Claude finished @bobrenjc93's task in 1m 27s —— View job One blocking issue: with this PR, a user-set PR Review: #198557SummaryThe PR binds API Design
Testing
Code Quality
RecommendationRequest Changes. The |
[ghstack-poisoned]
|
@claude review |
|
Claude finished @bobrenjc93's task in 45s —— View job The only code change since my last review fixes the PR Review: #198557SummaryWhen an installed artifact loads, the PR binds a Code Quality
RecommendationApprove once the PR description is updated. Nothing in the code blocks. |
[ghstack-poisoned]
|
@claude review |
|
Claude finished @bobrenjc93's task in 52s —— View job The code is unchanged since the round-43 version I approved, and the PR description now matches it. One small doc gap is left: PR Review: #198557SummaryWhen an installed artifact loads, the PR binds a I re-read the full diff against
I found no new code defects. API Design
RecommendationApprove after the one-line docstring addition. Nothing else blocks. |
[ghstack-poisoned]
…lobal stance
An installed artifact served every call under
`torch.compiler.set_stance("fail_on_recompile")`, which sets the process-global
`_stance`. While one thread was inside a served call, any other thread's
`torch.compile` read that stance and raised on its first compile, and two
overlapping calls could restore the stance out of order.
The stance is only consulted when a compiled callable is entered, to turn its
callback into fail_on_recompile's `fail_callback`; that callback then serves
cached entries for every frame of the call and raises instead of compiling.
This moves that callback out of `_callback_from_stance` into a small
`_FailOnRecompileCallback` class (a move plus closure-to-class, no behavior
change for the stance), and `_build_installed_forward` binds one into the
artifact's own `OptimizeContext` once at load, before `context(fn)` captures
the callback. No global state is touched per call.
A process-wide stance the caller sets still passes through
`_callback_from_stance` on every entry, and the compiling ones (force_backend,
"eager_then_compile", "aot_eager_then_compile") would replace or re-wrap the
bound callback and compile what the capture did not cover. So
`_callback_from_stance` now recognizes a `_RefuseRecompileCallback`, a
subclass only the driver builds (the per-call `_FailOnRecompileCallback`s the
stance builds, which `__exit__` can hand back as `self.prior`, keep the old
path), and,
reading the stance once, keeps it under every stance except "force_eager"
(every call runs eagerly, bypassing the installed entries) and
"eager_on_recompile" (captured variants serve, other calls run eagerly). The
decision lives where the stance is read, so a concurrent `set_stance` cannot
land between a check and the choice, and a new stance defaults to keeping the
refusal. The load() docstring and the artifact's serving notes say so.
Binding needs an `OptimizeContext`, which `optimize()` does not return when
Dynamo is disabled (TORCHDYNAMO_DISABLE=1 or the enable_dynamo killswitch);
load now refuses that case with its own PrecompileError instead of the
install-failure message, whose hint blames a source change. It also refuses
`torch._dynamo.config.compiled_autograd`: `OptimizeContext` would compile
backward graphs through a fresh `rebuild_ctx()` callback that does not refuse.
The subclass overrides the message prefix and the exception class: it raises
`_RecompileRefusedError` (a `RuntimeError`) with a message that does not claim a stance is set, and the driver's `forward` catches
it by type rather than matching the message text.
Setting the stance once at load and documenting it was rejected: it would put
every other torch.compile in the process under fail_on_recompile for as long as
the artifact is loaded.
Test Plan:
The new test serves an installed artifact whose entry calls a
`torch._dynamo.disable`d function mid-call that records the global stance; the
fresh process sees "default" (it saw "fail_on_recompile" before this change).
A second test loads an installed artifact under TORCHDYNAMO_DISABLE=1 and
checks the refusal names Dynamo being disabled, not the source; another
enables compiled_autograd before load and checks that refusal. A third
captures `tags_compiled` (which adds 1 only in a compiled frame) on one shape
and serves it in a fresh process under each process-wide stance:
force_backend, "eager_then_compile", "aot_eager_then_compile" and
"fail_on_recompile" serve the captured shape compiled and refuse the uncovered
one with PrecompileError whose text names no stance (twice, past the
delayed-compile warmup);
"force_eager" runs both eagerly; "eager_on_recompile" serves the captured one
compiled and the uncovered one eagerly. The stance recorder also runs a plain
`torch.compile` mid-call, which must still compile. Dropping the
`_FailOnRecompileCallback` branch in `_callback_from_stance` fails the third
test ("compiled under default"); raising a plain RuntimeError from the subclass
callback fails it too, and dropping the compiled_autograd check fails its test.
```
python test/test_precompile.py -k stance -k served_installed -k changed_source -k dynamo_disabled -k compiled_autograd -k own_errors
python test/test_precompile.py
cd test && python -m pytest -q dynamo/test_recompiles.py dynamo/test_decorators.py -k "stance or recompile"
```
This PR was authored with the help of an AI assistant (Claude).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 38e3f46
Pull-Request: #198557
…lobal stance
An installed artifact served every call under
`torch.compiler.set_stance("fail_on_recompile")`, which sets the process-global
`_stance`. While one thread was inside a served call, any other thread's
`torch.compile` read that stance and raised on its first compile, and two
overlapping calls could restore the stance out of order.
The stance is only consulted when a compiled callable is entered, to turn its
callback into fail_on_recompile's `fail_callback`; that callback then serves
cached entries for every frame of the call and raises instead of compiling.
This moves that callback out of `_callback_from_stance` into a small
`_FailOnRecompileCallback` class (a move plus closure-to-class, no behavior
change for the stance), and `_build_installed_forward` binds one into the
artifact's own `OptimizeContext` once at load, before `context(fn)` captures
the callback. No global state is touched per call.
A process-wide stance the caller sets still passes through
`_callback_from_stance` on every entry, and the compiling ones (force_backend,
"eager_then_compile", "aot_eager_then_compile") would replace or re-wrap the
bound callback and compile what the capture did not cover. So
`_callback_from_stance` now recognizes a `_RefuseRecompileCallback`, a
subclass only the driver builds (the per-call `_FailOnRecompileCallback`s the
stance builds, which `__exit__` can hand back as `self.prior`, keep the old
path), and,
reading the stance once, keeps it under every stance except "force_eager"
(every call runs eagerly, bypassing the installed entries) and
"eager_on_recompile" (captured variants serve, other calls run eagerly). The
decision lives where the stance is read, so a concurrent `set_stance` cannot
land between a check and the choice, and a new stance defaults to keeping the
refusal. The load() docstring and the artifact's serving notes say so.
Binding needs an `OptimizeContext`, which `optimize()` does not return when
Dynamo is disabled (TORCHDYNAMO_DISABLE=1 or the enable_dynamo killswitch);
load now refuses that case with its own PrecompileError instead of the
install-failure message, whose hint blames a source change. It also refuses
`torch._dynamo.config.compiled_autograd`: `OptimizeContext` would compile
backward graphs through a fresh `rebuild_ctx()` callback that does not refuse.
The subclass overrides the message prefix and the exception class: it raises
`_RecompileRefusedError` (a `RuntimeError`) with a message that does not claim a stance is set, and the driver's `forward` catches
it by type rather than matching the message text.
Setting the stance once at load and documenting it was rejected: it would put
every other torch.compile in the process under fail_on_recompile for as long as
the artifact is loaded.
Test Plan:
The new test serves an installed artifact whose entry calls a
`torch._dynamo.disable`d function mid-call that records the global stance; the
fresh process sees "default" (it saw "fail_on_recompile" before this change).
A second test loads an installed artifact under TORCHDYNAMO_DISABLE=1 and
checks the refusal names Dynamo being disabled, not the source; another
enables compiled_autograd before load and checks that refusal. A third
captures `tags_compiled` (which adds 1 only in a compiled frame) on one shape
and serves it in a fresh process under each process-wide stance:
force_backend, "eager_then_compile", "aot_eager_then_compile" and
"fail_on_recompile" serve the captured shape compiled and refuse the uncovered
one with PrecompileError whose text names no stance (twice, past the
delayed-compile warmup);
"force_eager" runs both eagerly; "eager_on_recompile" serves the captured one
compiled and the uncovered one eagerly. The stance recorder also runs a plain
`torch.compile` mid-call, which must still compile. Dropping the
`_FailOnRecompileCallback` branch in `_callback_from_stance` fails the third
test ("compiled under default"); raising a plain RuntimeError from the subclass
callback fails it too, and dropping the compiled_autograd check fails its test.
```
python test/test_precompile.py -k stance -k served_installed -k changed_source -k dynamo_disabled -k compiled_autograd -k own_errors
python test/test_precompile.py
cd test && python -m pytest -q dynamo/test_recompiles.py dynamo/test_decorators.py -k "stance or recompile"
```
This PR was authored with the help of an AI assistant (Claude).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 38e3f46
Pull-Request: #198557
…lobal stance
An installed artifact served every call under
`torch.compiler.set_stance("fail_on_recompile")`, which sets the process-global
`_stance`. While one thread was inside a served call, any other thread's
`torch.compile` read that stance and raised on its first compile, and two
overlapping calls could restore the stance out of order.
The stance is only consulted when a compiled callable is entered, to turn its
callback into fail_on_recompile's `fail_callback`; that callback then serves
cached entries for every frame of the call and raises instead of compiling.
This moves that callback out of `_callback_from_stance` into a small
`_FailOnRecompileCallback` class (a move plus closure-to-class, no behavior
change for the stance), and `_build_installed_forward` binds one into the
artifact's own `OptimizeContext` once at load, before `context(fn)` captures
the callback. No global state is touched per call.
A process-wide stance the caller sets still passes through
`_callback_from_stance` on every entry, and the compiling ones (force_backend,
"eager_then_compile", "aot_eager_then_compile") would replace or re-wrap the
bound callback and compile what the capture did not cover. So
`_callback_from_stance` now recognizes a `_RefuseRecompileCallback`, a
subclass only the driver builds (the per-call `_FailOnRecompileCallback`s the
stance builds, which `__exit__` can hand back as `self.prior`, keep the old
path), and,
reading the stance once, keeps it under every stance except "force_eager"
(every call runs eagerly, bypassing the installed entries) and
"eager_on_recompile" (captured variants serve, other calls run eagerly). The
decision lives where the stance is read, so a concurrent `set_stance` cannot
land between a check and the choice, and a new stance defaults to keeping the
refusal. The load() docstring and the artifact's serving notes say so.
Binding needs an `OptimizeContext`, which `optimize()` does not return when
Dynamo is disabled (TORCHDYNAMO_DISABLE=1 or the enable_dynamo killswitch);
load now refuses that case with its own PrecompileError instead of the
install-failure message, whose hint blames a source change. It also refuses
`torch._dynamo.config.compiled_autograd`: `OptimizeContext` would compile
backward graphs through a fresh `rebuild_ctx()` callback that does not refuse.
The subclass overrides the message prefix and the exception class: it raises
`_RecompileRefusedError` (a `RuntimeError`) with a message that does not claim a stance is set, and the driver's `forward` catches
it by type rather than matching the message text.
Setting the stance once at load and documenting it was rejected: it would put
every other torch.compile in the process under fail_on_recompile for as long as
the artifact is loaded.
Test Plan:
The new test serves an installed artifact whose entry calls a
`torch._dynamo.disable`d function mid-call that records the global stance; the
fresh process sees "default" (it saw "fail_on_recompile" before this change).
A second test loads an installed artifact under TORCHDYNAMO_DISABLE=1 and
checks the refusal names Dynamo being disabled, not the source; another
enables compiled_autograd before load and checks that refusal. A third
captures `tags_compiled` (which adds 1 only in a compiled frame) on one shape
and serves it in a fresh process under each process-wide stance:
force_backend, "eager_then_compile", "aot_eager_then_compile" and
"fail_on_recompile" serve the captured shape compiled and refuse the uncovered
one with PrecompileError whose text names no stance (twice, past the
delayed-compile warmup);
"force_eager" runs both eagerly; "eager_on_recompile" serves the captured one
compiled and the uncovered one eagerly. The stance recorder also runs a plain
`torch.compile` mid-call, which must still compile. Dropping the
`_FailOnRecompileCallback` branch in `_callback_from_stance` fails the third
test ("compiled under default"); raising a plain RuntimeError from the subclass
callback fails it too, and dropping the compiled_autograd check fails its test.
```
python test/test_precompile.py -k stance -k served_installed -k changed_source -k dynamo_disabled -k compiled_autograd -k own_errors
python test/test_precompile.py
cd test && python -m pytest -q dynamo/test_recompiles.py dynamo/test_decorators.py -k "stance or recompile"
```
This PR was authored with the help of an AI assistant (Claude).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 38e3f46
Pull-Request: #198557
…lobal stance
An installed artifact served every call under
`torch.compiler.set_stance("fail_on_recompile")`, which sets the process-global
`_stance`. While one thread was inside a served call, any other thread's
`torch.compile` read that stance and raised on its first compile, and two
overlapping calls could restore the stance out of order.
The stance is only consulted when a compiled callable is entered, to turn its
callback into fail_on_recompile's `fail_callback`; that callback then serves
cached entries for every frame of the call and raises instead of compiling.
This moves that callback out of `_callback_from_stance` into a small
`_FailOnRecompileCallback` class (a move plus closure-to-class, no behavior
change for the stance), and `_build_installed_forward` binds one into the
artifact's own `OptimizeContext` once at load, before `context(fn)` captures
the callback. No global state is touched per call.
A process-wide stance the caller sets still passes through
`_callback_from_stance` on every entry, and the compiling ones (force_backend,
"eager_then_compile", "aot_eager_then_compile") would replace or re-wrap the
bound callback and compile what the capture did not cover. So
`_callback_from_stance` now recognizes a `_RefuseRecompileCallback`, a
subclass only the driver builds (the per-call `_FailOnRecompileCallback`s the
stance builds, which `__exit__` can hand back as `self.prior`, keep the old
path), and,
reading the stance once, keeps it under every stance except "force_eager"
(every call runs eagerly, bypassing the installed entries) and
"eager_on_recompile" (captured variants serve, other calls run eagerly). The
decision lives where the stance is read, so a concurrent `set_stance` cannot
land between a check and the choice, and a new stance defaults to keeping the
refusal. The load() docstring and the artifact's serving notes say so.
Binding needs an `OptimizeContext`, which `optimize()` does not return when
Dynamo is disabled (TORCHDYNAMO_DISABLE=1 or the enable_dynamo killswitch);
load now refuses that case with its own PrecompileError instead of the
install-failure message, whose hint blames a source change. It also refuses
`torch._dynamo.config.compiled_autograd`: `OptimizeContext` would compile
backward graphs through a fresh `rebuild_ctx()` callback that does not refuse.
The subclass overrides the message prefix and the exception class: it raises
`_RecompileRefusedError` (a `RuntimeError`) with a message that does not claim a stance is set, and the driver's `forward` catches
it by type rather than matching the message text.
Setting the stance once at load and documenting it was rejected: it would put
every other torch.compile in the process under fail_on_recompile for as long as
the artifact is loaded.
Test Plan:
The new test serves an installed artifact whose entry calls a
`torch._dynamo.disable`d function mid-call that records the global stance; the
fresh process sees "default" (it saw "fail_on_recompile" before this change).
A second test loads an installed artifact under TORCHDYNAMO_DISABLE=1 and
checks the refusal names Dynamo being disabled, not the source; another
enables compiled_autograd before load and checks that refusal. A third
captures `tags_compiled` (which adds 1 only in a compiled frame) on one shape
and serves it in a fresh process under each process-wide stance:
force_backend, "eager_then_compile", "aot_eager_then_compile" and
"fail_on_recompile" serve the captured shape compiled and refuse the uncovered
one with PrecompileError whose text names no stance (twice, past the
delayed-compile warmup);
"force_eager" runs both eagerly; "eager_on_recompile" serves the captured one
compiled and the uncovered one eagerly. The stance recorder also runs a plain
`torch.compile` mid-call, which must still compile. Dropping the
`_FailOnRecompileCallback` branch in `_callback_from_stance` fails the third
test ("compiled under default"); raising a plain RuntimeError from the subclass
callback fails it too, and dropping the compiled_autograd check fails its test.
```
python test/test_precompile.py -k stance -k served_installed -k changed_source -k dynamo_disabled -k compiled_autograd -k own_errors
python test/test_precompile.py
cd test && python -m pytest -q dynamo/test_recompiles.py dynamo/test_decorators.py -k "stance or recompile"
```
This PR was authored with the help of an AI assistant (Claude).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 38e3f46
Pull-Request: #198557
…lobal stance
An installed artifact served every call under
`torch.compiler.set_stance("fail_on_recompile")`, which sets the process-global
`_stance`. While one thread was inside a served call, any other thread's
`torch.compile` read that stance and raised on its first compile, and two
overlapping calls could restore the stance out of order.
The stance is only consulted when a compiled callable is entered, to turn its
callback into fail_on_recompile's `fail_callback`; that callback then serves
cached entries for every frame of the call and raises instead of compiling.
This moves that callback out of `_callback_from_stance` into a small
`_FailOnRecompileCallback` class (a move plus closure-to-class, no behavior
change for the stance), and `_build_installed_forward` binds one into the
artifact's own `OptimizeContext` once at load, before `context(fn)` captures
the callback. No global state is touched per call.
A process-wide stance the caller sets still passes through
`_callback_from_stance` on every entry, and the compiling ones (force_backend,
"eager_then_compile", "aot_eager_then_compile") would replace or re-wrap the
bound callback and compile what the capture did not cover. So
`_callback_from_stance` now recognizes a `_RefuseRecompileCallback`, a
subclass only the driver builds (the per-call `_FailOnRecompileCallback`s the
stance builds, which `__exit__` can hand back as `self.prior`, keep the old
path), and,
reading the stance once, keeps it under every stance except "force_eager"
(every call runs eagerly, bypassing the installed entries) and
"eager_on_recompile" (captured variants serve, other calls run eagerly). The
decision lives where the stance is read, so a concurrent `set_stance` cannot
land between a check and the choice, and a new stance defaults to keeping the
refusal. The load() docstring and the artifact's serving notes say so.
Binding needs an `OptimizeContext`, which `optimize()` does not return when
Dynamo is disabled (TORCHDYNAMO_DISABLE=1 or the enable_dynamo killswitch);
load now refuses that case with its own PrecompileError instead of the
install-failure message, whose hint blames a source change. It also refuses
`torch._dynamo.config.compiled_autograd`: `OptimizeContext` would compile
backward graphs through a fresh `rebuild_ctx()` callback that does not refuse.
The subclass overrides the message prefix and the exception class: it raises
`_RecompileRefusedError` (a `RuntimeError`) with a message that does not claim a stance is set, and the driver's `forward` catches
it by type rather than matching the message text.
Setting the stance once at load and documenting it was rejected: it would put
every other torch.compile in the process under fail_on_recompile for as long as
the artifact is loaded.
Test Plan:
The new test serves an installed artifact whose entry calls a
`torch._dynamo.disable`d function mid-call that records the global stance; the
fresh process sees "default" (it saw "fail_on_recompile" before this change).
A second test loads an installed artifact under TORCHDYNAMO_DISABLE=1 and
checks the refusal names Dynamo being disabled, not the source; another
enables compiled_autograd before load and checks that refusal. A third
captures `tags_compiled` (which adds 1 only in a compiled frame) on one shape
and serves it in a fresh process under each process-wide stance:
force_backend, "eager_then_compile", "aot_eager_then_compile" and
"fail_on_recompile" serve the captured shape compiled and refuse the uncovered
one with PrecompileError whose text names no stance (twice, past the
delayed-compile warmup);
"force_eager" runs both eagerly; "eager_on_recompile" serves the captured one
compiled and the uncovered one eagerly. The stance recorder also runs a plain
`torch.compile` mid-call, which must still compile. Dropping the
`_FailOnRecompileCallback` branch in `_callback_from_stance` fails the third
test ("compiled under default"); raising a plain RuntimeError from the subclass
callback fails it too, and dropping the compiled_autograd check fails its test.
```
python test/test_precompile.py -k stance -k served_installed -k changed_source -k dynamo_disabled -k compiled_autograd -k own_errors
python test/test_precompile.py
cd test && python -m pytest -q dynamo/test_recompiles.py dynamo/test_decorators.py -k "stance or recompile"
```
This PR was authored with the help of an AI assistant (Claude).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 38e3f46
Pull-Request: #198557
|
@claude review |
|
Claude finished @bobrenjc93's task in 25s —— View job I found no blocking issues. The only change since my last review adds the Dynamo-disabled refusal to the PR Review: #198557SummaryWhen an installed artifact loads, the PR binds a At the new head ( I re-read the RecommendationApprove. Nothing blocks. |
…lobal stance
An installed artifact served every call under
`torch.compiler.set_stance("fail_on_recompile")`, which sets the process-global
`_stance`. While one thread was inside a served call, any other thread's
`torch.compile` read that stance and raised on its first compile, and two
overlapping calls could restore the stance out of order.
The stance is only consulted when a compiled callable is entered, to turn its
callback into fail_on_recompile's `fail_callback`; that callback then serves
cached entries for every frame of the call and raises instead of compiling.
This moves that callback out of `_callback_from_stance` into a small
`_FailOnRecompileCallback` class (a move plus closure-to-class, no behavior
change for the stance), and `_build_installed_forward` binds one into the
artifact's own `OptimizeContext` once at load, before `context(fn)` captures
the callback. No global state is touched per call.
A process-wide stance the caller sets still passes through
`_callback_from_stance` on every entry, and the compiling ones (force_backend,
"eager_then_compile", "aot_eager_then_compile") would replace or re-wrap the
bound callback and compile what the capture did not cover. So
`_callback_from_stance` now recognizes a `_RefuseRecompileCallback`, a
subclass only the driver builds (the per-call `_FailOnRecompileCallback`s the
stance builds, which `__exit__` can hand back as `self.prior`, keep the old
path), and,
reading the stance once, keeps it under every stance except "force_eager"
(every call runs eagerly, bypassing the installed entries) and
"eager_on_recompile" (captured variants serve, other calls run eagerly). The
decision lives where the stance is read, so a concurrent `set_stance` cannot
land between a check and the choice, and a new stance defaults to keeping the
refusal. The load() docstring and the artifact's serving notes say so.
Binding needs an `OptimizeContext`, which `optimize()` does not return when
Dynamo is disabled (TORCHDYNAMO_DISABLE=1 or the enable_dynamo killswitch);
load now refuses that case with its own PrecompileError instead of the
install-failure message, whose hint blames a source change. It also refuses
`torch._dynamo.config.compiled_autograd`: `OptimizeContext` would compile
backward graphs through a fresh `rebuild_ctx()` callback that does not refuse.
The subclass overrides the message prefix and the exception class: it raises
`_RecompileRefusedError` (a `RuntimeError`) with a message that does not claim a stance is set, and the driver's `forward` catches
it by type rather than matching the message text.
Setting the stance once at load and documenting it was rejected: it would put
every other torch.compile in the process under fail_on_recompile for as long as
the artifact is loaded.
Test Plan:
The new test serves an installed artifact whose entry calls a
`torch._dynamo.disable`d function mid-call that records the global stance; the
fresh process sees "default" (it saw "fail_on_recompile" before this change).
A second test loads an installed artifact under TORCHDYNAMO_DISABLE=1 and
checks the refusal names Dynamo being disabled, not the source; another
enables compiled_autograd before load and checks that refusal. A third
captures `tags_compiled` (which adds 1 only in a compiled frame) on one shape
and serves it in a fresh process under each process-wide stance:
force_backend, "eager_then_compile", "aot_eager_then_compile" and
"fail_on_recompile" serve the captured shape compiled and refuse the uncovered
one with PrecompileError whose text names no stance (twice, past the
delayed-compile warmup);
"force_eager" runs both eagerly; "eager_on_recompile" serves the captured one
compiled and the uncovered one eagerly. The stance recorder also runs a plain
`torch.compile` mid-call, which must still compile. Dropping the
`_FailOnRecompileCallback` branch in `_callback_from_stance` fails the third
test ("compiled under default"); raising a plain RuntimeError from the subclass
callback fails it too, and dropping the compiled_autograd check fails its test.
```
python test/test_precompile.py -k stance -k served_installed -k changed_source -k dynamo_disabled -k compiled_autograd -k own_errors
python test/test_precompile.py
cd test && python -m pytest -q dynamo/test_recompiles.py dynamo/test_decorators.py -k "stance or recompile"
```
This PR was authored with the help of an AI assistant (Claude).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 38e3f46
Pull-Request: #198557
…lobal stance
An installed artifact served every call under
`torch.compiler.set_stance("fail_on_recompile")`, which sets the process-global
`_stance`. While one thread was inside a served call, any other thread's
`torch.compile` read that stance and raised on its first compile, and two
overlapping calls could restore the stance out of order.
The stance is only consulted when a compiled callable is entered, to turn its
callback into fail_on_recompile's `fail_callback`; that callback then serves
cached entries for every frame of the call and raises instead of compiling.
This moves that callback out of `_callback_from_stance` into a small
`_FailOnRecompileCallback` class (a move plus closure-to-class, no behavior
change for the stance), and `_build_installed_forward` binds one into the
artifact's own `OptimizeContext` once at load, before `context(fn)` captures
the callback. No global state is touched per call.
A process-wide stance the caller sets still passes through
`_callback_from_stance` on every entry, and the compiling ones (force_backend,
"eager_then_compile", "aot_eager_then_compile") would replace or re-wrap the
bound callback and compile what the capture did not cover. So
`_callback_from_stance` now recognizes a `_RefuseRecompileCallback`, a
subclass only the driver builds (the per-call `_FailOnRecompileCallback`s the
stance builds, which `__exit__` can hand back as `self.prior`, keep the old
path), and,
reading the stance once, keeps it under every stance except "force_eager"
(every call runs eagerly, bypassing the installed entries) and
"eager_on_recompile" (captured variants serve, other calls run eagerly). The
decision lives where the stance is read, so a concurrent `set_stance` cannot
land between a check and the choice, and a new stance defaults to keeping the
refusal. The load() docstring and the artifact's serving notes say so.
Binding needs an `OptimizeContext`, which `optimize()` does not return when
Dynamo is disabled (TORCHDYNAMO_DISABLE=1 or the enable_dynamo killswitch);
load now refuses that case with its own PrecompileError instead of the
install-failure message, whose hint blames a source change. It also refuses
`torch._dynamo.config.compiled_autograd`: `OptimizeContext` would compile
backward graphs through a fresh `rebuild_ctx()` callback that does not refuse.
The subclass overrides the message prefix and the exception class: it raises
`_RecompileRefusedError` (a `RuntimeError`) with a message that does not claim a stance is set, and the driver's `forward` catches
it by type rather than matching the message text.
Setting the stance once at load and documenting it was rejected: it would put
every other torch.compile in the process under fail_on_recompile for as long as
the artifact is loaded.
Test Plan:
The new test serves an installed artifact whose entry calls a
`torch._dynamo.disable`d function mid-call that records the global stance; the
fresh process sees "default" (it saw "fail_on_recompile" before this change).
A second test loads an installed artifact under TORCHDYNAMO_DISABLE=1 and
checks the refusal names Dynamo being disabled, not the source; another
enables compiled_autograd before load and checks that refusal. A third
captures `tags_compiled` (which adds 1 only in a compiled frame) on one shape
and serves it in a fresh process under each process-wide stance:
force_backend, "eager_then_compile", "aot_eager_then_compile" and
"fail_on_recompile" serve the captured shape compiled and refuse the uncovered
one with PrecompileError whose text names no stance (twice, past the
delayed-compile warmup);
"force_eager" runs both eagerly; "eager_on_recompile" serves the captured one
compiled and the uncovered one eagerly. The stance recorder also runs a plain
`torch.compile` mid-call, which must still compile. Dropping the
`_FailOnRecompileCallback` branch in `_callback_from_stance` fails the third
test ("compiled under default"); raising a plain RuntimeError from the subclass
callback fails it too, and dropping the compiled_autograd check fails its test.
```
python test/test_precompile.py -k stance -k served_installed -k changed_source -k dynamo_disabled -k compiled_autograd -k own_errors
python test/test_precompile.py
cd test && python -m pytest -q dynamo/test_recompiles.py dynamo/test_decorators.py -k "stance or recompile"
```
This PR was authored with the help of an AI assistant (Claude).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 38e3f46
Pull-Request: #198557
[ghstack-poisoned]
|
@claude review |
|
Claude finished @bobrenjc93's task in 1m 6s —— View job No blocking issues: since my last approval at PR Review: #198557SummaryWhen an installed artifact loads, the PR binds a The new head
The base update doesn't touch
None of these touch the stance, RecommendationApprove. Nothing blocks. |
|
/easycla |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…ing it (#198523) **Problem.** A capture whose graph break sits inside a function the entry calls (a child module's `forward`, say) was refused at write time, which rules out most real models with a graph break below the top-level function. **Fix.** Such a capture is now written as an installed-mode artifact: loading it installs the captured Dynamo entries onto the live code objects and serves every call under `fail_on_recompile`. **Summary.** A standalone artifact can serve only frames the entry reaches through a graph-break continuation, because its driver dispatches by binding the resume names the entry's bytecode loads. A graph break inside a called function compiles that function as its own frame, which the entry reaches by an ordinary call the dispatcher never sees, so `_build_multigraph_artifact` refused the capture ("cannot reach from the entry"). This PR renders those captures as `SERVING_MODE = "installed"`, carrying the whole Dynamo package in one `_PACKAGE` blob, and their driver `_build_installed_forward` installs it with `CompilePackage.install`, exactly as a warm torch.compile package load does. Every call is served under `fail_on_recompile`, so the standalone contract holds: the artifact serves only what capture exercised, and an uncovered call raises instead of compiling. Everything used is already on main, so the change is Python-only, and unload, error wrapping, the process-global stance and same-process loading are left to the PRs above. ## Background: how a standalone artifact serves When a captured function graph-breaks, Dynamo splits it into frames: the entry compiled up to the break, and a resume function the entry's rewritten bytecode reaches by `LOAD_GLOBAL`. The standalone driver, `_build_multigraph_forward`, rebuilds each frame from its code object and guard trees and binds a dispatcher under each resume name, so the entry's bytecode loads the dispatcher, which picks the captured variant whose guards match. Nothing is installed into Dynamo, and no compiler runs at serve time. That reaches only frames named by a resume name. The test fixture: ```python def breaking_helper(y): y = y.sin() torch._dynamo.graph_break() return y.cos() def calls_breaking_helper(model, x): return breaking_helper(model(x)) + 1 ``` The break is inside `breaking_helper`, not in the entry. Dynamo compiles `breaking_helper` (and its own continuation) as separate frames, and the entry reaches it with an ordinary Python call. The standalone dispatcher never sees that call, so served standalone, `breaking_helper` would silently run eager. ```python # parent: capture(calls_breaking_helper) -> PrecompileError "... cannot reach from the entry ..." # now: the artifact is written with SERVING_MODE = "installed" and serves compiled ``` ## Render side (`torch/_precompile.py`): a mode choice instead of a refusal ```python unreachable = _unreachable_frames(frames) # compiled frames no continuation reaches, by fqn mode = "installed" if unreachable else "standalone" binding = _entry_binding(entry_fn) if mode == "standalone" else None ``` An installed artifact writes the following instead of `_FRAMES`, `_BACKENDS` and `_ENTRY_BINDING`: ```python SERVING_MODE = "installed" # Frames entered by an ordinary call; served by installing. UNREACHABLE_WITHOUT_INSTALL = ['mod.breaking_helper', 'mod.torch_dynamo_resume_in_breaking_helper_at_N'] _PACKAGE = '...' # base64(pickle) of the Dynamo cache entry (guards, transformed # bytecode) and the backend artifacts it calls by id -- OPAQUE forward = _build_installed_forward() ``` `_parse_artifact_metadata` already reads `SERVING_MODE`, so the format needs no new concept, and `load()` now sets the handle's `installed` flag from it. A reachable frame with no variant still keeps the capture standalone: the driver runs a trivial one eager, as capture did, and refuses a bypassed one when a call reaches it. ## Serve side (`torch/_precompile_driver.py`): hand serving back to Dynamo ```python package = CompilePackage(fn, dynamo) # fn imported by module name + FN_NAME compiled = torch._dynamo.optimize(BACKEND, package=package)(fn) package.install(package_state["backends"]) # guarded entries onto the live code objects def forward(*args, **kwargs): with torch.compiler.set_stance("fail_on_recompile"): return compiled(*args, **kwargs) ``` `CompilePackage.install` puts the captured entries onto the real code objects of `calls_breaking_helper` and `breaking_helper`, so the ordinary call into the helper hits an installed cache entry through Dynamo's own frame evaluator. Before installing, the driver refuses with a `PrecompileError` when the Python version differs from capture (the bytecode is marshalled), when the torch version differs (the guard state is pickled), when the function was defined in `__main__`, or when it is no longer importable. ## Why `fail_on_recompile`, and the rejected alternative Serving under `fail_on_recompile` keeps the standalone contract: the artifact serves only what capture exercised, and an uncovered call raises rather than compiling. The alternative, letting the installed artifact recompile at serve time (the old #195921 design), needs serve-time-compile accounting and isolation machinery this stack does not have. ## Tests | Test | Pins | |---|---| | `test_multigraph_artifact_installs_a_frame_the_entry_cannot_reach` | the render: `SERVING_MODE = "installed"`, an `UNREACHABLE_WITHOUT_INSTALL` list, `forward = _build_installed_forward()`, and no `_FRAMES` blob | | `test_a_frame_the_entry_cannot_reach_is_served_installed` (both backends) | captures `calls_breaking_helper` on two shapes, checks the unreachable list names the helper and its continuation, and serves it in a fresh process; since that process serves under `fail_on_recompile`, a correct result proves every frame, the helper's included, hit an installed entry instead of compiling, and an uncovered dtype must raise | ## Left to the PRs above This PR is kept to the minimum that serves; each gap is its own draft on top. | Gap in this PR | Addressed in | |---|---| | An uncovered call surfaces as fail_on_recompile's `RuntimeError`, not a `PrecompileError`, and a failed install is not wrapped | #198556 | | `set_stance(...)` per call flips the process-global stance, so another thread's `torch.compile` raises while a call is being served | #198557 (the refusal is bound into the artifact's own callback; compiled_autograd is refused at load) | | Loading in a process that already compiled those frames leaves live entries that serve misses | #198558 (refused at load) | | `unload()`/`__exit__` do not take the install back out (`CompilePackage.uninstall` resets only the entry's code object) | not yet; the docs say to serve from a process you can throw away | Test Plan: The new end-to-end test captures `calls_breaking_helper` (whose helper graph breaks) on two shapes, checks the artifact renders as installed, and serves it in a fresh process for both backends. Because the fresh process serves under fail_on_recompile, a correct result also proves every frame, the helper's included, hit an installed entry instead of compiling. The loader then checks that an uncovered dtype raises. ``` python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves python test/test_precompile.py ``` This PR was authored with the help of an AI assistant (Claude). Co-Authored-By: Claude Opus 5.5 <[email protected]> Pull Request resolved: #198523 Approved by: https://github.com/Mingming-Ding
Stack from ghstack (oldest at bottom):
Problem. An installed artifact served every call by setting the process-global
fail_on_recompilestance, so while one thread was inside a served call, every other thread'storch.compileraised on its first compile, and two overlapping calls could restore the stance out of order.Fix. The artifact now carries its own refusing callback, a
_RefuseRecompileCallbackbound into itsOptimizeContextonce at load, and touches no global state per call;_callback_from_stancekeeps that callback under every process-wide stance that would compile.Summary. The installed driver wrapped each call in
torch.compiler.set_stance("fail_on_recompile"), which mutates the process-global_stancethat everytorch.compilein the process reads on entry. That broke concurrent compiles in other threads and could restore the stance out of order when calls overlapped. The stance's per-callfail_callbackclosure is now a small_FailOnRecompileCallbackclass, and_build_installed_forwardbinds a_RefuseRecompileCallbacksubclass of it into the artifact's ownOptimizeContextbeforecontext(fn)captures the callback. Because a stance the caller sets still passes through_callback_from_stanceon every entry, that function recognizes the subclass and, reading the stance once, keeps it under every stance except"force_eager"and"eager_on_recompile". Load also refuses two configurations the binding cannot cover, Dynamo disabled andcompiled_autograd, and the refusal raises its own_RecompileRefusedError, which the driver catches by type instead of matching message text.Before and after, per call
The stance is only consulted when a compiled callable is entered, to turn its callback into fail_on_recompile's callback; that callback then serves cached entries for every frame of the call and raises instead of compiling. Moving it out of
_callback_from_stanceinto_FailOnRecompileCallbackis a move plus closure-to-class, with no behavior change for the stance. The binding has to happen beforecontext(fn), which captures the callback when it wraps.What another thread sees
Under a process-wide stance
A stance the caller sets still passes through
_callback_from_stanceon every entry, and the compiling ones (force_backend,"eager_then_compile","aot_eager_then_compile") would replace or re-wrap the bound callback and compile what the capture did not cover. So the function checks for the subclass first:force_backend,"eager_then_compile","aot_eager_then_compile","fail_on_recompile"PrecompileError"force_eager""eager_on_recompile"Only the driver builds a
_RefuseRecompileCallback. The per-call_FailOnRecompileCallbacks that the stance builds, which__exit__can hand back asself.prior, keep the old path. The decision lives where the stance is read, so a concurrentset_stancecannot land between a check and the choice, and a new stance defaults to keeping the refusal. Theload()docstring and the artifact's serving notes say so.Errors the refusal raises
The subclass overrides the message prefix and the exception class. It raises
_RecompileRefusedError(aRuntimeError) with a message that does not claim a stance is set, and the driver'sforwardcatches it by type rather than matching the message text.Two configurations load now refuses
Binding needs an
OptimizeContext, whichoptimize()does not return when Dynamo is disabled. Withcompiled_autograd,OptimizeContextwould compile backward graphs through a freshrebuild_ctx()callback that does not refuse.Rejected: set the stance once at load
Setting the stance once at load and documenting it would put every other
torch.compilein the process under fail_on_recompile for as long as the artifact is loaded.Tests
test_an_installed_artifact_leaves_the_global_stance_alonetorch._dynamo.disabled function mid-call that records the global stance; a fresh process sees"default"(it saw"fail_on_recompile"before this change); the recorder also runs a plaintorch.compile, which must still compiletest_an_installed_artifact_under_a_process_wide_stancetags_compiled(adds 1 only in a compiled frame) captured on one shape, served in a fresh process under each stance in the table above; the refusal's text names no stance, checked twice to get past the delayed-compile warmuptest_an_installed_artifact_refuses_to_load_with_dynamo_disabledTORCHDYNAMO_DISABLE=1the refusal names Dynamo being disabled, not the sourcetest_an_installed_artifact_refuses_to_load_with_compiled_autogradcompiled_autogradenabled before load, load refusesTest Plan:
The new test serves an installed artifact whose entry calls a
torch._dynamo.disabled function mid-call that records the global stance; thefresh process sees "default" (it saw "fail_on_recompile" before this change).
A second test loads an installed artifact under TORCHDYNAMO_DISABLE=1 and
checks the refusal names Dynamo being disabled, not the source; another
enables compiled_autograd before load and checks that refusal. A third
captures
tags_compiled(which adds 1 only in a compiled frame) on one shapeand serves it in a fresh process under each process-wide stance:
force_backend, "eager_then_compile", "aot_eager_then_compile" and
"fail_on_recompile" serve the captured shape compiled and refuse the uncovered
one with PrecompileError whose text names no stance (twice, past the
delayed-compile warmup);
"force_eager" runs both eagerly; "eager_on_recompile" serves the captured one
compiled and the uncovered one eagerly. The stance recorder also runs a plain
torch.compilemid-call, which must still compile. Dropping the_FailOnRecompileCallbackbranch in_callback_from_stancefails the thirdtest ("compiled under default"); raising a plain RuntimeError from the subclass
callback fails it too, and dropping the compiled_autograd check fails its test.
This PR was authored with the help of an AI assistant (Claude).
Co-Authored-By: Claude Opus 5.5 [email protected]
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98