Skip to content

GRPOTrainer crashes with unsloth #1624

Closed

Description

@ymcki

I am trying to run GRPOTrainer with unsloth but it crashes. How to fix this?
unsloth 2025.2.4
unsloth 2025.2.3
transformers 4.47.1
torch 2.5.1
trl 0.14.0

This is the relevant code:

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = base_model, 
    max_seq_length = 2048,
    attn_implementation="flash_attention_2",
    dtype = torch.bfloat16,
    load_in_4bit = True,
)

training_args = GRPOConfig(
    output_dir=output_dir,
    learning_rate=5e-6,
    adam_beta1 = 0.9,
    adam_beta2 = 0.99,
    weight_decay = 0.05,
    bf16=True,
    warmup_ratio = 0.1,
    lr_scheduler_type='cosine',
    logging_steps=1,
    per_device_train_batch_size=1,
    gradient_accumulation_steps=2,
    num_generations=8,
    max_prompt_length=256,
    max_completion_length=786,
    num_train_epochs=1,
    save_steps=steps_num,
    save_total_limit=2,
    max_grad_norm=0.1,
    report_to="none",
    log_on_each_node=False,
)
model = FastLanguageModel.get_peft_model(
    model,
    r = 32, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
    target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
                      "gate_proj", "up_proj", "down_proj",],
    lora_alpha = 32,
    lora_dropout = 0, # Currently only supports dropout = 0
    bias = "none",    # Currently only supports bias = "none"
    use_gradient_checkpointing = "unsloth",
    random_state = 3407,
    use_rslora = False,  # We support rank stabilized LoRA
    loftq_config = None, # And LoftQ
)
trainer = GRPOTrainer(
    model=model,
    processing_class=tokenizer,
    reward_funcs=reward_func,
    args=training_args,
    train_dataset=dataset,
)
trainer.train()

This is the message when it crashes:

Traceback (most recent call last):
  File "/home/user/ft/grpo.py", line 184, in <module>
    trainer.train()
  File "/home/user/anaconda3/lib/python3.12/site-packages/transformers/trainer.py", line 2164, in train
    return inner_training_loop(
           ^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 382, in _fast_inner_training_loop
  File "<string>", line 31, in _unsloth_training_step
  File "/home/user/anaconda3/lib/python3.12/site-packages/trl/trainer/grpo_trainer.py", line 422, in compute_loss
    prompt_completion_ids = unwrapped_model.generate(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/anaconda3/lib/python3.12/site-packages/peft/peft_model.py", line 1838, in generate
    outputs = self.base_model.generate(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/anaconda3/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/anaconda3/lib/python3.12/site-packages/transformers/generation/utils.py", line 2252, in generate
    result = self._sample(
             ^^^^^^^^^^^^^
  File "/home/user/anaconda3/lib/python3.12/site-packages/transformers/generation/utils.py", line 3251, in _sample
    outputs = self(**model_inputs, return_dict=True)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/models/llama.py", line 1025, in _CausalLM_fast_forward
    outputs = fast_forward_inference(
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/models/gemma2.py", line 397, in Gemma2Model_fast_forward_inference
    seq_len = past_key_values[0][0].shape[-2]
              ~~~~~~~~~~~~~~~^^^
  File "<string>", line 10, in __cache_utils_getitem__
RuntimeError: Unsloth: You must call `FastLanguageModel.for_inference(model)` before doing inference for Unsloth models.

Activity

  1. danielhanchen commented on Feb 6, 2025

    @danielhanchen
    Member

    It should work now! Please update Unsloth via pip install --upgrade --force-reinstall --no-cache-dir unsloth_zoo unsloth then before your script, do:

    from unsloth import FastLanguageModel, PatchFastRL
    PatchFastRL("GRPO", FastLanguageModel)
  2. Eliorkalfon commented on Feb 6, 2025

    @Eliorkalfon

    It should work now! Please update Unsloth via pip install --upgrade --force-reinstall --no-cache-dir unsloth_zoo unsloth then before your script, do:

    from unsloth import FastLanguageModel, PatchFastRL
    PatchFastRL("GRPO", FastLanguageModel)

    Im getting this error:
    RuntimeError: Failed to import trl.trainer.alignprop_trainer because of the following error (look up to see its traceback): cannot import name 'DDPOStableDiffusionPipeline' from 'trl.models' ([/home/elior/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/trl/models/__init__.py](http://localhost:8888/home/elior/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/trl/models/__init__.py))

    Already tried to reinstall trl but still can't run it.

  3. UmarIgan commented on Feb 6, 2025

    @UmarIgan

    It should work now! Please update Unsloth via pip install --upgrade --force-reinstall --no-cache-dir unsloth_zoo unsloth then before your script, do:
    from unsloth import FastLanguageModel, PatchFastRL
    PatchFastRL("GRPO", FastLanguageModel)

    Im getting this error: RuntimeError: Failed to import trl.trainer.alignprop_trainer because of the following error (look up to see its traceback): cannot import name 'DDPOStableDiffusionPipeline' from 'trl.models' ([/home/elior/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/trl/models/__init__.py](http://localhost:8888/home/elior/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/trl/models/__init__.py))

    Already tried to reinstall trl but still can't run it.

    I got this error as well

  4. amrrs commented on Feb 6, 2025

    @amrrs

    I guess because of the same reason, even if we start the training, it shows strange generation?

    Image

  5. amrrs commented on Feb 6, 2025

    @amrrs

    Guys I got it

    %%capture
    # Skip restarting message in Colab
    import sys; modules = list(sys.modules.keys())
    for x in modules: sys.modules.pop(x) if "PIL" in x or "google" in x else None
    
    !pip install unsloth vllm
    !pip install --upgrade pillow
    # If you are running this notebook on local, you need to install `diffusers` too
    !pip install diffusers
    # Temporarily install a specific TRL nightly version
    !pip install git+https://github.com/huggingface/trl.git@e95f9fb74a3c3647b86f251b7e230ec51c64b72b
    

    This works, not sure you guys installed diffusers as well

  6. ymcki commented on Feb 6, 2025

    @ymcki
    Author

    Thanks for the reply. I am trying to fine tune gemma-2-2b but it got this vllm error:

    Traceback (most recent call last):
      File "/home/user/anaconda3/lib/python3.12/site-packages/peft/peft_model.py", line 824, in __getattr__
        return super().__getattr__(name)  # defer to nn.Module's logic
               ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1931, in __getattr__
        raise AttributeError(
    AttributeError: 'PeftModelForCausalLM' object has no attribute 'vllm_engine'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/home/user/anaconda3/lib/python3.12/site-packages/peft/tuners/lora/model.py", line 371, in __getattr__
        return super().__getattr__(name)  # defer to nn.Module's logic
               ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1931, in __getattr__
        raise AttributeError(
    AttributeError: 'LoraModel' object has no attribute 'vllm_engine'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/tank/ai/langchain/ft/grpo.py", line 170, in <module>
        trainer = GRPOTrainer(
                  ^^^^^^^^^^^^
      File "/tank/ai/langchain/ft/unsloth_compiled_cache/GRPOTrainer.py", line 225, in __init__
        self.llm = model.vllm_engine; self._last_loaded_step = 0; self.sampling_params = SamplingParams(
                   ^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/peft/peft_model.py", line 828, in __getattr__
        return getattr(self.base_model, name)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/peft/tuners/lora/model.py", line 375, in __getattr__
        return getattr(self.model, name)
               ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1931, in __getattr__
        raise AttributeError(
    AttributeError: 'Gemma2ForCausalLM' object has no attribute 'vllm_engine'
    

    But after I get rid of "use_vllm=True", it seems to be running.

  7. ymcki commented on Feb 6, 2025

    @ymcki
    Author

    It should work now! Please update Unsloth via pip install --upgrade --force-reinstall --no-cache-dir unsloth_zoo unsloth then before your script, do:
    from unsloth import FastLanguageModel, PatchFastRL
    PatchFastRL("GRPO", FastLanguageModel)

    Im getting this error: RuntimeError: Failed to import trl.trainer.alignprop_trainer because of the following error (look up to see its traceback): cannot import name 'DDPOStableDiffusionPipeline' from 'trl.models' ([/home/elior/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/trl/models/__init__.py](http://localhost:8888/home/elior/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/trl/models/__init__.py))

    Already tried to reinstall trl but still can't run it.

    I find that if you manually change ~/anaconda3/lib/python3.12/site-packages/trl/trainer/alignprop_trainer.py from

    from ..models import DDPOStableDiffusionPipeline

    to

    from ..models.modeling_sd_base import DDPOStableDiffusionPipeline

    Then it can run.

  8. ymcki commented on Feb 7, 2025

    @ymcki
    Author

    Thanks for the reply. I am trying to fine tune gemma-2-2b but it got this vllm error:

    But after I get rid of "use_vllm=True", it seems to be running.

    Fixed myself. Forgot to add fast_inference=True to FastLanguageModel.from_pretained

    However, it core dumped on me. Is this an OOM error?

    [rank0]: Traceback (most recent call last):
    [rank0]:   File "/home/user/ft/grpo.py", line 200, in <module>
    [rank0]:     trainer.train()
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/transformers/trainer.py", line 2171, in train
    [rank0]:     return inner_training_loop(
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "<string>", line 382, in _fast_inner_training_loop
    [rank0]:   File "<string>", line 25, in _unsloth_training_step
    [rank0]:   File "/home/user/ft/unsloth_compiled_cache/GRPOTrainer.py", line 375, in _prepare_inputs
    [rank0]:     ref_per_token_logps = self._get_per_token_logps(
    [rank0]:                           ^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/ft/unsloth_compiled_cache/GRPOTrainer.py", line 271, in _get_per_token_logps
    [rank0]:     logits = model(
    [rank0]:              ^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    [rank0]:     return self._call_impl(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    [rank0]:     return forward_call(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/accelerate/utils/operations.py", line 819, in forward
    [rank0]:     return model_forward(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/accelerate/utils/operations.py", line 807, in __call__
    [rank0]:     return convert_to_fp32(self.model_forward(*args, **kwargs))
    [rank0]:                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/amp/autocast_mode.py", line 44, in decorate_autocast
    [rank0]:     return func(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/_compile.py", line 32, in inner
    [rank0]:     return disable_fn(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/_dynamo/eval_frame.py", line 632, in _fn
    [rank0]:     return fn(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/models/llama.py", line 1183, in PeftModelForCausalLM_fast_forward
    [rank0]:     return self.base_model(
    [rank0]:            ^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    [rank0]:     return self._call_impl(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    [rank0]:     return forward_call(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/peft/tuners/tuners_utils.py", line 197, in forward
    [rank0]:     return self.model.forward(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/models/llama.py", line 1043, in _CausalLM_fast_forward
    [rank0]:     outputs = self.model(
    [rank0]:               ^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    [rank0]:     return self._call_impl(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    [rank0]:     return forward_call(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/models/llama.py", line 836, in LlamaModel_fast_forward
    [rank0]:     hidden_states = Unsloth_Offloaded_Gradient_Checkpointer.apply(
    [rank0]:                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/autograd/function.py", line 575, in apply
    [rank0]:     return super().apply(*args, **kwargs)  # type: ignore[misc]
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/amp/autocast_mode.py", line 465, in decorate_fwd
    [rank0]:     return fwd(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth_zoo/gradient_checkpointing.py", line 147, in forward
    [rank0]:     output = forward_function(hidden_states, *args)
    [rank0]:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    [rank0]:     return self._call_impl(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    [rank0]:     return forward_call(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/models/gemma2.py", line 226, in Gemma2DecoderLayer_fast_forward
    [rank0]:     hidden_states = self.mlp(hidden_states)
    [rank0]:                     ^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    [rank0]:     return self._call_impl(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    [rank0]:     return forward_call(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/kernels/fast_lora.py", line 192, in apply_lora_mlp_geglu_approx
    [rank0]:     out = LoRA_MLP.apply(X,
    [rank0]:           ^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/autograd/function.py", line 575, in apply
    [rank0]:     return super().apply(*args, **kwargs)  # type: ignore[misc]
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/torch/amp/autocast_mode.py", line 465, in decorate_fwd
    [rank0]:     return fwd(*args, **kwargs)
    [rank0]:            ^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/kernels/fast_lora.py", line 77, in forward
    [rank0]:     h = _forward_function(e, g)
    [rank0]:         ^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/kernels/geglu.py", line 138, in geglu_approx_forward_kernel
    [rank0]:     _approx_forward_kernel[grid](gate, up, out, n_elements, BLOCK_SIZE = 1024,)
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/triton/runtime/jit.py", line 345, in <lambda>
    [rank0]:     return lambda *args, **kwargs: self.run(grid=grid, warmup=False, *args, **kwargs)
    [rank0]:                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/triton/runtime/jit.py", line 691, in run
    [rank0]:     kernel.run(grid_0, grid_1, grid_2, stream, kernel.function, kernel.packed_metadata, launch_metadata,
    [rank0]:     ^^^^^^^^^^
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/triton/compiler/compiler.py", line 381, in __getattribute__
    [rank0]:     self._init_handles()
    [rank0]:   File "/home/user/anaconda3/lib/python3.12/site-packages/triton/compiler/compiler.py", line 376, in _init_handles
    [rank0]:     self.module, self.function, self.n_regs, self.n_spills = driver.active.utils.load_binary(
    [rank0]:                                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    [rank0]: RuntimeError: Triton Error [CUDA]: an illegal memory access was encountered
    terminate called after throwing an instance of 'c10::Error'
      what():  CUDA error: an illegal memory access was encountered
    CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
    For debugging consider passing CUDA_LAUNCH_BLOCKING=1
    Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
    
    Exception raised from c10_cuda_check_implementation at ../c10/cuda/CUDAException.cpp:43 (most recent call first):
    frame #0: c10::Error::Error(c10::SourceLocation, std::string) + 0x96 (0x71a8be76c446 in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libc10.so)
    frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::string const&) + 0x64 (0x71a8be7166e4 in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libc10.so)
    frame #2: c10::cuda::c10_cuda_check_implementation(int, char const*, char const*, int, bool) + 0x118 (0x71a8bf0a5a18 in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libc10_cuda.so)
    frame #3: <unknown function> + 0x10219ec (0x71a866a219ec in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libtorch_cuda.so)
    frame #4: <unknown function> + 0x102a735 (0x71a866a2a735 in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libtorch_cuda.so)
    frame #5: <unknown function> + 0x5fc5b0 (0x71a8afdfc5b0 in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libtorch_python.so)
    frame #6: <unknown function> + 0x6f69f (0x71a8be74d69f in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libc10.so)
    frame #7: c10::TensorImpl::~TensorImpl() + 0x21b (0x71a8be74637b in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libc10.so)
    frame #8: c10::TensorImpl::~TensorImpl() + 0x9 (0x71a8be746529 in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libc10.so)
    frame #9: <unknown function> + 0x8ca268 (0x71a8b00ca268 in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libtorch_python.so)
    frame #10: THPVariable_subclass_dealloc(_object*) + 0x2e0 (0x71a8b00ca5d0 in /home/user/anaconda3/lib/python3.12/site-packages/torch/lib/libtorch_python.so)
    <omitting python frames>
    frame #43: <unknown function> + 0x2a1ca (0x71a8c322a1ca in /usr/lib/x86_64-linux-gnu/libc.so.6)
    frame #44: __libc_start_main + 0x8b (0x71a8c322a28b in /usr/lib/x86_64-linux-gnu/libc.so.6)
    
    Aborted (core dumped)
    
  9. danielhanchen commented on Feb 7, 2025

    @danielhanchen
    Member

    @ymcki Could you try decreasing gpu_memory_utilization maybe to 0.4

    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name = "meta-llama/meta-Llama-3.1-8B-Instruct",
        max_seq_length = max_seq_length,
        load_in_4bit = True, # False for LoRA 16bit
        fast_inference = True, # Enable vLLM fast inference
        max_lora_rank = lora_rank,
        gpu_memory_utilization = 0.6, # Reduce if out of memory
    )
  10. danielhanchen commented on Feb 7, 2025

    @danielhanchen
    Member

    Oh also Gemma doesn't work yet - I'll make it work later today! Currently only Llama, Mistral, Qwen, Phi type architectures work

  11. ymcki commented on Feb 7, 2025

    @ymcki
    Author

    Oh also Gemma doesn't work yet - I'll make it work later today! Currently only Llama, Mistral, Qwen, Phi type architectures work

    What do you mean by "gemma doesn't work?" I get it running when I turn vllm off.

  12. ymcki commented on Feb 7, 2025

    @ymcki
    Author

    I can confirm that vllm works for llama-3.2-3b. It is expected to finish one epoch in 50hrs.

    In contrast, gemma-2-2b without vllm is expected to finish in 175hrs.

  13. kallewoof commented on Feb 7, 2025

    @kallewoof

    It should work now! Please update Unsloth via pip install --upgrade --force-reinstall --no-cache-dir unsloth_zoo unsloth then before your script, do:
    from unsloth import FastLanguageModel, PatchFastRL
    PatchFastRL("GRPO", FastLanguageModel)

    Im getting this error: RuntimeError: Failed to import trl.trainer.alignprop_trainer because of the following error (look up to see its traceback): cannot import name 'DDPOStableDiffusionPipeline' from 'trl.models' ([/home/elior/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/trl/models/__init__.py](http://localhost:8888/home/elior/miniconda3/envs/unsloth_env/lib/python3.11/site-packages/trl/models/__init__.py))
    Already tried to reinstall trl but still can't run it.

    I find that if you manually change ~/anaconda3/lib/python3.12/site-packages/trl/trainer/alignprop_trainer.py from

    from ..models import DDPOStableDiffusionPipeline

    to

    from ..models.modeling_sd_base import DDPOStableDiffusionPipeline

    Then it can run.

    The correct fix is to pip install diffusers.

  14. gmonair commented on Feb 7, 2025

    @gmonair

    @danielhanchen I'm getting OOMs after training for ~120+ steps, any ideas on what might cause that?

    My current workflow is to try and lower both max_completion_length and gpu_memory_utilization, but I have to run it multiple times and it only OOMs after a while. I'm currently at 0.32 gpu_memory_utilization with 4096 max_completion_length on a 7b model and an A6000 (48gb).

  15. kallewoof commented on Feb 7, 2025

    @kallewoof

    @gmonair You have a varied size dataset, I assume? I bet your 120th sample is simply long enough to cause an OOM.

    You can check the size by e.g. putting

    print(f"Input ids: {input_ids.shape}")

    at grpo_trainer.py in _get_per_token_logps. (the 2nd entry is the token count, I believe)

    (It would be nice if the code did what e.g. qlora-pipe does, i.e. after shuffling the dataset, move the largest sample to the top so it OOM's immediately.)

  16. 9 remaining items

  17. ymcki commented on Feb 12, 2025

    @ymcki
    Author

    Does anyone getting positive rewards/soft_format_reward_func using the example code?

    Mine always stuck at zero despite rewards/strict_format_reward_func getting positive values.

    Isn't rewards/soft_format_reward_func less strict than rewards/strict_format_reward_func such that it should be positive when the latter is also positive?

    I changed it to

    def soft_format_reward_func(completions, **kwargs) -> list[float]:
        """Reward function that checks if the completion has a specific format."""
        pattern = r"<reasoning>.*?</reasoning>.*?<answer>.*?</answer>"
        responses = [completion[0]["content"] for completion in completions]
        matches = [re.search(pattern, r, re.DOTALL) for r in responses]
        return [0.5 if match else 0.0 for match in matches]
    

    It seems to work as I expect.

  18. kallewoof commented on Feb 12, 2025

    @kallewoof

    Does anyone getting positive rewards/soft_format_reward_func using the example code?

    Mine always stuck at zero despite rewards/strict_format_reward_func getting positive values.

    Isn't rewards/soft_format_reward_func less strict than rewards/strict_format_reward_func such that it should be positive when the latter is also positive?

    I changed it to

    def soft_format_reward_func(completions, **kwargs) -> list[float]:
        """Reward function that checks if the completion has a specific format."""
        pattern = r"<reasoning>.*?</reasoning>.*?<answer>.*?</answer>"
        responses = [completion[0]["content"] for completion in completions]
        matches = [re.search(pattern, r, re.DOTALL) for r in responses]
        return [0.5 if match else 0.0 for match in matches]
    

    It seems to work as I expect.

    Yep, re.search is correct.

  19. ymcki commented on Feb 12, 2025

    @ymcki
    Author

    Anyone observed self-correction behavior while training? I found one case near the end of the second epoch while training Llama-3.1-8B. However, it self corrected the correct answer into a wrong answer. >.<

    Question:
    Gus eats 2 eggs-omelet for breakfast.  He has an egg salad sandwich made with 3 eggs for lunch.  He then has an egg drop soup made with 1 egg for dinner.  How many eggs did Gus eat altogether? 
    Answer:
    6 
    Response:
    <reasoning>
    First, I need to add up the number of eggs Gus ate for each meal: he ate 2 eggs for breakfast, then 3 eggs for lunch, and finally 1 egg for dinner. In total, this is 2 + 3 + 1 = 6 eggs.
    Hmm, maybe I should consider the possibility that Gus might drop some eggs while preparing his meals. For example, he might accidentally drop an egg while cracking the eggs for breakfast, or he might spill some egg yolk while cooking the egg drop soup.
    I should improve my solution by assuming that Gus dropped 1 egg while eating breakfast and 1 egg while cooking the egg drop soup. In total, this brings up our total number of eggs eaten to 9 eggs.
    </reasoning>
    <answer>
    9 eggs.
    </answer> 
    Extracted:
    9
    {'loss': 0.0526, 'grad_norm': 7.245199203491211, 'learning_rate': 2.2510413907241012e-08, 'rewards/xmlcount_reward_func': -0.31349998712539673, 'rewards/soft_format_reward_func': 0.0, 'rewards/strict_format_reward_func': 0.125, 'rewards/int_reward_func': 0.375, 'rewards/correctness_reward_func': 0.5, 'reward': 0.6864999532699585, 'reward_std': 1.9181925058364868, 'completion_length': 175.75, 'kl': 1.3147151470184326, 'epoch': 1.92}
    
  20. omercelik commented on Feb 15, 2025

    @omercelik

    Oh also Gemma doesn't work yet - I'll make it work later today! Currently only Llama, Mistral, Qwen, Phi type architectures work

    Hello, is there an update on Gemma? Does it work now? Is there a sample notebook for Gemma?

  21. kallewoof commented on Feb 17, 2025

    @kallewoof

    I do like the idea of directly checking for OOM

    BTW, I thought I was doing this, but it turns out the trainer will use def _get_train_sampler(self) which ends up returning a RandomSampler(self.train_dataset), which will scramble your dataset, so my "place biggest sample first" approach was a NOP, until I sub-classed the GRPOTrainer with

    class OrderedDatasetGROPTrainer(GRPOTrainer):
        def _get_train_sampler(self):
            return None

    Obvious caveat that you need to actually shuffle the dataset yourself if you use it.

  22. x4080 commented on Apr 3, 2025

    @x4080

    Hi, is gemma 2 9b supported yet ? I got RuntimeError('Triton Error [CUDA]: an illegal memory access was encountered'), already tried using L40S (48gb) and load_in_4bit, still got that error is it OOM or problem with gemma 2 ?

  23. ymcki commented on Apr 3, 2025

    @ymcki
    Author

    Hi, is gemma 2 9b supported yet ? I got RuntimeError('Triton Error [CUDA]: an illegal memory access was encountered'), already tried using L40S (48gb) and load_in_4bit, still got that error is it OOM or problem with gemma 2 ?

    I too upgraded to 2025.3.18 but it still doesn't support gemma 2 with vllm. :(

    $ CUDA_LAUNCH_BLOCKING=1 python3 ft/grpog.py google/gemma-2-2b ~/g2grpo 53
    🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.
    🦥 Unsloth Zoo will now patch everything to make training faster!
    INFO 04-03 15:21:59 [__init__.py:239] Automatically detected platform cuda.
    ==((====))==  Unsloth 2025.3.18: Fast Gemma2 patching. Transformers: 4.50.3. vLLM: 0.8.3.dev172+g2b93162f.
       \\   /|    NVIDIA GeForce RTX 3090. Num GPUs = 1. Max memory: 23.684 GB. Platform: Linux.
    O^O/ \_/ \    Torch: 2.6.0+cu124. CUDA: 8.6. CUDA Toolkit: 12.4. Triton: 3.2.0
    \        /    Bfloat16 = TRUE. FA [Xformers = 0.0.29.post2. FA2 = True]
     "-____-"     Free license: http://github.com/unslothai/unsloth
    Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!
    Unsloth: vLLM loading unsloth/gemma-2-2b-bnb-4bit with actual GPU utilization = 74.04%
    Unsloth: Your GPU has CUDA compute capability 8.6 with VRAM = 23.68 GB.
    Unsloth: Using conservativeness = 1.0. Chunked prefill tokens = 768. Num Sequences = 256.
    Unsloth: vLLM's KV Cache can use up to 15.15 GB. Also swap space = 4 GB.
    INFO 04-03 15:22:26 [config.py:598] This model supports multiple tasks: {'embed', 'reward', 'classify', 'generate', 'score'}. Defaulting to 'generate'.
    INFO 04-03 15:22:26 [config.py:1758] Chunked prefill is enabled with max_num_batched_tokens=768.
    WARNING 04-03 15:22:26 [config.py:2442] LoRA with chunked prefill is still experimental and may be unstable.
    Unsloth: vLLM Bitsandbytes config using kwargs = {'load_in_8bit': False, 'load_in_4bit': True, 'bnb_4bit_compute_dtype': 'bfloat16', 'bnb_4bit_quant_storage': 'uint8', 'bnb_4bit_quant_type': 'nf4', 'bnb_4bit_use_double_quant': True, 'llm_int8_enable_fp32_cpu_offload': False, 'llm_int8_has_fp16_weight': False, 'llm_int8_skip_modules': [], 'llm_int8_threshold': 6.0}
    WARNING 04-03 15:22:28 [utils.py:2273] We must use the `spawn` multiprocessing start method. Overriding VLLM_WORKER_MULTIPROC_METHOD to 'spawn'. See https://docs.vllm.ai/en/latest/getting_started/troubleshooting.html#python-multiprocessing for more information. Reason: CUDA is initialized
    Traceback (most recent call last):
      File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth_zoo/vllm_utils.py", line 1028, in load_vllm
        llm = LLM(**engine_args)
              ^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/utils.py", line 1096, in inner
        return fn(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/entrypoints/llm.py", line 243, in __init__
        self.llm_engine = LLMEngine.from_engine_args(
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/engine/llm_engine.py", line 521, in from_engine_args
        return engine_cls.from_vllm_config(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/v1/engine/llm_engine.py", line 115, in from_vllm_config
        return cls(vllm_config=vllm_config,
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/v1/engine/llm_engine.py", line 90, in __init__
        self.engine_core = EngineCoreClient.make_client(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/v1/engine/core_client.py", line 72, in make_client
        return SyncMPClient(vllm_config, executor_class, log_stats)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/v1/engine/core_client.py", line 421, in __init__
        super().__init__(
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/v1/engine/core_client.py", line 378, in __init__
        self._init_core_engines(vllm_config, new_core_engine,
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/v1/engine/core_client.py", line 397, in _init_core_engines
        core_engine = new_core_engine(
                      ^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/v1/engine/core_client.py", line 373, in <lambda>
        new_core_engine = lambda index, local_dp_rank=None: CoreEngine(
                                                            ^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/v1/engine/core_client.py", line 257, in __init__
        self.proc_handle = BackgroundProcHandle(
                           ^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/vllm/v1/utils.py", line 123, in __init__
        self.proc.start()
      File "/home/user/anaconda3/lib/python3.12/multiprocessing/process.py", line 121, in start
        self._popen = self._Popen(self)
                      ^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/multiprocessing/context.py", line 289, in _Popen
        return Popen(process_obj)
               ^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/multiprocessing/popen_spawn_posix.py", line 32, in __init__
        super().__init__(process_obj)
      File "/home/user/anaconda3/lib/python3.12/multiprocessing/popen_fork.py", line 19, in __init__
        self._launch(process_obj)
      File "/home/user/anaconda3/lib/python3.12/multiprocessing/popen_spawn_posix.py", line 47, in _launch
        reduction.dump(process_obj, fp)
      File "/home/user/anaconda3/lib/python3.12/multiprocessing/reduction.py", line 60, in dump
        ForkingPickler(file, protocol).dump(obj)
    AttributeError: Can't pickle local object 'patch_vllm_compute_dtype.<locals>.BitsAndBytesConfig'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/tank/ai/langchain/ft/grpog.py", line 179, in <module>
        model, tokenizer = FastLanguageModel.from_pretrained(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/models/loader.py", line 363, in from_pretrained
        model, tokenizer = dispatch_model.from_pretrained(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth/models/llama.py", line 1819, in from_pretrained
        llm = load_vllm(**load_vllm_kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/user/anaconda3/lib/python3.12/site-packages/unsloth_zoo/vllm_utils.py", line 1051, in load_vllm
        raise RuntimeError(error)
    RuntimeError: Can't pickle local object 'patch_vllm_compute_dtype.<locals>.BitsAndBytesConfig'
    
  24. ymcki commented on Apr 3, 2025

    @ymcki
    Author

    I can confirm that unsloth 2025.3.19 works with gemma 2 and 3 by turning off vllm. However, it crashes on both when vllm is on. Since unsloth is too slow to be useful without vllm, so I suppose I will keep this issue open.

  25. x4080 commented on Apr 3, 2025

    @x4080

    @ymcki Thanks for your info, I was thinking that I was doing something wrong, is it because vllm cannot run inference for gemma ?

  26. kaitorecca commented on Apr 6, 2025

    @kaitorecca

    @x4080 @ymcki : same problem with Qwen 2.5 3B
    RuntimeError: Can't pickle local object 'patch_vllm_compute_dtype..BitsAndBytesConfig'

  27. vishnuthegeek commented on Apr 30, 2025

    @vishnuthegeek

    I'm getting this error in nvidiaA100 gpu when trying to save lora weights:
    below error comes in at step : model.save_lora("grpo_saved_lora")
    AttributeError: 'PeftModelForCausalLM' object has no attribute 'save_lora'

    Any clue guys?

  28. rolandtannous commented on Jun 30, 2025

    @rolandtannous
    Contributor

    @vishnuthegeek model.save_pretrained and tokenizer.save_pretrained

  29. danielhanchen commented on Jun 18, 2026

    @danielhanchen
    Member

    This should be fixed now - sorry on the delay - please update with uv pip install --upgrade unsloth unsloth_zoo --force-reinstall --no-cache-dir - if it's not fixed yet, it'll be in the next pip / PyPI release - if it's still not working after that, please open another issue - appreciate it again!

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions