Skip to content

multi-GPU training #2435

Closed
Closed

Description

@duyuankai1992

Hi, may I ask if Unsloth currently supports multi-GPU training? I tried running it with torchrun for multi-GPU training using my own setup, but it failed. Could you please advise me on how to do it correctly? Thank you!

Activity

  1. lot-insts commented on Apr 30, 2025

    @lot-insts

    Same question. How to train GRPO in multi-GPU?

  2. sangmandu commented on Apr 30, 2025

    @sangmandu

    I am sooooooo looking forward to 2.0.
    They said it comes out on end of April.

  3. shimmyshimmer commented on Apr 30, 2025

    @shimmyshimmer
    Member

    Have you guys tried running it on multiGPUs?

    It's coming, I'll update you guys once we release it.

  4. tianlinzx commented on May 2, 2025

    @tianlinzx

    @shimmyshimmer How is it going ?

  5. haoyuhan1 commented on May 8, 2025

    @haoyuhan1

    Any update? It's too slow to finetune large models now.

  6. Micropot commented on May 9, 2025

    @Micropot

    @haoyuhan1 still in beta. I haven't found any bypass yet...

  7. chuangzhidan commented on May 10, 2025

    @chuangzhidan

    support train GRPO in multi-GPU now?

  8. kadirnar commented on May 10, 2025

    @kadirnar

    Please add full-finetuning support in float16 format.

  9. rolandtannous commented on May 10, 2025

    @rolandtannous
    Contributor

    @kadirnar is this related to multi-gpu support?

  10. kadirnar commented on May 10, 2025

    @kadirnar

    @kadirnar is this related to multi-gpu support?

    I don't want multi-GPU support to only work in FP8 or INT4. We can already perform full-finetuning of many models in fp8 format. I think it's very important that float16 support is coming along with multi-GPU support.

  11. lot-insts commented on May 14, 2025

    @lot-insts

    Hope for updating

  12. neozhao98 commented on May 20, 2025

    @neozhao98

    I tried accelerate + unsloth, it works well

  13. chuangzhidan commented on May 20, 2025

    @chuangzhidan

    I tried accelerate + unsloth, it works well

    supports multi gpu reinforce training ?

  14. risqaliyevds commented on May 20, 2025

    @risqaliyevds

    I tried accelerate + unsloth, it works well

    Well done @neozhao98. Can you share your tuning scripts?

  15. 73 remaining items

  16. Skyruller commented on Oct 24, 2025

    @Skyruller

    Yesterday I wrote out some incorrect code, but today I updated and tested it. There were problems saving LoRa adapters.

    Run:

    CUDA_VISIBLE_DEVICES=0,1
    torchrun --nproc_per_node=2 train_fsdp_unsloth.py
    --model "/home/skyruller/text-generation-webui/user_data/models/Qwen3-4B-Instruct-2507"
    --data "/media/skyruller/NovyTom/dataset/opensloth_powermill_dataset/dataset.txt"
    --out "./out_fsdp_lora"
    --epochs 1 --microbatch 1 --grad_accum 1 --lr 2e-4
    --max_seq_len 896 --log_every 10 --save_every 45

    train_fsdp_unsloth.py

    (train-py313) skyruller@Skyruller:~/SSD$ CUDA_VISIBLE_DEVICES=0,1 \
    torchrun --nproc_per_node=2 train_fsdp_lora.py \
      --model "/home/skyruller/text-generation-webui/user_data/models/Qwen3-4B-Instruct-2507" \
      --data "/media/skyruller/NovyTom/dataset/opensloth_powermill_dataset/dataset.txt" \
      --out "./out_fsdp_lora" \
      --epochs 1 --microbatch 1 --grad_accum 1 --lr 2e-4 \
      --max_seq_len 896 --log_every 10 --save_every 45
    W1024 11:28:47.742000 1488886 site-packages/torch/distributed/run.py:774] 
    W1024 11:28:47.742000 1488886 site-packages/torch/distributed/run.py:774] *****************************************
    W1024 11:28:47.742000 1488886 site-packages/torch/distributed/run.py:774] Setting OMP_NUM_THREADS environment variable for each process to be 1 in default, to avoid your system being overloaded, please further tune the variable for optimal performance in your application as needed. 
    W1024 11:28:47.742000 1488886 site-packages/torch/distributed/run.py:774] *****************************************
    🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.
    🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.
    🦥 Unsloth Zoo will now patch everything to make training faster!
    🦥 Unsloth Zoo will now patch everything to make training faster!
    [Init] procs=2 | out=./out_fsdp_lora
    [Paths] model=/home/skyruller/text-generation-webui/user_data/models/Qwen3-4B-Instruct-2507
            data =/media/skyruller/NovyTom/dataset/opensloth_powermill_dataset/dataset.txt
    ==((====))==  Unsloth 2025.9.9: Fast Qwen3 patching. Transformers: 4.56.2.
       \\   /|    NVIDIA GeForce RTX 5060 Ti. Num GPUs = 2. Max memory: 15.477 GB. Platform: Linux.
    O^O/ \_/ \    Torch: 2.8.0+cu128. CUDA: 12.0. CUDA Toolkit: 12.8. Triton: 3.4.0
    \        /    Bfloat16 = TRUE. FA [Xformers = None. FA2 = True]
     "-____-"     Free license: http://github.com/unslothai/unsloth
    Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!
    ==((====))==  Unsloth 2025.9.9: Fast Qwen3 patching. Transformers: 4.56.2.
       \\   /|    NVIDIA GeForce RTX 5060 Ti. Num GPUs = 2. Max memory: 15.477 GB. Platform: Linux.
    O^O/ \_/ \    Torch: 2.8.0+cu128. CUDA: 12.0. CUDA Toolkit: 12.8. Triton: 3.4.0
    \        /    Bfloat16 = TRUE. FA [Xformers = None. FA2 = True]
     "-____-"     Free license: http://github.com/unslothai/unsloth
    Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!
    Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00, 130.43it/s]
    Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00, 131.70it/s]
    Unsloth 2025.9.9 patched 36 layers with 36 QKV layers, 36 O layers and 36 MLP layers.
    Unsloth 2025.9.9 patched 36 layers with 36 QKV layers, 36 O layers and 36 MLP layers.
    `torch_dtype` is deprecated! Use `dtype` instead!
    `torch_dtype` is deprecated! Use `dtype` instead!
    [DATA] Диалогов (блоков): 427
    [FSDP] auto_wrap по классу: Qwen3DecoderLayer
    NCCL version 2.27.3+cuda12.9
    NCCL version (2, 27, 3)
    🚀 Начинаем обучение... total_steps=214
    Unsloth: Will smartly offload gradients to save VRAM!
    [step    1/214] loss=2.9889 lr=3.33e-05
    Unsloth: Will smartly offload gradients to save VRAM!
    [step    2/214] loss=6.0202 lr=6.67e-05
    [step    3/214] loss=5.1834 lr=1.00e-04
    [step    4/214] loss=5.2626 lr=1.33e-04
    [step    5/214] loss=4.0537 lr=1.67e-04
    [step   10/214] loss=1.6656 lr=1.96e-04
    [step   20/214] loss=0.4640 lr=1.87e-04
    [step   30/214] loss=0.3731 lr=1.77e-04
    [step   40/214] loss=0.4841 lr=1.67e-04
    💾 Сохранено: ./out_fsdp_lora/checkpoint-step45
    [step   50/214] loss=1.8882 lr=1.58e-04
    [step   60/214] loss=0.9320 lr=1.48e-04
    [step   70/214] loss=0.4621 lr=1.38e-04
    [step   80/214] loss=0.2343 lr=1.29e-04
    [step   90/214] loss=0.2535 lr=1.19e-04
    💾 Сохранено: ./out_fsdp_lora/checkpoint-step90
    [step  100/214] loss=0.3085 lr=1.10e-04
    [step  110/214] loss=1.7849 lr=1.00e-04
    [step  120/214] loss=1.4324 lr=9.04e-05
    [step  130/214] loss=0.2631 lr=8.08e-05
    💾 Сохранено: ./out_fsdp_lora/checkpoint-step135
    [step  140/214] loss=0.1154 lr=7.12e-05
    [step  150/214] loss=0.2843 lr=6.15e-05
    [step  160/214] loss=0.2717 lr=5.19e-05
    [step  170/214] loss=1.2447 lr=4.23e-05
    [step  180/214] loss=0.2980 lr=3.27e-05
    💾 Сохранено: ./out_fsdp_lora/checkpoint-step180
    [step  190/214] loss=1.0501 lr=2.31e-05
    [step  200/214] loss=0.1994 lr=1.35e-05
    [step  210/214] loss=0.1167 lr=3.85e-06
    💾 Сохранено: ./out_fsdp_lora/final-checkpoint
    ✅ Обучение завершено. Шагов: 214, время: 8.9 мин.
    (train-py313) skyruller@Skyruller:~/SSD$ 
    
  17. timho1047 commented on Oct 24, 2025

    @timho1047

    @cwpeng-cn Did you complete your training? In my case, after all epochs are completed, the process get stuck in trainer.train(), never exit or execute next line. Unable to find out the cause of it.

    Image

    I think this issue happens when I use load_best_model_at_end=True at SFTConfig and provide an evaluation dataset. When I try on very small dataset, such as 8 samples, for both train and eval dataset, this happens quite frequently. I suspect that the trainer hangs when loading best model at the end, if this best model is not the latest checkpoint. But I am not sure.

  18. timho1047 commented on Oct 26, 2025

    @timho1047

    @cwpeng-cn Did you complete your training? In my case, after all epochs are completed, the process get stuck in trainer.train(), never exit or execute next line. Unable to find out the cause of it.
    Image

    I think this issue happens when I use load_best_model_at_end=True at SFTConfig and provide an evaluation dataset. When I try on very small dataset, such as 8 samples, for both train and eval dataset, this happens quite frequently. I suspect that the trainer hangs when loading best model at the end, if this best model is not the latest checkpoint. But I am not sure.

    I solved this by setting load_best_model_at_end=False, then used a callback to save the best model.

    class SaveBestModelCallback(TrainerCallback):
        """Track the best model and save. Only used when in DDP with unsloth."""
        def __init__(self, trainer: SFTTrainer, rank: int = 0):
            self.trainer = trainer
            self.rank = rank
            self.best_eval_loss = float("inf")
            self.best_epoch = 0
    
        def on_evaluate(self, args, state, control, **kwargs):
            logs = state.log_history[-1]
            current_eval_loss = logs.get("eval_loss", float("inf"))
            
            if current_eval_loss < self.best_eval_loss:
                if self.rank == 0:
                    print(f"\n✓ Eval loss improved from {self.best_eval_loss:.4f} to {current_eval_loss:.4f}, will save the model\n")
                self.best_eval_loss = current_eval_loss
                self.trainer.save_model(self.trainer.args.output_dir)
                self.best_epoch = state.epoch
            else:
                if self.rank == 0:
                    print(f"\nCurrent eval loss: {current_eval_loss:.4f} > best eval loss: {self.best_eval_loss:.4f}\n")
    
        
    trainer = SFTTrainer(
        model=model,
        args=config.training_args,
        train_dataset=train_dataset,
        eval_dataset=eval_dataset,
        tokenizer=tokenizer,
    )
    trainer.add_callback(SaveBestModelCallback(trainer, rank=rank))
    trainer.train(resume_from_checkpoint=config.resume_from_checkpoint)
  19. santoshkosgi commented on Oct 29, 2025

    @santoshkosgi

    Able to run this with FastLanguageModel but when I try to use a Vision model using FastVisionModel, the code gets struck in FastVisionModel.from_pretrained. Multi-Gpu finetuning for vision models isn't supported yet ? This looks similar to an old issue #2280

  20. 521bibi commented on Nov 4, 2025

    @521bibi

    How to train GRPO in multi-GPU?
    Only get how to train SFT in multi-GPU

  21. minhtcai commented on Nov 8, 2025

    @minhtcai

    I modified @cwpeng-cn work and made it run on Qwen3-VL on 2x L4:
    https://github.com/minhtcai/unsloth-multi-gpu-vision

    Able to run this with FastLanguageModel but when I try to use a Vision model using FastVisionModel, the code gets struck in FastVisionModel.from_pretrained. Multi-Gpu finetuning for vision models isn't supported yet ? This looks similar to an old issue #2280

  22. jamesnulliu commented on Nov 12, 2025

    @jamesnulliu

    @timho1047 You right, Thanks! Runs with torchrun --nproc_per_node=4 train_ddp.py

    Did you split the dataset manually based on local rank inside your train script? And do I have to initialize and distroy torch distributed environment just like the normal DDP training? (Really appreciate it if you can provide something like pseudocode)

  23. antonheryanto commented on Dec 3, 2025

    @antonheryanto

    Thank You all for the thread,
    successfuly running unsloth fine-tuning using multi GPU, but when adding target module "embed_tokens","lm_head", the training crash with error ValueError: DistributedDataParallel's input module must be on the same type of devices, but input module parameters locate in {'cpu', 'cuda'}.
    Its it because unsloth offload input_embeddings and output_embeddings to disk cause the device mixup?
    if it the case, how to disable embedding oflload?

  24. Skyruller commented on Dec 24, 2025

    @Skyruller

    I checked, and it works on 3 graphics cards.

    ProArt Z790-CREATOR WIFI, Intel® Core™ i7 processor 14700K, 2*48 RAM

    train_fsdp_lora.py

    r=512, lora_alpha=1024
    target_modules=["q_proj","k_proj","v_proj","o_proj","gate_proj","up_proj","down_proj"]

    Image Image Image

    CUDA_VISIBLE_DEVICES=0,1,2
    torchrun --nproc_per_node=3 train_fsdp_lora.py
    --model "/home/skyruller/text-generation-webui/user_data/models/Qwen3-4B-Instruct-2507"
    --data "/home/skyruller/SSD/FSDP/dataset.txt"
    --out "./out_fsdp_lora4"
    --epochs 10 --microbatch 1 --grad_accum 1 --lr 2e-4
    --max_seq_len 2048 --log_every 10 --save_every 45

  25. Niklas-dev commented on Mar 13, 2026

    @Niklas-dev

    Is it possible now to use unsloth with fsdp?
    And with how many devices if it works?

  26. madmax0404 commented on Mar 21, 2026

    @madmax0404

    vllm + unsloth + deepspeed would be a monstrosity

  27. Imagineer99 commented on Jun 11, 2026

    @Imagineer99
    Member

    Added in #3917, #4602, #4063, #4143.

  28. thad0ctor commented on Jun 11, 2026

    @thad0ctor
    Contributor

    Added in #3917, #4602, #4063, #4143.

    Is this really closed with comparable feature parity?

    https://unsloth.ai/docs/basics/multi-gpu-training-with-unsloth still says this is a work-in-progress

  29. Skyruller commented on Aug 26, 2026

    @Skyruller

    Has anyone run into this issue with Unsloth Studio multi-GPU training?

    I can successfully train Qwen3.8-27B with QLoRA on 3× RTX 5060 Ti 16GB using a standalone Unsloth Python script with:

    device_map="balanced"
    custom max_memory for all 3 GPUs
    QLoRA 4-bit
    batch size 1

    The standalone training works and completes normally.

    However, when I try to train the same model on the same hardware in Unsloth Studio, GPU memory usage becomes very uneven and one GPU eventually hits CUDA OOM, even with batch size 1.

    Does Unsloth Studio use a different multi-GPU/device placement strategy than standalone Unsloth training with device_map="balanced" and custom max_memory?

    I also successfully completed the full workflow outside Studio:

    LoRA training → merge with the base model → GGUF conversion → quantization.

    The resulting quantized GGUF loads correctly in Unsloth Studio, but LM Studio refuses to load it.

    If useful, I can post the working standalone training script, merge script, GGUF conversion steps, quantization command, and GPU memory usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestmultigpuon roadmap

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions