Skip to content

[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

Open
wants to merge 12 commits into
base: gh/guilhermeleobas/193/base
Choose a base branch
from

Conversation

guilhermeleobas
Copy link
Collaborator

@guilhermeleobas guilhermeleobas commented Jun 27, 2025

Stack from ghstack (oldest at bottom):

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

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

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang @amjames

[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Jun 27, 2025
**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
Copy link

pytorch-bot bot commented Jun 27, 2025

🔗 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 Failures

As of commit 7100f6f with merge base f33ce40 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Jun 27, 2025
**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
guilhermeleobas added a commit that referenced this pull request Jun 27, 2025
**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
guilhermeleobas added a commit that referenced this pull request Jun 27, 2025
**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
guilhermeleobas added a commit that referenced this pull request Jun 27, 2025
**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
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@@ -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()
Copy link
Contributor

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?

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@pytorchmergebot
Copy link
Collaborator

Rebased gh/guilhermeleobas/194/orig onto refs/remotes/origin/viable/strict because #157152 was rebased, please pull locally before adding more changes (for example, via ghstack checkout https://github.com/pytorch/pytorch/pull/157149)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants