forked from AI-Hypercomputer/maxdiffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxdiffusion_utils_test.py
More file actions
168 lines (137 loc) · 6.5 KB
/
Copy pathmaxdiffusion_utils_test.py
File metadata and controls
168 lines (137 loc) · 6.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
"""
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
from types import SimpleNamespace
import unittest
from unittest.mock import Mock
from jax.sharding import Mesh
from .. import pyconfig
from maxdiffusion.max_utils import (
create_device_mesh,
get_flash_block_sizes,
)
from maxdiffusion import (FlaxStableDiffusionXLPipeline, FlaxDDIMScheduler, FlaxDDPMScheduler, maxdiffusion_utils)
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
class MaxDiffusionUtilsTest(unittest.TestCase):
"""Test maxdiffusion_utils.py functions"""
def setUp(self):
MaxDiffusionUtilsTest.dummy_data = {}
def test_get_dummy_wan_inputs_generates_latents_without_pipeline_prepare_latents(self):
config = SimpleNamespace(height=64, width=80, num_frames=9, seed=0)
pipeline = SimpleNamespace(
transformer=SimpleNamespace(config=SimpleNamespace(in_channels=16)),
vae_scale_factor_temporal=4,
vae_scale_factor_spatial=8,
prepare_latents=Mock(side_effect=AssertionError("prepare_latents should not be called")),
)
latents, prompt_embeds, timesteps = maxdiffusion_utils.get_dummy_wan_inputs(config, pipeline, batch_size=2)
pipeline.prepare_latents.assert_not_called()
self.assertEqual(latents.shape, (2, 16, 3, 8, 10))
self.assertEqual(prompt_embeds.shape, (2, 512, 4096))
self.assertEqual(timesteps.shape, (2,))
def test_get_dummy_wan_inputs_supports_two_expert_pipeline(self):
config = SimpleNamespace(height=64, width=80, num_frames=9, seed=0)
pipeline = SimpleNamespace(
low_noise_transformer=SimpleNamespace(config=SimpleNamespace(in_channels=48)),
high_noise_transformer=SimpleNamespace(config=SimpleNamespace(in_channels=48)),
vae_scale_factor_temporal=4,
vae_scale_factor_spatial=8,
prepare_latents=Mock(side_effect=AssertionError("prepare_latents should not be called")),
)
latents, prompt_embeds, timesteps = maxdiffusion_utils.get_dummy_wan_inputs(config, pipeline, batch_size=2)
pipeline.prepare_latents.assert_not_called()
self.assertEqual(latents.shape, (2, 48, 3, 8, 10))
self.assertEqual(prompt_embeds.shape, (2, 512, 4096))
self.assertEqual(timesteps.shape, (2,))
def test_create_scheduler(self):
"""Test create scheduler with different schedulers"""
pyconfig.initialize(
[
None,
os.path.join(THIS_DIR, "..", "configs", "base_xl.yml"),
"pretrained_model_name_or_path=gs://maxdiffusion-github-runner-test-assets/checkpoints/models--stabilityai--stable-diffusion-xl-base-1.0",
"revision=refs/pr/95",
"activations_dtype=bfloat16",
'diffusion_scheduler_config={"prediction_type" : "v_prediction", '
'"rescale_zero_terminal_snr" : true, "timestep_spacing" : "trailing"}',
],
unittest=True,
)
config = pyconfig.config
# Setup Mesh
devices_array = create_device_mesh(config)
mesh = Mesh(devices_array, config.mesh_axes)
flash_block_sizes = get_flash_block_sizes(config)
pipeline, _ = FlaxStableDiffusionXLPipeline.from_pretrained(
config.pretrained_model_name_or_path,
revision=config.revision,
dtype=config.activations_dtype,
split_head_dim=config.split_head_dim,
norm_num_groups=config.norm_num_groups,
attention_kernel=config.attention,
flash_block_sizes=flash_block_sizes,
mesh=mesh,
tokenizer=None,
tokenizer_2=None,
text_encoder=None,
text_encoder_2=None,
unet=None,
)
scheduler_config = pipeline.scheduler.config
assert scheduler_config["prediction_type"] == "epsilon"
assert not scheduler_config.get("rescale_zero_terminal_snr", False)
assert scheduler_config["timestep_spacing"] == "leading"
scheduler, _ = maxdiffusion_utils.create_scheduler(scheduler_config, config)
assert scheduler.config["prediction_type"] == "v_prediction"
assert scheduler.config["rescale_zero_terminal_snr"]
assert scheduler.config["timestep_spacing"] == "trailing"
# Test class name override without Flax Name.
pyconfig.initialize(
[
None,
os.path.join(THIS_DIR, "..", "configs", "base_xl.yml"),
"pretrained_model_name_or_path=gs://maxdiffusion-github-runner-test-assets/checkpoints/models--stabilityai--stable-diffusion-xl-base-1.0",
"revision=refs/pr/95",
"activations_dtype=bfloat16",
'diffusion_scheduler_config={"_class_name" : "DDIMScheduler", "prediction_type" : "v_prediction", '
'"rescale_zero_terminal_snr" : true, "timestep_spacing" : "trailing"}',
],
unittest=True,
)
config = pyconfig.config
scheduler_config = scheduler.config
scheduler, _ = maxdiffusion_utils.create_scheduler(scheduler_config, config)
assert scheduler.config["prediction_type"] == "v_prediction"
assert scheduler.config["rescale_zero_terminal_snr"]
assert scheduler.config["timestep_spacing"] == "trailing"
assert type(scheduler) is FlaxDDIMScheduler
# Test class name override with Flax Name.
pyconfig.initialize(
[
None,
os.path.join(THIS_DIR, "..", "configs", "base_xl.yml"),
"pretrained_model_name_or_path=gs://maxdiffusion-github-runner-test-assets/checkpoints/models--stabilityai--stable-diffusion-xl-base-1.0",
"revision=refs/pr/95",
"activations_dtype=bfloat16",
'diffusion_scheduler_config={"_class_name" : "FlaxDDPMScheduler", "prediction_type" : "v_prediction", '
'"rescale_zero_terminal_snr" : true, "timestep_spacing" : "trailing"}',
],
unittest=True,
)
config = pyconfig.config
scheduler_config = scheduler.config
scheduler, _ = maxdiffusion_utils.create_scheduler(scheduler_config, config)
assert scheduler.config["prediction_type"] == "v_prediction"
assert scheduler.config["rescale_zero_terminal_snr"]
assert scheduler.config["timestep_spacing"] == "trailing"
assert type(scheduler) is FlaxDDPMScheduler