Madhav189 Claude Opus 4.7 (1M context) commited on
Commit
215c8ad
·
1 Parent(s): 091927e

notebook: fix GRPO prompts — apply chat template before passing to TRL

Browse files

Symptom on user's run: GRPO step 2 showed reward=-0.5, reward_std=0,
completions/clipped_ratio=1.0. Every K=4 rollout failed JSON parse in
the same way, generating exactly max_completion_length=256 tokens
without ever producing EOS.

Cause: SFT trained with full ChatML wrapping (system+user+assistant turns
delimited by im_start/im_end markers), but Cell 7 passed obs.prompt_text
(raw env text) to TRL. TRL's GRPOTrainer treats the prompt column as raw
text and feeds it straight to model.generate, with no chat template
applied. The Qwen2.5-Instruct model saw out-of-distribution input and
rambled until clipped.

Fix: pre-render the chat template in build_grpo_prompts via
tokenizer.apply_chat_template(..., add_generation_prompt=True). The
prompt now ends with the assistant generation marker so the model knows
to start its turn and produce JSON.

Also inlined SFT_SYSTEM_PROMPT in Cell 7 (already inline in Cell 10) so
the cell is self-contained after kernel restart.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

notebooks/01_triage_train_grpo.ipynb CHANGED
@@ -2,7 +2,7 @@
2
  "cells": [
3
  {
4
  "cell_type": "markdown",
5
- "id": "f1f2a52d",
6
  "metadata": {},
7
  "source": [
8
  "# sre-gym Triage — Qwen2.5-3B SFT → GRPO training notebook\n",
@@ -46,7 +46,7 @@
46
  },
47
  {
48
  "cell_type": "markdown",
49
- "id": "c906574f",
50
  "metadata": {},
51
  "source": [
52
  "## Cell 0 — Bootstrap (RUN ME FIRST after every kernel restart)\n",
@@ -67,7 +67,7 @@
67
  {
68
  "cell_type": "code",
69
  "execution_count": null,
70
- "id": "c000dde4",
71
  "metadata": {},
72
  "outputs": [],
73
  "source": [
@@ -184,7 +184,7 @@
184
  },
185
  {
186
  "cell_type": "markdown",
187
- "id": "656279e0",
188
  "metadata": {},
189
  "source": [
190
  "## Cell 1 — Verify PyTorch sees the GPU\n",
@@ -196,7 +196,7 @@
196
  {
197
  "cell_type": "code",
198
  "execution_count": null,
199
- "id": "61661087",
200
  "metadata": {},
201
  "outputs": [],
202
  "source": [
@@ -238,7 +238,7 @@
238
  },
239
  {
240
  "cell_type": "markdown",
241
- "id": "ab3e3ef1",
242
  "metadata": {},
243
  "source": [
244
  "## Cell 2 — Build the SFT corpus\n",
@@ -250,7 +250,7 @@
250
  {
251
  "cell_type": "code",
252
  "execution_count": null,
253
- "id": "5a12815b",
254
  "metadata": {},
255
  "outputs": [],
256
  "source": [
@@ -278,7 +278,7 @@
278
  },
279
  {
280
  "cell_type": "markdown",
281
- "id": "9476e6b4",
282
  "metadata": {},
283
  "source": [
284
  "## Cell 3 — Sanity-check the corpus\n",
@@ -290,7 +290,7 @@
290
  {
291
  "cell_type": "code",
292
  "execution_count": null,
293
- "id": "e0374674",
294
  "metadata": {},
295
  "outputs": [],
296
  "source": [
@@ -335,7 +335,7 @@
335
  },
336
  {
337
  "cell_type": "markdown",
338
- "id": "1982bb01",
339
  "metadata": {},
340
  "source": [
341
  "## Cell 4 — Convert each step into a (prompt, completion) ChatML pair\n",
@@ -346,7 +346,7 @@
346
  {
347
  "cell_type": "code",
348
  "execution_count": null,
349
- "id": "b08985b7",
350
  "metadata": {},
351
  "outputs": [],
352
  "source": [
@@ -421,7 +421,7 @@
421
  },
422
  {
423
  "cell_type": "markdown",
424
- "id": "278f554f",
425
  "metadata": {},
426
  "source": [
427
  "## Cell 5 — Load Qwen2.5-3B-Instruct (4-bit + LoRA r=32)\n",
@@ -434,7 +434,7 @@
434
  {
435
  "cell_type": "code",
436
  "execution_count": null,
437
- "id": "e1415b35",
438
  "metadata": {},
439
  "outputs": [],
440
  "source": [
@@ -469,7 +469,7 @@
469
  },
470
  {
471
  "cell_type": "markdown",
472
- "id": "9b29b0a4",
473
  "metadata": {},
474
  "source": [
475
  "## Cell 6 — SFT cold-start (50 steps) + perplexity gate\n",
@@ -494,7 +494,7 @@
494
  {
495
  "cell_type": "code",
496
  "execution_count": null,
497
- "id": "f17adaa9",
498
  "metadata": {},
499
  "outputs": [],
500
  "source": [
@@ -579,7 +579,7 @@
579
  },
580
  {
581
  "cell_type": "markdown",
582
- "id": "0ce876b5",
583
  "metadata": {},
584
  "source": [
585
  "## Cell 7 — Build the GRPO prompt dataset\n",
@@ -592,7 +592,7 @@
592
  {
593
  "cell_type": "code",
594
  "execution_count": null,
595
- "id": "394affe3",
596
  "metadata": {},
597
  "outputs": [],
598
  "source": [
@@ -608,8 +608,41 @@
608
  "from unified_incident_env.models import UnifiedIncidentAction\n",
609
  "from unified_incident_env.server.environment import UnifiedIncidentEnvironment\n",
610
  "\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
611
  "\n",
612
  "def build_grpo_prompts(num_prompts=120, seed=0):\n",
 
 
 
 
 
 
 
 
 
 
 
613
  " templates = [\n",
614
  " \"worker_deploy_cascade\", \"db_config_rollout\", \"gateway_auth_rollout\",\n",
615
  " \"payment_webhook_misconfig\", \"schema_drift_missing_migration\", \"cache_stale_state\",\n",
@@ -629,7 +662,17 @@
629
  " obs = env.reset(scenario_id=scenario)\n",
630
  " if rng.random() > 0.5:\n",
631
  " obs = env.step(UnifiedIncidentAction(action_type=\"query_logs\", service=\"worker\"))\n",
632
- " out.append({\"prompt\": obs.prompt_text, \"scenario_id\": scenario})\n",
 
 
 
 
 
 
 
 
 
 
633
  " return out\n",
634
  "\n",
635
  "\n",
@@ -641,7 +684,7 @@
641
  },
642
  {
643
  "cell_type": "markdown",
644
- "id": "53f7711f",
645
  "metadata": {},
646
  "source": [
647
  "## Cell 8 — Per-turn proxy reward function\n",
@@ -656,7 +699,7 @@
656
  {
657
  "cell_type": "code",
658
  "execution_count": null,
659
- "id": "578cc709",
660
  "metadata": {},
661
  "outputs": [],
662
  "source": [
@@ -728,7 +771,7 @@
728
  },
729
  {
730
  "cell_type": "markdown",
731
- "id": "b80d68ce",
732
  "metadata": {},
733
  "source": [
734
  "## Cell 9 — GRPO online training (50 steps × K=4)\n",
@@ -752,7 +795,7 @@
752
  {
753
  "cell_type": "code",
754
  "execution_count": null,
755
- "id": "c6dd2ce5",
756
  "metadata": {},
757
  "outputs": [],
758
  "source": [
@@ -830,7 +873,7 @@
830
  },
831
  {
832
  "cell_type": "markdown",
833
- "id": "b35d5120",
834
  "metadata": {},
835
  "source": [
836
  "## Cell 10 — Eval comparison sweep\n",
@@ -848,7 +891,7 @@
848
  {
849
  "cell_type": "code",
850
  "execution_count": null,
851
- "id": "9d39a69b",
852
  "metadata": {},
853
  "outputs": [],
854
  "source": [
@@ -1039,7 +1082,7 @@
1039
  },
1040
  {
1041
  "cell_type": "markdown",
1042
- "id": "1eff7af4",
1043
  "metadata": {},
1044
  "source": [
1045
  "## Cell 11 — Summary table + hero plot"
@@ -1048,7 +1091,7 @@
1048
  {
1049
  "cell_type": "code",
1050
  "execution_count": null,
1051
- "id": "c22469b2",
1052
  "metadata": {},
1053
  "outputs": [],
1054
  "source": [
@@ -1112,7 +1155,7 @@
1112
  },
1113
  {
1114
  "cell_type": "markdown",
1115
- "id": "c6efc975",
1116
  "metadata": {},
1117
  "source": [
1118
  "## Cell 12 — Package artifacts for download\n",
@@ -1124,7 +1167,7 @@
1124
  {
1125
  "cell_type": "code",
1126
  "execution_count": null,
1127
- "id": "72d8c2a2",
1128
  "metadata": {},
1129
  "outputs": [],
1130
  "source": [
 
2
  "cells": [
3
  {
4
  "cell_type": "markdown",
5
+ "id": "3396f4be",
6
  "metadata": {},
7
  "source": [
8
  "# sre-gym Triage — Qwen2.5-3B SFT → GRPO training notebook\n",
 
46
  },
47
  {
48
  "cell_type": "markdown",
49
+ "id": "d72032e7",
50
  "metadata": {},
51
  "source": [
52
  "## Cell 0 — Bootstrap (RUN ME FIRST after every kernel restart)\n",
 
67
  {
68
  "cell_type": "code",
69
  "execution_count": null,
70
+ "id": "2e95fe82",
71
  "metadata": {},
72
  "outputs": [],
73
  "source": [
 
184
  },
185
  {
186
  "cell_type": "markdown",
187
+ "id": "22ca4050",
188
  "metadata": {},
189
  "source": [
190
  "## Cell 1 — Verify PyTorch sees the GPU\n",
 
196
  {
197
  "cell_type": "code",
198
  "execution_count": null,
199
+ "id": "bff2260c",
200
  "metadata": {},
201
  "outputs": [],
202
  "source": [
 
238
  },
239
  {
240
  "cell_type": "markdown",
241
+ "id": "e27848b8",
242
  "metadata": {},
243
  "source": [
244
  "## Cell 2 — Build the SFT corpus\n",
 
250
  {
251
  "cell_type": "code",
252
  "execution_count": null,
253
+ "id": "cc011328",
254
  "metadata": {},
255
  "outputs": [],
256
  "source": [
 
278
  },
279
  {
280
  "cell_type": "markdown",
281
+ "id": "d6424b89",
282
  "metadata": {},
283
  "source": [
284
  "## Cell 3 — Sanity-check the corpus\n",
 
290
  {
291
  "cell_type": "code",
292
  "execution_count": null,
293
+ "id": "c256cf4b",
294
  "metadata": {},
295
  "outputs": [],
296
  "source": [
 
335
  },
336
  {
337
  "cell_type": "markdown",
338
+ "id": "7e307d37",
339
  "metadata": {},
340
  "source": [
341
  "## Cell 4 — Convert each step into a (prompt, completion) ChatML pair\n",
 
346
  {
347
  "cell_type": "code",
348
  "execution_count": null,
349
+ "id": "e9100f77",
350
  "metadata": {},
351
  "outputs": [],
352
  "source": [
 
421
  },
422
  {
423
  "cell_type": "markdown",
424
+ "id": "f3904eb1",
425
  "metadata": {},
426
  "source": [
427
  "## Cell 5 — Load Qwen2.5-3B-Instruct (4-bit + LoRA r=32)\n",
 
434
  {
435
  "cell_type": "code",
436
  "execution_count": null,
437
+ "id": "6f91338c",
438
  "metadata": {},
439
  "outputs": [],
440
  "source": [
 
469
  },
470
  {
471
  "cell_type": "markdown",
472
+ "id": "35751fb0",
473
  "metadata": {},
474
  "source": [
475
  "## Cell 6 — SFT cold-start (50 steps) + perplexity gate\n",
 
494
  {
495
  "cell_type": "code",
496
  "execution_count": null,
497
+ "id": "3c6044fa",
498
  "metadata": {},
499
  "outputs": [],
500
  "source": [
 
579
  },
580
  {
581
  "cell_type": "markdown",
582
+ "id": "562aaaf9",
583
  "metadata": {},
584
  "source": [
585
  "## Cell 7 — Build the GRPO prompt dataset\n",
 
592
  {
593
  "cell_type": "code",
594
  "execution_count": null,
595
+ "id": "44e06dc9",
596
  "metadata": {},
597
  "outputs": [],
598
  "source": [
 
608
  "from unified_incident_env.models import UnifiedIncidentAction\n",
609
  "from unified_incident_env.server.environment import UnifiedIncidentEnvironment\n",
610
  "\n",
611
+ "# Inline the SFT system prompt so this cell survives kernel restart standalone\n",
612
+ "# (must be IDENTICAL to the one in Cell 4 — same chat template = same model\n",
613
+ "# distribution at test time).\n",
614
+ "SFT_SYSTEM_PROMPT = \"\"\"You are a senior SRE on-call agent inside the sre-gym Triage environment.\n",
615
+ "\n",
616
+ "Output EXACTLY one JSON object per turn — no prose, no markdown, no fences.\n",
617
+ "The 11 actions are:\n",
618
+ " query_logs(service) query_metrics(service, metric)\n",
619
+ " query_dependencies(service) query_deploys(service)\n",
620
+ " rollback_deploy(service) restart_service(service)\n",
621
+ " isolate_service(service) run_check(check_name)\n",
622
+ " submit_hypothesis(hypothesis) escalate\n",
623
+ " declare_resolved\n",
624
+ "\n",
625
+ "Services: api-gateway / cache / database / worker.\n",
626
+ "metric in {cpu, error_rate, latency}; check_name in {database_recovery, end_to_end}.\n",
627
+ "\n",
628
+ "A successful episode looks like: gather evidence -> submit_hypothesis -> rollback ->\n",
629
+ "restart -> both run_checks pass -> declare_resolved. Wrong rollback / premature\n",
630
+ "restart / premature declare_resolved are penalized. Repeated identical hypotheses\n",
631
+ "score 0.\"\"\"\n",
632
+ "\n",
633
  "\n",
634
  "def build_grpo_prompts(num_prompts=120, seed=0):\n",
635
+ " \"\"\"Build GRPO prompts already wrapped in the SAME chat template that SFT used.\n",
636
+ "\n",
637
+ " CRITICAL: TRL's GRPOTrainer treats the dataset's \"prompt\" column as raw\n",
638
+ " text and feeds it straight to model.generate. If we pass raw env prompts\n",
639
+ " (no <|im_start|> markers), the SFT-trained model sees out-of-distribution\n",
640
+ " input, doesn't produce JSON, and every rollout gets the -0.5 format\n",
641
+ " penalty (reward_std=0 → no learning signal).\n",
642
+ "\n",
643
+ " The fix: pre-render the chat template here so the prompt looks identical\n",
644
+ " to what the model saw during SFT, ending with `<|im_start|>assistant\\n`.\n",
645
+ " \"\"\"\n",
646
  " templates = [\n",
647
  " \"worker_deploy_cascade\", \"db_config_rollout\", \"gateway_auth_rollout\",\n",
648
  " \"payment_webhook_misconfig\", \"schema_drift_missing_migration\", \"cache_stale_state\",\n",
 
662
  " obs = env.reset(scenario_id=scenario)\n",
663
  " if rng.random() > 0.5:\n",
664
  " obs = env.step(UnifiedIncidentAction(action_type=\"query_logs\", service=\"worker\"))\n",
665
+ "\n",
666
+ " # Render the prompt in the same ChatML format SFT trained with.\n",
667
+ " chat_prompt = tokenizer.apply_chat_template(\n",
668
+ " [\n",
669
+ " {\"role\": \"system\", \"content\": SFT_SYSTEM_PROMPT},\n",
670
+ " {\"role\": \"user\", \"content\": obs.prompt_text},\n",
671
+ " ],\n",
672
+ " tokenize=False,\n",
673
+ " add_generation_prompt=True, # appends `<|im_start|>assistant\\n`\n",
674
+ " )\n",
675
+ " out.append({\"prompt\": chat_prompt, \"scenario_id\": scenario})\n",
676
  " return out\n",
677
  "\n",
678
  "\n",
 
684
  },
685
  {
686
  "cell_type": "markdown",
687
+ "id": "b421cc98",
688
  "metadata": {},
689
  "source": [
690
  "## Cell 8 — Per-turn proxy reward function\n",
 
699
  {
700
  "cell_type": "code",
701
  "execution_count": null,
702
+ "id": "e5787ac3",
703
  "metadata": {},
704
  "outputs": [],
705
  "source": [
 
771
  },
772
  {
773
  "cell_type": "markdown",
774
+ "id": "6f08cd4b",
775
  "metadata": {},
776
  "source": [
777
  "## Cell 9 — GRPO online training (50 steps × K=4)\n",
 
795
  {
796
  "cell_type": "code",
797
  "execution_count": null,
798
+ "id": "17223592",
799
  "metadata": {},
800
  "outputs": [],
801
  "source": [
 
873
  },
874
  {
875
  "cell_type": "markdown",
876
+ "id": "56a59f8d",
877
  "metadata": {},
878
  "source": [
879
  "## Cell 10 — Eval comparison sweep\n",
 
891
  {
892
  "cell_type": "code",
893
  "execution_count": null,
894
+ "id": "f4009004",
895
  "metadata": {},
896
  "outputs": [],
897
  "source": [
 
1082
  },
1083
  {
1084
  "cell_type": "markdown",
1085
+ "id": "af6e0d43",
1086
  "metadata": {},
1087
  "source": [
1088
  "## Cell 11 — Summary table + hero plot"
 
1091
  {
1092
  "cell_type": "code",
1093
  "execution_count": null,
1094
+ "id": "8ae96e2e",
1095
  "metadata": {},
1096
  "outputs": [],
1097
  "source": [
 
1155
  },
1156
  {
1157
  "cell_type": "markdown",
1158
+ "id": "d0d0c3e9",
1159
  "metadata": {},
1160
  "source": [
1161
  "## Cell 12 — Package artifacts for download\n",
 
1167
  {
1168
  "cell_type": "code",
1169
  "execution_count": null,
1170
+ "id": "f432e809",
1171
  "metadata": {},
1172
  "outputs": [],
1173
  "source": [
notebooks/01_triage_train_grpo.py CHANGED
@@ -519,8 +519,41 @@ if str(Path(".").resolve()) not in sys.path:
519
  from unified_incident_env.models import UnifiedIncidentAction
520
  from unified_incident_env.server.environment import UnifiedIncidentEnvironment
521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522
 
523
  def build_grpo_prompts(num_prompts=120, seed=0):
 
 
 
 
 
 
 
 
 
 
 
524
  templates = [
525
  "worker_deploy_cascade", "db_config_rollout", "gateway_auth_rollout",
526
  "payment_webhook_misconfig", "schema_drift_missing_migration", "cache_stale_state",
@@ -540,7 +573,17 @@ def build_grpo_prompts(num_prompts=120, seed=0):
540
  obs = env.reset(scenario_id=scenario)
541
  if rng.random() > 0.5:
542
  obs = env.step(UnifiedIncidentAction(action_type="query_logs", service="worker"))
543
- out.append({"prompt": obs.prompt_text, "scenario_id": scenario})
 
 
 
 
 
 
 
 
 
 
544
  return out
545
 
546
 
 
519
  from unified_incident_env.models import UnifiedIncidentAction
520
  from unified_incident_env.server.environment import UnifiedIncidentEnvironment
521
 
522
+ # Inline the SFT system prompt so this cell survives kernel restart standalone
523
+ # (must be IDENTICAL to the one in Cell 4 — same chat template = same model
524
+ # distribution at test time).
525
+ SFT_SYSTEM_PROMPT = """You are a senior SRE on-call agent inside the sre-gym Triage environment.
526
+
527
+ Output EXACTLY one JSON object per turn — no prose, no markdown, no fences.
528
+ The 11 actions are:
529
+ query_logs(service) query_metrics(service, metric)
530
+ query_dependencies(service) query_deploys(service)
531
+ rollback_deploy(service) restart_service(service)
532
+ isolate_service(service) run_check(check_name)
533
+ submit_hypothesis(hypothesis) escalate
534
+ declare_resolved
535
+
536
+ Services: api-gateway / cache / database / worker.
537
+ metric in {cpu, error_rate, latency}; check_name in {database_recovery, end_to_end}.
538
+
539
+ A successful episode looks like: gather evidence -> submit_hypothesis -> rollback ->
540
+ restart -> both run_checks pass -> declare_resolved. Wrong rollback / premature
541
+ restart / premature declare_resolved are penalized. Repeated identical hypotheses
542
+ score 0."""
543
+
544
 
545
  def build_grpo_prompts(num_prompts=120, seed=0):
546
+ """Build GRPO prompts already wrapped in the SAME chat template that SFT used.
547
+
548
+ CRITICAL: TRL's GRPOTrainer treats the dataset's "prompt" column as raw
549
+ text and feeds it straight to model.generate. If we pass raw env prompts
550
+ (no <|im_start|> markers), the SFT-trained model sees out-of-distribution
551
+ input, doesn't produce JSON, and every rollout gets the -0.5 format
552
+ penalty (reward_std=0 → no learning signal).
553
+
554
+ The fix: pre-render the chat template here so the prompt looks identical
555
+ to what the model saw during SFT, ending with `<|im_start|>assistant\n`.
556
+ """
557
  templates = [
558
  "worker_deploy_cascade", "db_config_rollout", "gateway_auth_rollout",
559
  "payment_webhook_misconfig", "schema_drift_missing_migration", "cache_stale_state",
 
573
  obs = env.reset(scenario_id=scenario)
574
  if rng.random() > 0.5:
575
  obs = env.step(UnifiedIncidentAction(action_type="query_logs", service="worker"))
576
+
577
+ # Render the prompt in the same ChatML format SFT trained with.
578
+ chat_prompt = tokenizer.apply_chat_template(
579
+ [
580
+ {"role": "system", "content": SFT_SYSTEM_PROMPT},
581
+ {"role": "user", "content": obs.prompt_text},
582
+ ],
583
+ tokenize=False,
584
+ add_generation_prompt=True, # appends `<|im_start|>assistant\n`
585
+ )
586
+ out.append({"prompt": chat_prompt, "scenario_id": scenario})
587
  return out
588
 
589