Skip to content
Latchkey

Hugging Face "ImportError: transformers requires ... torch/accelerate" in CI

transformers can build a model only if the backend it targets (PyTorch, TensorFlow, or Flax) and any required extra (accelerate for device_map) are installed. A slim CI environment that installed only transformers hits an ImportError at load time.

What this error means

Loading a model raises "ImportError: <model> requires the PyTorch library but it was not found in your environment" or "Using low_cpu_mem_usage=True or device_map requires Accelerate: pip install accelerate".

transformers
ImportError: AutoModelForCausalLM requires the PyTorch library but it was not found in your
environment. Checkout the instructions on the installation page: https://pytorch.org/get-started/locally/

Common causes

The deep-learning backend is not installed

Installing only transformers does not pull torch or tensorflow; the model class needs one of them.

A feature needs an extra like accelerate

device_map, low_cpu_mem_usage, and sharded loading require the accelerate package, which is not a base dependency.

How to fix it

Install the backend and extras the model needs

Install torch (or tensorflow) and accelerate so the loader has its dependencies.

Terminal
pip install "transformers[torch]" accelerate

Pin CPU wheels for CI where no GPU exists

On CPU runners, install the CPU torch build so the import succeeds without CUDA.

Terminal
pip install torch --index-url https://download.pytorch.org/whl/cpu

How to prevent it

  • Install the framework extra (transformers[torch]) explicitly.
  • Add accelerate when using device_map or sharded loading.
  • Use CPU wheels on CPU-only runners.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →