Skip to content

Commit a6f043a

Browse files
sayakpaula-r-r-o-w
andauthored
[LoRA] allow big CUDA tests to run properly for LoRA (and others) (#9845)
* allow big lora tests to run on the CI. * print * print. * print * print * print * print * more * print * remove print. * remove print * directly place on cuda. * remove pipeline. * remove * fix * fix * spaces * quality * updates * directly place flux controlnet pipeline on cuda. * torch_device instead of cuda. * style * device placement. * fixes * add big gpu marker for mochi; rename test correctly * address feedback * fix --------- Co-authored-by: Aryan <[email protected]>
1 parent 12fbe3f commit a6f043a

File tree

5 files changed

+38
-29
lines changed

5 files changed

+38
-29
lines changed

Diff for: tests/lora/test_lora_layers_flux.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,8 @@ def test_modify_padding_mode(self):
796796
@nightly
797797
@require_torch_gpu
798798
@require_peft_backend
799-
@unittest.skip("We cannot run inference on this model with the current CI hardware")
800-
# TODO (DN6, sayakpaul): move these tests to a beefier GPU
799+
@require_big_gpu_with_torch_cuda
800+
@pytest.mark.big_gpu_with_torch_cuda
801801
class FluxLoRAIntegrationTests(unittest.TestCase):
802802
"""internal note: The integration slices were obtained on audace.
803803
@@ -819,14 +819,18 @@ def setUp(self):
819819
def tearDown(self):
820820
super().tearDown()
821821

822+
del self.pipeline
822823
gc.collect()
823824
torch.cuda.empty_cache()
824825

825826
def test_flux_the_last_ben(self):
826827
self.pipeline.load_lora_weights("TheLastBen/Jon_Snow_Flux_LoRA", weight_name="jon_snow.safetensors")
827828
self.pipeline.fuse_lora()
828829
self.pipeline.unload_lora_weights()
829-
self.pipeline.enable_model_cpu_offload()
830+
# Instead of calling `enable_model_cpu_offload()`, we do a cuda placement here because the CI
831+
# run supports it. We have about 34GB RAM in the CI runner which kills the test when run with
832+
# `enable_model_cpu_offload()`. We repeat this for the other tests, too.
833+
self.pipeline = self.pipeline.to(torch_device)
830834

831835
prompt = "jon snow eating pizza with ketchup"
832836

@@ -848,7 +852,7 @@ def test_flux_kohya(self):
848852
self.pipeline.load_lora_weights("Norod78/brain-slug-flux")
849853
self.pipeline.fuse_lora()
850854
self.pipeline.unload_lora_weights()
851-
self.pipeline.enable_model_cpu_offload()
855+
self.pipeline = self.pipeline.to(torch_device)
852856

853857
prompt = "The cat with a brain slug earring"
854858
out = self.pipeline(
@@ -870,7 +874,7 @@ def test_flux_kohya_with_text_encoder(self):
870874
self.pipeline.load_lora_weights("cocktailpeanut/optimus", weight_name="optimus.safetensors")
871875
self.pipeline.fuse_lora()
872876
self.pipeline.unload_lora_weights()
873-
self.pipeline.enable_model_cpu_offload()
877+
self.pipeline = self.pipeline.to(torch_device)
874878

875879
prompt = "optimus is cleaning the house with broomstick"
876880
out = self.pipeline(
@@ -892,7 +896,7 @@ def test_flux_xlabs(self):
892896
self.pipeline.load_lora_weights("XLabs-AI/flux-lora-collection", weight_name="disney_lora.safetensors")
893897
self.pipeline.fuse_lora()
894898
self.pipeline.unload_lora_weights()
895-
self.pipeline.enable_model_cpu_offload()
899+
self.pipeline = self.pipeline.to(torch_device)
896900

897901
prompt = "A blue jay standing on a large basket of rainbow macarons, disney style"
898902

Diff for: tests/lora/test_lora_layers_sd3.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import unittest
1818

1919
import numpy as np
20+
import pytest
2021
import torch
2122
from transformers import AutoTokenizer, CLIPTextModelWithProjection, CLIPTokenizer, T5EncoderModel
2223

@@ -31,9 +32,9 @@
3132
from diffusers.utils.testing_utils import (
3233
nightly,
3334
numpy_cosine_similarity_distance,
35+
require_big_gpu_with_torch_cuda,
3436
require_peft_backend,
3537
require_torch_gpu,
36-
slow,
3738
torch_device,
3839
)
3940

@@ -128,11 +129,12 @@ def test_modify_padding_mode(self):
128129
pass
129130

130131

131-
@slow
132132
@nightly
133133
@require_torch_gpu
134134
@require_peft_backend
135-
class LoraSD3IntegrationTests(unittest.TestCase):
135+
@require_big_gpu_with_torch_cuda
136+
@pytest.mark.big_gpu_with_torch_cuda
137+
class SD3LoraIntegrationTests(unittest.TestCase):
136138
pipeline_class = StableDiffusion3Img2ImgPipeline
137139
repo_id = "stabilityai/stable-diffusion-3-medium-diffusers"
138140

@@ -166,14 +168,17 @@ def get_inputs(self, device, seed=0):
166168

167169
def test_sd3_img2img_lora(self):
168170
pipe = self.pipeline_class.from_pretrained(self.repo_id, torch_dtype=torch.float16)
169-
pipe.load_lora_weights("zwloong/sd3-lora-training-rank16-v2", weight_name="pytorch_lora_weights.safetensors")
170-
pipe.enable_sequential_cpu_offload()
171+
pipe.load_lora_weights("zwloong/sd3-lora-training-rank16-v2")
172+
pipe.fuse_lora()
173+
pipe.unload_lora_weights()
174+
pipe = pipe.to(torch_device)
171175

172176
inputs = self.get_inputs(torch_device)
173177

174178
image = pipe(**inputs).images[0]
175179
image_slice = image[0, -3:, -3:]
176180
expected_slice = np.array([0.5396, 0.5776, 0.7432, 0.5151, 0.5586, 0.7383, 0.5537, 0.5933, 0.7153])
181+
177182
max_diff = numpy_cosine_similarity_distance(expected_slice.flatten(), image_slice.flatten())
178183

179184
assert max_diff < 1e-4, f"Outputs are not close enough, got {max_diff}"

Diff for: tests/pipelines/controlnet_flux/test_controlnet_flux.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
from diffusers.utils import load_image
3333
from diffusers.utils.testing_utils import (
3434
enable_full_determinism,
35+
nightly,
3536
numpy_cosine_similarity_distance,
3637
require_big_gpu_with_torch_cuda,
37-
slow,
3838
torch_device,
3939
)
4040
from diffusers.utils.torch_utils import randn_tensor
@@ -204,7 +204,7 @@ def test_flux_image_output_shape(self):
204204
assert (output_height, output_width) == (expected_height, expected_width)
205205

206206

207-
@slow
207+
@nightly
208208
@require_big_gpu_with_torch_cuda
209209
@pytest.mark.big_gpu_with_torch_cuda
210210
class FluxControlNetPipelineSlowTests(unittest.TestCase):
@@ -230,8 +230,7 @@ def test_canny(self):
230230
text_encoder_2=None,
231231
controlnet=controlnet,
232232
torch_dtype=torch.bfloat16,
233-
)
234-
pipe.enable_model_cpu_offload()
233+
).to(torch_device)
235234
pipe.set_progress_bar_config(disable=None)
236235

237236
generator = torch.Generator(device="cpu").manual_seed(0)
@@ -241,12 +240,12 @@ def test_canny(self):
241240

242241
prompt_embeds = torch.load(
243242
hf_hub_download(repo_id="diffusers/test-slices", repo_type="dataset", filename="flux/prompt_embeds.pt")
244-
)
243+
).to(torch_device)
245244
pooled_prompt_embeds = torch.load(
246245
hf_hub_download(
247246
repo_id="diffusers/test-slices", repo_type="dataset", filename="flux/pooled_prompt_embeds.pt"
248247
)
249-
)
248+
).to(torch_device)
250249

251250
output = pipe(
252251
prompt_embeds=prompt_embeds,

Diff for: tests/pipelines/flux/test_pipeline_flux.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from diffusers import AutoencoderKL, FlowMatchEulerDiscreteScheduler, FluxPipeline, FluxTransformer2DModel
1111
from diffusers.utils.testing_utils import (
12+
nightly,
1213
numpy_cosine_similarity_distance,
1314
require_big_gpu_with_torch_cuda,
1415
slow,
@@ -209,7 +210,7 @@ def test_flux_image_output_shape(self):
209210
assert (output_height, output_width) == (expected_height, expected_width)
210211

211212

212-
@slow
213+
@nightly
213214
@require_big_gpu_with_torch_cuda
214215
@pytest.mark.big_gpu_with_torch_cuda
215216
class FluxPipelineSlowTests(unittest.TestCase):
@@ -227,19 +228,16 @@ def tearDown(self):
227228
torch.cuda.empty_cache()
228229

229230
def get_inputs(self, device, seed=0):
230-
if str(device).startswith("mps"):
231-
generator = torch.manual_seed(seed)
232-
else:
233-
generator = torch.Generator(device="cpu").manual_seed(seed)
231+
generator = torch.Generator(device="cpu").manual_seed(seed)
234232

235233
prompt_embeds = torch.load(
236234
hf_hub_download(repo_id="diffusers/test-slices", repo_type="dataset", filename="flux/prompt_embeds.pt")
237-
)
235+
).to(torch_device)
238236
pooled_prompt_embeds = torch.load(
239237
hf_hub_download(
240238
repo_id="diffusers/test-slices", repo_type="dataset", filename="flux/pooled_prompt_embeds.pt"
241239
)
242-
)
240+
).to(torch_device)
243241
return {
244242
"prompt_embeds": prompt_embeds,
245243
"pooled_prompt_embeds": pooled_prompt_embeds,
@@ -253,8 +251,7 @@ def get_inputs(self, device, seed=0):
253251
def test_flux_inference(self):
254252
pipe = self.pipeline_class.from_pretrained(
255253
self.repo_id, torch_dtype=torch.bfloat16, text_encoder=None, text_encoder_2=None
256-
)
257-
pipe.enable_model_cpu_offload()
254+
).to(torch_device)
258255

259256
inputs = self.get_inputs(torch_device)
260257

Diff for: tests/pipelines/mochi/test_mochi.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
import unittest
1818

1919
import numpy as np
20+
import pytest
2021
import torch
2122
from transformers import AutoTokenizer, T5EncoderModel
2223

2324
from diffusers import AutoencoderKLMochi, FlowMatchEulerDiscreteScheduler, MochiPipeline, MochiTransformer3DModel
2425
from diffusers.utils.testing_utils import (
2526
enable_full_determinism,
27+
nightly,
2628
numpy_cosine_similarity_distance,
29+
require_big_gpu_with_torch_cuda,
2730
require_torch_gpu,
28-
slow,
2931
torch_device,
3032
)
3133

@@ -260,8 +262,10 @@ def test_vae_tiling(self, expected_diff_max: float = 0.2):
260262
)
261263

262264

263-
@slow
265+
@nightly
264266
@require_torch_gpu
267+
@require_big_gpu_with_torch_cuda
268+
@pytest.mark.big_gpu_with_torch_cuda
265269
class MochiPipelineIntegrationTests(unittest.TestCase):
266270
prompt = "A painting of a squirrel eating a burger."
267271

@@ -293,7 +297,7 @@ def test_mochi(self):
293297
).frames
294298

295299
video = videos[0]
296-
expected_video = torch.randn(1, 16, 480, 848, 3).numpy()
300+
expected_video = torch.randn(1, 19, 480, 848, 3).numpy()
297301

298302
max_diff = numpy_cosine_similarity_distance(video, expected_video)
299303
assert max_diff < 1e-3, f"Max diff is too high. got {video}"

0 commit comments

Comments
 (0)