|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import gc |
| 5 | +import torch |
| 6 | +import pytest |
| 7 | +from fastgen.methods import DMD2Model |
| 8 | +from fastgen.configs.methods.config_sft import ModelConfig as SFTModelConfig |
| 9 | +from fastgen.configs.methods.config_dmd2 import ModelConfig as DMD2ModelConfig |
| 10 | +from fastgen.configs.config_utils import override_config_with_opts |
| 11 | +from fastgen.methods.fine_tuning.sft import SFTModel |
| 12 | + |
| 13 | + |
| 14 | +def _is_compiled(module): |
| 15 | + # nn.Module.compile() compiles the module in place: it stores the compiled |
| 16 | + # callable on `_compiled_call_impl` rather than replacing the module. |
| 17 | + return getattr(module, "_compiled_call_impl", None) is not None |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def sft_model_compiled(): |
| 22 | + gc.collect() |
| 23 | + instance = SFTModelConfig() |
| 24 | + opts = ["-", "img_resolution=8", "channel_mult=[1]", "channel_mult_noise=1", "r_timestep=False"] |
| 25 | + instance.net = override_config_with_opts(instance.net, opts) |
| 26 | + instance.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 27 | + instance.precision = "float32" if instance.device == torch.device("cpu") else "bfloat16" |
| 28 | + instance.pretrained_model_path = "" |
| 29 | + instance.input_shape = [3, 8, 8] |
| 30 | + instance.torch_compile_mode = "default" |
| 31 | + instance.cond_dropout_prob = 0.1 |
| 32 | + instance.cond_keys_no_dropout = [] |
| 33 | + instance.guidance_scale = None |
| 34 | + model = SFTModel(instance) |
| 35 | + # Compilation is applied by the trainer after DDP/FSDP wrapping; emulate that here. |
| 36 | + model.apply_torch_compile() |
| 37 | + return model |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def sft_model_not_compiled(): |
| 42 | + gc.collect() |
| 43 | + instance = SFTModelConfig() |
| 44 | + opts = ["-", "img_resolution=8", "channel_mult=[1]", "channel_mult_noise=1", "r_timestep=False"] |
| 45 | + instance.net = override_config_with_opts(instance.net, opts) |
| 46 | + instance.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 47 | + instance.precision = "float32" if instance.device == torch.device("cpu") else "bfloat16" |
| 48 | + instance.pretrained_model_path = "" |
| 49 | + instance.input_shape = [3, 8, 8] |
| 50 | + instance.torch_compile_mode = None |
| 51 | + instance.cond_dropout_prob = 0.1 |
| 52 | + instance.cond_keys_no_dropout = [] |
| 53 | + instance.guidance_scale = None |
| 54 | + model = SFTModel(instance) |
| 55 | + model.apply_torch_compile() |
| 56 | + return model |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture |
| 60 | +def dmd2_model_compiled(): |
| 61 | + gc.collect() |
| 62 | + instance = DMD2ModelConfig() |
| 63 | + opts = ["-", "img_resolution=8", "channel_mult=[1]", "channel_mult_noise=1"] |
| 64 | + instance.net = override_config_with_opts(instance.net, opts) |
| 65 | + opts_discriminator = ["-", "feature_indices=[0]", "all_res=[8]", "in_channels=128"] |
| 66 | + instance.discriminator = override_config_with_opts(instance.discriminator, opts_discriminator) |
| 67 | + instance.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 68 | + instance.precision = "float32" if instance.device == torch.device("cpu") else "bfloat16" |
| 69 | + instance.pretrained_model_path = "" |
| 70 | + instance.student_update_freq = 2 |
| 71 | + instance.input_shape = [3, 8, 8] |
| 72 | + instance.torch_compile_mode = "default" |
| 73 | + model = DMD2Model(instance) |
| 74 | + model.apply_torch_compile() |
| 75 | + return model |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture |
| 79 | +def dmd2_model_not_compiled(): |
| 80 | + gc.collect() |
| 81 | + instance = DMD2ModelConfig() |
| 82 | + opts = ["-", "img_resolution=8", "channel_mult=[1]", "channel_mult_noise=1"] |
| 83 | + instance.net = override_config_with_opts(instance.net, opts) |
| 84 | + opts_discriminator = ["-", "feature_indices=[0]", "all_res=[8]", "in_channels=128"] |
| 85 | + instance.discriminator = override_config_with_opts(instance.discriminator, opts_discriminator) |
| 86 | + instance.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 87 | + instance.precision = "float32" if instance.device == torch.device("cpu") else "bfloat16" |
| 88 | + instance.pretrained_model_path = "" |
| 89 | + instance.student_update_freq = 2 |
| 90 | + instance.input_shape = [3, 8, 8] |
| 91 | + instance.torch_compile_mode = None |
| 92 | + model = DMD2Model(instance) |
| 93 | + model.apply_torch_compile() |
| 94 | + return model |
| 95 | + |
| 96 | + |
| 97 | +def test_default_torch_compile_mode_is_none(): |
| 98 | + from fastgen.configs.config import BaseModelConfig |
| 99 | + |
| 100 | + config = BaseModelConfig() |
| 101 | + assert config.torch_compile_mode is None |
| 102 | + |
| 103 | + |
| 104 | +def test_sft_compile_enabled(sft_model_compiled): |
| 105 | + assert _is_compiled(sft_model_compiled.net) |
| 106 | + |
| 107 | + |
| 108 | +def test_sft_compile_disabled(sft_model_not_compiled): |
| 109 | + assert not _is_compiled(sft_model_not_compiled.net) |
| 110 | + |
| 111 | + |
| 112 | +def test_dmd2_compile_enabled(dmd2_model_compiled): |
| 113 | + # apply_torch_compile draws from model_dict (net, fake_score, discriminator) plus the teacher. |
| 114 | + assert _is_compiled(dmd2_model_compiled.net) |
| 115 | + assert _is_compiled(dmd2_model_compiled.teacher) |
| 116 | + assert _is_compiled(dmd2_model_compiled.fake_score) |
| 117 | + assert _is_compiled(dmd2_model_compiled.discriminator) |
| 118 | + |
| 119 | + |
| 120 | +def test_dmd2_compile_disabled(dmd2_model_not_compiled): |
| 121 | + assert not _is_compiled(dmd2_model_not_compiled.net) |
| 122 | + assert not _is_compiled(dmd2_model_not_compiled.teacher) |
| 123 | + assert not _is_compiled(dmd2_model_not_compiled.fake_score) |
| 124 | + |
| 125 | + |
| 126 | +def test_compile_excludes_ema(sft_model_not_compiled): |
| 127 | + # EMA networks live in model_dict but are weight-averaged copies that are not run |
| 128 | + # during training, so apply_torch_compile must not compile them. |
| 129 | + model = sft_model_not_compiled |
| 130 | + model.use_ema = ["ema"] |
| 131 | + model.ema = torch.nn.Linear(4, 4) # any nn.Module suffices for ema_dict/model_dict |
| 132 | + assert "ema" in model.ema_dict and "ema" in model.model_dict |
| 133 | + |
| 134 | + model.config.torch_compile_mode = "default" |
| 135 | + model.apply_torch_compile() |
| 136 | + assert _is_compiled(model.net) |
| 137 | + assert not _is_compiled(model.ema) |
| 138 | + |
| 139 | + |
| 140 | +def test_compile_discovers_preprocessor_submodules(sft_model_not_compiled): |
| 141 | + # Preprocessor wrappers (VAE, text/image encoders) are not nn.Modules themselves but |
| 142 | + # hold the actual nn.Module under an attribute; apply_torch_compile must find and |
| 143 | + # compile those submodules. |
| 144 | + model = sft_model_not_compiled |
| 145 | + |
| 146 | + class _DummyVAEWrapper: # mimics WanVideoEncoder/SDVAE (not an nn.Module) |
| 147 | + def __init__(self): |
| 148 | + self.vae = torch.nn.Linear(4, 4) |
| 149 | + self.scaling_factor = 0.18 # non-module attributes are ignored |
| 150 | + |
| 151 | + model.net.vae = _DummyVAEWrapper() |
| 152 | + # An attribute that is itself an nn.Module is compiled directly under its own name. |
| 153 | + model.net.text_encoder = torch.nn.Linear(4, 4) |
| 154 | + |
| 155 | + model.config.torch_compile_mode = "default" |
| 156 | + model.apply_torch_compile() |
| 157 | + assert _is_compiled(model.net.vae.vae) |
| 158 | + assert _is_compiled(model.net.text_encoder) |
| 159 | + |
| 160 | + |
| 161 | +def test_sft_compiled_train_step(sft_model_compiled): |
| 162 | + model = sft_model_compiled |
| 163 | + model.on_train_begin() |
| 164 | + model.init_optimizers() |
| 165 | + |
| 166 | + batch_size = 1 |
| 167 | + labels = torch.nn.functional.one_hot(torch.randint(0, 10, (batch_size,)), num_classes=10).float() |
| 168 | + data = { |
| 169 | + "real": torch.randn(batch_size, 3, 8, 8).to(model.device, model.precision), |
| 170 | + "condition": labels.to(model.device, model.precision), |
| 171 | + "neg_condition": torch.zeros(batch_size, 10).to(model.device, model.precision), |
| 172 | + } |
| 173 | + |
| 174 | + loss_map, outputs = model.single_train_step(data, 0) |
| 175 | + assert "total_loss" in loss_map |
| 176 | + assert not torch.isnan(loss_map["total_loss"]) |
| 177 | + loss_map["total_loss"].backward() |
| 178 | + |
| 179 | + |
| 180 | +def test_dmd2_compiled_train_step(dmd2_model_compiled): |
| 181 | + model = dmd2_model_compiled |
| 182 | + model.on_train_begin() |
| 183 | + model.init_optimizers() |
| 184 | + |
| 185 | + batch_size = 1 |
| 186 | + labels = torch.nn.functional.one_hot(torch.randint(0, 10, (batch_size,)), num_classes=10) |
| 187 | + data = { |
| 188 | + "real": torch.randn(batch_size, 3, 8, 8).to(model.device, model.precision), |
| 189 | + "condition": labels.to(model.device, model.precision), |
| 190 | + "neg_condition": torch.zeros(batch_size, 10).to(model.device, model.precision), |
| 191 | + } |
| 192 | + |
| 193 | + # Student update step |
| 194 | + loss_map, outputs = model.single_train_step(data, 0) |
| 195 | + assert "total_loss" in loss_map |
| 196 | + assert not torch.isnan(loss_map["total_loss"]) |
| 197 | + |
| 198 | + # Fake score update step |
| 199 | + model.optimizers_zero_grad(1) |
| 200 | + loss_map, outputs = model.single_train_step(data, 1) |
| 201 | + assert "total_loss" in loss_map |
| 202 | + assert not torch.isnan(loss_map["total_loss"]) |
0 commit comments