> 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/de/loslegen/reinforcement-learning-rl-guide/advanced-rl-documentation/gspo-reinforcement-learning.md).

# GSPO-Reinforcement-Learning

Trainiere mit GSPO (Group Sequence Policy Optimization) RL in Unsloth.

Wir stellen GSPO vor, eine Variante von [GRPO](/docs/de/loslegen/reinforcement-learning-rl-guide.md#from-rlhf-ppo-to-grpo-and-rlvr) vom Qwen-Team bei Alibaba entwickelt. Sie stellten fest, dass GRPO für jedes Token Wichtigkeitsgewichte verwendet, obwohl sich die Vorteile von Natur aus nicht mit jedem Token skalieren oder verändern. Dies führte zur Entwicklung von GSPO, das nun die Wichtigkeit auf die Sequenzwahrscheinlichkeit statt auf die einzelnen Token-Wahrscheinlichkeiten der Tokens legt.

* Verwenden Sie unsere kostenlosen GSPO-Notebooks für: [**gpt-oss-20b**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/gpt-oss-\(20B\)-GRPO.ipynb) und [**Qwen2.5-VL**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen2_5_7B_VL_GRPO.ipynb)

Aktivieren Sie GSPO in Unsloth, indem Sie `importance_sampling_level = "sequence"` in der GRPO-Konfiguration festlegen. Der Unterschied zwischen diesen beiden Algorithmen ist unten zu sehen, beide aus dem GSPO-Paper von Qwen und Alibaba:

<figure><img src="https://797013937-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-Algorithmus, Quelle: <a href="https://arxiv.org/abs/2507.18071">Qwen</a></p></figcaption></figure>

<figure><img src="https://797013937-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-Algorithmus, Quelle: <a href="https://arxiv.org/abs/2507.18071">Qwen</a></p></figcaption></figure>

In Gleichung 1 ist zu sehen, dass die Advantages jede der Zeilen in die Token-Logprobs skalieren, bevor dieser Tensor summiert wird. Im Wesentlichen erhält jedes Token dieselbe Skalierung, obwohl diese Skalierung für die gesamte Sequenz und nicht für jedes einzelne Token vergeben wurde. Ein einfaches Diagramm dazu ist unten zu sehen:

<figure><img src="https://797013937-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-Verhältnis zeilenweise mit Advantages skaliert</p></figcaption></figure>

Gleichung 2 zeigt, dass die Logprob-Verhältnisse für jede Sequenz summiert und exponentiert werden, nachdem die Logprob-Verhältnisse berechnet wurden, und erst die daraus resultierenden Sequenzverhältnisse anschließend zeilenweise mit den Advantages multipliziert werden.

<figure><img src="https://797013937-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-Sequenzverhältnis zeilenweise mit Advantages skaliert</p></figcaption></figure>

GSPO zu aktivieren ist einfach; alles, was Sie tun müssen, ist den `importance_sampling_level = "sequence"` Schalter in der GRPO-Konfiguration zu setzen.

```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", # Auf "wandb" setzen, wenn Sie nach Weights & Biases protokollieren möchten
    num_train_epochs = 2, # Für einen schnellen Testlauf, für vollständiges Training erhöhen
    
    # GSPO ist unten:
    importance_sampling_level = "sequence",
    
    # Dr GRPO / GAPO usw.
    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/de/loslegen/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.
