> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide/advanced-rl-documentation/gspo-reinforcement-learning.md).

# GSPO強化学習

UnslothでGSPO（Group Sequence Policy Optimization）RLを使って学習します。

私たちは、〜の変種であるGSPOを紹介します [GRPO](https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide.md#from-rlhf-ppo-to-grpo-and-rlvr) AlibabaのQwenチームによって作られました。彼らは、GRPOが各トークンに重要度重みを適用する一方で、本質的にはアドバンテージは各トークンごとにスケールも変化もしない、という点に気づきました。これがGSPOの誕生につながり、GSPOでは現在、個々のトークンの尤度ではなく、系列の尤度に重要度を割り当てます。

* 以下の用途に、無料のGSPOノートブックをご利用ください： [**gpt-oss-20b**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/gpt-oss-\(20B\)-GRPO.ipynb) そして [**Qwen2.5-VL**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen2_5_7B_VL_GRPO.ipynb)

次の設定を行うことで、UnslothでGSPOを有効にします `importance_sampling_level = "sequence"` GRPOの設定内で。これら2つのアルゴリズムの違いは以下で確認できます。どちらもQwenとAlibabaによるGSPO論文に基づいています：

<figure><img src="https://735611837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-45d743dd5dcd590626777ce09cfab61808aa8c24%2Fimage.png?alt=media" alt="" width="563"><figcaption><p>GRPO アルゴリズム、出典： <a href="https://arxiv.org/abs/2507.18071">Qwen</a></p></figcaption></figure>

<figure><img src="https://735611837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-ee755850cbe17482ce240dde227d55c62e9a3e64%2Fimage.png?alt=media" alt="" width="563"><figcaption><p>GSPO アルゴリズム、出典： <a href="https://arxiv.org/abs/2507.18071">Qwen</a></p></figcaption></figure>

式1では、advantage がテンソルが合計される前に各行をトークンの logprob にスケールしていることが分かります。本質的には、そのスケーリングは各トークンではなくシーケンス全体に与えられたにもかかわらず、各トークンに同じスケーリングが適用されています。これを示す簡単な図を以下に示します：

<figure><img src="https://735611837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-b3c944808a15dde0a7ff45782f9f074993304bf1%2FCopy%20of%20GSPO%20diagram%20(1).jpg?alt=media" alt="" width="286"><figcaption><p>GRPO の Logprob 比率を advantage で行ごとにスケーリング</p></figcaption></figure>

式2では、各シーケンスの logprob 比率が、Logprob 比率を計算した後に合計されて指数化され、その結果得られたシーケンス比率だけが行ごとに advantage で乗算されることが示されています。

<figure><img src="https://735611837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-62fc5b50921e79cce155d2794201c9b96faf941e%2FGSPO%20diagram%20(1).jpg?alt=media" alt="" width="313"><figcaption><p>GSPO のシーケンス比率を advantage で行ごとにスケーリング</p></figcaption></figure>

GSPO の有効化は簡単で、必要なのは `importance_sampling_level = "sequence"` フラグを GRPO 設定で指定するだけです。

```python
training_args = GRPOConfig(
    output_dir = "vlm-grpo-unsloth",
    per_device_train_batch_size = 8,
    gradient_accumulation_steps = 4,
    learning_rate = 5e-6,
    adam_beta1 = 0.9,
    adam_beta2 = 0.99,
    weight_decay = 0.1,
    warmup_ratio = 0.1,
    lr_scheduler_type = "cosine",
    optim = "adamw_8bit",
    # beta = 0.00,
    epsilon = 3e-4,
    epsilon_high = 4e-4,
    num_generations = 8,    
    max_prompt_length = 1024,
    max_completion_length = 1024,
    log_completions = False,
    max_grad_norm = 0.1,
    temperature = 0.9,
    # report_to = "none", # Weights & Biases にログを取りたい場合は "wandb" に設定
    num_train_epochs = 2, # 簡単なテスト実行用。本学習では増やしてください
    
    # GSPO は以下：
    importance_sampling_level = "sequence",
    
    # Dr GRPO / GAPO など
    loss_type = "dr_grpo",
)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide/advanced-rl-documentation/gspo-reinforcement-learning.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
