Installing transformers and torch
Why this matters
You can't run any model without the right dependencies installed. A broken installation will fail at import time, wasting debugging time. Getting this right first prevents cascading errors later.
Explanation
What it is: Installing transformers (the Hugging Face library for working with models) and torch (the deep learning framework transformers depends on). In transformers 5.5.x, the library has major breaking changes from 4.x that affect how models are loaded and quantized.
How it works mechanically: pip install downloads packages from PyPI and installs them into your Python environment. The transformers library depends on torch, but you install torch separately to control which version and which compute backend (CPU, CUDA, Metal) you get. Without explicitly specifying versions, you risk incompatible combinations.
When to use it: Always do this first when setting up a new machine or virtual environment for transformer work. Use a virtual environment (venv or conda) to isolate dependencies and avoid conflicts with other projects.
Analogy
It's like installing a kitchen: torch is the electrical wiring and appliances, transformers is the cookware and recipes. You need to install the wiring before the cookware will work properly.
Code
import subprocess
import sys
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'pip'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch', 'torchvision', 'torchaudio'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'transformers>=5.5.0'])
import torch
import transformers
print(f'PyTorch version: {torch.__version__}')
print(f'Transformers version: {transformers.__version__}')
print(f'CUDA available: {torch.cuda.is_available()}')
print(f'Device: {torch.device("cuda" if torch.cuda.is_available() else "cpu")}') PyTorch version: 2.4.0 Transformers version: 5.5.0 CUDA available: False Device: cpu
What just happened?
The code ran pip install three times: first upgrading pip itself, then installing torch and related packages, then installing transformers at version 5.5.0 or higher. Then it imported both libraries and printed their versions along with a check of whether CUDA (GPU support) is available on this machine. The output shows both libraries imported successfully and CUDA is not available on this system (so it defaults to CPU).
Common gotcha
Installing transformers without explicitly pinning the version (e.g., pip install transformers with no version spec) can give you a very old 4.x version or a brand-new 5.x version with breaking changes. Always pin to transformers>=5.5.0 for this course. Also, torch installation is OS and hardware-specific: the command above works for CPU. For CUDA 12.1, you need: pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
Error recovery
ModuleNotFoundError: No module named 'torch'RuntimeError: CUDA out of memoryImportError: cannot import name 'AutoTokenizer' from transformersNo module named 'pip'Experienced dev note
In transformers 4.x, you could load a model with AutoModel.from_pretrained(name) and it would work on CPU. In 5.5.x, that same code will often cause OOM errors or slower inference. Always add device_map='auto' and torch_dtype=torch.bfloat16 from the start: it's not optional for production. Also: use virtual environments religiously. Installing torch globally has burned every developer once.
Check your understanding
You have transformers 4.30 installed, but this course requires 5.5+. What single pip command will upgrade just transformers without touching torch?
Show answer hint
A correct answer uses pip install with a version specifier (>=, ==, or similar) and targets only the transformers package. The answer should explain why you don't reinstall torch: because pip is smart enough to skip already-installed dependencies unless forced.