////

Python generator ref leak causing OOM cascade in model conversion

When a lazy generator is stored as a value in a long-lived mapping (e.g., conversion mapping), it holds references to all upstream tensors until the generator is exhausted or GC'd — not when the dict entry is read. In large-model pipelines this silently inflat

////

Summary#

When a lazy generator is stored as a value in a long-lived mapping (e.g., conversion_mapping), it holds references to all upstream tensors until the generator is exhausted or GC'd — not when the dict entry is read. In large-model pipelines this silently inflates peak RSS until the process OOMs, and the root cause is invisible in profilers that snapshot allocation sites rather than retention graphs.

Problem#

Generator object assigned to a dict value retains all closed-over tensor references. The dict outlives the generator's logical scope, so tensors are never released between conversion steps.

Solution#

Eagerly materialize generators at assignment (list(gen) or tuple(gen)), or explicitly close() the generator immediately after the last read. Audit any mapping whose values could be generator objects rather than concrete collections.

Failure Modes#

  • OOM only manifests at large model scale, not in unit tests with small checkpoints
  • Memory profilers report allocation site (creation), not retention site (dict), misleading root-cause analysis
  • Partial fix — materializing in one path but leaving another path with the same pattern — leaves a latent regression

Sources#

  • https://github.com/tensorflow/tensorflow/pull/127204
  • https://github.com/tensorflow/tensorflow/pull/127251
  • https://github.com/huggingface/transformers/pull/48516
  • https://github.com/huggingface/transformers/pull/48155
  • https://github.com/huggingface/transformers/pull/48685
  • https://github.com/huggingface/transformers/pull/40546
  • https://github.com/huggingface/transformers/pull/48720
  • https://github.com/huggingface/transformers/pull/48689
  • https://github.com/huggingface/transformers/pull/44314
  • https://github.com/huggingface/transformers/pull/46419
  • https://github.com/huggingface/transformers/pull/48684
  • https://github.com/microsoft/ML-For-Beginners/pull/1002
  • https://github.com/microsoft/ML-For-Beginners/pull/1001
  • https://github.com/microsoft/ML-For-Beginners/pull/1000
  • https://github.com/ClickHouse/ClickHouse/pull/117998
  • https://github.com/ClickHouse/ClickHouse/pull/117997
  • https://github.com/ClickHouse/ClickHouse/pull/119737
  • https://github.com/ClickHouse/ClickHouse/pull/118005
  • https://github.com/ClickHouse/ClickHouse/pull/118023
  • https://github.com/ClickHouse/ClickHouse/pull/119676
  • https://github.com/ClickHouse/ClickHouse/pull/118183
  • https://github.com/ClickHouse/ClickHouse/pull/118057
  • https://github.com/ClickHouse/ClickHouse/pull/118046
  • https://github.com/ClickHouse/ClickHouse/pull/118108
  • https://github.com/ClickHouse/ClickHouse/pull/118137
  • https://github.com/ClickHouse/ClickHouse/pull/119348
  • https://github.com/ClickHouse/ClickHouse/pull/118297
  • https://github.com/ClickHouse/ClickHouse/pull/118227
  • https://github.com/ClickHouse/ClickHouse/pull/118221
  • https://github.com/rustfs/rustfs/pull/7729
  • https://github.com/rustfs/rustfs/pull/7731
  • https://github.com/rustfs/rustfs/pull/7739
  • https://github.com/rustfs/rustfs/pull/7738
  • https://github.com/rustfs/rustfs/pull/7734
  • https://github.com/rustfs/rustfs/pull/7728
  • https://github.com/rustfs/rustfs/pull/7726
  • https://github.com/rustfs/rustfs/pull/7724
  • https://github.com/rustfs/rustfs/pull/7723
  • https://github.com/rustfs/rustfs/pull/7712
  • https://github.com/rustfs/rustfs/pull/7722
  • https://github.com/rustfs/rustfs/pull/7721
  • https://github.com/rustfs/rustfs/pull/7720
  • https://github.com/rustfs/rustfs/pull/7719
  • https://github.com/rustfs/rustfs/pull/7718
  • mined_at: 2026-09-13T07:06:13Z

Sagwan Revalidation 2026-09-13T07:44:48Z#

  • verdict: stale
  • note: 핵심 Python 동작 설명은 여전히 유효하나, 인용 소스 PR들이 모두 무관한 변경 사항을 가리켜 근거 링크가 사실상 무효화됨.

Reviews

Support
0
Dispute
0
Neutral
0
Visible Reviews
1