Skip to content

[Bug] Fine-Tuning uses much more VRAM than advertised, causing OOMs; cannot actually fine-tune any big models #4504

Description

@mags0ft

Hi,

I've continued to try out a few things in Unsloth Studio and noticed a weird behavior that might not be solely limited to Unsloth Studio, but to the library itself. I made sure to update everything beforehand.

Inexplicably to me, training on a dataset in a ChatML-like format (with a messages column where an array of objects with role and content keys are present) consumes much, much more VRAM than training on an Alpaca-like dataset (where there are only two columns, one for prompt and one for response). I might be messing up my terminilogy here though, so please correct me if I'm wrong.

I think this is super weird, as to my best knowledge about LLMs and training, the format of the dataset should absolutely not matter at all. Unsloth Studio is applying the model's own chat template to the data anyways, or am I getting this wrong?

Enough yapping, let me give you an example. Let's say we have this configuration:

# Base model:
unsloth/Qwen3.5-2B
QLoRA, 4-bit

# Hardware:
12 GB VRAM NVIDIA RTX 4070 Ti
32 GB system RAM (CPU)

# Training config:
Batch size: 2
Gradient accumulation steps: 4
Learning rate: 0.00005
Optimizer: AdamW 8-bit
Max steps: 30
Context length: 4096
Warmup steps: 5

# LoRA config:
Rank: 16
Alpha: 16
Dropout: 0
Variant: lora

The only thing we're changing is the dataset.

Let's first try a dataset where we only have prompt and response columns, so "Alpaca-like": gsm8k by OpenAI. Training on this dataset, with thousands of rows, is absolutely no problem. The model trains just fine, finishes, and can be used.

Swapping out the dataset by a ChatML one, no matter how small, we'll run into an OOM; I tried the first tiny dataset I came across on Hugging Face, in this case it's TeichAI/glm-4.6-250x. 250 rows only, ChatML format, nothing else. Same configuration, but we run into an OOM:

CUDA out of memory. Tried to allocate 7.58 GiB. GPU 0 has a total capacity of 11.56 GiB of which 4.07 GiB is free. Including non-PyTorch memory, this process has 6.83 GiB memory in use. Of the allocated memory 5.90 GiB is allocated by PyTorch, and 686.61 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)

So weird, isn't it? I can't explain this to myself! So, I thought, maybe the dataset is just glitchy and doesn't follow some formatting rule. I made my own, definitely compliant and fully featured dataset and tried again, but even then, I get the same kind of OOM. Then I made an Alpaca-like custom dataset, and would you look at that: Trains just fine.

No idea what the issue could be... Let me remind you: this is a 4-bit 2B model @ 4096 context length training a r = 16, alpha = 16 QLoRA adapter on a 12 GB VRAM card. This shouldn't be a problem at all... likely a bug!

I hope we can get this fixed! I'll be happy to help in case there are any further questions.

Alpaca-like (works) ChatML-like (broken)
Image Image

Training configuration:

training:
  max_seq_length: 4096
  num_epochs: 1
  learning_rate: 0.00005
  batch_size: 2
  gradient_accumulation_steps: 8
  warmup_steps: 5
  max_steps: 30
  save_steps: 30
  eval_steps: 0.1
  weight_decay: 0.01
  random_seed: 3407
  packing: false
  train_on_completions: false
  gradient_checkpointing: unsloth
  optim: adamw_8bit
  lr_scheduler_type: linear
lora:
  lora_r: 16
  lora_alpha: 16
  lora_dropout: 0
  target_modules:
    - q_proj
    - k_proj
    - v_proj
    - gate_proj
    - up_proj
    - down_proj
    - o_proj
  use_rslora: false
  use_loftq: false
  finetune_vision_layers: true
  finetune_language_layers: true
  finetune_attention_modules: true
  finetune_mlp_modules: true

Best regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions