Measure first, then scale: making good use of one or several GPUs, for inference and for training.
Talk to us about performance. Before switching to a larger model or to several GPUs, or whenever a job seems slow, post a message in the cluster's Discord channel. The ACSS team can help you measure, choose a model and speed up your code — often with a simpler solution than more hardware. It also helps us make the best use of shared resources.
1. Measure before you scale#
Most projects on the cluster run a model on a corpus — classifying, annotating, extracting, transcribing, embedding. For these tasks, the first question is not "how many GPUs?" but "is my current setup good enough for my task?" Three things are worth measuring, in this order:
Quality for your task. Compare the outputs with a small sample you have annotated yourself. A smaller model often does as well as a larger one on a specific task; a larger model is only worth it if it clearly does better.
Time. How long does it take for 100 documents? Multiply to estimate the full corpus. A progress bar (tqdm) is enough.
GPU usage. While the job runs, watch the GPU:
bashwatch -n 1 nvidia-smiWhat you see What it usually means GPU-Utilsteady at 90–100%The GPU is busy: see easy optimisations GPU-Utiljumping between 0% and 100%The GPU waits for data: see batching and data loading Memory close to the maximum Risk of out-of-memory errors: smaller batches, or a smaller or quantized model
Only scale up (larger model, more GPUs) when these measurements show you need to. Several GPUs add complexity and keep resources busy for other users.
To go further: the PyTorch Profiler shows which operations take the most time. Useful once the basics above are in place; requires being comfortable with PyTorch.
2. Key notions#
CPU, GPU and VRAM#
A processor (CPU) is like a few very capable workers doing tasks one after another. A GPU is like thousands of simple workers doing the same small calculation at the same time. Neural networks are mostly large multiplications of tables of numbers — exactly the kind of repetitive work GPUs do well.
Two numbers matter:
- VRAM — the GPU's memory: the model and the data being processed must fit in it.
- Memory bandwidth — how fast data moves inside the card: it largely sets the speed.
Batch#
A batch is a group of inputs (sentences, images, audio segments) processed together in one pass. Processing documents one by one leaves most of the GPU unused; processing them in batches keeps it busy. Larger batches are faster, up to the point where they no longer fit in VRAM.
Inference and training#
- Inference means using a trained model: you give it inputs, it returns predictions. This is what most projects do.
- Training (including fine-tuning) means changing the model's parameters so it gets better at a task. It needs more memory and more concepts (loss, gradients, optimiser).
The tools are not the same for the two cases: this page treats them separately.
Curious how GPUs work under the hood? See GPU programming basics from Marc Lelarge's deep learning course (dataflowr / MVA).
3. Inference#
Batch inference#
The idea: send inputs to the model in batches rather than one at a time.
- When it helps: almost always — it is the first thing to check when inference is slow.
- Prerequisites: none beyond running a model in Python. Most libraries take a
batch_sizeargument (for example Hugging Face pipelines). - To learn: Pipeline batching (Hugging Face).
Feed the GPU — the DataLoader#
The idea: reading and preparing data (opening files, tokenizing, decoding audio) happens on the CPU. If it is slower than the GPU, the GPU sits idle. A PyTorch DataLoader with several workers prepares the next batches in parallel while the GPU computes.
┌──────────┐
│ Worker 1 │──┐
┌────────┐ ├──────────┤ │ ┌─────────┐ ┌─────┐
│Dataset │─>│ Worker 2 │──┼─>│ Queue │──>│ GPU │
│on disk │ ├──────────┤ │ │prefetch │ └─────┘
└────────┘ │ Worker N │──┘ └─────────┘
└──────────┘- When it helps:
GPU-Utilkeeps dropping to 0% innvidia-smi. - Prerequisites: writing a small PyTorch
Dataset. Start withnum_workersbetween 4 and 8; each worker uses RAM, so do not go too high. - To learn: Datasets & DataLoaders (PyTorch).
Several GPUs for inference: split the corpus#
The idea: if the model fits on one GPU, the simplest way to use two GPUs is to run two independent copies of your script, each on its own GPU and its own half of the corpus. This is data parallelism in its simplest form, with no communication between GPUs.
CUDA_VISIBLE_DEVICES=0 python annotate.py --part 1 &
CUDA_VISIBLE_DEVICES=1 python annotate.py --part 2 &- When it helps: a large corpus, a model that fits on one GPU, and enough free GPUs (check
nvidia-smiand announce it on Discord: see Sharing the GPU servers). - Prerequisites: your script can process one part of the corpus and save its results separately.
- To learn: Distributed inference (Hugging Face Accelerate) does the same thing from a single script.
Serving a model — vLLM#
For generative models (LLMs), vLLM handles batching and GPU memory for you, and is much faster than a plain Python loop. See AI Tools and Good practices with vLLM.
4. Training and fine-tuning#
Training is more technical than inference. If you are starting a fine-tuning project, come and discuss it on Discord first: in many cases parameter-efficient fine-tuning (LoRA) on a single GPU is enough.
Data parallelism — DDP and Accelerate#
The idea: each GPU holds a full copy of the model and trains on a different slice of each batch; after each step, the GPUs average what they have learnt (the gradients) so the copies stay identical.
Training batch
/ | \
slice 1 slice 2 slice 3
| | |
┌────────┐┌────────┐┌────────┐
│ GPU 0 ││ GPU 1 ││ GPU 2 │
│ full ││ full ││ full │
│ model ││ model ││ model │
└────────┘└────────┘└────────┘
\ | /
gradients averaged- When it helps: the model fits on one GPU, but training takes too long.
- Prerequisites: a working single-GPU training loop, and the notions of loss, gradient and optimiser.
- Tools: Accelerate (simplest: a few lines change in your script) or PyTorch's
DistributedDataParallel(DDP). - To learn: Accelerate quick tour, Getting started with DDP (PyTorch).
Sharding — FSDP and DeepSpeed#
The idea: when the model, its gradients and the optimiser state do not fit on one GPU, each GPU stores only a share of them and they exchange the pieces as needed.
- When it helps: full fine-tuning of a model too large for one GPU.
- Prerequisites: being comfortable with data parallelism first. Start with FSDP, which is built into PyTorch; DeepSpeed offers more options.
- To learn: Getting started with FSDP (PyTorch), DeepSpeed tutorials, Efficient training on multiple GPUs (Hugging Face).
Saving memory during training#
- Gradient accumulation — simulates a large batch by adding up several small ones.
- Gradient checkpointing — frees a lot of memory at the cost of slower training (~20%).
Both are single options in Hugging Face's TrainingArguments: see Efficient training on a single GPU.
5. Easy optimisations#
These often bring larger gains than more GPUs, for both inference and training:
- bf16 precision — half the memory of fp32, faster, stable on H100s (
torch_dtype=torch.bfloat16). See dtype. - Flash Attention 2 — a faster implementation of attention for Transformer models. See Flash Attention 2.
torch.compile— one line,model = torch.compile(model), lets PyTorch optimise the model. See the torch.compile tutorial.
6. Large models#
A large model is not always a better model for your task (see Measure before you scale). It is also slower, and on shared machines it keeps GPUs busy that other users need. Use one when your measurements show a clear gain, and announce it on Discord.
What fits on our machines?#
Rule of thumb: a model needs ~2 GB of VRAM per billion parameters in bf16/fp16 (see How much VRAM do I need?).
| Machine | Total VRAM | Roughly fits (fp16) |
|---|---|---|
| 2×H100 NVL | 188 GB | Models up to ~90B parameters |
| 4×H100 NVL | 376 GB | Models up to ~180B parameters |
A 7B model (~14 GB) runs on a single GPU. Before splitting a model across GPUs, consider quantization, which divides the memory footprint by 2 or 4.
Splitting a model across GPUs#
When a model does not fit on one GPU, it is split across several:
- Tensor parallelism — each layer is cut in pieces spread over the GPUs, which exchange results at every layer. This is what vLLM's
--tensor-parallel-sizedoes. It needs a fast link between GPUs: on the 4×H100, use an NVLink pair, GPUs(0,1)or(2,3)(see the NVLink rule). The 2×H100 has no NVLink. - Pipeline parallelism — consecutive groups of layers go on different GPUs, like an assembly line. Mostly used for very large models, combined with the other techniques.
In Hugging Face code, device_map="auto" spreads a model over all the GPUs it can see: choose them first with CUDA_VISIBLE_DEVICES.
The reference on all these techniques is the Ultra-Scale Playbook (Hugging Face).
Useful resources#
| Topic | Resource |
|---|---|
| Batching with Hugging Face pipelines | Pipeline batching |
| Data loading | Datasets & DataLoaders |
| Inference on several GPUs | Accelerate — distributed inference |
| Training on several GPUs | Accelerate, DDP tutorial, FSDP tutorial |
| Profiling | PyTorch Profiler |
| Large-scale training | Ultra-Scale Playbook |
| How GPUs work | GPU programming basics — Marc Lelarge (dataflowr / MVA) |
What's next?#
| I want to… | Go to |
|---|---|
| Discuss the performance of my code | The cluster's Discord channel |
| Serve a model with vLLM | AI Tools |
| Check the rules for shared GPUs | Sharing the GPU servers |
| Go back to server basics | Working on the Machine |