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".
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.
pip install "transformers[torch]" acceleratePin CPU wheels for CI where no GPU exists
On CPU runners, install the CPU torch build so the import succeeds without CUDA.
pip install torch --index-url https://download.pytorch.org/whl/cpuHow to prevent it
- Install the framework extra (
transformers[torch]) explicitly. - Add
acceleratewhen using device_map or sharded loading. - Use CPU wheels on CPU-only runners.