Skip to content

BLD: require peft>=0.18.0 for transformers 5.x compatibility - #5260

Merged
qinxuye merged 1 commit into
xorbitsai:mainfrom
OliverBryant:fix/peft-transformers5-hybridcache
Jul 30, 2026
Merged

BLD: require peft>=0.18.0 for transformers 5.x compatibility#5260
qinxuye merged 1 commit into
xorbitsai:mainfrom
OliverBryant:fix/peft-transformers5-hybridcache

Conversation

@OliverBryant

Copy link
Copy Markdown
Collaborator

Problem

Launching any rerank model on the sentence_transformers engine fails at load time with:

ImportError: Failed to import module 'sentence-transformers': cannot import name
'HybridCache' from 'transformers' (.../transformers/__init__.py)

Please make sure 'sentence-transformers' is installed. You can install it by
`pip install sentence-transformers`

The suggested remedy is misleading — sentence-transformers is installed and is not the problem.

Root cause

peft<=0.17.1 imports HybridCache at module level in peft/peft_model.py:

from transformers import Cache, DynamicCache, EncoderDecoderCache, HybridCache, PreTrainedModel

HybridCache was removed in transformers 5.0.0 (present in 4.57.1, gone from 5.0.0 onward). Because peft declares an unbounded transformers requirement, pip resolves the incompatible pair without any warning, so images build fine and the failure only appears at runtime.

The failing import chain:

xinference/model/rerank/sentence_transformers/core.py  ->  import sentence_transformers
  -> sentence_transformers/base/model_card.py          ->  from transformers import TrainerCallback
  -> transformers/trainer_utils.py                     ->  from peft import PeftMixedModel, PeftModel
  -> peft/peft_model.py                                ->  ImportError: cannot import name 'HybridCache'

core.py catches the ImportError and re-raises it as "Failed to import module 'sentence-transformers'", which hides the real cause. GGUF LLMs never touch this chain, so on an affected image LLMs work while rerank/embedding models appear broken — which makes this easy to misdiagnose as a rerank bug.

The <=0.17.1 upper bound was introduced in #4249 while transformers was still 4.x. It became self-inconsistent once xinference/deploy/docker/requirements-runtime.txt moved to transformers==5.13.1: the GPU image installs xinference under that constraint, and the CPU image pins both in requirements_cpu-ml.txt (transformers>=4.53.3 + peft<=0.17.1).

peft 0.18.0 moved the HybridCache import inside the function that needs it, so it no longer breaks at import time.

Fix

Require peft>=0.18.0 in the three places the pin appears:

  • pyproject.toml — also covers the GPU image, which installs xinference with --constraint requirements-runtime.txt
  • xinference/deploy/docker/requirements_cpu/requirements_cpu-ml.txt
  • .github/workflows/python.yaml

Comments were added at the first two sites recording the transformers-5.x constraint, so the bound is not lowered again without checking the shipped transformers major version.

Verification

In a clean venv:

  • transformers==5.13.1 + peft==0.17.1 installs with no pip warning and reproduces the exact ImportError from the report.
  • With peft>=0.18.0 (resolved 0.20.0) against the same transformers==5.13.1: import sentence_transformers, from sentence_transformers.cross_encoder import CrossEncoder, a full CrossEncoder.predict rerank call, and from peft import PeftModel, PeftMixedModel (used by llm/transformers/core.py) all succeed.
  • pre-commit run --files <changed files> passes; pyproject.toml parses and resolves to peft>=0.18.0.

Not run locally: rerank tests that require model downloads or GPU, and a full image rebuild — both too costly in this environment. test_auto_detect_type fails on my machine both with and without this change, due to a pre-existing local numpy/scipy ABI mismatch unrelated to the patch.

peft 0.17.1 and below import HybridCache at module level in
peft/peft_model.py:

    from transformers import Cache, DynamicCache, EncoderDecoderCache, \
        HybridCache, PreTrainedModel

HybridCache was removed in transformers 5.0.0, and peft declares an
unbounded transformers requirement, so pip happily resolves the pair and
the failure only surfaces at runtime.

This broke launching any rerank model on the sentence_transformers
engine, via the import chain:

    rerank/sentence_transformers/core.py -> import sentence_transformers
      -> base/model_card.py -> from transformers import TrainerCallback
      -> trainer_utils.py -> from peft import PeftMixedModel, PeftModel
      -> ImportError: cannot import name 'HybridCache'

The reported error was misleading: core.py wraps the ImportError as
"Failed to import module 'sentence-transformers'" and suggests installing
sentence-transformers, which is unrelated and already present. GGUF LLMs
were unaffected because they never touch this chain, so only rerank and
embedding models appeared broken.

The <=0.17.1 upper bound was added in xorbitsai#4249 when transformers was still
4.x; it became inconsistent once requirements-runtime.txt moved to
transformers==5.13.1. peft 0.18.0 made the HybridCache import lazy, so
requiring >=0.18.0 fixes the conflict.

Verified in a clean venv that transformers 5.13.1 + peft 0.17.1
reproduces the ImportError, and that peft>=0.18.0 restores both
`import sentence_transformers` and a full CrossEncoder rerank predict.
@XprobeBot XprobeBot added this to the v3.x milestone Jul 29, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the dependency constraint for peft from <=0.17.1 to >=0.18.0 in both pyproject.toml and requirements_cpu-ml.txt to avoid compatibility issues with transformers 5.0.0. Feedback points out that the corresponding update to .github/workflows/python.yaml is missing and should be added to maintain consistency across all configuration files.

Comment thread pyproject.toml
@OliverBryant
OliverBryant requested a review from qinxuye July 30, 2026 03:58

@qinxuye qinxuye left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@qinxuye
qinxuye merged commit 48c71ee into xorbitsai:main Jul 30, 2026
13 of 14 checks passed
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.

3 participants