forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_issue341_seed_and_fullft.py
More file actions
702 lines (601 loc) · 28.9 KB
/
Copy pathtest_issue341_seed_and_fullft.py
File metadata and controls
702 lines (601 loc) · 28.9 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
"""#341 (training seed) + #340 (first-class full fine-tuning).
Two schema gaps that share `config/schema.py` + `trainer/sft.py`.
**#341** — there was no general training seed. `TrainingArguments` took HF's
default on every run (`seed=42, data_seed=None`), so "run it again with a
different seed" was impossible and `sft.py`'s `getattr(tcfg, "seed", 0) or 0`
was a defensive lookup for an attribute that did not exist.
**#340** — `SFTTrainerWrapper._setup_transformers` had no path that trains
without adapters; the `else` branch applied LoRA unconditionally. The only
shipped spelling for full fine-tuning was `unfrozen_parameters: ['.*']`, the
Spectrum regex feature used as a workaround. `lora.r: 0` is now the
first-class switch — the spelling `trainer/classifier.py` already uses
(`_lora_active = classifier_lora and tcfg.lora.r > 0`) and the one
`commands/card.py::_is_adapter` already reads correctly.
The controls are the point:
* seed — two runs at the SAME seed must reproduce each other. Without that,
"different seeds give different weights" also passes for a seed that is
ignored entirely (any two runs differ for unrelated reasons).
* full-FT — a normal LoRA config must STILL produce a PEFT-wrapped model with
the base frozen. Without that, "no PEFT wrapper" also passes for a build
that silently stopped applying LoRA to everyone.
* backwards compatibility — a config setting neither new field must reach
`TrainingArguments` with `seed=42, data_seed=None`, i.e. exactly what HF's
own defaults produced before this change.
The schema half is import-light and runs without any `[train]` extra; the
behavioural half skips (loudly) when torch / peft / trl are absent.
"""
import json
import os
import pytest
import yaml
from soup_cli.config.loader import load_config_from_string
# ==========================================================================
# Shared config builders (pure YAML — no torch)
# ==========================================================================
_BASE_CFG = {
# A Llama-family name on purpose: several unrelated validators
# (`use_longlora`, multipack, MoD) carry architecture allowlists keyed off
# the base string, and a gpt2 name trips those first — masking the gate
# under test with someone else's rejection.
"base": "meta-llama/Llama-3.2-1B",
"task": "sft",
"backend": "transformers",
"modality": "text",
"data": {"train": "train.jsonl"},
"training": {"quantization": "none"},
}
def _cfg_yaml(**overrides):
"""Deep-ish merge of `training` / `data` overrides onto the base config."""
import copy
cfg = copy.deepcopy(_BASE_CFG)
for key, val in overrides.items():
if key in ("training", "data", "lora") and isinstance(val, dict):
if key == "lora":
cfg["training"].setdefault("lora", {}).update(val)
else:
cfg[key].update(val)
else:
cfg[key] = val
return yaml.safe_dump(cfg)
def _load(**overrides):
return load_config_from_string(_cfg_yaml(**overrides))
# ==========================================================================
# #341 — schema (light: no torch)
# ==========================================================================
class TestSeedSchema:
def test_seed_and_data_seed_exist_and_default_to_none(self):
"""`None` (not 42) is the schema default so the trainer can tell
"unset" from "explicitly 42" — the multipack sampler's historical
default of 0 has to survive an unset seed, while `TrainingArguments`
keeps HF's 42."""
cfg = _load()
assert cfg.training.seed is None
assert cfg.training.data_seed is None
def test_seed_accepts_an_int(self):
assert _load(training={"seed": 1234}).training.seed == 1234
def test_data_seed_accepts_an_int(self):
assert _load(training={"data_seed": 99}).training.data_seed == 99
def test_zero_is_a_legitimate_seed(self):
"""0 is a real seed, not a sentinel — a `ge=1` bound would silently
make `seed: 0` unusable."""
assert _load(training={"seed": 0}).training.seed == 0
@pytest.mark.parametrize("field", ["seed", "data_seed"])
def test_bool_is_rejected(self, field):
"""`bool` subclasses `int`, so `seed: true` would silently become
seed 1. Mirrors the v0.71.34 LISA / v0.41.0 expand_layers policy."""
with pytest.raises(ValueError, match="(?i)bool"):
_load(training={field: True})
@pytest.mark.parametrize("field", ["seed", "data_seed"])
def test_negative_is_rejected(self, field):
with pytest.raises(ValueError):
_load(training={field: -1})
@pytest.mark.parametrize("field", ["seed", "data_seed"])
def test_absurdly_large_is_rejected(self, field):
"""torch's RNG takes a 64-bit seed; numpy's `default_rng` and HF's
`set_seed` path go through 32-bit consumers, so an out-of-range value
raises deep inside numpy at step 0 instead of at config load."""
with pytest.raises(ValueError):
_load(training={field: 2**32})
def test_seed_survives_a_yaml_round_trip(self):
cfg = _load(training={"seed": 7, "data_seed": 8})
dumped = yaml.safe_dump(json.loads(cfg.model_dump_json()))
again = load_config_from_string(dumped)
assert (again.training.seed, again.training.data_seed) == (7, 8)
# ==========================================================================
# #340 — schema (light: no torch)
# ==========================================================================
class TestFullFinetuneSchema:
def test_lora_r_zero_is_accepted_on_sft(self):
cfg = _load(lora={"r": 0})
assert cfg.training.lora.r == 0
def test_control_the_default_lora_config_is_still_valid(self):
"""CONTROL — the new gates must not reject an ordinary LoRA run."""
cfg = _load()
assert cfg.training.lora.r == 64
def test_negative_rank_is_rejected(self):
"""Pre-existing hole: `r` had no lower bound, so `r: -5` parsed and
died inside peft. 0 now means full-FT; below 0 means nothing."""
with pytest.raises(ValueError):
_load(lora={"r": -5})
def test_full_ft_requires_transformers_backend(self):
with pytest.raises(ValueError, match="backend"):
_load(backend="unsloth", lora={"r": 0})
def test_full_ft_requires_text_modality(self):
with pytest.raises(ValueError, match="modality"):
_load(modality="vision", lora={"r": 0})
def test_full_ft_requires_quantization_none(self):
"""Quantized weights cannot be trained directly — the same reason
Spectrum and LISA carry this gate."""
with pytest.raises(ValueError, match="quantization"):
_load(training={"quantization": "4bit"}, lora={"r": 0})
@pytest.mark.parametrize(
"training_over,lora_over,needle",
[
({}, {"use_dora": True}, "use_dora"),
({}, {"use_vera": True}, "use_vera"),
({}, {"use_olora": True}, "use_olora"),
({}, {"use_rslora": True}, "use_rslora"),
({}, {"init_strategy": "pissa"}, "init_strategy"),
({}, {"rank_pattern": {"q_proj": 8}}, "rank_pattern"),
({}, {"alpha_pattern": {"q_proj": 8}}, "alpha_pattern"),
({"moe_lora": True}, {}, "moe_lora"),
({"relora_steps": 100}, {}, "relora_steps"),
({"loraplus_lr_ratio": 4.0}, {}, "loraplus_lr_ratio"),
({"use_longlora": True}, {}, "use_longlora"),
],
)
def test_full_ft_rejects_lora_features(self, training_over, lora_over, needle):
"""A LoRA knob alongside `r: 0` is a contradiction the user must see —
silently ignoring it is how someone ends up believing DoRA ran."""
over = dict(training_over)
with pytest.raises(ValueError, match=needle):
_load(training=over, lora={"r": 0, **lora_over})
def test_full_ft_rejects_unfrozen_parameters(self):
"""Both select what trains; together there is no defined answer."""
with pytest.raises(ValueError, match="unfrozen_parameters"):
_load(
training={"unfrozen_parameters": ["model.layers.0.mlp.down_proj"]},
lora={"r": 0},
)
def test_full_ft_rejects_lisa(self):
with pytest.raises(ValueError, match="lisa_enabled"):
_load(training={"lisa_enabled": True}, lora={"r": 0})
def test_full_ft_rejects_layer_streaming(self):
"""Streaming keeps the decoder on the meta device and trains only the
adapter, so full-FT is not merely unwise there, it is impossible. The
message must say so rather than the pre-#340 "nothing trainable"."""
with pytest.raises(ValueError, match="(?i)full fine-tun"):
_load(
training={
"stream_layers": True,
"batch_size": 1,
"gradient_accumulation_steps": 1,
},
lora={"r": 0},
)
def test_the_gates_are_scoped_to_sft_and_do_not_fire_elsewhere(self):
"""DELIBERATE SCOPING, pinned so it cannot be "tidied up" later.
`r: 0` gains full-FT semantics for `task='sft'` only. Widening the
gate to every task would reject configs that are legal TODAY — see
the two controls below — so a non-sft task keeps whatever it does
now (peft's "`r` should be a positive integer value" where the rank
is passed through unconditionally).
"""
cfg = _load(task="dpo", data={"format": "dpo"}, lora={"r": 0})
assert cfg.training.lora.r == 0
# ... and the sft-only gates did NOT fire: this combination would be
# refused under task='sft' (4bit), but is untouched here.
assert _load(
task="dpo",
data={"format": "dpo"},
training={"quantization": "4bit"},
lora={"r": 0},
).training.quantization == "4bit"
def test_control_asr_may_still_set_rank_zero(self):
"""CONTROL — `trainer/asr.py` gates LoRA behind `asr_lora` (v0.71.32,
default False = full fine-tune), so `r: 0` is already inert there."""
cfg = load_config_from_string(
yaml.safe_dump(
{
"base": "openai/whisper-tiny",
"task": "asr",
"backend": "transformers",
"data": {"train": "train.jsonl", "format": "asr"},
"training": {"quantization": "none", "lora": {"r": 0}},
}
)
)
assert cfg.training.lora.r == 0
def test_control_classifier_may_still_set_rank_zero(self):
"""CONTROL — `trainer/classifier.py` has read `lora.r > 0` as "no
adapter" since v0.71.12, and full fine-tuning is the classifier's
DEFAULT. A gate that refused `r: 0` outside sft would break a config
that is legal today."""
cfg = load_config_from_string(
yaml.safe_dump(
{
"base": "bert-base-uncased",
"task": "classifier",
"data": {"train": "train.jsonl", "format": "auto"},
"training": {"num_labels": 2, "lora": {"r": 0}},
}
)
)
assert cfg.training.lora.r == 0
# ==========================================================================
# Behavioural half — needs the [train] extra
# ==========================================================================
def _requires_train_extra():
for mod in ("torch", "transformers", "peft", "trl", "datasets"):
pytest.importorskip(mod, reason=f"{mod} is only in the [train] extra")
def _tiny_llama_dir(tmp_path, n_layers=2):
"""A real (tiny) Llama checkpoint + offline tokenizer on disk.
Complete on purpose: `from_pretrained` must initialise NOTHING at random,
or two runs in one process would differ for a reason that is not the seed.
"""
import torch
from safetensors.torch import save_file
from transformers import LlamaConfig, LlamaForCausalLM
torch.manual_seed(7)
config = LlamaConfig(
vocab_size=64,
hidden_size=32,
intermediate_size=64,
num_hidden_layers=n_layers,
num_attention_heads=4,
num_key_value_heads=2,
tie_word_embeddings=True,
max_position_embeddings=128,
# A downloaded checkpoint always carries this; a from-scratch config
# does not, and `multipack_trainer.detect_arch_name` reads it FIRST,
# falling back to `type(model).__name__` — which under LoRA is
# `PeftModelForCausalLM` and matches no allowlist. Without this the
# multipack tests would fail on a fixture artifact no user can hit.
architectures=["LlamaForCausalLM"],
)
model = LlamaForCausalLM(config).to(torch.float32).eval()
weights = tmp_path / "model"
weights.mkdir(parents=True, exist_ok=True)
state = {k: v.contiguous() for k, v in model.state_dict().items()}
state.pop("lm_head.weight", None) # tied
save_file(state, str(weights / "model.safetensors"))
config.save_pretrained(str(weights))
_write_tiny_tokenizer(str(weights))
return str(weights)
def _write_tiny_tokenizer(directory):
"""A real, offline PreTrainedTokenizerFast — no HF cache, no network."""
from tokenizers import Tokenizer, models, pre_tokenizers
vocab = {"<unk>": 0, "<s>": 1, "</s>": 2, "<pad>": 3}
for word in (
"hello", "world", "hi", "yo", "the", "cat", "sat", "on", "mat",
"dog", "ran", "fast", "slow", "red", "blue", "green", "one", "two",
):
vocab[word] = len(vocab)
tokenizer = Tokenizer(models.WordLevel(vocab=vocab, unk_token="<unk>"))
tokenizer.pre_tokenizer = pre_tokenizers.Whitespace()
tokenizer.save(os.path.join(directory, "tokenizer.json"))
with open(os.path.join(directory, "tokenizer_config.json"), "w", encoding="utf-8") as fh:
json.dump(
{
"tokenizer_class": "PreTrainedTokenizerFast",
"unk_token": "<unk>",
"bos_token": "<s>",
"eos_token": "</s>",
"pad_token": "<pad>",
"model_max_length": 128,
"clean_up_tokenization_spaces": False,
},
fh,
)
_ROWS = [
("hi", "hello world"),
("yo", "the cat sat"),
("hello", "on the mat"),
("the dog", "ran fast"),
("red", "blue green"),
("one", "two red"),
("cat", "sat slow"),
("world", "hi yo"),
]
def _dataset():
return {
"train": [
{
"messages": [
{"role": "user", "content": user},
{"role": "assistant", "content": assistant},
]
}
for user, assistant in _ROWS
]
}
def _wrapper(tmp_path, monkeypatch, base=None, **training_over):
"""A real SFTTrainerWrapper over a real tiny checkpoint."""
from soup_cli.trainer.sft import SFTTrainerWrapper
base = base or _tiny_llama_dir(tmp_path)
monkeypatch.chdir(tmp_path)
training = {
"batch_size": 1,
"gradient_accumulation_steps": 1,
"quantization": "none",
"epochs": 1,
"lr": 1e-3,
"logging_steps": 100,
"save_steps": 10_000,
"lora": {"r": 4, "alpha": 8, "dropout": 0.0, "target_modules": ["q_proj", "v_proj"]},
}
lora_over = training_over.pop("lora", None)
training.update(training_over)
if lora_over is not None:
training["lora"] = {**training["lora"], **lora_over}
cfg = load_config_from_string(
yaml.safe_dump(
{
"base": base,
"task": "sft",
"backend": "transformers",
"modality": "text",
"data": {"train": "train.jsonl", "max_length": 64, "chat_template": "chatml"},
"training": training,
"output": str(tmp_path / "out"),
}
)
)
return SFTTrainerWrapper(cfg, device="cpu"), _dataset()
# ==========================================================================
# #341 — the seed reaches TrainingArguments
# ==========================================================================
class TestSeedReachesTrainingArguments:
def test_default_is_still_42_and_data_seed_none(self, tmp_path, monkeypatch):
"""BACKWARDS COMPATIBILITY. A config that sets neither field must
produce byte-identical TrainingArguments to the pre-#341 build, where
HF's own defaults applied."""
_requires_train_extra()
wrapper, dataset = _wrapper(tmp_path, monkeypatch)
wrapper.setup(dataset)
assert wrapper.trainer.args.seed == 42
assert wrapper.trainer.args.data_seed is None
def test_configured_seed_reaches_the_trainer(self, tmp_path, monkeypatch):
_requires_train_extra()
wrapper, dataset = _wrapper(tmp_path, monkeypatch, seed=1234)
wrapper.setup(dataset)
assert wrapper.trainer.args.seed == 1234
def test_configured_data_seed_reaches_the_trainer(self, tmp_path, monkeypatch):
_requires_train_extra()
wrapper, dataset = _wrapper(tmp_path, monkeypatch, seed=5, data_seed=99)
wrapper.setup(dataset)
assert wrapper.trainer.args.seed == 5
assert wrapper.trainer.args.data_seed == 99
class TestSeedActuallyChangesTraining:
"""The acceptance pair from #341. Neither test means anything alone."""
@staticmethod
def _train(tmp_path, monkeypatch, base, seed):
import torch
# Normalise the pre-Trainer RNG state: `get_peft_model` draws lora_A
# from the GLOBAL generator, and these runs share one process, so
# without this the second run's adapter starts somewhere else and
# every comparison below measures init, not the seed.
torch.manual_seed(0)
wrapper, dataset = _wrapper(tmp_path, monkeypatch, base=base, seed=seed)
wrapper.setup(dataset)
wrapper.trainer.train()
# `.cpu()` because HF's Trainer moves the model to `args.device`, which
# is CUDA on a GPU box even though the wrapper was built with
# device="cpu" — comparing a cpu clone against a cuda tensor raises.
return {
name: param.detach().cpu().clone()
for name, param in wrapper.model.named_parameters()
if "lora_" in name and param.requires_grad
}
def test_same_seed_reproduces(self, tmp_path, monkeypatch):
"""THE CONTROL. Without it, `test_different_seed_diverges` passes for
a seed that is thrown away — two runs of anything differ."""
_requires_train_extra()
import torch
base = _tiny_llama_dir(tmp_path)
first = self._train(tmp_path, monkeypatch, base, seed=1234)
second = self._train(tmp_path, monkeypatch, base, seed=1234)
assert first and second, "no trainable LoRA parameters were captured"
for name in first:
assert torch.equal(first[name], second[name]), (
f"{name} differs across two runs at the SAME seed"
)
def test_different_seed_diverges(self, tmp_path, monkeypatch):
_requires_train_extra()
import torch
base = _tiny_llama_dir(tmp_path)
first = self._train(tmp_path, monkeypatch, base, seed=1234)
other = self._train(tmp_path, monkeypatch, base, seed=4321)
assert first and other
assert any(
not torch.equal(first[name], other[name]) for name in first
), "changing training.seed changed nothing — the value is being ignored"
class TestMultipackSamplerSeed:
"""`sft.py`'s `getattr(tcfg, "seed", 0) or 0` was a lookup for an
attribute that did not exist, so the FFD sampler was always seeded 0."""
@staticmethod
def _seed_passed_to_multipack(tmp_path, monkeypatch, **over):
import soup_cli.utils.multipack_trainer as mt
seen = {}
real = mt.attach_multipack_state
def spy(trainer, **kwargs):
seen.update(kwargs)
return real(trainer, **kwargs)
monkeypatch.setattr(mt, "attach_multipack_state", spy)
wrapper, dataset = _wrapper(tmp_path, monkeypatch, multipack=True, **over)
wrapper.setup(dataset)
return seen["seed"]
def test_configured_seed_reaches_the_sampler(self, tmp_path, monkeypatch):
_requires_train_extra()
assert self._seed_passed_to_multipack(tmp_path, monkeypatch, seed=77) == 77
def test_control_unset_seed_still_seeds_the_sampler_zero(self, tmp_path, monkeypatch):
"""CONTROL + backwards compatibility. An unset seed must keep the
historical 0, or every existing multipack run silently re-orders."""
_requires_train_extra()
assert self._seed_passed_to_multipack(tmp_path, monkeypatch) == 0
# ==========================================================================
# #340 — full fine-tuning in the trainer
# ==========================================================================
class TestFullFinetuneTrainer:
def test_rank_zero_leaves_the_model_unwrapped_and_fully_trainable(
self, tmp_path, monkeypatch
):
_requires_train_extra()
wrapper, dataset = _wrapper(tmp_path, monkeypatch, lora={"r": 0})
wrapper.setup(dataset)
from peft import PeftModel
assert not isinstance(wrapper.model, PeftModel), (
"full fine-tuning must not wrap the model in PEFT"
)
assert not any("lora_" in name for name, _ in wrapper.model.named_parameters())
untrainable = [
name
for name, param in wrapper.model.named_parameters()
if not param.requires_grad
]
assert not untrainable, f"full-FT left parameters frozen: {untrainable[:5]}"
def test_control_lora_still_wraps_in_peft_with_the_base_frozen(
self, tmp_path, monkeypatch
):
"""CONTROL. Without it, the test above also passes for a build that
stopped applying LoRA to everybody."""
_requires_train_extra()
wrapper, dataset = _wrapper(tmp_path, monkeypatch)
wrapper.setup(dataset)
from peft import PeftModel
assert isinstance(wrapper.model, PeftModel)
trainable = [
name
for name, param in wrapper.model.named_parameters()
if param.requires_grad
]
assert trainable, "the LoRA control trains nothing"
assert all("lora_" in name for name in trainable), (
f"base weights are trainable under LoRA: {trainable[:5]}"
)
def test_a_full_ft_step_actually_updates_base_weights(self, tmp_path, monkeypatch):
"""The end-to-end claim: forward + backward + optimizer step move the
BASE weights, which is the whole point of full fine-tuning."""
_requires_train_extra()
import torch
wrapper, dataset = _wrapper(tmp_path, monkeypatch, lora={"r": 0}, seed=3)
wrapper.setup(dataset)
target = "model.layers.0.self_attn.q_proj.weight"
before = dict(wrapper.model.named_parameters())[target].detach().cpu().clone()
wrapper.trainer.args.max_steps = 2
wrapper.trainer.train()
after = dict(wrapper.model.named_parameters())[target].detach().cpu()
assert not torch.equal(before, after), (
"a full fine-tuning step left the base weights untouched"
)
def test_the_summary_line_says_full_fine_tuning_not_lora_applied(
self, tmp_path, monkeypatch, capsys
):
"""`setup()` prints "LoRA applied: N trainable" — a false statement on
a run with no adapter, and the line an operator screenshots."""
_requires_train_extra()
wrapper, dataset = _wrapper(tmp_path, monkeypatch, lora={"r": 0})
wrapper.setup(dataset)
out = capsys.readouterr().out
assert "LoRA applied" not in out
assert "Full fine-tuning" in out
def test_the_summary_line_says_lisa_not_lora_applied(
self, tmp_path, monkeypatch, capsys
):
"""#471 review round 3 — the same false-statement bug as the
`lora.r=0` case above, for `lisa_enabled` instead: `setup()`'s
summary label was missing this flag entirely (only checked
`unfrozen_parameters` / `lora.r==0`), so a LISA run printed "LoRA
applied" though it never attaches an adapter. `lora.r` is left at
`_wrapper`'s LoRA-shaped default on purpose: the real mode-selection
chain in `_setup_transformers` takes the LISA branch whenever
`lisa_enabled` is set, regardless of `lora.r`, so this is the actual
precondition the bug fires under, not a hand-picked one."""
_requires_train_extra()
wrapper, dataset = _wrapper(tmp_path, monkeypatch, lisa_enabled=True)
wrapper.setup(dataset)
out = capsys.readouterr().out
assert "LoRA applied" not in out
assert "LISA" in out
def test_full_ft_trains_under_gradient_checkpointing(self, tmp_path, monkeypatch):
"""Gradient checkpointing is how full-FT fits at all, so the two are
the common pairing, not an exotic one. A frozen input embedding breaks
the backward pass under checkpointing ("none of the inputs have
requires_grad"), which is why the branch calls
`enable_input_require_grads` exactly as `get_peft_model` does."""
_requires_train_extra()
import torch
wrapper, dataset = _wrapper(
tmp_path,
monkeypatch,
lora={"r": 0},
gradient_checkpointing=True,
seed=11,
)
wrapper.setup(dataset)
target = "model.layers.0.mlp.down_proj.weight"
before = dict(wrapper.model.named_parameters())[target].detach().cpu().clone()
wrapper.trainer.args.max_steps = 1
wrapper.trainer.train()
after = dict(wrapper.model.named_parameters())[target].detach().cpu()
assert not torch.equal(before, after)
def test_full_ft_respects_freeze_layers_instead_of_undoing_it(
self, tmp_path, monkeypatch
):
"""`freeze_layers` stays legal with `r: 0` and the branch must NOT
`requires_grad_(True)` over the top of it — "train everything above
layer N" is a real technique, and silently unfreezing would train a
model the config said not to."""
_requires_train_extra()
wrapper, dataset = _wrapper(
tmp_path, monkeypatch, lora={"r": 0}, freeze_layers=1
)
wrapper.setup(dataset)
by_name = dict(wrapper.model.named_parameters())
assert not by_name["model.layers.0.self_attn.q_proj.weight"].requires_grad
assert by_name["model.layers.1.self_attn.q_proj.weight"].requires_grad
def test_full_ft_refuses_a_model_with_nothing_trainable(self, tmp_path, monkeypatch):
"""`freeze_layers` stays legal with `r: 0` (train everything above
layer N is a real technique), so a config CAN freeze the whole model.
That must fail loudly, not run for hours as a no-op."""
_requires_train_extra()
wrapper, dataset = _wrapper(tmp_path, monkeypatch, lora={"r": 0})
from soup_cli.trainer.sft import SFTTrainerWrapper
original = SFTTrainerWrapper._setup_transformers
def freeze_everything(self, cfg, tcfg):
import transformers
real_from_pretrained = transformers.AutoModelForCausalLM.from_pretrained
def loader(*args, **kwargs):
model = real_from_pretrained(*args, **kwargs)
model.requires_grad_(False)
return model
monkeypatch.setattr(
transformers.AutoModelForCausalLM, "from_pretrained", loader
)
return original(self, cfg, tcfg)
monkeypatch.setattr(
SFTTrainerWrapper, "_setup_transformers", freeze_everything
)
with pytest.raises(ValueError, match="(?i)no parameter is trainable"):
wrapper.setup(dataset)
# ==========================================================================
# Import hygiene — the schema half must stay on the light CLI path
# ==========================================================================
class TestSchemaHalfStaysLight:
def test_config_schema_still_imports_without_torch(self):
"""`config/schema.py` is imported by every light command; a stray
top-level torch import there would cost `soup --help` ~5 s."""
import ast
import pathlib
import soup_cli.config.schema as schema_mod
tree = ast.parse(pathlib.Path(schema_mod.__file__).read_text(encoding="utf-8"))
offenders = []
for node in ast.walk(tree):
if isinstance(node, ast.Import) and node.col_offset == 0:
offenders += [
a.name for a in node.names if a.name.split(".")[0] == "torch"
]
elif isinstance(node, ast.ImportFrom) and node.col_offset == 0:
if (node.module or "").split(".")[0] == "torch":
offenders.append(node.module)
assert not offenders, f"top-level torch import in schema.py: {offenders}"