-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathtest_model_utils.py
More file actions
297 lines (235 loc) · 12.5 KB
/
Copy pathtest_model_utils.py
File metadata and controls
297 lines (235 loc) · 12.5 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
import sys
from pathlib import Path
from unittest.mock import Mock
import pytest
project_root = Path(__file__).resolve().parent.parent.parent
sys.path.append(str(project_root))
from yolo.config.config import DataConfig, SchedulerConfig
from yolo.utils.model_utils import GradientAccumulation, lerp
class TestLerp:
"""Test the lerp (linear interpolation) function."""
def test_lerp_basic(self):
"""Test basic linear interpolation."""
assert lerp(0, 10, 0, 10) == 0
assert lerp(0, 10, 5, 10) == 5
assert lerp(0, 10, 10, 10) == 10
def test_lerp_fractional(self):
"""Test linear interpolation with fractional steps."""
assert lerp(0, 100, 25, 100) == 25
assert lerp(0, 100, 50, 100) == 50
assert lerp(0, 100, 75, 100) == 75
def test_lerp_negative_values(self):
"""Test linear interpolation with negative values."""
assert lerp(-10, 10, 0, 10) == -10
assert lerp(-10, 10, 5, 10) == 0
assert lerp(-10, 10, 10, 10) == 10
def test_lerp_reverse_range(self):
"""Test linear interpolation from larger to smaller value."""
assert lerp(10, 0, 0, 10) == 10
assert lerp(10, 0, 5, 10) == 5
assert lerp(10, 0, 10, 10) == 0
class TestGradientAccumulation:
"""Test the GradientAccumulation callback."""
@pytest.fixture
def data_cfg(self):
"""Create a mock DataConfig for testing."""
cfg = Mock(spec=DataConfig)
cfg.equivalent_batch_size = 64
cfg.batch_size = 16
return cfg
@pytest.fixture
def scheduler_cfg_with_warmup(self):
"""Create a mock SchedulerConfig with warmup."""
cfg = Mock(spec=SchedulerConfig)
# Create a warmup object that supports attribute access
warmup = Mock()
warmup.epochs = 3
cfg.warmup = warmup
return cfg
@pytest.fixture
def scheduler_cfg_without_warmup(self):
"""Create a mock SchedulerConfig without warmup."""
cfg = Mock(spec=SchedulerConfig)
# Create a warmup object without epochs attribute to simulate missing warmup config
warmup = Mock()
del warmup.epochs
cfg.warmup = warmup
return cfg
@pytest.fixture
def mock_trainer(self):
"""Create a mock Trainer."""
trainer = Mock()
trainer.world_size = 1
trainer.global_step = 0
trainer.accumulate_grad_batches = 1
return trainer
@pytest.fixture
def mock_pl_module(self):
"""Create a mock LightningModule."""
pl_module = Mock()
# Mock train_loader with 100 batches
pl_module.train_loader = list(range(100))
return pl_module
def test_init_with_warmup(self, data_cfg, scheduler_cfg_with_warmup):
"""Test initialization with warmup configuration."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
assert callback.equivalent_batch_size == 64
assert callback.actual_batch_size == 16
assert callback.warmup_epochs == 3
assert callback.current_batch == 0
assert callback.max_accumulation == 1
assert callback.warmup_batches == 0
def test_init_without_warmup(self, data_cfg, scheduler_cfg_without_warmup):
"""Test initialization without warmup configuration."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_without_warmup)
assert callback.equivalent_batch_size == 64
assert callback.actual_batch_size == 16
assert callback.warmup_epochs == 0
assert callback.current_batch == 0
assert callback.max_accumulation == 1
assert callback.warmup_batches == 0
def test_setup_single_gpu(self, data_cfg, scheduler_cfg_with_warmup, mock_trainer, mock_pl_module):
"""Test setup method with single GPU (world_size=1)."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
mock_trainer.world_size = 1
callback.setup(mock_trainer, mock_pl_module, "fit")
# equivalent_batch_size=64, actual_batch_size=16, world_size=1
# effective_batch_size = 16 * 1 = 16
# max_accumulation = round(64 / 16) = 4
assert callback.max_accumulation == 4
# warmup_batches = warmup_epochs * batches_per_epoch
# warmup_batches = 3 * (100 / 1) = 300
assert callback.warmup_batches == 300
def test_setup_multi_gpu(self, data_cfg, scheduler_cfg_with_warmup, mock_trainer, mock_pl_module):
"""Test setup method with multiple GPUs (world_size=4)."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
mock_trainer.world_size = 4
callback.setup(mock_trainer, mock_pl_module, "fit")
# equivalent_batch_size=64, actual_batch_size=16, world_size=4
# effective_batch_size = 16 * 4 = 64
# max_accumulation = round(64 / 64) = 1
assert callback.max_accumulation == 1
# warmup_batches = warmup_epochs * batches_per_epoch
# warmup_batches = 3 * (100 / 4) = 75
assert callback.warmup_batches == 75
def test_setup_fractional_accumulation(self, scheduler_cfg_with_warmup, mock_trainer, mock_pl_module):
"""Test setup with fractional accumulation (should round to nearest int)."""
data_cfg = Mock(spec=DataConfig)
data_cfg.equivalent_batch_size = 100
data_cfg.batch_size = 16
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
mock_trainer.world_size = 1
callback.setup(mock_trainer, mock_pl_module, "fit")
# equivalent_batch_size=100, actual_batch_size=16, world_size=1
# effective_batch_size = 16 * 1 = 16
# max_accumulation = round(100 / 16) = round(6.25) = 6
assert callback.max_accumulation == 6
def test_setup_minimum_accumulation(self, scheduler_cfg_with_warmup, mock_trainer, mock_pl_module):
"""Test that max_accumulation is at least 1."""
data_cfg = Mock(spec=DataConfig)
data_cfg.equivalent_batch_size = 16
data_cfg.batch_size = 32
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
mock_trainer.world_size = 2
callback.setup(mock_trainer, mock_pl_module, "fit")
# equivalent_batch_size=16, actual_batch_size=32, world_size=2
# effective_batch_size = 32 * 2 = 64
# max_accumulation = max(1, round(16 / 64)) = max(1, 0) = 1
assert callback.max_accumulation == 1
def test_on_train_epoch_start(self, data_cfg, scheduler_cfg_with_warmup, mock_trainer, mock_pl_module):
"""Test that current_batch is updated at epoch start."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
mock_trainer.global_step = 150
callback.on_train_epoch_start(mock_trainer, mock_pl_module)
assert callback.current_batch == 150
def test_on_train_batch_start_before_warmup(self, data_cfg, scheduler_cfg_with_warmup, mock_trainer, mock_pl_module):
"""Test gradient accumulation during warmup phase."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
callback.setup(mock_trainer, mock_pl_module, "fit")
# warmup_batches = 300, max_accumulation = 4
assert callback.warmup_batches == 300
assert callback.max_accumulation == 4
# At batch 0 (start of warmup), should be 1
callback.current_batch = 0
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == 1
# At batch 75 (25% through warmup), should interpolate
# lerp(1, 4, 75, 300) = 1 + (4-1) * 75/300 = 1 + 0.75 = 1.75 -> round to 2
callback.current_batch = 75
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == 2
# At batch 150 (50% through warmup), should be halfway
# lerp(1, 4, 150, 300) = 1 + (4-1) * 150/300 = 1 + 1.5 = 2.5 -> round to 2 or 3
callback.current_batch = 150
expected_accumulation = round(lerp(1, 4, 150, 300))
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == expected_accumulation
# At batch 225 (75% through warmup)
# lerp(1, 4, 225, 300) = 1 + (4-1) * 225/300 = 1 + 2.25 = 3.25 -> round to 3
callback.current_batch = 225
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == 3
def test_on_train_batch_start_after_warmup(self, data_cfg, scheduler_cfg_with_warmup, mock_trainer, mock_pl_module):
"""Test gradient accumulation after warmup phase."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
callback.setup(mock_trainer, mock_pl_module, "fit")
# warmup_batches = 300, max_accumulation = 4
assert callback.warmup_batches == 300
assert callback.max_accumulation == 4
# At batch 300 (end of warmup), should be max_accumulation
callback.current_batch = 300
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == 4
# At batch 500 (well after warmup), should still be max_accumulation
callback.current_batch = 500
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == 4
def test_on_train_batch_start_no_warmup(self, data_cfg, scheduler_cfg_without_warmup, mock_trainer, mock_pl_module):
"""Test gradient accumulation when warmup is disabled."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_without_warmup)
callback.setup(mock_trainer, mock_pl_module, "fit")
# warmup_batches = 0, max_accumulation = 4
assert callback.warmup_batches == 0
assert callback.max_accumulation == 4
# From the start, should use max_accumulation
callback.current_batch = 0
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == 4
callback.current_batch = 100
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == 4
def test_on_train_batch_end(self, data_cfg, scheduler_cfg_with_warmup, mock_trainer, mock_pl_module):
"""Test that current_batch is incremented after each batch."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
callback.current_batch = 0
callback.on_train_batch_end(mock_trainer, mock_pl_module)
assert callback.current_batch == 1
callback.on_train_batch_end(mock_trainer, mock_pl_module)
assert callback.current_batch == 2
callback.on_train_batch_end(mock_trainer, mock_pl_module)
assert callback.current_batch == 3
def test_full_training_cycle(self, data_cfg, scheduler_cfg_with_warmup, mock_trainer, mock_pl_module):
"""Test a complete training cycle with warmup."""
callback = GradientAccumulation(data_cfg, scheduler_cfg_with_warmup)
callback.setup(mock_trainer, mock_pl_module, "fit")
# Start of epoch
callback.on_train_epoch_start(mock_trainer, mock_pl_module)
assert callback.current_batch == 0
# First batch - should start at accumulation of 1
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == 1
callback.on_train_batch_end(mock_trainer, mock_pl_module)
assert callback.current_batch == 1
# Simulate warmup progression
for _ in range(149): # Complete up to batch 150
callback.on_train_batch_start(mock_trainer, mock_pl_module)
callback.on_train_batch_end(mock_trainer, mock_pl_module)
assert callback.current_batch == 150
# Continue past warmup (warmup_batches = 300)
for _ in range(200): # Go to batch 350
callback.on_train_batch_start(mock_trainer, mock_pl_module)
callback.on_train_batch_end(mock_trainer, mock_pl_module)
assert callback.current_batch == 350
# After warmup, should be at max_accumulation
callback.on_train_batch_start(mock_trainer, mock_pl_module)
assert mock_trainer.accumulate_grad_batches == 4