forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ipo.py
More file actions
428 lines (333 loc) · 14.2 KB
/
Copy pathtest_ipo.py
File metadata and controls
428 lines (333 loc) · 14.2 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
"""Tests for IPO training — config, data format, template, routing, sweep."""
from unittest.mock import MagicMock
from unittest.mock import patch as mock_patch
import pytest
from pydantic import ValidationError
from soup_cli.config.schema import TEMPLATES, SoupConfig
# ─── Config Tests ───────────────────────────────────────────────────────────
class TestIPOConfig:
"""Test IPO task config validation."""
def test_ipo_task_accepted(self):
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
)
assert cfg.task == "ipo"
def test_ipo_tau_default(self):
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
)
assert cfg.training.ipo_tau == 0.1
def test_ipo_tau_custom(self):
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
training={"ipo_tau": 0.5},
)
assert cfg.training.ipo_tau == pytest.approx(0.5)
def test_ipo_tau_must_be_positive(self):
with pytest.raises(ValidationError):
SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
training={"ipo_tau": 0},
)
def test_ipo_full_config(self):
cfg = SoupConfig(
base="meta-llama/Llama-3.1-8B-Instruct",
task="ipo",
data={"train": "./data.jsonl", "format": "dpo", "max_length": 2048},
training={
"epochs": 3,
"lr": 1e-5,
"ipo_tau": 0.2,
"lora": {"r": 64, "alpha": 16},
"quantization": "4bit",
},
)
assert cfg.task == "ipo"
assert cfg.training.ipo_tau == pytest.approx(0.2)
def test_ipo_uses_dpo_data_format(self):
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl", "format": "dpo"},
)
assert cfg.data.format == "dpo"
# ─── Template Tests ──────────────────────────────────────────────────────────
class TestIPOTemplate:
"""Test the IPO template."""
def test_ipo_template_exists(self):
assert "ipo" in TEMPLATES
def test_ipo_template_valid_yaml(self):
import yaml
config = yaml.safe_load(TEMPLATES["ipo"])
assert config["task"] == "ipo"
assert config["training"]["ipo_tau"] == 0.1
assert config["data"]["format"] == "dpo"
def test_ipo_template_valid_config(self):
import yaml
raw = yaml.safe_load(TEMPLATES["ipo"])
cfg = SoupConfig(**raw)
assert cfg.task == "ipo"
assert cfg.training.ipo_tau == 0.1
# ─── Train Command Routing Tests ─────────────────────────────────────────────
class TestIPOTrainRouting:
"""Test that train command routes to IPO trainer."""
def test_ipo_import_exists(self):
from soup_cli.trainer.ipo import IPOTrainerWrapper
assert IPOTrainerWrapper is not None
def test_ipo_wrapper_init(self):
from soup_cli.trainer.ipo import IPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
)
wrapper = IPOTrainerWrapper(cfg, device="cpu")
assert wrapper.config.task == "ipo"
assert wrapper.device == "cpu"
assert wrapper.model is None
assert wrapper.trainer is None
def test_ipo_wrapper_init_with_options(self):
from soup_cli.trainer.ipo import IPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
)
wrapper = IPOTrainerWrapper(
cfg, device="cuda", report_to="wandb", deepspeed_config="ds.json",
)
assert wrapper.report_to == "wandb"
assert wrapper.deepspeed_config == "ds.json"
# ─── Sweep Shortcut Tests ────────────────────────────────────────────────────
class TestIPOSweepParams:
"""Test IPO parameter shortcuts in sweep."""
def test_ipo_tau_shortcut(self):
from soup_cli.commands.sweep import _set_nested_param
config = {"training": {"ipo_tau": 0.1}}
_set_nested_param(config, "ipo_tau", 0.5)
assert config["training"]["ipo_tau"] == 0.5
def test_ipo_tau_shortcut_creates_nested_key(self):
from soup_cli.commands.sweep import _set_nested_param
config = {}
_set_nested_param(config, "ipo_tau", 0.2)
assert config["training"]["ipo_tau"] == pytest.approx(0.2)
def test_sweep_run_single_routes_to_ipo_trainer(self):
from soup_cli.commands.sweep import _run_single
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
)
fake_dataset = {
"train": [
{"prompt": "Q?", "chosen": "A", "rejected": "B"},
]
}
fake_result = {
"initial_loss": 1.0,
"final_loss": 0.5,
"total_steps": 10,
"duration_secs": 60.0,
"output_dir": "./output",
"duration": "1m",
}
fake_gpu_info = {"memory_total": "0 MB", "memory_total_bytes": 0}
with mock_patch("soup_cli.data.loader.load_dataset", return_value=fake_dataset), \
mock_patch("soup_cli.utils.gpu.detect_device", return_value=("cpu", "CPU")), \
mock_patch("soup_cli.utils.gpu.get_gpu_info", return_value=fake_gpu_info), \
mock_patch("soup_cli.experiment.tracker.ExperimentTracker") as mock_tracker_cls, \
mock_patch("soup_cli.monitoring.display.TrainingDisplay"), \
mock_patch("soup_cli.trainer.ipo.IPOTrainerWrapper.setup"), \
mock_patch(
"soup_cli.trainer.ipo.IPOTrainerWrapper.train", return_value=fake_result
) as mock_train:
mock_tracker = MagicMock()
mock_tracker.start_run.return_value = "run-ipo-1"
mock_tracker_cls.return_value = mock_tracker
result = _run_single(cfg, {}, "ipo_run_1", None)
mock_train.assert_called_once()
assert result["run_id"] == "run-ipo-1"
# ─── Config Validation Edge Cases ────────────────────────────────────────────
class TestIPOConfigEdgeCases:
def test_ipo_tau_negative_rejected(self):
with pytest.raises(ValidationError):
SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
training={"ipo_tau": -0.1},
)
def test_ipo_tau_very_large_accepted(self):
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
training={"ipo_tau": 10.0},
)
assert cfg.training.ipo_tau == pytest.approx(10.0)
def test_ipo_config_unsloth_backend(self):
cfg = SoupConfig(
base="some-model",
task="ipo",
backend="unsloth",
data={"train": "./data.jsonl"},
)
assert cfg.backend == "unsloth"
assert cfg.task == "ipo"
def test_ipo_tokenizer_none_before_setup(self):
from soup_cli.trainer.ipo import IPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
)
wrapper = IPOTrainerWrapper(cfg)
assert wrapper.tokenizer is None
def test_ipo_output_dir_none_before_setup(self):
from soup_cli.trainer.ipo import IPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
)
wrapper = IPOTrainerWrapper(cfg)
assert wrapper._output_dir is None
# ─── Train Method Guard Test ──────────────────────────────────────────────────
class TestIPOTrainGuard:
def test_train_before_setup_raises_runtime_error(self):
from soup_cli.trainer.ipo import IPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
)
wrapper = IPOTrainerWrapper(cfg)
with pytest.raises(RuntimeError, match="setup\\(dataset\\) first"):
wrapper.train()
# ─── Train Method Result Structure ───────────────────────────────────────────
class TestIPOTrainResults:
def _make_wrapper_with_mock_trainer(self, log_history=None, global_step=20):
from soup_cli.trainer.ipo import IPOTrainerWrapper
cfg = SoupConfig(
base="some-model",
task="ipo",
data={"train": "./data.jsonl"},
output="./output",
)
wrapper = IPOTrainerWrapper(cfg, device="cpu")
mock_trainer = MagicMock()
mock_trainer.train = MagicMock()
mock_trainer.state.log_history = log_history if log_history is not None else []
mock_trainer.state.global_step = global_step
mock_trainer.save_model = MagicMock()
wrapper.trainer = mock_trainer
wrapper.tokenizer = MagicMock()
wrapper._output_dir = "./output"
return wrapper, mock_trainer
def test_train_returns_expected_keys(self):
wrapper, _ = self._make_wrapper_with_mock_trainer(
log_history=[{"loss": 1.5}, {"loss": 0.8}], global_step=10
)
result = wrapper.train()
assert "initial_loss" in result
assert "final_loss" in result
assert "duration" in result
assert "output_dir" in result
assert "total_steps" in result
def test_train_result_losses(self):
wrapper, _ = self._make_wrapper_with_mock_trainer(
log_history=[{"loss": 2.0}, {"loss": 0.5}], global_step=20
)
result = wrapper.train()
assert result["initial_loss"] == pytest.approx(2.0)
assert result["final_loss"] == pytest.approx(0.5)
def test_train_calls_save_model(self):
wrapper, mock_trainer = self._make_wrapper_with_mock_trainer(
log_history=[{"loss": 1.0}], global_step=5
)
wrapper.train()
mock_trainer.save_model.assert_called_once_with("./output")
def test_train_passes_resume_checkpoint(self):
wrapper, mock_trainer = self._make_wrapper_with_mock_trainer(
log_history=[{"loss": 1.0}], global_step=5
)
wrapper.train(resume_from_checkpoint="/ckpt/checkpoint-50")
mock_trainer.train.assert_called_once_with(
resume_from_checkpoint="/ckpt/checkpoint-50"
)
# ─── CLI Init IPO Template Tests ─────────────────────────────────────────────
class TestIPOInitTemplate:
def test_init_ipo_template_creates_file(self, tmp_path):
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
output = tmp_path / "soup.yaml"
result = runner.invoke(app, ["init", "--template", "ipo", "--output", str(output)])
assert result.exit_code == 0
assert output.exists()
content = output.read_text()
assert "task: ipo" in content
assert "ipo_tau" in content
assert "format: dpo" in content
def test_init_ipo_template_produces_valid_config(self, tmp_path):
from pathlib import Path
from typer.testing import CliRunner
from soup_cli.cli import app
from soup_cli.config.loader import load_config
runner = CliRunner()
output = tmp_path / "soup.yaml"
runner.invoke(app, ["init", "--template", "ipo", "--output", str(output)])
cfg = load_config(Path(output))
assert cfg.task == "ipo"
assert cfg.training.ipo_tau == pytest.approx(0.1)
# ─── Wizard IPO Path Tests ────────────────────────────────────────────────────
class TestIPOWizardPath:
def test_wizard_ipo_task_sets_dpo_format(self):
from soup_cli.commands.init import _interactive_wizard
with mock_patch("soup_cli.commands.init.Prompt.ask", side_effect=[
"some-model",
"ipo",
"./data.jsonl",
"3",
"yes",
]):
config_text = _interactive_wizard()
assert "task: ipo" in config_text
assert "format: dpo" in config_text
assert "ipo_tau: 0.1" in config_text
# ─── Config Loader Round-trip Tests ──────────────────────────────────────────
class TestIPOConfigLoaderRoundTrip:
def test_ipo_template_round_trip(self):
from soup_cli.config.loader import load_config_from_string
cfg = load_config_from_string(TEMPLATES["ipo"])
assert cfg.task == "ipo"
assert cfg.training.ipo_tau == pytest.approx(0.1)
assert cfg.data.format == "dpo"
def test_ipo_custom_yaml_round_trip(self):
from soup_cli.config.loader import load_config_from_string
yaml_str = """
base: custom-model/llama-7b
task: ipo
data:
train: ./ipo_data.jsonl
format: dpo
max_length: 1024
training:
epochs: 5
lr: 5e-6
ipo_tau: 0.05
quantization: none
output: ./ipo_output
"""
cfg = load_config_from_string(yaml_str)
assert cfg.task == "ipo"
assert cfg.training.ipo_tau == pytest.approx(0.05)
assert cfg.training.epochs == 5