Step 2: Install the tools
Now that your project folder is set up, let's install the libraries you'll need. We'll use a virtual environment to keep everything isolated and reproducible. This way, the dependencies for this project won't interfere with other Python projects on your machine.
First, create and activate a virtual environment:
python -m venv venvsource venv/bin/activate # On Windows: venv\Scripts\activateOnce your virtual environment is active, install the core libraries:
pip install -U transformers datasets accelerate trl peft torchHere's what each library does:
transformersprovides pre-trained models and tokenizers from Hugging Face. This is the backbone of nearly all modern NLP work.datasetsmakes it easy to load, process, and iterate over datasets in a memory-efficient way.acceleratehandles device placement, mixed precision, and distributed training. Even if you're only using one GPU, it simplifies your training code.trl(Transformer Reinforcement Learning) includesSFTTrainer, which we'll use for supervised fine-tuning. It's built on top oftransformersand optimized for instruction-tuning workflows.peft(Parameter-Efficient Fine-Tuning) provides techniques like LoRA, which we won't use in this project, but it's good to have installed in case you want to experiment later.torchis PyTorch, the deep learning framework everything else is built on.
If you're working with a GPU that has limited VRAM (like 8GB or less), you should also install bitsandbytes. This library enables 8-bit quantization, which can significantly reduce memory usage during training:
pip install -U bitsandbytesYou won't need it for this first run if you're using a 12GB+ GPU, but it's good to have available. We'll cover memory-saving techniques in detail later in the chapter.
Finally, configure accelerate so it knows about your hardware setup:
accelerate configThis will ask you a series of questions about your environment: how many GPUs you have, whether you want to use mixed precision, and so on. If you're unsure, choose the defaults. For a single GPU setup, the defaults are almost always correct.
Once this is done, you're ready to start building your dataset.