Below is an explanation of one common approach using current tools for LLaMA-style models—namely, offloading part of the model to your limited GPU memory while keeping the rest on CPU—to maximize throughput (tokens per second). The basic idea is to “split” the model so that the most computationally intensive parts run on your GPU (even if it only has 2 GB of RAM) while the rest stays on the CPU (using your 8 GB RAM). Note that you may need to experiment with the settings to find the best balance. One popular solution is to use a fork of llama.cpp that supports GPU offloading. Here’s a walkthrough:
1. Prepare Your Environment
-
Ensure you have a recent version of llama.cpp (or an offshoot) with GPU offloading support.
Some forks (or branches) add parameters to let you put a certain number of model layers on the GPU.
-
Build or install the code with GPU support enabled.
When compiling, ensure that CUDA is enabled (e.g., using cmake -DUSE_CUBLAS=ON or similar flags depending on the repository’s instructions).
-
Make sure your OS and drivers are set up for CUDA on your GPU.
2. Convert or Prepare Your Model File
- Most instructions assume a model file in a compatible format (for example, a quantized GGML/gguf file).
- Since your model is 3.2 GB, verify that it was converted to a format that can be partially loaded into GPU memory and partially into CPU memory.
3. Decide How Many Layers Go to GPU
-
Tools like llama.cpp let you specify something like the number of layers to process on the GPU using a parameter (often called --n_gpu_layers).
-
Why is this necessary?
Loading too many layers onto a GPU with only 2 GB RAM will cause your system to run out of GPU memory; too few may under-utilize the GPU.
-
How to decide?
- Most LLaMA models have 20–40 transformer layers. A common starting point is to offload just the first few layers (or sometimes the last few)—often somewhere between 1 and 4 layers—depending on your model size, GPU capabilities, and the offloading method used.
- For example: You might try running with
--n_gpu_layers 2 and then profile the token generation speed. Increase or decrease this number and observe the tokens per second until you find an optimal balance.
4. Use Command-Line Options to Launch Inference
A sample command might look like this (adjust paths and parameters as needed):
./main \
-m /path/to/your/model.bin \
--n_gpu_layers 2 \
--gpu_batch_size 8 \
[other options such as context length, prompt, etc.]
-
Explanation of parameters:
-m /path/to/your/model.bin: Path to your 3.2 GB model file.
--n_gpu_layers 2: Tells the executable to run 2 layers on the GPU. With only 2 GB, you likely need to keep this small.
--gpu_batch_size 8: This is an example value for how many tokens/batches you want processed per GPU batch. (Some versions may use a different flag such as --gpu_batch.)
Adjust this value to find the optimal balance between throughput and GPU memory usage.
-
Other parameters (e.g., prompt, context length, or any quantization-specific flags) can be added as needed.
5. Experiment and Monitor Performance
-
Tokens per second (TPS) is the main metric.
After launching a few test runs, monitor the output. Many implementations print TPS or you can measure the time for generating a fixed number of tokens.
-
Experiment with:
- The number of GPU layers (
--n_gpu_layers): Increasing might improve TPS up to the memory limit; decreasing might shift extra computation to the CPU.
- The GPU batch size: A larger batch may improve GPU utilization but also uses more memory.
-
Profile your memory usage:
Use tools like nvidia-smi to monitor GPU memory and ensure you aren’t overcommitting.
6. Consider Alternatives If Needed
-
Other repositories/tools:
Projects like exllama or the text-generation-webui offer interfaces for mixed CPU/GPU inference. They sometimes have built-in heuristics for offloading layers based on available GPU memory, which may simplify the tuning process.
-
Model Quantization:
If performance is still a concern, consider using a more aggressively quantized model (e.g., 4-bit quantization). This lowers memory requirements and might permit more layers on the GPU.
Final Notes
-
Balancing Act:
With only 2 GB on your GPU, the bulk of the model will run on the CPU. The key is finding the “sweet spot” where the GPU handles the parts that benefit most from its parallelism without overloading its limited memory, and the CPU covers the rest.
-
Documentation and Community:
Check the documentation of your chosen repository and search community forums (like GitHub issues or relevant Discord channels) where users with similar hardware setups share their tuning parameters.
By following these guidelines and carefully tuning the parameters (especially --n_gpu_layers and batch sizes), you can distribute the workload between your GPU and CPU effectively and achieve the best tokens-per-second performance possible with your hardware.