Skip to content

Fix _patch_transformers_hybrid_cache for peft#4844

Merged
albertvillanova merged 1 commit intohuggingface:mainfrom
albertvillanova:fix-4820
Jan 16, 2026
Merged

Fix _patch_transformers_hybrid_cache for peft#4844
albertvillanova merged 1 commit intohuggingface:mainfrom
albertvillanova:fix-4820

Conversation

@albertvillanova
Copy link
Copy Markdown
Member

@albertvillanova albertvillanova commented Jan 16, 2026

Fix _patch_transformers_hybrid_cache for peft.

This PR fixes the transformers patch required by peft. In particular, peft import HybridCache via:

from transformers import HybridCache

whereas liger-kernel imports it via:

from transformers.cache_utils import HybridCache

As a result, patching _LazyModule is also required to ensure the fix is applied correctly in both cases.

Follow-up to:

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

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.

Comment thread trl/_compat.py
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
Copy link
Copy Markdown
Member

@qgallouedec qgallouedec Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this solution, but any reason not to simply use?

transformers.HybridCache = Cache

is it to leverage lazy import?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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:

  1. Transformers' __init__.py replaces itself with a _LazyModule at initialization
  2. When peft imported transformers, this caused the module to be re-initialized
  3. The new _LazyModule instance didn't have our HybridCache patch

@albertvillanova albertvillanova merged commit c3108ff into huggingface:main Jan 16, 2026
9 of 10 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants