Fine-tuning LLM on a laptop: VRAM - Shared Memory - GPU Load - Performance
Tags: ai, machinelearning, genai, programming
Published: 2024-04-09 13:56:42

I have been playing with Supervised Fine Tuning and LORA using my laptop with NVIDIA RTX 4060 8GB. The subject of SFT is vast, picking the correct training hyperparams is more magic than science, and there's a good deal of experimentation...

Yet let me share one small finding. GPU utilization and Shared Memory effect on training speed.

I used [Stable LM 2](https://huggingface.co/stabilityai/stablelm-2-1_6b?text=My+name+is+Lewis+and+I+like+to) 1.6B base model and turned it into a chat model using [4400 samples](https://huggingface.co/datasets/g-ronimo/oasst2_top4k_en) from OASTT2 dataset. Here is the [training file](https://github.com/maxim-saplin/finetuning/blob/main/qlora_oastt2.py).

Below is a screenshot from W&B showing system metrics for 2 runs. The only difference was the batch size, all other params were the same:

![Weights & Biases report](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iirhb5anmg6bksdqs92j.png)

1) Batch size 1 (brown line) - 16.5 minutes per epoch
2) Batch size 2 (the other color) - 25.0 minutes per epoch 

Pay attention to GPU Power Usage and how the brown line fluctuates around 82W while the 2nd run averages 65W. Apparently, during the 1st run, the GPU was loaded better which resulted in 50% faster completion of a single epoch. Even despite the fact that large [batch size](https://huggingface.co/docs/transformers/en/perf_train_gpu_one#batch-size-choice) is supposed to speed up the training.

The reason for that is the use of system RAM instead of VRAM - when GPU is out of fast memory it will happily spill over its data into slower system memory. It took just 0.9GB excess to slow down the whole process by 45%. The larger the portion of data in system RAM - the slower the training.

![Task Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxttzix1w2vezvxoiq01.png)

The takeaway is you should be considerate of GPU load and be on the watch-out for the training job getting outside of VRAM - doing it silently without any warnings and extending your training to another day :) If it happens that you are slightly short of VRAM - you better play with quantization or batch size and see if there's a way to fit all the data into VRAM and don't have anything in RAM.

**P.S.>**

GPU load is not the only factor and is not the most important one in determining the total execution time. I.e. I could see 100W usage when trying out [Galore](https://github.com/maxim-saplin/finetuning/blob/main/galore_oastt2.py) PEFT method. Yet it was way slower with the same dataset and similar params.

![QLORA vs Galore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ycf0v8rh1hte4ohpikls.png)

- the gap/straight line is due to a missing internet connection during this period.

Besides I could see a 50% with LORA by simply enabling [Flash Attention 2](https://huggingface.co/docs/transformers/en/perf_train_gpu_one#flash-attention-2) - though has to use WSL2 and run the job there since Windows is not supported yet. Latter I switched to SDPA (scaled dot product attention).

**P.P.S.>**

A life hack to quickly see if the GPU is fully utilized - check the GPU temp in Windows Task Manager. If it is just a  few degrees above idle temp (e.g. 50-60°C), you are underutilized. If you hit 80°C degrees, you are good. E.g. RTX 4060 has a throttling temperature at 87°C degrees which means 100% utilisation. The advice won't hold for desktops - there's typically an extra cooling capacity and the GPU won't be thermally throttled.