视觉语言模型强化学习中的奖励修正与GSPO

Unsloth可在免费Colab T4上以GSPO或GRPO训练Qwen3-VL-8B等视觉语言模型。针对Qwen2.5-VL推理出现addCriterion等乱码,可修改奖励函数惩罚该词与换行;GSPO把重要性权重放在序列似然而非逐token似然。

解丝现在支持视觉/多模式 RL 与 [Quen3 - VL (英语).](<https://unsloth.ai/docs/models/tutorials/qwen3-how-to-run-and-fine-tune/qwen3-vl-how-to-run-and-fine-tune>), [Gemma 3 (英语).](<https://unsloth.ai/docs/models/tutorials/gemma-3-how-to-run-and-fine-tune>) 由于Unsloth的独特性 [体重共享](<https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide#what-unsloth-offers-for-rl>) 和自定义内核, Unsloth 制作VLMRL **1.5 – 2 – 更快的,** 用途 **90% 减 VRAM**,并启用 **15 × 更长的上下文** 长度比 FA2 设置长, 没有准确度损失。 此更新还引入了 Quen 的 [GSPO](<https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/vision-reinforcement-learning-vlm-rl#gspo-rl>) 算法。 Unsloth可以使用GSPO / GRPO在免费的Colab T4 GPU 上训练Qwen3-VL-8B. 其他VLM同样工作,但可能需要更大的GPU. Gemma需要比T4更新的GPU,因为vLLM [限制为 Bfloat16](<https://unsloth.ai/docs/models/tutorials/gemma-3-how-to-run-and-fine-tune#unsloth-fine-tuning-fixes>)因此,我们建议在Colab上使用NVIDIA L4。我们的笔记本解决了涉及图像和图表的数字数学问题: - **Qwen-3 VL-8B** (vLLM推论).**:** [科尔布](<https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_VL_(8B)-Vision-GRPO.ipynb>) - **Qwen-2.5 VL-7B** (vLLM推论).**:** [科尔布](<https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen2_5_7B_VL_GRPO.ipynb>) •[卡格勒语Name](<https://www.kaggle.com/notebooks/welcome?src=https://github.com/unslothai/notebooks/blob/main/nb/Kaggle-Qwen2_5_7B_VL_GRPO.ipynb&accelerator=nvidiaTeslaT4>) - **Gemma-3-4B** (不思议推论): [科尔布](<https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Gemma3_(4B)-Vision-GRPO.ipynb>) 我们还在本地将vLLM VLM 集成到 Unsloth 中,所以你只需要使用 vLLM 推论就可以了. `fast_inference=True` 初始化模式时的旗帜。特别感谢 [锡努埃GAD](<https://github.com/unslothai/unsloth/pull/2752>) 用于提供 [第一笔记](<https://github.com/GAD-cell/vlm-grpo/blob/main/examples/VLM_GRPO_basic_example.ipynb>) 这让整合VLM RL更容易!. 这种VLM支持还整合了我们最新的更新,以更高效的内存+更快的RL,包括我们 [备用功能](<https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/memory-efficient-rl#unsloth-standby>),这与其他执行相比,对速度下降有着独特的限制。 您只能使用( E) `fast_inference` 对于 vLLM 支持的 VLM 。 有些模型, 如 Llama 3.2 Vision , 因此只能在没有 vLLM 的情况下运行, 但它们仍然在 Unsloth 工作 。 ``` os.environ['UNSLOTH_VLLM_STANDBY'] = '1' # To enable memory efficient GRPO with vLLM model, tokenizer = FastVisionModel.from_pretrained( model_name = "Qwen/Qwen2.5-VL-7B-Instruct", max_seq_length = 16384, #Must be this large to fit image in context load_in_4bit = True, # False for LoRA 16bit fast_inference = True, # Enable vLLM fast inference gpu_memory_utilization = 0.8, # Reduce if out of memory ) ``` 还必须指出,vLLM不支持LoRA的视觉/编码层,因此设定了 `finetune_vision_layers = False` 装入 LoRA 适配器时。但是,如果您通过变压器/ Unsloth 进行推导, CAN 也会对视觉层进行训练。 ``` # Add LoRA adapter to the model for parameter efficient fine tuning model = FastVisionModel.get_peft_model( model, finetune_vision_layers = False,# fast_inference doesn't support finetune_vision_layers yet :( finetune_language_layers = True, # False if not finetuning language layers finetune_attention_modules = True, # False if not finetuning attention layers finetune_mlp_modules = True, # False if not finetuning MLP layers r = lora_rank, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128 lora_alpha = lora_rank*2, # *2 speeds up training use_gradient_checkpointing = "unsloth", # Reduces memory usage random_state = 3407, ) ``` ## Qwen 2.5 VL 愿景 RL 问题和Quirks 在RL用于Qwen 2.5的VL期间,你可能会看到以下推论输出: 这是 [报告](<https://github.com/QwenLM/Qwen2.5-VL/issues/759>) 以及 Qwen2 5 - VL-7B - 指示输出出乎意料的结果 " addCriterion " 。 事实上,我们也看到了这一点! 我们尝试了非 Unsloth、 bfloat16 和 flob16 的机器和其他东西,但是它仍然出现。 例如 165 ie 项目 `train_dataset[165]` 从 [AI4Math/MathVista](<https://huggingface.co/datasets/AI4Math/MathVista>) 数据集如下: ![](<https://unsloth.ai/docs/~gitbook/image?url=https%3A%2F%2F3215535692-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FxhOjnexMCB3dmuQFQ2Zq%252Fuploads%252Fgit-blob-61a659529171fcc10ed6398a15912b21d6b1a076%252FUntitled.png%3Falt%3Dmedia&width=768&dpr=3&quality=100&sign=15ecf4a3e929b88e24fed9eb82bcc3a6&sv=3>) 然后我们得到上面的bber输出。人们可以添加一个奖励功能来惩罚添加addCriterion,或者惩罚bber输出。但是,另一种方法是对其进行更长的训练。例如,在60步骤发生后,我们才看到模型通过RL实际学习: ![](<https://unsloth.ai/docs/~gitbook/image?url=https%3A%2F%2F3215535692-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FxhOjnexMCB3dmuQFQ2Zq%252Fuploads%252Fgit-blob-5f34f66f0ac6508fd28343b16592c59b889ec5ca%252Fimage.webp%3Falt%3Dmedia&width=768&dpr=3&quality=100&sign=9bf926afc432c6fb9399826ab6cddc37&sv=3>) 强制 `<|assistant|>` 但是,正如下一节所述,最好还是增加一个奖励功能来惩罚坏一代。 ## 减少胡言乱语的奖励函数 惩罚 `addCriterion` 我们编辑了奖赏功能 以惩罚太多的 `addCriterion` 还有新线条 ## GSPO 强化学习 此外,该更新还增加了GSPO([组序列政策优化](<https://arxiv.org/abs/2507.18071>)),是Alibaba的Quen团队制作的GRPO的一个变体. 他们注意到GRPO默认会给每个令牌带来重要性的权重,尽管明确的好处并不随每个令牌大小大小或变化. 这导致了GSPO的创建,现在它赋予了对序列概率的重视,而不是对符的单个符号概率的重视. 这两个算法的区别可以从下文中看出,从Quen和Alibaba的GSPO论文中都可以看出: ![](<https://unsloth.ai/docs/~gitbook/image?url=https%3A%2F%2F3215535692-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FxhOjnexMCB3dmuQFQ2Zq%252Fuploads%252Fgit-blob-45d743dd5dcd590626777ce09cfab61808aa8c24%252Fimage.png%3Falt%3Dmedia&width=768&dpr=3&quality=100&sign=7cbe2a19911c4f2aa6122e0ec2b4d696&sv=3>)GRPO 算法,来源: [文选](<https://arxiv.org/abs/2507.18071>) ![](<https://unsloth.ai/docs/~gitbook/image?url=https%3A%2F%2F3215535692-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FxhOjnexMCB3dmuQFQ2Zq%252Fuploads%252Fgit-blob-ee755850cbe17482ce240dde227d55c62e9a3e64%252Fimage.png%3Falt%3Dmedia&width=768&dpr=3&quality=100&sign=993d4564ff42ccb1d8fd204671c71e4b&sv=3>)GSPO算法,来源: [文选](<https://arxiv.org/abs/2507.18071>) 在方程式 1 中,可以看到在加亮器之前,每个行的优点被加亮到符号日志中。基本上,每个符号都得到同样的缩放,尽管缩放是给整个序列而不是给每个单个的符号的。下面可以看到一个简单的图表: ![](<https://unsloth.ai/docs/~gitbook/image?url=https%3A%2F%2F3215535692-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FxhOjnexMCB3dmuQFQ2Zq%252Fuploads%252Fgit-blob-b3c944808a15dde0a7ff45782f9f074993304bf1%252FCopy%2520of%2520GSPO%2520diagram%2520%281%29.jpg%3Falt%3Dmedia&width=768&dpr=3&quality=100&sign=9bd5ff0634d760603b8c4f0dd743338d&sv=3>)GRPO Logprob 比例行智能化,并带有优点 Equation 2显示,每个序列的对数比是在Logprob比率计算后进行汇总和推算的,只有现在产生的序列比才得到行的明智乘以优点. ![](<https://unsloth.ai/docs/~gitbook/image?url=https%3A%2F%2F3215535692-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FxhOjnexMCB3dmuQFQ2Zq%252Fuploads%252Fgit-blob-62fc5b50921e79cce155d2794201c9b96faf941e%252FGSPO%2520diagram%2520%281%29.jpg%3Falt%3Dmedia&width=768&dpr=3&quality=100&sign=849c131ad5be2e813dd116959ab77e54&sv=3>)GSPO 序列比 明智的行 启用 GSPO 很简单,你只需要设置 `importance_sampling_level = "sequence"` 在 GRPO 配置中的旗帜。 总体来说,Unsloth现在与VLM vLLM快速推论同时使得90%既能降低内存使用,又能与1.5 -2x快速使用GRPO和GSPO! 如果你想阅读更多关于强化学习的内容,请查看RL指南: [强化学习](<https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide>) ***作者数 :****非常感谢* [*凯斯*](<https://www.linkedin.com/in/keith-truongcao-7bb84a23b/>) *财务报告和财务报告* [*达塔*](<https://www.linkedin.com/in/datta0/>) *对这篇文章的贡献!* [前7x 更长的上下文 RL](<https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/grpo-long-context>)[NextFP8 RL](<https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/fp8-reinforcement-learning>) 上次更新的 11 几个月前 这有帮助吗?

查看原始来源