Tuning Large Language Models for Real-World ApplicationsChapter 23

Step 2: Install the tools

Section 3 of 10-~ 3 min read-Synced from Cuantum content

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\activate

Once your virtual environment is active, install the core libraries:

pip install -U transformers datasets accelerate trl peft torch

Here's what each library does:

  • transformers provides pre-trained models and tokenizers from Hugging Face. This is the backbone of nearly all modern NLP work.
  • datasets makes it easy to load, process, and iterate over datasets in a memory-efficient way.
  • accelerate handles device placement, mixed precision, and distributed training. Even if you're only using one GPU, it simplifies your training code.
  • trl (Transformer Reinforcement Learning) includes SFTTrainer, which we'll use for supervised fine-tuning. It's built on top of transformers and 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.
  • torch is 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 bitsandbytes

You 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 config

This 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.