Skip to content

Commit 1963b5c

Browse files
authored
Merge branch 'main' into allow-device-placement-bnb
2 parents e76f93a + 784b351 commit 1963b5c

21 files changed

+105
-77
lines changed

scripts/convert_cogview3_to_diffusers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from diffusers.utils.import_utils import is_accelerate_available
3737

3838

39-
CTX = init_empty_weights if is_accelerate_available else nullcontext
39+
CTX = init_empty_weights if is_accelerate_available() else nullcontext
4040

4141
TOKENIZER_MAX_LENGTH = 224
4242

scripts/convert_flux_to_diffusers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
--vae
3232
"""
3333

34-
CTX = init_empty_weights if is_accelerate_available else nullcontext
34+
CTX = init_empty_weights if is_accelerate_available() else nullcontext
3535

3636
parser = argparse.ArgumentParser()
3737
parser.add_argument("--original_state_dict_repo_id", default=None, type=str)

scripts/convert_mochi_to_diffusers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from diffusers.utils.import_utils import is_accelerate_available
1111

1212

13-
CTX = init_empty_weights if is_accelerate_available else nullcontext
13+
CTX = init_empty_weights if is_accelerate_available() else nullcontext
1414

1515
TOKENIZER_MAX_LENGTH = 256
1616

scripts/convert_sd3_to_diffusers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from diffusers.utils.import_utils import is_accelerate_available
1212

1313

14-
CTX = init_empty_weights if is_accelerate_available else nullcontext
14+
CTX = init_empty_weights if is_accelerate_available() else nullcontext
1515

1616
parser = argparse.ArgumentParser()
1717
parser.add_argument("--checkpoint_path", type=str)

src/diffusers/models/autoencoders/autoencoder_kl_mochi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ def __init__(self, start: int = 6, stop: int = 8, step: int = 1):
437437

438438
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
439439
r"""Forward method of the `FourierFeatures` class."""
440-
440+
original_dtype = inputs.dtype
441+
inputs = inputs.to(torch.float32)
441442
num_channels = inputs.shape[1]
442443
num_freqs = (self.stop - self.start) // self.step
443444

@@ -450,7 +451,7 @@ def forward(self, inputs: torch.Tensor) -> torch.Tensor:
450451
# Scale channels by frequency.
451452
h = w * h
452453

453-
return torch.cat([inputs, torch.sin(h), torch.cos(h)], dim=1)
454+
return torch.cat([inputs, torch.sin(h), torch.cos(h)], dim=1).to(original_dtype)
454455

455456

456457
class MochiEncoder3D(nn.Module):

tests/single_file/single_file_testing_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ def test_single_file_components_with_original_config_local_files_only(
156156
def test_single_file_format_inference_is_same_as_pretrained(self, expected_max_diff=1e-4):
157157
sf_pipe = self.pipeline_class.from_single_file(self.ckpt_path, safety_checker=None)
158158
sf_pipe.unet.set_attn_processor(AttnProcessor())
159-
sf_pipe.enable_model_cpu_offload()
159+
sf_pipe.enable_model_cpu_offload(device=torch_device)
160160

161161
inputs = self.get_inputs(torch_device)
162162
image_single_file = sf_pipe(**inputs).images[0]
163163

164164
pipe = self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None)
165165
pipe.unet.set_attn_processor(AttnProcessor())
166-
pipe.enable_model_cpu_offload()
166+
pipe.enable_model_cpu_offload(device=torch_device)
167167

168168
inputs = self.get_inputs(torch_device)
169169
image = pipe(**inputs).images[0]

tests/single_file/test_model_controlnet_single_file.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@
2222
ControlNetModel,
2323
)
2424
from diffusers.utils.testing_utils import (
25+
backend_empty_cache,
2526
enable_full_determinism,
26-
require_torch_gpu,
27+
require_torch_accelerator,
2728
slow,
29+
torch_device,
2830
)
2931

3032

3133
enable_full_determinism()
3234

3335

3436
@slow
35-
@require_torch_gpu
37+
@require_torch_accelerator
3638
class ControlNetModelSingleFileTests(unittest.TestCase):
3739
model_class = ControlNetModel
3840
ckpt_path = "https://huggingface.co/lllyasviel/ControlNet-v1-1/blob/main/control_v11p_sd15_canny.pth"
@@ -41,12 +43,12 @@ class ControlNetModelSingleFileTests(unittest.TestCase):
4143
def setUp(self):
4244
super().setUp()
4345
gc.collect()
44-
torch.cuda.empty_cache()
46+
backend_empty_cache(torch_device)
4547

4648
def tearDown(self):
4749
super().tearDown()
4850
gc.collect()
49-
torch.cuda.empty_cache()
51+
backend_empty_cache(torch_device)
5052

5153
def test_single_file_components(self):
5254
model = self.model_class.from_pretrained(self.repo_id)

tests/single_file/test_model_sd_cascade_unet_single_file.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
from diffusers import StableCascadeUNet
2222
from diffusers.utils import logging
2323
from diffusers.utils.testing_utils import (
24+
backend_empty_cache,
2425
enable_full_determinism,
25-
require_torch_gpu,
26+
require_torch_accelerator,
2627
slow,
28+
torch_device,
2729
)
2830

2931

@@ -33,17 +35,17 @@
3335

3436

3537
@slow
36-
@require_torch_gpu
38+
@require_torch_accelerator
3739
class StableCascadeUNetSingleFileTest(unittest.TestCase):
3840
def setUp(self):
3941
super().setUp()
4042
gc.collect()
41-
torch.cuda.empty_cache()
43+
backend_empty_cache(torch_device)
4244

4345
def tearDown(self):
4446
super().tearDown()
4547
gc.collect()
46-
torch.cuda.empty_cache()
48+
backend_empty_cache(torch_device)
4749

4850
def test_single_file_components_stage_b(self):
4951
model_single_file = StableCascadeUNet.from_single_file(

tests/single_file/test_model_vae_single_file.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
AutoencoderKL,
2323
)
2424
from diffusers.utils.testing_utils import (
25+
backend_empty_cache,
2526
enable_full_determinism,
2627
load_hf_numpy,
2728
numpy_cosine_similarity_distance,
28-
require_torch_gpu,
29+
require_torch_accelerator,
2930
slow,
3031
torch_device,
3132
)
@@ -35,7 +36,7 @@
3536

3637

3738
@slow
38-
@require_torch_gpu
39+
@require_torch_accelerator
3940
class AutoencoderKLSingleFileTests(unittest.TestCase):
4041
model_class = AutoencoderKL
4142
ckpt_path = (
@@ -48,12 +49,12 @@ class AutoencoderKLSingleFileTests(unittest.TestCase):
4849
def setUp(self):
4950
super().setUp()
5051
gc.collect()
51-
torch.cuda.empty_cache()
52+
backend_empty_cache(torch_device)
5253

5354
def tearDown(self):
5455
super().tearDown()
5556
gc.collect()
56-
torch.cuda.empty_cache()
57+
backend_empty_cache(torch_device)
5758

5859
def get_file_format(self, seed, shape):
5960
return f"gaussian_noise_s={seed}_shape={'_'.join([str(s) for s in shape])}.npy"

tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
from diffusers.loaders.single_file_utils import _extract_repo_id_and_weights_name
99
from diffusers.utils import load_image
1010
from diffusers.utils.testing_utils import (
11+
backend_empty_cache,
1112
enable_full_determinism,
1213
numpy_cosine_similarity_distance,
13-
require_torch_gpu,
14+
require_torch_accelerator,
1415
slow,
1516
torch_device,
1617
)
@@ -27,7 +28,7 @@
2728

2829

2930
@slow
30-
@require_torch_gpu
31+
@require_torch_accelerator
3132
class StableDiffusionControlNetPipelineSingleFileSlowTests(unittest.TestCase, SDSingleFileTesterMixin):
3233
pipeline_class = StableDiffusionControlNetPipeline
3334
ckpt_path = (
@@ -41,12 +42,12 @@ class StableDiffusionControlNetPipelineSingleFileSlowTests(unittest.TestCase, SD
4142
def setUp(self):
4243
super().setUp()
4344
gc.collect()
44-
torch.cuda.empty_cache()
45+
backend_empty_cache(torch_device)
4546

4647
def tearDown(self):
4748
super().tearDown()
4849
gc.collect()
49-
torch.cuda.empty_cache()
50+
backend_empty_cache(torch_device)
5051

5152
def get_inputs(self, device, generator_device="cpu", dtype=torch.float32, seed=0):
5253
generator = torch.Generator(device=generator_device).manual_seed(seed)

0 commit comments

Comments
 (0)