forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_grpo.py
More file actions
426 lines (321 loc) · 14.5 KB
/
Copy pathtest_grpo.py
File metadata and controls
426 lines (321 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""Tests for GRPO training — config, rewards, data preparation, template."""
import textwrap
import pytest
from soup_cli.config.schema import TEMPLATES, SoupConfig
# ─── Config Tests ───────────────────────────────────────────────────────────
class TestGRPOConfig:
"""Test GRPO task config validation."""
def test_grpo_task_accepted(self):
"""GRPO task should be a valid task type."""
cfg = SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
)
assert cfg.task == "grpo"
def test_grpo_beta_default(self):
"""grpo_beta should default to 0.1."""
cfg = SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
)
assert cfg.training.grpo_beta == 0.1
def test_grpo_beta_custom(self):
"""Custom grpo_beta should be accepted."""
cfg = SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
training={"grpo_beta": 0.04},
)
assert cfg.training.grpo_beta == pytest.approx(0.04)
def test_grpo_beta_must_be_positive(self):
"""grpo_beta must be > 0."""
with pytest.raises(Exception):
SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
training={"grpo_beta": 0},
)
def test_num_generations_default(self):
"""num_generations should default to 4."""
cfg = SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
)
assert cfg.training.num_generations == 4
def test_num_generations_custom(self):
"""Custom num_generations should be accepted."""
cfg = SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
training={"num_generations": 8},
)
assert cfg.training.num_generations == 8
def test_num_generations_minimum(self):
"""num_generations must be >= 2."""
with pytest.raises(Exception):
SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
training={"num_generations": 1},
)
def test_reward_fn_default(self):
"""reward_fn should default to 'accuracy'."""
cfg = SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
)
assert cfg.training.reward_fn == "accuracy"
def test_reward_fn_custom_path(self):
"""reward_fn should accept a custom file path."""
cfg = SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
training={"reward_fn": "./my_reward.py"},
)
assert cfg.training.reward_fn == "./my_reward.py"
def test_grpo_full_config(self):
"""Full GRPO config should validate correctly."""
cfg = SoupConfig(
base="meta-llama/Llama-3.1-8B-Instruct",
task="grpo",
data={"train": "./data.jsonl", "format": "sharegpt", "max_length": 4096},
training={
"epochs": 3,
"lr": 1e-5,
"grpo_beta": 0.1,
"num_generations": 4,
"reward_fn": "format",
"lora": {"r": 64, "alpha": 16},
"quantization": "4bit",
},
)
assert cfg.task == "grpo"
assert cfg.training.reward_fn == "format"
assert cfg.training.num_generations == 4
assert cfg.data.max_length == 4096
# ─── Reward Function Tests ──────────────────────────────────────────────────
class TestAccuracyReward:
"""Test the accuracy reward function."""
def test_exact_match(self):
from soup_cli.trainer.rewards import accuracy_reward
completions = [[{"role": "assistant", "content": "The answer is #### 42"}]]
rewards = accuracy_reward(completions, answer=["42"])
assert rewards == [1.0]
def test_boxed_match(self):
from soup_cli.trainer.rewards import accuracy_reward
completions = [[{"role": "assistant", "content": "So \\boxed{42} is the result"}]]
rewards = accuracy_reward(completions, answer=["42"])
assert rewards == [1.0]
def test_partial_match(self):
from soup_cli.trainer.rewards import accuracy_reward
completions = [[{"role": "assistant", "content": "The answer is 42 degrees"}]]
rewards = accuracy_reward(completions, answer=["42"])
assert rewards == [0.5]
def test_no_match(self):
from soup_cli.trainer.rewards import accuracy_reward
completions = [[{"role": "assistant", "content": "I don't know"}]]
rewards = accuracy_reward(completions, answer=["42"])
assert rewards == [0.0]
def test_multiple_completions(self):
from soup_cli.trainer.rewards import accuracy_reward
completions = [
[{"role": "assistant", "content": "#### 42"}],
[{"role": "assistant", "content": "Wrong answer"}],
[{"role": "assistant", "content": "The answer is 42"}],
]
rewards = accuracy_reward(completions, answer=["42", "42", "42"])
assert rewards == [1.0, 0.0, 0.5]
def test_empty_completion(self):
from soup_cli.trainer.rewards import accuracy_reward
completions = [[]]
rewards = accuracy_reward(completions, answer=["42"])
assert rewards == [0.0]
class TestFormatReward:
"""Test the format reward function."""
def test_perfect_format(self):
from soup_cli.trainer.rewards import format_reward
content = "<think>Let me think step by step...</think>\nThe answer is 42."
completions = [[{"role": "assistant", "content": content}]]
rewards = format_reward(completions)
assert rewards == [1.0]
def test_think_only(self):
from soup_cli.trainer.rewards import format_reward
content = "<think>Thinking...</think>"
completions = [[{"role": "assistant", "content": content}]]
rewards = format_reward(completions)
assert rewards == [0.5]
def test_no_format(self):
from soup_cli.trainer.rewards import format_reward
completions = [[{"role": "assistant", "content": "Just a plain answer"}]]
rewards = format_reward(completions)
assert rewards == [0.0]
def test_multiple_completions(self):
from soup_cli.trainer.rewards import format_reward
completions = [
[{"role": "assistant", "content": "<think>A</think>\nB"}],
[{"role": "assistant", "content": "No format"}],
]
rewards = format_reward(completions)
assert rewards == [1.0, 0.0]
class TestExtractAnswer:
"""Test answer extraction from model output."""
def test_hash_format(self):
from soup_cli.trainer.rewards import _extract_answer
assert _extract_answer("Some work\n#### 42") == "42"
def test_boxed_format(self):
from soup_cli.trainer.rewards import _extract_answer
assert _extract_answer("So \\boxed{42} is the answer") == "42"
def test_no_answer(self):
from soup_cli.trainer.rewards import _extract_answer
assert _extract_answer("Just plain text") is None
def test_multiple_hashes(self):
from soup_cli.trainer.rewards import _extract_answer
assert _extract_answer("#### step\n#### 42") == "42"
class TestLoadRewardFn:
"""Test reward function loading."""
def test_load_builtin_accuracy(self):
from soup_cli.trainer.rewards import accuracy_reward, load_reward_fn
fn = load_reward_fn("accuracy")
assert fn is accuracy_reward
def test_load_builtin_format(self):
from soup_cli.trainer.rewards import format_reward, load_reward_fn
fn = load_reward_fn("format")
assert fn is format_reward
def test_load_custom_file(self, tmp_path):
from soup_cli.trainer.rewards import load_reward_fn
custom_file = tmp_path / "my_reward.py"
custom_file.write_text(textwrap.dedent("""\
def reward_fn(completions, **kwargs):
return [1.0] * len(completions)
"""))
fn = load_reward_fn(str(custom_file))
result = fn([[{"content": "test"}]])
assert result == [1.0]
def test_load_custom_file_missing_fn(self, tmp_path):
from soup_cli.trainer.rewards import load_reward_fn
custom_file = tmp_path / "bad_reward.py"
custom_file.write_text("x = 1\n")
with pytest.raises(ValueError, match="must define a 'reward_fn'"):
load_reward_fn(str(custom_file))
def test_load_unknown_name(self):
from soup_cli.trainer.rewards import load_reward_fn
with pytest.raises(ValueError, match="Unknown reward function"):
load_reward_fn("nonexistent")
# ─── Data Preparation Tests ─────────────────────────────────────────────────
class TestPrepareGRPODataset:
"""Test GRPO dataset preparation."""
def test_from_prompt_string(self):
from soup_cli.trainer.grpo import _prepare_grpo_dataset
data = [{"prompt": "What is 2+2?", "answer": "4"}]
result = _prepare_grpo_dataset(data)
assert len(result) == 1
assert result[0]["prompt"] == [{"role": "user", "content": "What is 2+2?"}]
assert result[0]["answer"] == "4"
def test_from_messages(self):
from soup_cli.trainer.grpo import _prepare_grpo_dataset
data = [
{
"messages": [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi!"},
]
}
]
result = _prepare_grpo_dataset(data)
assert len(result) == 1
# Should only include non-assistant messages as prompt
assert len(result[0]["prompt"]) == 2
assert result[0]["prompt"][0]["role"] == "system"
assert result[0]["prompt"][1]["role"] == "user"
def test_from_prompt_message_list(self):
from soup_cli.trainer.grpo import _prepare_grpo_dataset
data = [
{
"prompt": [{"role": "user", "content": "What is 2+2?"}],
"answer": "4",
}
]
result = _prepare_grpo_dataset(data)
assert result[0]["prompt"] == [{"role": "user", "content": "What is 2+2?"}]
assert result[0]["answer"] == "4"
def test_from_alpaca_format(self):
from soup_cli.trainer.grpo import _prepare_grpo_dataset
data = [{"instruction": "Translate hello", "input": "", "output": "hola"}]
result = _prepare_grpo_dataset(data)
assert result[0]["prompt"] == [{"role": "user", "content": "Translate hello"}]
assert result[0]["answer"] == "hola"
def test_multiple_rows(self):
from soup_cli.trainer.grpo import _prepare_grpo_dataset
data = [
{"prompt": "Q1", "answer": "A1"},
{"prompt": "Q2", "answer": "A2"},
{"prompt": "Q3", "answer": "A3"},
]
result = _prepare_grpo_dataset(data)
assert len(result) == 3
# ─── Template Tests ──────────────────────────────────────────────────────────
class TestReasoningTemplate:
"""Test the reasoning/GRPO template."""
def test_reasoning_template_exists(self):
assert "reasoning" in TEMPLATES
def test_reasoning_template_valid_yaml(self):
import yaml
config = yaml.safe_load(TEMPLATES["reasoning"])
assert config["task"] == "grpo"
assert config["training"]["grpo_beta"] == 0.1
assert config["training"]["num_generations"] == 4
assert config["training"]["reward_fn"] == "accuracy"
def test_reasoning_template_valid_config(self):
import yaml
raw = yaml.safe_load(TEMPLATES["reasoning"])
cfg = SoupConfig(**raw)
assert cfg.task == "grpo"
assert cfg.training.grpo_beta == 0.1
# ─── Train Command Routing Tests ─────────────────────────────────────────────
class TestGRPOTrainRouting:
"""Test that train command routes to GRPO trainer."""
def test_grpo_import_exists(self):
"""GRPOTrainerWrapper should be importable."""
from soup_cli.trainer.grpo import GRPOTrainerWrapper
assert GRPOTrainerWrapper is not None
def test_grpo_wrapper_init(self):
"""GRPOTrainerWrapper should initialize without error."""
from soup_cli.trainer.grpo import GRPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="grpo",
data={"train": "./data.jsonl"},
)
wrapper = GRPOTrainerWrapper(cfg, device="cpu")
assert wrapper.config.task == "grpo"
assert wrapper.device == "cpu"
assert wrapper.model is None
assert wrapper.trainer is None
# ─── Sweep Shortcut Tests ────────────────────────────────────────────────────
class TestGRPOSweepParams:
"""Test GRPO parameter shortcuts in sweep."""
def test_grpo_beta_shortcut(self):
from soup_cli.commands.sweep import _set_nested_param
config = {"training": {"grpo_beta": 0.1}}
_set_nested_param(config, "grpo_beta", 0.04)
assert config["training"]["grpo_beta"] == 0.04
def test_num_generations_shortcut(self):
from soup_cli.commands.sweep import _set_nested_param
config = {"training": {"num_generations": 4}}
_set_nested_param(config, "num_generations", 8)
assert config["training"]["num_generations"] == 8
def test_reward_fn_shortcut(self):
from soup_cli.commands.sweep import _set_nested_param
config = {"training": {"reward_fn": "accuracy"}}
_set_nested_param(config, "reward_fn", "format")
assert config["training"]["reward_fn"] == "format"