forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_part_c.py
More file actions
353 lines (274 loc) · 12.8 KB
/
Copy pathtest_part_c.py
File metadata and controls
353 lines (274 loc) · 12.8 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
"""Part C — v0.28.1 speed/memory live (#43, #44, #47) for v0.33.0.
Covers:
- #43 Multi-trainer wiring: apply_v028_speed_memory helper +
supports_v028_features extension to dpo + pretrain. Schema gate
softens to allow sft / dpo / pretrain.
- #44 install_selective_hooks: per-layer checkpoint hooks across the
three granularities, with a fake transformer-shaped model.
- #47 CrossDocCollator: block-diagonal mask injection via the
underlying build_cross_doc_mask helper.
"""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import MagicMock
import pytest
# ---------------------------------------------------------------------------
# #43 — apply_v028_speed_memory helper
# ---------------------------------------------------------------------------
class TestApplyV028SpeedMemory:
def test_no_features_returns_all_false(self):
from soup_cli.utils.v028_features import apply_v028_speed_memory
tcfg = SimpleNamespace(
use_cut_ce=False, quantization_aware=False,
kernel_auto_compose=False,
)
result = apply_v028_speed_memory(
model=MagicMock(), tcfg=tcfg, base_model="x/y",
)
assert result == {
"cut_ce": False, "fp8": False, "kernel_auto_compose": False,
}
def test_cut_ce_failure_logs_yellow(self, monkeypatch, capsys):
from soup_cli.utils import v028_features as vf
# Force apply_cut_ce import to raise
def _boom(_name):
raise RuntimeError("no cut_cross_entropy")
monkeypatch.setattr(
"soup_cli.utils.cut_ce.apply_cut_ce", _boom, raising=False,
)
from rich.console import Console
console = Console()
tcfg = SimpleNamespace(
use_cut_ce=True, quantization_aware=False,
kernel_auto_compose=False,
)
result = vf.apply_v028_speed_memory(
model=MagicMock(), tcfg=tcfg,
base_model="x/y", console=console,
)
assert result["cut_ce"] is False
def test_supports_v028_features_extends_to_all_transformer_trainers(self):
"""v0.35.0 #60 expanded support from {sft, dpo, pretrain} to every
transformer-backend trainer."""
from soup_cli.utils.v028_features import supports_v028_features
for task in (
"sft", "dpo", "pretrain", "grpo", "kto", "orpo",
"simpo", "ipo", "ppo", "reward_model", "embedding",
):
assert supports_v028_features(task) is True
# Unknown / future tasks default to False
assert supports_v028_features("nonexistent") is False
def test_warn_unsupported_returns_none_for_supported(self):
from soup_cli.utils.v028_features import warn_unsupported_features
tcfg = SimpleNamespace(
use_cut_ce=True, quantization_aware="fp8",
kernel_auto_compose=True, activation_offloading="cpu",
)
for task in (
"sft", "dpo", "pretrain", "grpo", "kto", "orpo",
"simpo", "ipo", "ppo", "reward_model", "embedding",
):
assert warn_unsupported_features(tcfg, task) is None
def test_warn_unsupported_lists_offenders_for_unknown_task(self):
from soup_cli.utils.v028_features import warn_unsupported_features
tcfg = SimpleNamespace(
use_cut_ce=True, quantization_aware="fp8",
kernel_auto_compose=False, activation_offloading=None,
)
msg = warn_unsupported_features(tcfg, "future_task")
assert msg is not None
assert "use_cut_ce" in msg
assert "fp8" in msg
class TestSchemaGateExpanded:
def _config(self, task: str, **training_extra):
import yaml
from soup_cli.config.loader import load_config_from_string
body = {
"base": "test/model",
"task": task,
"data": {"train": "data.jsonl", "format": "alpaca"}
if task != "pretrain"
else {"train": "data.jsonl", "format": "plaintext"},
"training": {"epochs": 1, "lr": 1e-4, "batch_size": 1, **training_extra},
}
if task in ("dpo", "kto", "orpo", "simpo", "ipo", "grpo"):
body["data"]["format"] = "dpo"
return load_config_from_string(yaml.safe_dump(body))
def test_dpo_now_accepts_v028_features(self):
cfg = self._config("dpo", use_cut_ce=True)
assert cfg.task == "dpo"
assert cfg.training.use_cut_ce is True
def test_pretrain_now_accepts_v028_features(self):
cfg = self._config("pretrain", use_cut_ce=True)
assert cfg.task == "pretrain"
def test_kto_now_accepts_v028_features(self):
"""v0.35.0 #60 lifted the SFT-only schema gate; KTO now accepts."""
cfg = self._config("kto", use_cut_ce=True)
assert cfg.task == "kto"
assert cfg.training.use_cut_ce is True
def test_grpo_now_accepts_v028_features(self):
cfg = self._config("grpo", quantization_aware="fp8")
assert cfg.task == "grpo"
assert cfg.training.quantization_aware == "fp8"
# ---------------------------------------------------------------------------
# #44 — install_selective_hooks
# ---------------------------------------------------------------------------
class TestInstallSelectiveHooks:
def test_unknown_granularity_rejected(self):
from soup_cli.utils.gradient_ckpt import install_selective_hooks
with pytest.raises(ValueError, match="must be one of"):
install_selective_hooks(MagicMock(), "weird")
def test_full_wraps_every_block(self):
"""A 4-block fake model with 'medium' wraps 2 blocks (every other)."""
from soup_cli.utils.gradient_ckpt import install_selective_hooks
class FakeBlock:
def __init__(self, idx):
self.idx = idx
def forward(self, *args, **kwargs):
return ("orig", self.idx)
# Simulate having attention sub-modules
def named_modules(self, prefix=""):
yield (f"{prefix}", self)
yield (f"{prefix}.self_attn", _AttnStub(self.idx))
class _AttnStub:
def __init__(self, idx):
self.idx = idx
def forward(self, *args, **kwargs):
return ("attn", self.idx)
class FakeModel:
def __init__(self):
self.blocks = [FakeBlock(i) for i in range(4)]
def named_modules(self):
# HF-style names: ``model.layers.<i>``
for idx, blk in enumerate(self.blocks):
yield (f"model.layers.{idx}", blk)
for child_name, child in blk.named_modules(
prefix=f"model.layers.{idx}",
):
if child_name != f"model.layers.{idx}":
yield (child_name, child)
# Full granularity wraps every numbered child
model = FakeModel()
hooked_full = install_selective_hooks(model, "full")
assert hooked_full == 4
def test_medium_wraps_every_other_block(self):
from soup_cli.utils.gradient_ckpt import install_selective_hooks
class FakeBlock:
def forward(self, *args, **kwargs):
return None
def named_modules(self, prefix=""):
yield (prefix, self)
class FakeModel:
def __init__(self, n):
self.blocks = [FakeBlock() for _ in range(n)]
def named_modules(self):
for i, blk in enumerate(self.blocks):
yield (f"model.layers.{i}", blk)
hooked = install_selective_hooks(FakeModel(6), "medium")
# 6 blocks, every-other → 3
assert hooked == 3
def test_selective_wraps_attention_only(self):
from soup_cli.utils.gradient_ckpt import install_selective_hooks
class _Attn:
def forward(self, *args, **kwargs):
return None
class FakeBlock:
def __init__(self):
self._attn = _Attn()
self._mlp = MagicMock()
self._mlp.forward = lambda *_a, **_k: None
def forward(self, *args, **kwargs):
return None
def named_modules(self, prefix=""):
yield (prefix, self)
yield (f"{prefix}.self_attn", self._attn)
yield (f"{prefix}.mlp", self._mlp)
class FakeModel:
def __init__(self):
self.blocks = [FakeBlock() for _ in range(3)]
def named_modules(self):
for i, blk in enumerate(self.blocks):
yield (f"model.layers.{i}", blk)
for child_name, child in blk.named_modules(
prefix=f"model.layers.{i}",
):
if child_name != f"model.layers.{i}":
yield (child_name, child)
hooked = install_selective_hooks(FakeModel(), "selective")
# 3 blocks, each with one attention child = 3 hooks
assert hooked == 3
# ---------------------------------------------------------------------------
# #47 — CrossDocCollator
# ---------------------------------------------------------------------------
class TestCrossDocCollator:
def test_requires_base_collator(self):
from soup_cli.data.collators import CrossDocCollator
with pytest.raises(ValueError, match="base_collator"):
CrossDocCollator(base_collator=None)
def test_passes_through_when_no_doc_lengths(self):
from soup_cli.data.collators import CrossDocCollator
base = MagicMock(return_value={"input_ids": MagicMock()})
collator = CrossDocCollator(base_collator=base)
result = collator([{"input_ids": [1, 2, 3]}])
assert "input_ids" in result
# No cross_doc_attn_mask since no doc_lengths supplied
assert "cross_doc_attn_mask" not in result
def test_strips_doc_lengths_before_base_call(self):
from soup_cli.data.collators import CrossDocCollator
captured: list[dict] = []
def _base(features):
captured.extend(features)
return {"input_ids": MagicMock()}
collator = CrossDocCollator(base_collator=_base)
collator([{"input_ids": [1, 2], "doc_lengths": [1, 1]}])
# base collator must NOT see the doc_lengths key
assert "doc_lengths" not in captured[0]
def test_mismatched_doc_lengths_falls_back_to_causal(self):
"""If sum(doc_lengths) > seq_length the collator must NOT crash —
it falls back to a plain lower-triangular mask."""
from soup_cli.data.collators import CrossDocCollator
seq_len = 4
class _Tensor:
shape = (1, seq_len)
def _base(features):
return {"input_ids": _Tensor()}
collator = CrossDocCollator(base_collator=_base)
# 3 + 3 = 6 > seq_len 4 → fallback path
result = collator([{"doc_lengths": [3, 3]}])
# Either no mask emitted (graceful skip) or fallback mask present.
# Either is acceptable; what matters is no exception.
assert "input_ids" in result
def test_does_not_mutate_input_dict(self):
"""Regression: collator pops doc_lengths in v0.33.0 wave; HF Dataset
rows are cached, so mutation breaks subsequent batches."""
from soup_cli.data.collators import CrossDocCollator
def _base(features):
return {"input_ids": object()}
collator = CrossDocCollator(base_collator=_base)
original = {"doc_lengths": [2, 2], "input_ids": [1, 2, 3, 4]}
collator([original])
# The original dict must still have doc_lengths after the call.
assert original.get("doc_lengths") == [2, 2]
def test_injects_block_diag_mask_with_doc_lengths(self):
"""End-to-end: when doc_lengths are present, cross_doc_attn_mask
appears on the batch with the right shape."""
import numpy as np
from soup_cli.data.collators import CrossDocCollator
# 2 documents, each 2 tokens long, packed into seq_length=4
seq_len = 4
class _Tensor:
shape = (1, seq_len)
def _base(features):
return {"input_ids": _Tensor()}
collator = CrossDocCollator(base_collator=_base)
result = collator([{"doc_lengths": [2, 2]}])
assert "cross_doc_attn_mask" in result
mask = result["cross_doc_attn_mask"]
# Shape: (batch=1, seq, seq)
assert mask.shape == (1, seq_len, seq_len)
# Token in doc 0 should NOT attend to token in doc 1 (positions 0,1 vs 2,3)
np_mask = mask[0].numpy() if hasattr(mask[0], "numpy") else np.array(mask[0])
# Position 0 attending to position 2 → should be 0
assert np_mask[0, 2] == 0
# Position 2 attending to position 2 → 1 (causal within doc 1)
assert np_mask[2, 2] == 1