Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix(data): pin the reasoning template's answer markers and the GRPO demo fixture copies (#1471)

Momoyeyu677af2969ff8
diff --git a/changelog.d/0.75.1/1471.fixed.md b/changelog.d/0.75.1/1471.fixed.md
new file mode 100644
index 00000000..a56c421c
--- /dev/null
+++ b/changelog.d/0.75.1/1471.fixed.md
@@ -0,0 +1 @@
+- `soup data generate --template reasoning` now tells the generator how to end each solution, so the golds it writes are readable by GRPO's `accuracy` and `verifiable` math rewards: `math` rows end with `#### <number>` on its own line, `logic` and `code` rows with an `Answer: <answer>` line. Before, the prompt asked only for "the final answer", and a gold such as `So there are 42 apples.` makes `soup train` refuse the dataset before generation (#1349 by @Momoyeyu in #1471).
diff --git a/docs/data.md b/docs/data.md
index 23f26b0f..1c063bdb 100644
--- a/docs/data.md
+++ b/docs/data.md
@@ -479,7 +479,9 @@ soup data generate --prompt "..." --template qa --context document.txt
# Preference data (DPO/KTO/ORPO)
soup data generate --prompt "..." --template preference --pref-task dpo
-# Chain-of-thought reasoning (GRPO)
+# Chain-of-thought reasoning (GRPO). `math` rows end with `#### <number>`, so the
+# `accuracy` / `verifiable` math rewards can read the gold; `logic` / `code` rows
+# end with an `Answer: <answer>` line.
soup data generate --prompt "..." --template reasoning --domain math
```
diff --git a/src/soup_cli/data/templates/reasoning.py b/src/soup_cli/data/templates/reasoning.py
index f9c6798b..c75eeff0 100644
--- a/src/soup_cli/data/templates/reasoning.py
+++ b/src/soup_cli/data/templates/reasoning.py
@@ -41,6 +41,12 @@ def build_prompt(
}
domain_desc = domain_descriptions.get(domain, domain_descriptions["math"])
+ # GRPO's accuracy / verifiable-math rewards read the gold with
+ # soup_cli.utils.final_answer, so each solution must end on a marker it parses.
+ if domain in ("logic", "code"):
+ answer_rule = "End the solution with a final line of the form 'Answer: <answer>'."
+ else:
+ answer_rule = "Put the single numeric answer after '####' on its own line."
return (
f"You are a training data generator. Generate exactly {count} diverse "
@@ -48,7 +54,7 @@ def build_prompt(
f"Domain: {domain_desc}\n\n"
f"Each solution must show detailed chain-of-thought reasoning. "
f"Use <think>...</think> tags to wrap the reasoning steps, then provide "
- f"the final answer.\n\n"
+ f"the final answer. {answer_rule}\n\n"
f"Format: {format_spec}\n\n"
f"Return ONLY a JSON array of {count} examples. No markdown, no explanation."
)
diff --git a/tests/test_issue1349_grpo_demo_golds.py b/tests/test_issue1349_grpo_demo_golds.py
new file mode 100644
index 00000000..0cbbfb31
--- /dev/null
+++ b/tests/test_issue1349_grpo_demo_golds.py
@@ -0,0 +1,84 @@
+"""#1349: the shipped GRPO demo data and the ``reasoning`` template give GRPO numeric golds.
+
+``grpo_demo`` ships ``reasoning_math.jsonl`` twice: the package copy that ``soup data demo``
+installs and the ``examples/`` copy the example config trains on. Every row ends with an
+``Answer:`` line that the shared final-answer parser reads to its value — including the two
+rows whose answer names a variable (``c = 5``, ``x = 7``, read as ``5`` and ``7`` since
+#1371) — so ``\\boxed{5}`` pays under ``accuracy`` and ``verifiable/math``.
+"""
+
+from __future__ import annotations
+
+import json
+from decimal import Decimal
+from pathlib import Path
+
+import pytest
+
+from soup_cli.data.templates.reasoning import build_prompt
+from soup_cli.trainer.grpo import _prepare_grpo_dataset
+from soup_cli.trainer.rewards import load_reward_fn
+from soup_cli.utils.final_answer import parse_reference
+
+_REPO = Path(__file__).resolve().parents[1]
+_PACKAGE_COPY = _REPO / "src" / "soup_cli" / "data" / "_fixtures" / "reasoning_math.jsonl"
+_EXAMPLES_COPY = _REPO / "examples" / "data" / "reasoning_math.jsonl"
+_GOLDS = [Decimal(17), Decimal(5), Decimal(6), Decimal(7), Decimal(30)]
+
+
+def _prepared(path: Path) -> list[dict]:
+ rows = [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines()]
+ return _prepare_grpo_dataset(
+ [{"prompt": row["instruction"], "answer": row["output"]} for row in rows]
+ )
+
+
+def _msg(text: str) -> list[dict]:
+ return [{"role": "assistant", "content": text}]
+
+
+def test_the_two_fixture_copies_are_byte_identical():
+ assert _PACKAGE_COPY.read_bytes() == _EXAMPLES_COPY.read_bytes()
+
+
[email protected]("path", [_PACKAGE_COPY, _EXAMPLES_COPY], ids=["package", "examples"])
+def test_every_row_parses_to_a_numeric_gold(path):
+ parsed = [parse_reference(row["answer"]) for row in _prepared(path)]
+ assert [p.number for p in parsed] == _GOLDS
+
+
+ ("reward_fn", "domain"), [("accuracy", None), ("verifiable", "math")]
+)
[email protected]("completion", [r"\boxed{5}", "The answer is 5.", "#### 5"])
+def test_row_one_pays_the_value_however_it_is_written(reward_fn, domain, completion):
+ gold = _prepared(_PACKAGE_COPY)[1]["answer"]
+ reward = load_reward_fn(reward_fn, verifiable_domain=domain)
+ assert reward([_msg(completion)], answer=[gold]) == [1.0]
+ assert reward([_msg(r"\boxed{6}")], answer=[gold]) == [0.0]
+
+
+def test_the_math_domain_asks_for_the_hash_marker():
+ assert "'####' on its own line" in build_prompt(5, "alpaca", "spec", domain="math")
+
+
[email protected]("domain", ["logic", "code"])
+def test_the_other_domains_ask_for_an_answer_line(domain):
+ prompt = build_prompt(5, "alpaca", "spec", domain=domain)
+ assert "'Answer: <answer>'" in prompt
+ assert "####" not in prompt
+
+
+ ("gold", "number", "text"),
+ [
+ ("<think>6 * 7 = 42</think>\n#### 42", Decimal(42), "42"),
+ ("<think>p implies q</think>\nAnswer: yes", None, "yes"),
+ ("<think>one pass</think>\nAnswer: O(n)", None, "O(n)"),
+ ],
+)
+def test_a_gold_written_the_way_the_template_asks_is_readable(gold, number, text):
+ parsed = parse_reference(gold)
+ assert parsed is not None
+ assert parsed.number == number
+ assert parsed.text == text