Repository navigation
Conversation
…compile_compatible_method_lru_cache Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
|
@yonigozlan could you take a look when you get a chance? This is waiting on a maintainer: yonigozlan said he'd fix it himself after the ViT refactor (#41693, merged 2026-05-08). |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace class-level
lru_cachewith per-instance cache incompile_compatible_method_lru_cacheto prevent memory leaks when model instances are deleted.Fixes #45412
Changes
src/transformers/pytorch_utils.py: For methods (detected via first parameter namedself), the decorator now stores a per-instancelru_cachein the instance's__dict__using a weakref destructor for cleanup. For standalone functions, the original shared cache behavior is preserved.Root Cause
compile_compatible_method_lru_cachewraps methods with a class-levelfunctools.lru_cache. Sinceselfis the first positional argument,lru_cacheholds a strong reference to the model instance as a cache key, preventing garbage collection even afterdel model+gc.collect(). This affects RT-DETR, RT-DETR-v2, MaskFormer, Mask2Former, OneFormer, SAM2, and 20+ other models that use this decorator.Duplicate Check
gh pr list --state open --search "45412"→ no duplicate PRsgh pr list --state open --search "memory leak lru_cache"→ no duplicate PRsTesting
AI Disclosure
This PR was created with AI assistance (Claude). The human maintainer has reviewed and approved the changes before submission.