forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ppo.py
More file actions
621 lines (496 loc) · 21.3 KB
/
Copy pathtest_ppo.py
File metadata and controls
621 lines (496 loc) · 21.3 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
"""Tests for PPO / Full RLHF Pipeline — config, data prep, template, routing, sweep."""
import pytest
import yaml
from soup_cli.config.schema import TEMPLATES, SoupConfig
# ─── PPO Config Tests ──────────────────────────────────────────────────────
class TestPPOConfig:
"""Test PPO task config validation."""
def test_ppo_task_accepted(self):
"""PPO task should be a valid task type."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
)
assert cfg.task == "ppo"
def test_ppo_epochs_default(self):
"""ppo_epochs should default to 4."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
)
assert cfg.training.ppo_epochs == 4
def test_ppo_epochs_custom(self):
"""Custom ppo_epochs should be accepted."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"ppo_epochs": 8},
)
assert cfg.training.ppo_epochs == 8
def test_ppo_epochs_minimum(self):
"""ppo_epochs must be >= 1."""
with pytest.raises(Exception):
SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"ppo_epochs": 0},
)
def test_ppo_clip_ratio_default(self):
"""ppo_clip_ratio should default to 0.2."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
)
assert cfg.training.ppo_clip_ratio == pytest.approx(0.2)
def test_ppo_clip_ratio_custom(self):
"""Custom ppo_clip_ratio should be accepted."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"ppo_clip_ratio": 0.1},
)
assert cfg.training.ppo_clip_ratio == pytest.approx(0.1)
def test_ppo_clip_ratio_must_be_positive(self):
"""ppo_clip_ratio must be > 0."""
with pytest.raises(Exception):
SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"ppo_clip_ratio": 0},
)
def test_ppo_clip_ratio_max_one(self):
"""ppo_clip_ratio must be <= 1.0."""
with pytest.raises(Exception):
SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"ppo_clip_ratio": 1.5},
)
def test_ppo_kl_penalty_default(self):
"""ppo_kl_penalty should default to 0.05."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
)
assert cfg.training.ppo_kl_penalty == pytest.approx(0.05)
def test_ppo_kl_penalty_custom(self):
"""Custom ppo_kl_penalty should be accepted."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"ppo_kl_penalty": 0.1},
)
assert cfg.training.ppo_kl_penalty == pytest.approx(0.1)
def test_ppo_kl_penalty_zero_allowed(self):
"""ppo_kl_penalty can be 0 (no KL penalty)."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"ppo_kl_penalty": 0},
)
assert cfg.training.ppo_kl_penalty == 0
def test_reward_model_default_none(self):
"""reward_model should default to None."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
)
assert cfg.training.reward_model is None
def test_reward_model_custom_path(self):
"""reward_model should accept a path."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"reward_model": "./output_rm"},
)
assert cfg.training.reward_model == "./output_rm"
def test_reward_model_hf_id(self):
"""reward_model should accept an HF model ID."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"reward_model": "OpenAssistant/reward-model-deberta-v3-large-v2"},
)
assert "reward-model" in cfg.training.reward_model
def test_ppo_full_config(self):
"""Full PPO config should validate correctly."""
cfg = SoupConfig(
base="meta-llama/Llama-3.1-8B-Instruct",
task="ppo",
data={"train": "./data.jsonl", "format": "chatml", "max_length": 2048},
training={
"epochs": 1,
"lr": 1e-6,
"ppo_epochs": 4,
"ppo_clip_ratio": 0.2,
"ppo_kl_penalty": 0.05,
"reward_model": "./output_rm",
"lora": {"r": 64, "alpha": 16},
"quantization": "4bit",
},
)
assert cfg.task == "ppo"
assert cfg.training.ppo_epochs == 4
assert cfg.training.reward_model == "./output_rm"
assert cfg.data.max_length == 2048
# ─── Reward Model Config Tests ────────────────────────────────────────────
class TestRewardModelConfig:
"""Test reward_model task config validation."""
def test_reward_model_task_accepted(self):
"""reward_model task should be a valid task type."""
cfg = SoupConfig(
base="some-model",
task="reward_model",
data={"train": "./data.jsonl"},
)
assert cfg.task == "reward_model"
def test_reward_model_with_dpo_format(self):
"""reward_model should work with DPO data format."""
cfg = SoupConfig(
base="some-model",
task="reward_model",
data={"train": "./data.jsonl", "format": "dpo"},
)
assert cfg.task == "reward_model"
assert cfg.data.format == "dpo"
def test_reward_model_full_config(self):
"""Full reward model config should validate correctly."""
cfg = SoupConfig(
base="meta-llama/Llama-3.1-8B-Instruct",
task="reward_model",
data={"train": "./pref_data.jsonl", "format": "dpo"},
training={
"epochs": 1,
"lr": 1e-5,
"lora": {"r": 32, "alpha": 16},
"quantization": "4bit",
},
output="./output_rm",
)
assert cfg.task == "reward_model"
assert cfg.output == "./output_rm"
# ─── PPO Data Preparation Tests ───────────────────────────────────────────
class TestPreparePPODataset:
"""Test PPO dataset preparation."""
def test_from_prompt_string(self):
from soup_cli.trainer.ppo import _prepare_ppo_dataset
data = [{"prompt": "What is 2+2?", "answer": "4"}]
result = _prepare_ppo_dataset(data)
assert len(result) == 1
assert result[0]["prompt_text"] == "What is 2+2?"
assert result[0]["answer"] == "4"
def test_from_messages(self):
from soup_cli.trainer.ppo import _prepare_ppo_dataset
data = [
{
"messages": [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi!"},
]
}
]
result = _prepare_ppo_dataset(data)
assert len(result) == 1
# Should join system + user content
assert "You are helpful." in result[0]["prompt_text"]
assert "Hello" in result[0]["prompt_text"]
def test_from_prompt_message_list(self):
from soup_cli.trainer.ppo import _prepare_ppo_dataset
data = [
{
"prompt": [{"role": "user", "content": "What is 2+2?"}],
"answer": "4",
}
]
result = _prepare_ppo_dataset(data)
assert "What is 2+2?" in result[0]["prompt_text"]
assert result[0]["answer"] == "4"
def test_from_alpaca_format(self):
from soup_cli.trainer.ppo import _prepare_ppo_dataset
data = [{"instruction": "Translate hello", "input": "", "output": "hola"}]
result = _prepare_ppo_dataset(data)
assert result[0]["prompt_text"] == "Translate hello"
assert result[0]["answer"] == "hola"
def test_multiple_rows(self):
from soup_cli.trainer.ppo import _prepare_ppo_dataset
data = [
{"prompt": "Q1", "answer": "A1"},
{"prompt": "Q2", "answer": "A2"},
{"prompt": "Q3", "answer": "A3"},
]
result = _prepare_ppo_dataset(data)
assert len(result) == 3
def test_prompt_without_answer(self):
from soup_cli.trainer.ppo import _prepare_ppo_dataset
data = [{"prompt": "Tell me a joke"}]
result = _prepare_ppo_dataset(data)
assert result[0]["prompt_text"] == "Tell me a joke"
assert "answer" not in result[0]
# ─── Reward Model Data Preparation Tests ──────────────────────────────────
class TestPrepareRewardDataset:
"""Test reward model dataset preparation."""
def test_from_dpo_format(self):
from soup_cli.trainer.reward_model import _prepare_reward_dataset
data = [{"prompt": "What is AI?", "chosen": "AI is...", "rejected": "I dunno"}]
result = _prepare_reward_dataset(data)
assert len(result) == 1
assert "What is AI?" in result[0]["chosen"]
assert "AI is..." in result[0]["chosen"]
assert "What is AI?" in result[0]["rejected"]
assert "I dunno" in result[0]["rejected"]
def test_chosen_rejected_message_lists(self):
from soup_cli.trainer.reward_model import _prepare_reward_dataset
data = [
{
"prompt": "Hello",
"chosen": [{"role": "assistant", "content": "Hi there!"}],
"rejected": [{"role": "assistant", "content": "Go away"}],
}
]
result = _prepare_reward_dataset(data)
assert "Hi there!" in result[0]["chosen"]
assert "Go away" in result[0]["rejected"]
def test_without_prompt(self):
from soup_cli.trainer.reward_model import _prepare_reward_dataset
data = [{"chosen": "Good answer", "rejected": "Bad answer"}]
result = _prepare_reward_dataset(data)
assert result[0]["chosen"] == "Good answer"
assert result[0]["rejected"] == "Bad answer"
def test_multiple_rows(self):
from soup_cli.trainer.reward_model import _prepare_reward_dataset
data = [
{"prompt": "Q1", "chosen": "Good1", "rejected": "Bad1"},
{"prompt": "Q2", "chosen": "Good2", "rejected": "Bad2"},
]
result = _prepare_reward_dataset(data)
assert len(result) == 2
def test_prompt_as_message_list(self):
from soup_cli.trainer.reward_model import _prepare_reward_dataset
data = [
{
"prompt": [{"role": "user", "content": "Hello"}],
"chosen": "Hi!",
"rejected": "Bye",
}
]
result = _prepare_reward_dataset(data)
assert "Hello" in result[0]["chosen"]
# ─── RLHF Template Tests ─────────────────────────────────────────────────
class TestRLHFTemplate:
"""Test the RLHF template."""
def test_rlhf_template_exists(self):
assert "rlhf" in TEMPLATES
def test_rlhf_template_valid_yaml(self):
config = yaml.safe_load(TEMPLATES["rlhf"])
assert config["task"] == "ppo"
assert config["training"]["ppo_epochs"] == 4
assert config["training"]["ppo_clip_ratio"] == 0.2
assert config["training"]["ppo_kl_penalty"] == 0.05
assert config["training"]["reward_model"] == "./output_rm"
def test_rlhf_template_valid_config(self):
raw = yaml.safe_load(TEMPLATES["rlhf"])
cfg = SoupConfig(**raw)
assert cfg.task == "ppo"
assert cfg.training.ppo_epochs == 4
assert cfg.training.reward_model == "./output_rm"
# ─── Train Command Routing Tests ─────────────────────────────────────────
class TestPPOTrainRouting:
"""Test that train command routes to PPO trainer."""
def test_ppo_import_exists(self):
"""PPOTrainerWrapper should be importable."""
from soup_cli.trainer.ppo import PPOTrainerWrapper
assert PPOTrainerWrapper is not None
def test_ppo_wrapper_init(self):
"""PPOTrainerWrapper should initialize without error."""
from soup_cli.trainer.ppo import PPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
)
wrapper = PPOTrainerWrapper(cfg, device="cpu")
assert wrapper.config.task == "ppo"
assert wrapper.device == "cpu"
assert wrapper.model is None
assert wrapper.trainer is None
def test_ppo_wrapper_with_reward_config(self):
"""PPOTrainerWrapper should accept reward model config."""
from soup_cli.trainer.ppo import PPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"reward_model": "./output_rm"},
)
wrapper = PPOTrainerWrapper(cfg, device="cpu")
assert wrapper.config.training.reward_model == "./output_rm"
def test_ppo_wrapper_deepspeed(self):
"""PPOTrainerWrapper should accept deepspeed config."""
from soup_cli.trainer.ppo import PPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
)
wrapper = PPOTrainerWrapper(cfg, device="cuda", deepspeed_config="/tmp/ds.json")
assert wrapper.deepspeed_config == "/tmp/ds.json"
class TestRewardModelTrainRouting:
"""Test that train command routes to RewardModel trainer."""
def test_reward_model_import_exists(self):
"""RewardModelTrainerWrapper should be importable."""
from soup_cli.trainer.reward_model import RewardModelTrainerWrapper
assert RewardModelTrainerWrapper is not None
def test_reward_model_wrapper_init(self):
"""RewardModelTrainerWrapper should initialize without error."""
from soup_cli.trainer.reward_model import RewardModelTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="reward_model",
data={"train": "./data.jsonl"},
)
wrapper = RewardModelTrainerWrapper(cfg, device="cpu")
assert wrapper.config.task == "reward_model"
assert wrapper.device == "cpu"
assert wrapper.model is None
assert wrapper.trainer is None
# ─── Sweep Shortcut Tests ────────────────────────────────────────────────
class TestPPOSweepParams:
"""Test PPO parameter shortcuts in sweep."""
def test_ppo_epochs_shortcut(self):
from soup_cli.commands.sweep import _set_nested_param
config = {"training": {"ppo_epochs": 4}}
_set_nested_param(config, "ppo_epochs", 8)
assert config["training"]["ppo_epochs"] == 8
def test_ppo_clip_ratio_shortcut(self):
from soup_cli.commands.sweep import _set_nested_param
config = {"training": {"ppo_clip_ratio": 0.2}}
_set_nested_param(config, "ppo_clip_ratio", 0.1)
assert config["training"]["ppo_clip_ratio"] == 0.1
def test_ppo_kl_penalty_shortcut(self):
from soup_cli.commands.sweep import _set_nested_param
config = {"training": {"ppo_kl_penalty": 0.05}}
_set_nested_param(config, "ppo_kl_penalty", 0.1)
assert config["training"]["ppo_kl_penalty"] == 0.1
def test_reward_model_shortcut(self):
from soup_cli.commands.sweep import _set_nested_param
config = {"training": {"reward_model": None}}
_set_nested_param(config, "reward_model", "./my_rm")
assert config["training"]["reward_model"] == "./my_rm"
# ─── Init Command Tests ──────────────────────────────────────────────────
class TestInitRLHF:
"""Test init command with RLHF template."""
@staticmethod
def _strip_ansi(text: str) -> str:
"""Remove ANSI escape codes from text."""
import re
return re.sub(r"\x1b\[[0-9;]*m", "", text)
def test_init_rlhf_template(self, tmp_path):
"""soup init --template rlhf should create a valid config."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
output_file = tmp_path / "soup_ppo.yaml"
result = runner.invoke(app, ["init", "--template", "rlhf", "--output", str(output_file)])
assert result.exit_code == 0
assert output_file.exists()
config = yaml.safe_load(output_file.read_text())
assert config["task"] == "ppo"
def test_init_help_shows_rlhf(self):
"""soup init --help should mention rlhf template."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
result = runner.invoke(app, ["init", "--help"])
assert result.exit_code == 0
clean = self._strip_ansi(result.output)
assert "rlhf" in clean
# ─── CLI Registration Tests ─────────────────────────────────────────────
class TestTrainCliRegistration:
"""Test train command handles PPO and reward_model tasks."""
@staticmethod
def _strip_ansi(text: str) -> str:
"""Remove ANSI escape codes from text."""
import re
return re.sub(r"\x1b\[[0-9;]*m", "", text)
def test_train_help_shows_config(self):
"""soup train --help should show --config option."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
result = runner.invoke(app, ["train", "--help"])
assert result.exit_code == 0
clean = self._strip_ansi(result.output)
assert "--config" in clean
# ─── Edge Cases ──────────────────────────────────────────────────────────
class TestPPOEdgeCases:
"""Test edge cases for PPO configuration."""
def test_ppo_with_reward_fn_and_no_model(self):
"""PPO with reward_fn but no reward_model should be valid."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"reward_fn": "format"},
)
assert cfg.training.reward_fn == "format"
assert cfg.training.reward_model is None
def test_ppo_with_both_reward_sources(self):
"""PPO with both reward_model and reward_fn should be valid."""
cfg = SoupConfig(
base="some-model",
task="ppo",
data={"train": "./data.jsonl"},
training={"reward_model": "./output_rm", "reward_fn": "format"},
)
assert cfg.training.reward_model == "./output_rm"
assert cfg.training.reward_fn == "format"
def test_ppo_with_unsloth_backend(self):
"""PPO should accept unsloth backend."""
cfg = SoupConfig(
base="some-model",
task="ppo",
backend="unsloth",
data={"train": "./data.jsonl"},
)
assert cfg.backend == "unsloth"
def test_reward_model_with_quantization(self):
"""Reward model should accept quantization settings."""
cfg = SoupConfig(
base="some-model",
task="reward_model",
data={"train": "./data.jsonl"},
training={"quantization": "8bit"},
)
assert cfg.training.quantization == "8bit"
def test_all_five_tasks(self):
"""All five task types should be valid."""
for task in ["sft", "dpo", "grpo", "ppo", "reward_model"]:
cfg = SoupConfig(
base="some-model",
task=task,
data={"train": "./data.jsonl"},
)
assert cfg.task == task
def test_invalid_task_rejected(self):
"""Invalid task should be rejected by validation."""
with pytest.raises(Exception):
SoupConfig(
base="some-model",
task="invalid_task",
data={"train": "./data.jsonl"},
)