Fix _patch_transformers_hybrid_cache for peft#4844
Merged
albertvillanova merged 1 commit intohuggingface:mainfrom Jan 16, 2026
Merged
Fix _patch_transformers_hybrid_cache for peft#4844albertvillanova merged 1 commit intohuggingface:mainfrom
albertvillanova merged 1 commit intohuggingface:mainfrom
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
qgallouedec
reviewed
Jan 16, 2026
Comment on lines
+192
to
+210
| _original_lazy_module_init = _LazyModule.__init__ | ||
|
|
||
| def _patched_lazy_module_init(self, name, *args, **kwargs): | ||
| _original_lazy_module_init(self, name, *args, **kwargs) | ||
| if name == "transformers": | ||
| # Update _LazyModule's internal structures | ||
| if hasattr(self, "_import_structure") and "cache_utils" in self._import_structure: | ||
| if "HybridCache" not in self._import_structure["cache_utils"]: | ||
| self._import_structure["cache_utils"].append("HybridCache") | ||
|
|
||
| if hasattr(self, "_class_to_module"): | ||
| self._class_to_module["HybridCache"] = "cache_utils" | ||
|
|
||
| if hasattr(self, "__all__") and "HybridCache" not in self.__all__: | ||
| self.__all__.append("HybridCache") | ||
|
|
||
| self.HybridCache = Cache | ||
|
|
||
| _LazyModule.__init__ = _patched_lazy_module_init |
Member
There was a problem hiding this comment.
I'm fine with this solution, but any reason not to simply use?
transformers.HybridCache = Cacheis it to leverage lazy import?
Member
Author
There was a problem hiding this comment.
@qgallouedec, that was my first approach, but it did not work: after the patch, when peft imports transformers, the transformers module was being replaced with a fresh _LazyModule instance that didn't have the HybridCache patch applied. This happened because:
- Transformers'
__init__.pyreplaces itself with a_LazyModuleat initialization - When peft imported transformers, this caused the module to be re-initialized
- The new
_LazyModuleinstance didn't have ourHybridCachepatch
qgallouedec
approved these changes
Jan 16, 2026
marcandrelarochelle
pushed a commit
to marcandrelarochelle/trl
that referenced
this pull request
Mar 25, 2026
songhappy
pushed a commit
to songhappy/trl
that referenced
this pull request
Apr 20, 2026
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.
Fix
_patch_transformers_hybrid_cachefor peft.This PR fixes the transformers patch required by peft. In particular, peft import
HybridCachevia:whereas liger-kernel imports it via:
As a result, patching
_LazyModuleis also required to ensure the fix is applied correctly in both cases.Follow-up to: