> 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/moderu/gpt-oss-how-to-run-and-fine-tune/gpt-oss-reinforcement-learning/tutorial-how-to-train-gpt-oss-with-rl.md). Agents can also search and read this documentation through its [MCP server](https://unsloth.ai/docs/~gitbook/mcp).

# チュートリアル: RLでgpt-ossを学習する方法

OpenAI gpt-ossをGRPOで学習し、ローカルまたはColabで2048を自律的に打ち負かす方法を学びましょう。

LLMは、複雑な環境を伴うタスクにしばしば苦戦します。しかし、 [強化学習](https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide.md) （RL）を適用し、カスタムの [報酬関数](https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide.md#reward-functions-verifiers)を設計することで、これらの課題は克服できます。

RLは、自動カーネルや戦略の作成などのタスクに適用できます。このチュートリアルでは、 **gpt-oss** を [**GRPO**](https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide.md#from-rlhf-ppo-to-grpo-and-rlvr) とUnslothを使って2048に自律的に勝つ方法を紹介します。

| [2048ノートブック](https://colab.research.google.com/github/openai/gpt-oss/blob/main/examples/reinforcement-fine-tuning.ipynb) （OpenAI公式の例） | [カーネル生成ノートブック](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/gpt-oss-\(20B\)-GRPO.ipynb) |
| ------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |

**作成するもの：**

* gpt-oss-20bを学習し、モデルが2048で自動的に勝てるようにする
* モデルが対話できる最小限の2048環境を作成する
* 定義する **報酬関数** は次を行う：
  1. 生成された戦略がコンパイルでき、実行できることを確認し、
  2. 報酬ハッキングを防ぎ（外部インポートを禁止し）、さらに
  3. 実際のゲーム成功に報酬を与える
* 推論を実行し、モデルをエクスポートする（MXFP4 4ビットまたは統合済みFP16）

{% hint style="info" %}
**ハードウェア：** 2048の例は無料のColab T4で動作しますが、学習は遅くなります。A100/H100ならはるかに高速です。4ビット読み込み + LoRAを使えば、20Bモデルを控えめなVRAMに収められます。
{% endhint %}

{% stepper %}
{% step %}

#### Unslothをインストール

ノートブックの先頭でこのセルを実行してください（Colabで動作します）。

```bash
!pip install --upgrade -qqq uv
try: import numpy; get_numpy = f"numpy=={numpy.__version__}"
except: get_numpy = "numpy"
!uv pip install -qqq \
    "torch>=2.8.0" "triton>=3.4.0" {get_numpy} torchvision bitsandbytes "transformers==4.56.2" \
    "unsloth_zoo[base] @ git+https://github.com/unslothai/unsloth-zoo" \
    "unsloth[base] @ git+https://github.com/unslothai/unsloth" \
    git+https://github.com/triton-lang/triton.git@05b2c186c1b6c9a08375389d5efe9cb4c401c075#subdirectory=python/triton_kernels
!uv pip install --upgrade --no-deps transformers==4.56.2 tokenizers
!uv pip install --no-deps trl==0.22.2
```

{% endstep %}

{% step %}

#### Unslothでgpt-ossを読み込む

メモリ効率のために20Bモデルを4ビットQLoRAで読み込み、その後LoRAアダプタを付与します。16ビットLoRAでも学習できますが、メモリ使用量は4倍になります。その他の設定については、こちらの [設定ガイド](https://unsloth.ai/pages/ae7926a0f371a1f594e3344d85571edf864a5259#id-2.-choose-the-right-model--method).

```python
from unsloth import FastLanguageModel
import torch

max_seq_length = 768        # タスクでより長い出力が必要なら増やす
lora_rank      = 4          # ランクを上げるほど性能は向上するが、VRAM/計算量が増える

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name        = "unsloth/gpt-oss-20b",  # もしくはH100では unsloth/gpt-oss-20b-BF16
    max_seq_length    = max_seq_length,
    load_in_4bit      = True,                    # 16ビットなら False
    offload_embedding = True,                    # 約1GBのVRAMを節約
)

model = FastLanguageModel.get_peft_model(
    model,
    r = lora_rank,
    target_modules = [
        "q_proj", "k_proj", "v_proj", "o_proj",
        "gate_proj", "up_proj", "down_proj",
    ],
    lora_alpha = lora_rank * 2,
    use_gradient_checkpointing = "unsloth",     # 大幅なメモリ節約
    random_state = 3407,
)
```

{% hint style="info" %}
OOMになったら、次を下げてみてください `max_seq_length`, `lora_rank`、または `num_generations` （後で）を下げ、 `load_in_4bit=True`.
{% endhint %}
{% endstep %}

{% step %}

#### 2048ゲーム環境（最小構成）

* ある `GameBoard` クラスがサポートする **W/A/S/D** 移動
* マージ/スコア処理
* `execute_with_time_limit` ラッパーにより、出来の悪い戦略がカーネルをハングさせるのを防ぎます

まずは単純なポリシーで素早くスモークテストできます：

```python
def always_move_left(board):
    return "W"

steps, outcome = execute_strategy(always_move_left, GameBoard(size=8, seed=42, target=2048, probability_fours=0.10))
```

{% endstep %}

{% step %}

#### 安全なコード実行と不正対策のチェック

生成された戦略は **Python関数**です。実行の安全性を保ち、報酬ハッキングを防ぐために：

* **モジュールのホワイトリストチェック** — Python標準ライブラリのシンボルのみを許可：

  ```python
  from unsloth import check_python_modules
  ok, info = check_python_modules("""
  def strategy(board):
      import math
      from typing import Callable
      return "W"
  """)
  # ok == True は、Pythonレベルのインポートのみが使われたことを意味します
  ```
* **禁止されたインポートをブロックする** （例：NumPy）：

  ```python
  sample = """
  def strategy(board):
      from numpy import matmul
      return "W"
  """
  ok, info = check_python_modules(sample)  # ok => False
  ```
* **実行を制限する** サンドボックス化された関数に：

  ```python
  from unsloth import create_locked_down_function
  function = """
  def add(a, b):
      def adder(a):
          return a + b
      return adder(b) + b
  """
  f = create_locked_down_function(function)  # globals / imports を使うとエラー
  ```
* **戦略実行に厳密なウォールクロック制限を課す** ：

  ```python
  from unsloth import execute_with_time_limit
  @execute_with_time_limit(2)
  def execute_strategy(strategy, game):
      # ゲーム終了またはタイムアウトまでループする
      ...
  ```

{% endstep %}

{% step %}
\### プロンプトとデータセット

モデルには次のように促します **短い戦略関数を出力する** トリプルバッククォートの内側に：

````
ネイティブPythonコードのみを使って、新しい短い2048戦略を作成してください。
現在の盤面状態として、数値のリストのリストが与えられます。
次の最適手として "W", "A", "S", "D" のいずれか1つのアクションを出力してください。
以下の形式で、短い新しい関数をバッククォート内に出力してください：
```python
def strategy(board):
    return "W"  # 例
````

補助関数はすべて def strategy の中に入れてください。短い関数だけを出力してください `strategy`.

````

小さな合成データセット（同じプロンプトを再利用）を作成し、GRPOが何トークンの完成をサンプリングすべきか分かるようにプロンプト長を計算します：

```python
from datasets import Dataset

prompt = ...  # 上記のとおり

maximum_length = len(tokenizer.apply_chat_template(
    [{"role": "user", "content": prompt}], add_generation_prompt=True
))

dataset = Dataset.from_list([
    {"prompt": [{"role": "user", "content": prompt}], "answer": 0, "reasoning_effort": "low"}
] * 1000)
````

{% hint style="info" %} このデータセットは、あなた自身のRLタスク用に実際のプロンプトへ置き換えられます。 {% endhint %} {% endstep %}

{% step %}

#### 報酬関数の時間です！

1. **コードブロックを抽出する** モデルの応答から：

   ````python
   def extract_function(text):
       if text.count("```") >= 2:
           first = text.find("```") + 3
           second = text.find("```", first)
           fx = text[first:second].strip()
           fx = fx.removeprefix("python\n")
           fx = fx[fx.find("def"):]
           if fx.startswith("def strategy(board):"):
               return fx
       return None
   ````
2. **`function_works`** - コンパイルして呼び出し可能な関数を作成できるか？

   ```python
   from unsloth import create_locked_down_function, check_python_modules

   def function_works(completions, **kwargs):
       scores = []
       for completion in completions:
           response = completion[0]["content"]
           function = extract_function(response)
           if function is None:
               scores.append(-2.0)
               continue
           ok, info = check_python_modules(function)
           if "error" in info:
               scores.append(-2.0)
               continue
           try:
               _ = create_locked_down_function(function)
               scores.append(1.0)
           except Exception:
               scores.append(-0.5)
       return scores
   ```
3. **`no_cheating`** - 標準ライブラリ以外のインポートは不可：

   ```python
   def no_cheating(completions, **kwargs):
       scores = []
       for completion in completions:
           response = completion[0]["content"]
           function = extract_function(response)
           if function is None:
               scores.append(-1.0)
               continue
           ok, _ = check_python_modules(function)
           scores.append(1.0 if ok else -20.0)  # 不正なら重いペナルティ
       return scores
   ```
4. **`strategy_succeeds`** - ランダムな盤面でプレイし、成功に報酬を与える：

   ```python
   import numpy as np

   PRINTER = 0  # デバッグのために時々表示する

   def strategy_succeeds(completions, **kwargs):
       global PRINTER
       scores = []
       seed = np.random.randint(10000)
       for completion in completions:
           response = completion[0]["content"]
           function = extract_function(response)
           if function is None:
               scores.append(-2.0)
               continue
           try:
               new_strategy = create_locked_down_function(function)
           except Exception:
               scores.append(0.0)
               continue
           try:
               game = GameBoard(size=6, seed=seed, target=2048, probability_fours=0.10)
               steps, state = execute_strategy(new_strategy, game)
               if PRINTER % 5 == 0:
                   print(function)
                   print(f"Steps={steps} State={state}")
                   print(game.board().pretty())
               PRINTER += 1
               if state == "success":
                   scores.append(20.0)
               else:
                   scores.append(2.0)   # うまくいったが2048には到達しなかった
           except TimeoutError:
               scores.append(-1.0)      # タイムアウト
           except Exception:
               scores.append(-3.0)      # クラッシュ
       return scores
   ```

{% endstep %}

{% step %}

#### GRPOを設定する

以下を使います **GRPOTrainer**。プロンプト/完成の長さを設定してから、 `GRPOConfig`を作成します。RLアルゴリズムの種類は、たとえば [GSPO](https://unsloth.ai/docs/jp/meru/reinforcement-learning-rl-guide/advanced-rl-documentation/gspo-reinforcement-learning.md) やDr. GRPOなどにしてもよいことを覚えておいてください。

```python
from trl import GRPOConfig, GRPOTrainer

max_prompt_length     = maximum_length + 1
max_completion_length = max_seq_length - max_prompt_length

training_args = GRPOConfig(
    temperature=1.0,
    learning_rate=5e-5,
    weight_decay=0.01,
    warmup_ratio=0.1,
    lr_scheduler_type="linear",
    optim="adamw_8bit",
    logging_steps=1,
    per_device_train_batch_size=1,
    gradient_accumulation_steps=1,    # より滑らかな報酬信号のために4へ増やす
    num_generations=2,                # OOMなら下げる
    max_prompt_length=max_prompt_length,
    max_completion_length=max_completion_length,
    max_steps=1000,                   # または num_train_epochs=1 を設定
    save_steps=100,
    report_to="none",
    output_dir="outputs",
)

trainer = GRPOTrainer(
    model=model,
    processing_class=tokenizer,
    reward_funcs=[function_works, no_cheating, strategy_succeeds],
    args=training_args,
    train_dataset=dataset,
    # 任意のeval分割：
    # train_dataset=new_dataset["train"],
    # eval_dataset=new_dataset["test"],
)
```

{% hint style="info" %} **ログの読み方：** 見るポイントは `reward` そして `reward_std`です。学習の初期（小さいGPUでは最初の約100〜200ステップ）に報酬が低い/ゼロになるのは正常です。 {% endhint %} {% endstep %}

{% step %}

#### モデルを学習する

```python
trainer.train()
```

これで完全なRLループが起動します：完成をサンプル → 報酬でスコア付け → ポリシー（LoRA）を最適化。 {% endstep %}

{% step %}

#### 推論（学習後）

学習済みアダプタで新しい戦略を生成します：

```python
from transformers import TextStreamer

text = tokenizer.apply_chat_template(
    [{"role": "user", "content": prompt}],
    tokenize=False,
    add_generation_prompt=True,
    reasoning_effort="low",
)

_ = model.generate(
    **tokenizer(text, return_tensors="pt").to("cuda"),
    temperature=1.0,
    max_new_tokens=1024,
    streamer=TextStreamer(tokenizer, skip_prompt=False)
```

{% endstep %}

{% step %}

#### 微調整済みモデルを保存 / エクスポートする

* **4ビット（MXFP4）でマージして保存**

  ```
  ```

python model.save\_pretrained\_merged("finetuned\_model", tokenizer, save\_method="mxfp4") # または model.push\_to\_hub\_merged("\<org\_or\_user>/", tokenizer, token="\<hf\_token>", save\_method="mxfp4") をプッシュ \`\`\`

* **16ビットでマージして保存**

  ```python
  model.save_pretrained_merged("finetuned_model", tokenizer, save_method="merged_16bit")
  # またはプッシュ
  model.push_to_hub_merged("<org_or_user>/<repo>", tokenizer, token="<hf_token>", save_method="merged_16bit")
  ```

{% endstep %}

{% step %}

#### トラブルシューティングとヒント

* **OOM / 遅い**：次を下げる `max_seq_length`, `num_generations`, `lora_rank`; 4ビットを維持する; 可能ならA100を試す。
* **報酬が改善しない**：学習ステップを増やす、ペナルティを緩和する、またはカリキュラムを追加する（小さい盤面 / 低い目標から始める）。
* **報酬ハッキング**： `check_python_modules` を厳格に保つ; 複数のランダムシードで戦略の挙動を検証する。
* **学習が不安定**：次を上げる `gradient_accumulation_steps` で更新を滑らかにする; 次を下げる `learning_rate` （例：2e-5）。
* **長時間ハングする**：確認する `execute_with_time_limit` が戦略実行をすべてラップしていることを。
  {% endstep %}

{% step %}

#### 独自のRLタスクに適応する

* 2048環境を独自の環境に置き換え、 **3つの報酬**を用意してください：（a）構文/コンパイル、（b）不正対策/安全性、（c）タスク成功。
* 次を更新する **プロンプト** 必要な関数や出力の種類を要求するように。
* Unsloth + GRPO の足場は同じままにし、環境と報酬だけを差し替えてください。
  {% endstep %}
  {% endstepper %}


---

# 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/moderu/gpt-oss-how-to-run-and-fine-tune/gpt-oss-reinforcement-learning/tutorial-how-to-train-gpt-oss-with-rl.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.
