forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_issue308_callback_subclass.py
More file actions
195 lines (154 loc) · 7.13 KB
/
Copy pathtest_issue308_callback_subclass.py
File metadata and controls
195 lines (154 loc) · 7.13 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
"""Issue #308 — duck-typed TrainerCallbacks crash on HF event dispatch.
HF ``transformers.trainer_callback.CallbackHandler.call_event`` dispatches
**every** Trainer lifecycle event via ``getattr(callback, event)(...)`` with no
``hasattr`` guard. A callback added via ``trainer.add_callback(obj)`` that does
NOT subclass ``transformers.TrainerCallback`` therefore raises ``AttributeError``
on the first event it does not implement (e.g. ``on_epoch_begin``, fired right
after ``on_train_begin`` — before the first optimizer step).
``ReLoRACallback`` (``utils/relora.py``) and ``HFPushCallback``
(``monitoring/hf_push.py``) were plain duck-typed classes. This suite pins the
fix: both must be real ``TrainerCallback`` subclasses, so they inherit the no-op
defaults for the ~13 unimplemented events.
The subclassing is done via ``_try_import_callback_base()`` (or the equivalent
``_get_trainer_callback_base()``). Since #320, the factory is no longer called at
class-definition time: the callback class is built lazily via PEP 562
``__getattr__`` on first access, so importing the module no longer pulls
transformers or torch. ``tests/test_cli_startup_is_light.py`` enforces this
invariant at runtime with ``test_callback_module_imports_without_transformers``.
"""
from __future__ import annotations
import ast
from pathlib import Path
from unittest.mock import MagicMock, patch
# ---------------------------------------------------------------------------
# isinstance + inherited-no-op-event checks (light, no torch)
# ---------------------------------------------------------------------------
class TestReLoRACallbackSubclass:
def test_is_real_trainer_callback_subclass(self):
from transformers import TrainerCallback
from soup_cli.utils.relora import ReLoRACallback, ReLoRAPolicy
cb = ReLoRACallback(ReLoRAPolicy(steps=10))
assert isinstance(cb, TrainerCallback)
def test_inherits_noop_unimplemented_event(self):
# on_epoch_begin is NOT overridden — it must exist as an inherited
# no-op stub, or HF's getattr dispatch crashes.
from soup_cli.utils.relora import ReLoRACallback, ReLoRAPolicy
cb = ReLoRACallback(ReLoRAPolicy(steps=10))
assert callable(cb.on_epoch_begin)
# calling it must not raise (inherited no-op)
cb.on_epoch_begin(None, None, None)
def test_none_policy_still_subclass(self):
from transformers import TrainerCallback
from soup_cli.utils.relora import ReLoRACallback
cb = ReLoRACallback(None)
assert isinstance(cb, TrainerCallback)
def test_no_top_level_transformers_import(self):
import soup_cli.utils.relora as mod
src = Path(mod.__file__).read_text(encoding="utf-8")
tree = ast.parse(src)
for node in tree.body:
if isinstance(node, (ast.Import, ast.ImportFrom)):
names = (
[a.name for a in node.names]
if isinstance(node, ast.Import)
else [node.module or ""]
)
for nm in names:
assert nm.split(".")[0] not in {
"torch",
"transformers",
"peft",
}, f"top-level import of {nm} in relora.py"
class TestHFPushCallbackSubclass:
def test_is_real_trainer_callback_subclass(self):
from transformers import TrainerCallback
from soup_cli.monitoring.hf_push import HFPushCallback
cb = HFPushCallback(repo_id="user/repo")
assert isinstance(cb, TrainerCallback)
def test_inherits_noop_unimplemented_event(self):
from soup_cli.monitoring.hf_push import HFPushCallback
cb = HFPushCallback(repo_id="user/repo")
assert callable(cb.on_epoch_begin)
cb.on_epoch_begin(None, None, None)
def test_no_top_level_transformers_import(self):
import soup_cli.monitoring.hf_push as mod
src = Path(mod.__file__).read_text(encoding="utf-8")
tree = ast.parse(src)
for node in tree.body:
if isinstance(node, (ast.Import, ast.ImportFrom)):
names = (
[a.name for a in node.names]
if isinstance(node, ast.Import)
else [node.module or ""]
)
for nm in names:
assert nm.split(".")[0] not in {
"torch",
"transformers",
"peft",
}, f"top-level import of {nm} in hf_push.py"
# ---------------------------------------------------------------------------
# Real tiny Trainer.train() — reproduces the AttributeError on on_epoch_begin
# ---------------------------------------------------------------------------
def _tiny_causal_lm():
"""A from-config 2-layer Llama — no download, runs on CPU in ms."""
import torch # noqa: F401
from transformers import LlamaConfig, LlamaForCausalLM
cfg = LlamaConfig(
vocab_size=64,
hidden_size=16,
intermediate_size=32,
num_hidden_layers=2,
num_attention_heads=2,
num_key_value_heads=2,
max_position_embeddings=32,
)
return LlamaForCausalLM(cfg)
class _TinyLMDataset:
"""Minimal map-style dataset yielding input_ids + labels for causal LM."""
def __init__(self, n=8, seq=8, vocab=64):
import torch
self._rows = [
{
"input_ids": torch.randint(0, vocab, (seq,)),
"labels": torch.randint(0, vocab, (seq,)),
"attention_mask": torch.ones(seq, dtype=torch.long),
}
for _ in range(n)
]
def __len__(self):
return len(self._rows)
def __getitem__(self, idx):
return self._rows[idx]
def _run_two_steps(callback, tmp_path):
"""Build a real Trainer with ``callback`` and run 2 steps."""
import torch # noqa: F401
from transformers import Trainer, TrainingArguments
model = _tiny_causal_lm()
ds = _TinyLMDataset()
args = TrainingArguments(
output_dir=str(tmp_path / "out"),
max_steps=2,
per_device_train_batch_size=2,
logging_steps=1,
save_steps=1,
report_to=[],
use_cpu=True,
dataloader_num_workers=0,
)
trainer = Trainer(model=model, args=args, train_dataset=ds)
trainer.add_callback(callback)
trainer.train()
return trainer
class TestRealTrainerDispatch:
def test_relora_callback_survives_train(self, tmp_path):
from soup_cli.utils.relora import ReLoRACallback, ReLoRAPolicy
cb = ReLoRACallback(ReLoRAPolicy(steps=1, warmup_ratio=0.0))
# Must NOT raise AttributeError on on_epoch_begin during train().
_run_two_steps(cb, tmp_path)
def test_hfpush_callback_survives_train(self, tmp_path):
from soup_cli.monitoring.hf_push import HFPushCallback
cb = HFPushCallback(repo_id="user/repo", output_dir=str(tmp_path / "out"))
# Mock the Hub API so on_train_begin/on_save never touch the network.
with patch("soup_cli.monitoring.hf_push.get_hf_api", return_value=MagicMock()):
_run_two_steps(cb, tmp_path)