-
Notifications
You must be signed in to change notification settings - Fork 24.9k
[generator] Close all open generators in compile_subgraph #157149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh/guilhermeleobas/193/base
Are you sure you want to change the base?
[generator] Close all open generators in compile_subgraph #157149
Conversation
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: e1abab6 Pull-Request: #156880 ghstack-source-id: 45df03f Pull Request resolved: #157149
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/157149
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 7100f6f with merge base f33ce40 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: c4e4678 Pull-Request: #156880 ghstack-source-id: c4e4678 Pull Request resolved: #157149
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: c4e4678 Pull-Request: #156880 ghstack-source-id: c4e4678 Pull Request resolved: #157149
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: c4e4678 Pull-Request: #156880 ghstack-source-id: c4e4678 Pull Request resolved: #157149
**Motivation and Example** Explictly close all open generators in compile_subgraph to ensure that all remaining finally blocks are executed. In CPython this is done by invoking the `tp_finalize` function of the generator object, which triggers `genclose`: https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134 ```python import gc def whoo(t): nonlocal z z = 0 try: z += 1 yield t.sin() except ValueError: z += 10 yield t.cos() except RuntimeError: z += 100 yield t.tan() finally: z += 1000 z += 10_000 gen = whoo(t) a = next(gen) b = gen.throw(RuntimeError) gc.collect() print(z) # 1101 ``` ghstack-source-id: c4e4678 Pull-Request: #156880 ghstack-source-id: c4e4678 Pull Request resolved: #157149
@@ -1397,6 +1397,7 @@ def compile_subgraph( | |||
overridden_sources=overridden_sources, | |||
) | |||
self.codegen_suffix(tx, stack_values_flat, pass1) | |||
self.side_effects.close_local_generators() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comment about why this is here.
also TODO(rzou): is this actually the right place?
Rebased |
Stack from ghstack (oldest at bottom):
StopIteration(value)
with value from the return stmt #157152contextlib.suppress
#147990Motivation and Example
Explictly close all open generators in compile_subgraph to ensure that
all remaining finally blocks are executed. In CPython this is done by
invoking the
tp_finalize
function of the generator object, which triggersgenclose
:https://github.com/python/cpython/blob/58a42dea97f4fa0df38ef4a95a2ede65e0549f71/Objects/genobject.c#L128-L134
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang @amjames