Skip to content

Commit 06949f2

Browse files
committed
Fix test fixture ordering to match trainer: on_train_begin before apply_torch_compile
All four fixtures previously called apply_torch_compile() before on_train_begin(). In the trainer (trainer.py:97,124) the order is reversed: on_train_begin() runs first (moving parameters to device/dtype and initialising preprocessors), then apply_torch_compile(). With the wrong order, preprocessors don't exist yet when compile runs and are never compiled even when torch_compile_mode is set. Also remove the now-redundant on_train_begin() calls from the two train-step tests since the fixture already covers it, and fix the ruff blank-line formatting issue.
1 parent cf2216f commit 06949f2

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

tests/test_torch_compile.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def sft_model_compiled():
3232
instance.cond_keys_no_dropout = []
3333
instance.guidance_scale = None
3434
model = SFTModel(instance)
35-
# Compilation is applied by the trainer after DDP/FSDP wrapping; emulate that here.
35+
# Mirror the trainer order: on_train_begin() moves parameters to device/dtype and
36+
# initialises preprocessors; apply_torch_compile() must come after so that
37+
# preprocessors exist and are on the right device when compiled.
38+
model.on_train_begin()
3639
model.apply_torch_compile()
3740
return model
3841

@@ -52,6 +55,7 @@ def sft_model_not_compiled():
5255
instance.cond_keys_no_dropout = []
5356
instance.guidance_scale = None
5457
model = SFTModel(instance)
58+
model.on_train_begin()
5559
model.apply_torch_compile()
5660
return model
5761

@@ -71,6 +75,7 @@ def dmd2_model_compiled():
7175
instance.input_shape = [3, 8, 8]
7276
instance.torch_compile_mode = "default"
7377
model = DMD2Model(instance)
78+
model.on_train_begin()
7479
model.apply_torch_compile()
7580
return model
7681

@@ -90,6 +95,7 @@ def dmd2_model_not_compiled():
9095
instance.input_shape = [3, 8, 8]
9196
instance.torch_compile_mode = None
9297
model = DMD2Model(instance)
98+
model.on_train_begin()
9399
model.apply_torch_compile()
94100
return model
95101

@@ -123,6 +129,7 @@ def test_dmd2_compile_disabled(dmd2_model_not_compiled):
123129
assert not _is_compiled(dmd2_model_not_compiled.fake_score)
124130
assert not _is_compiled(dmd2_model_not_compiled.discriminator)
125131

132+
126133
def test_compile_excludes_ema(sft_model_not_compiled):
127134
# EMA networks live in model_dict but are weight-averaged copies that are not run
128135
# during training, so apply_torch_compile must not compile them.
@@ -160,7 +167,6 @@ def __init__(self):
160167

161168
def test_sft_compiled_train_step(sft_model_compiled):
162169
model = sft_model_compiled
163-
model.on_train_begin()
164170
model.init_optimizers()
165171

166172
batch_size = 1
@@ -179,7 +185,6 @@ def test_sft_compiled_train_step(sft_model_compiled):
179185

180186
def test_dmd2_compiled_train_step(dmd2_model_compiled):
181187
model = dmd2_model_compiled
182-
model.on_train_begin()
183188
model.init_optimizers()
184189

185190
batch_size = 1

0 commit comments

Comments
 (0)