forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fp8_recipe.py
More file actions
451 lines (365 loc) · 16.1 KB
/
Copy pathtest_fp8_recipe.py
File metadata and controls
451 lines (365 loc) · 16.1 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
"""Tests for FP8 recipe support (v0.28.1).
Covers:
- Schema: fp8_recipe field accepts valid literals, rejects invalid strings
- Schema: fp8_recipe requires quantization_aware='fp8' when non-default
- Schema: default recipe is 'tensorwise' (backward compat with v0.28.0)
- Dispatch: apply_fp8_training passes correct recipe to Float8LinearConfig
- Integration: fp8_recipe with non-SFT tasks rejected (via quantization_aware gate)
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from pydantic import ValidationError
from soup_cli.config.schema import SoupConfig, TrainingConfig
# ─── Schema: fp8_recipe field ──────────────────────────────────────────────
class TestFP8RecipeSchema:
"""fp8_recipe accepts 'tensorwise', 'rowwise', 'rowwise_with_gw_hp'."""
def test_fp8_recipe_default_tensorwise(self):
cfg = SoupConfig(base="test/model", data={"train": "./data.jsonl"})
assert cfg.training.fp8_recipe == "tensorwise"
def test_fp8_recipe_tensorwise_explicit(self):
cfg = SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={"quantization_aware": "fp8", "fp8_recipe": "tensorwise"},
)
assert cfg.training.fp8_recipe == "tensorwise"
def test_fp8_recipe_rowwise(self):
cfg = SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={"quantization_aware": "fp8", "fp8_recipe": "rowwise"},
)
assert cfg.training.fp8_recipe == "rowwise"
def test_fp8_recipe_rowwise_with_gw_hp(self):
cfg = SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={
"quantization_aware": "fp8",
"fp8_recipe": "rowwise_with_gw_hp",
},
)
assert cfg.training.fp8_recipe == "rowwise_with_gw_hp"
def test_fp8_recipe_invalid_string_rejected(self):
"""Only the three literal values are accepted."""
with pytest.raises(ValidationError) as exc:
SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={
"quantization_aware": "fp8",
"fp8_recipe": "delayed",
},
)
assert "fp8_recipe" in str(exc.value)
def test_fp8_recipe_invalid_empty_string_rejected(self):
with pytest.raises(ValidationError):
SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={"quantization_aware": "fp8", "fp8_recipe": ""},
)
def test_fp8_recipe_invalid_int_rejected(self):
with pytest.raises(ValidationError):
SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={"quantization_aware": "fp8", "fp8_recipe": 42},
)
# ─── Schema: fp8_recipe requires quantization_aware='fp8' ─────────────────
class TestFP8RecipeRequiresFP8:
"""Non-default fp8_recipe without quantization_aware='fp8' is rejected."""
def test_default_recipe_allowed_without_fp8(self):
"""tensorwise (default) is fine even without fp8 — it's the default."""
cfg = SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={"fp8_recipe": "tensorwise"},
)
assert cfg.training.fp8_recipe == "tensorwise"
assert cfg.training.quantization_aware is False
def test_rowwise_without_fp8_rejected(self):
with pytest.raises(ValidationError) as exc:
SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={"fp8_recipe": "rowwise"},
)
assert "quantization_aware" in str(exc.value)
def test_rowwise_with_gw_hp_without_fp8_rejected(self):
with pytest.raises(ValidationError) as exc:
SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={"fp8_recipe": "rowwise_with_gw_hp"},
)
assert "quantization_aware" in str(exc.value)
def test_rowwise_with_bool_true_qat_rejected(self):
"""Bool True = int8 QAT, not FP8 — recipe should be rejected."""
with pytest.raises(ValidationError) as exc:
SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={
"quantization_aware": True,
"fp8_recipe": "rowwise",
},
)
assert "quantization_aware" in str(exc.value)
# ─── Dispatch: apply_fp8_training recipe parameter ────────────────────────
class TestFP8RecipeDispatch:
"""apply_fp8_training passes recipe to Float8LinearConfig.from_recipe_name."""
def test_apply_fp8_dispatches_tensorwise(self):
"""Default recipe passes 'tensorwise' to from_recipe_name."""
mock_config = MagicMock()
mock_from_recipe = MagicMock(return_value=mock_config)
mock_convert = MagicMock()
fake_float8 = MagicMock()
fake_float8.convert_to_float8_training = mock_convert
fake_config_mod = MagicMock()
fake_config_mod.Float8LinearConfig.from_recipe_name = mock_from_recipe
with patch.dict(
"sys.modules",
{
"torchao": MagicMock(),
"torchao.float8": fake_float8,
"torchao.float8.config": fake_config_mod,
},
):
# Need to reimport to pick up the mocked modules
import importlib
import soup_cli.utils.fp8 as fp8_mod
importlib.reload(fp8_mod)
# Mock is_fp8_available to return True
with patch.object(fp8_mod, "is_fp8_available", return_value=True):
model = MagicMock()
result = fp8_mod.apply_fp8_training(model, recipe="tensorwise")
mock_from_recipe.assert_called_once_with("tensorwise")
mock_convert.assert_called_once_with(model, config=mock_config)
assert result is True
def test_apply_fp8_dispatches_rowwise(self):
"""Rowwise recipe passes 'rowwise' to from_recipe_name."""
mock_config = MagicMock()
mock_from_recipe = MagicMock(return_value=mock_config)
mock_convert = MagicMock()
fake_float8 = MagicMock()
fake_float8.convert_to_float8_training = mock_convert
fake_config_mod = MagicMock()
fake_config_mod.Float8LinearConfig.from_recipe_name = mock_from_recipe
with patch.dict(
"sys.modules",
{
"torchao": MagicMock(),
"torchao.float8": fake_float8,
"torchao.float8.config": fake_config_mod,
},
):
import importlib
import soup_cli.utils.fp8 as fp8_mod
importlib.reload(fp8_mod)
with patch.object(fp8_mod, "is_fp8_available", return_value=True):
model = MagicMock()
result = fp8_mod.apply_fp8_training(model, recipe="rowwise")
mock_from_recipe.assert_called_once_with("rowwise")
assert result is True
def test_apply_fp8_dispatches_rowwise_with_gw_hp(self):
"""rowwise_with_gw_hp recipe passes through correctly."""
mock_config = MagicMock()
mock_from_recipe = MagicMock(return_value=mock_config)
mock_convert = MagicMock()
fake_float8 = MagicMock()
fake_float8.convert_to_float8_training = mock_convert
fake_config_mod = MagicMock()
fake_config_mod.Float8LinearConfig.from_recipe_name = mock_from_recipe
with patch.dict(
"sys.modules",
{
"torchao": MagicMock(),
"torchao.float8": fake_float8,
"torchao.float8.config": fake_config_mod,
},
):
import importlib
import soup_cli.utils.fp8 as fp8_mod
importlib.reload(fp8_mod)
with patch.object(fp8_mod, "is_fp8_available", return_value=True):
model = MagicMock()
result = fp8_mod.apply_fp8_training(
model, recipe="rowwise_with_gw_hp"
)
mock_from_recipe.assert_called_once_with("rowwise_with_gw_hp")
assert result is True
def test_apply_fp8_returns_false_when_unavailable(self):
"""When FP8 deps are missing, apply_fp8_training returns False."""
from soup_cli.utils.fp8 import apply_fp8_training
with patch("soup_cli.utils.fp8.is_fp8_available", return_value=False):
model = MagicMock()
assert apply_fp8_training(model, recipe="rowwise") is False
def test_apply_fp8_default_recipe_is_tensorwise(self):
"""Calling without recipe= uses 'tensorwise' (v0.28.0 compat)."""
mock_config = MagicMock()
mock_from_recipe = MagicMock(return_value=mock_config)
mock_convert = MagicMock()
fake_float8 = MagicMock()
fake_float8.convert_to_float8_training = mock_convert
fake_config_mod = MagicMock()
fake_config_mod.Float8LinearConfig.from_recipe_name = mock_from_recipe
with patch.dict(
"sys.modules",
{
"torchao": MagicMock(),
"torchao.float8": fake_float8,
"torchao.float8.config": fake_config_mod,
},
):
import importlib
import soup_cli.utils.fp8 as fp8_mod
importlib.reload(fp8_mod)
with patch.object(fp8_mod, "is_fp8_available", return_value=True):
model = MagicMock()
fp8_mod.apply_fp8_training(model)
# Default should be tensorwise
mock_from_recipe.assert_called_once_with("tensorwise")
# ─── Integration: fp8_recipe + non-SFT tasks ──────────────────────────────
class TestFP8RecipeNonSFT:
"""FP8 recipe on non-SFT tasks: accepted on transformer backends (v0.35.0)."""
def test_fp8_recipe_on_dpo_accepted(self):
"""DPO + fp8 + recipe is valid (all transformer trainers wired in v0.35.0)."""
cfg = SoupConfig(
base="m",
task="dpo",
data={"train": "./d.jsonl", "format": "dpo"},
training={
"quantization_aware": "fp8",
"fp8_recipe": "rowwise",
},
)
assert cfg.training.fp8_recipe == "rowwise"
def test_fp8_recipe_on_grpo_accepted(self):
cfg = SoupConfig(
base="m",
task="grpo",
data={"train": "./d.jsonl"},
training={
"quantization_aware": "fp8",
"fp8_recipe": "rowwise_with_gw_hp",
},
)
assert cfg.training.fp8_recipe == "rowwise_with_gw_hp"
def test_fp8_recipe_on_mlx_rejected(self):
"""MLX backend does not support FP8 — rejected at config level."""
with pytest.raises(ValidationError):
SoupConfig(
base="m",
task="sft",
backend="mlx",
data={"train": "./d.jsonl"},
training={
"quantization_aware": "fp8",
"fp8_recipe": "rowwise",
},
)
def test_fp8_recipe_on_sft_accepted(self):
"""SFT + fp8 + recipe is valid."""
cfg = SoupConfig(
base="m",
task="sft",
data={"train": "./d.jsonl"},
training={
"quantization_aware": "fp8",
"fp8_recipe": "rowwise",
},
)
assert cfg.training.fp8_recipe == "rowwise"
assert cfg.training.quantization_aware == "fp8"
def test_all_recipes_accepted_on_sft(self):
"""All three recipes are valid on SFT with fp8."""
for recipe in ("tensorwise", "rowwise", "rowwise_with_gw_hp"):
cfg = SoupConfig(
base="m",
task="sft",
data={"train": "./d.jsonl"},
training={
"quantization_aware": "fp8",
"fp8_recipe": recipe,
},
)
assert cfg.training.fp8_recipe == recipe
# ─── Backward compatibility ───────────────────────────────────────────────
class TestFP8RecipeBackwardCompat:
"""v0.28.0 configs without fp8_recipe still work (defaults to tensorwise)."""
def test_v028_config_no_recipe_field(self):
"""Config with quantization_aware='fp8' but no fp8_recipe is valid."""
cfg = SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={"quantization_aware": "fp8"},
)
assert cfg.training.quantization_aware == "fp8"
assert cfg.training.fp8_recipe == "tensorwise"
def test_v028_bool_true_unaffected(self):
"""Bool True (int8 QAT) is unaffected by fp8_recipe field."""
cfg = SoupConfig(
base="test/model",
data={"train": "./data.jsonl"},
training={"quantization_aware": True},
)
assert cfg.training.quantization_aware is True
assert cfg.training.fp8_recipe == "tensorwise" # default, unused
def test_training_config_fp8_recipe_default(self):
"""TrainingConfig alone defaults fp8_recipe to tensorwise."""
tcfg = TrainingConfig()
assert tcfg.fp8_recipe == "tensorwise"
# ─── Dispatch through apply_v028_speed_memory (covers 10 non-SFT trainers) ──
class TestFP8RecipeViaV028Features:
"""All non-SFT trainers go through apply_v028_speed_memory; verify it
passes the user-configured recipe through to apply_fp8_training instead
of always defaulting to tensorwise (silent no-op bug fix)."""
def _run_dispatch(self, recipe: str) -> MagicMock:
"""Invoke apply_v028_speed_memory with quantization_aware=fp8 and
the given recipe, returning the patched apply_fp8_training mock."""
tcfg = MagicMock()
tcfg.quantization_aware = "fp8"
tcfg.fp8_recipe = recipe
tcfg.use_cut_ce = False
tcfg.kernel_auto_compose = False
with patch("soup_cli.utils.fp8.apply_fp8_training", return_value=True) as m:
from soup_cli.utils.v028_features import apply_v028_speed_memory
apply_v028_speed_memory(
model=MagicMock(),
tcfg=tcfg,
base_model="test/model",
console=None,
device="cuda",
backend="transformers",
)
return m
def test_v028_dispatch_tensorwise(self):
m = self._run_dispatch("tensorwise")
m.assert_called_once()
assert m.call_args.kwargs.get("recipe") == "tensorwise"
def test_v028_dispatch_rowwise(self):
m = self._run_dispatch("rowwise")
m.assert_called_once()
assert m.call_args.kwargs.get("recipe") == "rowwise"
def test_v028_dispatch_rowwise_with_gw_hp(self):
m = self._run_dispatch("rowwise_with_gw_hp")
m.assert_called_once()
assert m.call_args.kwargs.get("recipe") == "rowwise_with_gw_hp"
def test_v028_skips_apply_when_quant_not_fp8(self):
"""If quantization_aware != 'fp8', apply_fp8_training is not called."""
tcfg = MagicMock()
tcfg.quantization_aware = True # int8 QAT, not fp8
tcfg.fp8_recipe = "rowwise"
tcfg.use_cut_ce = False
tcfg.kernel_auto_compose = False
with patch("soup_cli.utils.fp8.apply_fp8_training") as m:
from soup_cli.utils.v028_features import apply_v028_speed_memory
apply_v028_speed_memory(
model=MagicMock(),
tcfg=tcfg,
base_model="test/model",
console=None,
)
m.assert_not_called()