You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] Use GRPO to train Qwen3 model failed when vllm is enabled #2610
Use GRPO to train Qwen3 model failed:
RuntimeError: Inference tensors cannot be saved for backward. To work around you can make a clone to get a normal tensor and use it in autograd.
I can confirm the issue disappears when vllm is disabled.
@vinceamaz, I'm looking at #2434, and the recommended solution is
Use the latest version of Unsloth and vLLM
2025.4.7 is definitely not the latest version of unsloth. You'll need to upgrade unsloth, unsloth_zoo and vllm.
unsloth==2025.5.7
unsloth-zoo==2025.5.8
vllm==0.8.5.post1
I've upgraded the packages to
unsloth==2025.5.9
unsloth-zoo==2025.5.11
vllm==0.8.5.post1
With 'fast_inference' enabled in 'FastLanguageModel.from_pretrained()', I get error below:
[rank0]: File ".../model.py", line 32, in load_model_and_tokenizer
[rank0]: model, tokenizer = FastLanguageModel.from_pretrained(
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth/models/loader.py", line 376, in from_pretrained
[rank0]: model, tokenizer = dispatch_model.from_pretrained(
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth/models/qwen3.py", line 419, in from_pretrained
[rank0]: return FastLlamaModel.from_pretrained(
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth/models/llama.py", line 1826, in from_pretrained
[rank0]: llm = load_vllm(**load_vllm_kwargs)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth_zoo/vllm_utils.py", line 1208, in load_vllm
[rank0]: raise RuntimeError(error)
[rank0]: Duplicate layer name: model.layers.0.self_attn.attn
[rank0]:[W603 09:00:00.681909200 ProcessGroupNCCL.cpp:1496] Warning: WARNING: destroy_process_group() was not called before program exit, which can leak resources. For more info, please see https://pytorch.org/docs/stable/distributed.html#shutdown (function operator())
With 'fast_inference' disabled in 'FastLanguageModel.from_pretrained()', but 'use_vllm' enabled in 'GRPOConfig()', I get error below:
File ".../trainer.py", line 167, in train_rl_fn
trainer = GRPOTrainer(
^^^^^^^^^^^^
File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth/trainer.py", line 210, in new_init
original_init(self, *args, **kwargs)
File ".../projects/sft_grpo_math/src/unsloth_compiled_cache/UnslothGRPOTrainer.py", line 2283, in init
super().init(
File ".../projects/sft_grpo_math/src/unsloth_compiled_cache/UnslothGRPOTrainer.py", line 1226, in init
self.llm = model.vllm_engine
^^^^^^^^^^^^^^^^^
File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/peft/peft_model.py", line 797, in getattr
return getattr(self.base_model, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/peft/tuners/lora/model.py", line 363, in getattr
return getattr(self.model, name)
^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1928, in getattr
raise AttributeError(
'Qwen3ForCausalLM' object has no attribute 'vllm_engine'
With both 'fast_inference' and 'use_vllm' disabled, it runs with no error but some warnings:
UserWarning: An output with one or more elements was resized since it had shape [1, 4, 2560], which does not match the required output shape [4, 1, 2560]. This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0). (Triggered internally at /pytorch/aten/src/ATen/native/Resize.cpp:30.)
We recently updated unsloth and we verified that qwen3 grpo is working. If you're still having trouble locally you can do a !pip freeze in the colab env and cross reference which packages might be out of date. Then upgrade the package and try again. Look out for torch transformers trl peft bitsandbytes vllm triton.
Activity
mmathew23 commentedon May 24, 2025
@vinceamaz, I'm looking at #2434, and the recommended solution is
Use the latest version of Unsloth and vLLM2025.4.7 is definitely not the latest version of unsloth. You'll need to upgrade unsloth, unsloth_zoo and vllm.
unsloth==2025.5.7
unsloth-zoo==2025.5.8
vllm==0.8.5.post1
vinceamaz commentedon Jun 3, 2025
Hi @mmathew23 thanks for your reply.
I've upgraded the packages to
unsloth==2025.5.9
unsloth-zoo==2025.5.11
vllm==0.8.5.post1
[rank0]: File ".../model.py", line 32, in load_model_and_tokenizer
[rank0]: model, tokenizer = FastLanguageModel.from_pretrained(
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth/models/loader.py", line 376, in from_pretrained
[rank0]: model, tokenizer = dispatch_model.from_pretrained(
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth/models/qwen3.py", line 419, in from_pretrained
[rank0]: return FastLlamaModel.from_pretrained(
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth/models/llama.py", line 1826, in from_pretrained
[rank0]: llm = load_vllm(**load_vllm_kwargs)
[rank0]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth_zoo/vllm_utils.py", line 1208, in load_vllm
[rank0]: raise RuntimeError(error)
[rank0]: Duplicate layer name: model.layers.0.self_attn.attn
[rank0]:[W603 09:00:00.681909200 ProcessGroupNCCL.cpp:1496] Warning: WARNING: destroy_process_group() was not called before program exit, which can leak resources. For more info, please see https://pytorch.org/docs/stable/distributed.html#shutdown (function operator())
File ".../trainer.py", line 167, in train_rl_fn
trainer = GRPOTrainer(
^^^^^^^^^^^^
File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/unsloth/trainer.py", line 210, in new_init
original_init(self, *args, **kwargs)
File ".../projects/sft_grpo_math/src/unsloth_compiled_cache/UnslothGRPOTrainer.py", line 2283, in init
super().init(
File ".../projects/sft_grpo_math/src/unsloth_compiled_cache/UnslothGRPOTrainer.py", line 1226, in init
self.llm = model.vllm_engine
^^^^^^^^^^^^^^^^^
File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/peft/peft_model.py", line 797, in getattr
return getattr(self.base_model, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/peft/tuners/lora/model.py", line 363, in getattr
return getattr(self.model, name)
^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../miniconda3/envs/llm_rf/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1928, in getattr
raise AttributeError(
'Qwen3ForCausalLM' object has no attribute 'vllm_engine'
UserWarning: An output with one or more elements was resized since it had shape [1, 4, 2560], which does not match the required output shape [4, 1, 2560]. This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0). (Triggered internally at /pytorch/aten/src/ATen/native/Resize.cpp:30.)
mmathew23 commentedon Jun 6, 2025
Hi @vinceamaz could you try again with this colab notebook?
https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_(4B)-GRPO.ipynb
We recently updated unsloth and we verified that qwen3 grpo is working. If you're still having trouble locally you can do a
!pip freezein the colab env and cross reference which packages might be out of date. Then upgrade the package and try again. Look out fortorch transformers trl peft bitsandbytes vllm triton.github-actions commentedon Jul 1, 2025
Is this issue still important to you? Apologies in advance we might have missed this issue as well. For faster response times, please post on our Reddit server - https://www.reddit.com/r/unsloth or our Discord - https://discord.com/invite/unsloth
GRPO rollout scope: off for async tools that call vLLM; leak probe ch…