# Fine-tuning Llama with LoRA on a single H100



Learn how to fine-tune Llama 3 using LoRA on a single H100 GPU, optimizing for bfloat16 and maximum throughput on Podstack's on-demand infrastructure.



Training · 2026-09-09 · 3 min read



Large Language Models (LLMs) like Llama 3 have democratized access to high-tier natural intelligence, but for specialized tasks—like adhering to a specific brand voice or mastering a niche technical domain—out-of-the-box performance often falls short. While full parameter fine-tuning is computationally expensive, Low-Rank Adaptation (LoRA) allows you to achieve state-of-the-art results on a single H100 GPU in under an hour.

In this guide, we will walk through the practical steps to fine-tune Llama 3 using the Hugging Face ecosystem, optimized for the high-memory bandwidth of an NVIDIA H100.

## Why the H100 for LoRA?

While you can technically run LoRA on smaller cards, the H100’s 80GB of HBM3 memory and Transformer Engine provide two distinct advantages:
1.  **Throughput:** You can utilize much larger batch sizes, significantly reducing the wall-clock time for your training runs.
2.  **Efficiency:** The H100 handles FP8 and bfloat16 precision natively, maintaining model stability during the weight update process without the overflow issues common in older architectures.

## Prerequisites

Before starting, ensure you have your environment configured. You’ll need a Python environment with `torch`, `peft`, `transformers`, and `trl` installed. 

```bash
pip install torch transformers peft accelerate bitsandbytes trl datasets
```

You will also need to authenticate with Hugging Face to access the Llama weights:
```text
Podstack does not ship a command-line interface. Use the console, the Python package `podstack-sdk`, or the API at https://podstack.ai/api/v1 with a `ps_live_` key.
```

## The Fine-Tuning Script

We will use the SFT (Supervised Fine-Tuning) Trainer from the `trl` library. This script targets the linear layers of the Llama architecture for adaptation.

```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
from datasets import load_dataset
from trl import SFTTrainer
from peft import LoraConfig, get_peft_model

model_id = "meta-llama/Meta-Llama-3-8B"

# 1. Load Dataset
dataset = load_dataset("json", data_files="my_custom_data.jsonl", split="train")

# 2. Configure LoRA
lora_config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
)

# 3. Load Model in bfloat16 (Optimized for H100)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token

# 4. Define Training Arguments
training_args = TrainingArguments(
    output_dir="./llama-3-lora",
    per_device_train_batch_size=8,
    gradient_accumulation_steps=4,
    learning_rate=2e-4,
    logging_steps=10,
    num_train_epochs=3,
    bf16=True, # Utilize H100 bfloat16 capabilities
    save_strategy="epoch",
    report_to="none"
)

# 5. Initialize Trainer
trainer = SFTTrainer(
    model=model,
    train_dataset=dataset,
    peft_config=lora_config,
    dataset_text_field="text",
    max_seq_length=2048,
    tokenizer=tokenizer,
    args=training_args,
)

trainer.train()
trainer.model.save_pretrained("./final_lora_adapter")
```

## Practical Tips for H100 Optimization

### 1. Leverage `bf16`
The H100 is designed for `bfloat16`. Unlike `fp16`, which has a limited dynamic range and often requires loss scaling to prevent gradients from zeroing out, `bf16` matches the dynamic range of `fp32`. Always set `bf16=True` in your `TrainingArguments` to maximize the H100’s hardware accelerators.

### 2. Maximize Batch Size
With 80GB of VRAM, a standard 8B model fine-tune using LoRA barely scratches the surface. If you are using a sequence length of 2048, you can likely push your `per_device_train_batch_size` to 16 or 32. This keeps the GPU cores saturated and finishes the job faster, saving you money on per-second billing.

### 3. Target More Modules
LoRA traditionally only targets the query and value projections (`q_proj`, `v_proj`). However, the H100 has enough headroom to target all linear layers—including `k_proj`, `o_proj`, and the MLP layers—without significant performance hits. This often leads to better convergence in fewer epochs.

## Deploying on Podstack

Moving from local experimentation to a production-grade H100 is seamless with Podstack. Instead of managing drivers and CUDA versions, you can deploy this fine-tuning job using our API.

First, set your environment variable:
```bash
export PODSTACK_API_KEY="your_api_key_here"
```

You can trigger a fine-tuning job as a Podstack Job, which spins up an H100, executes your script, and spins down immediately when the `trainer.train()` call completes. This ensures you only pay for the exact seconds the H100 was active.

```text
Podstack does not ship a command-line interface. Use the console, the Python package `podstack-sdk`, or the API at https://podstack.ai/api/v1 with a `ps_live_` key.
```

Once the adapter is saved, you can serve it using Podstack's serverless inference endpoints. Since LoRA adapters are small (usually <200MB), you can hot-swap them onto a base Llama 3 model in our serverless environment, allowing you to serve multiple fine-tuned versions of your model without paying for dedicated standing GPUs for each one.

## Conclusion

Fine-tuning Llama 3 on an H100 is the gold standard for creating specialized AI agents. By utilizing LoRA and `bfloat16`, you balance efficiency with power. With Podstack, you get the raw power of the H100 with the agility of a serverless workflow, ensuring that your ML pipeline is as cost-effective as it is performant.
