forked from xdit-project/xDiT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd3cn_example.py
239 lines (219 loc) · 7.03 KB
/
sd3cn_example.py
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import argparse
import torch
from legacy.pipefuser.pipelines.sd3cn import DistriSD3CNPipeline
from legacy.pipefuser.utils import DistriConfig
from torch.profiler import profile, ProfilerActivity
from diffusers.utils import load_image
import time
HAS_LONG_CTX_ATTN = False
try:
from yunchang import set_seq_parallel_pg
HAS_LONG_CTX_ATTN = True
except ImportError:
print("yunchang not found")
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_id",
default="stabilityai/stable-diffusion-3-medium-diffusers",
type=str,
help="Path to the pretrained model.",
)
parser.add_argument(
"--parallelism",
"-p",
default="pipefusion",
type=str,
choices=["patch", "naive_patch", "pipefusion", "tensor", "sequence"],
help="Parallelism to use.",
)
parser.add_argument(
"--use_seq_parallel_attn",
action="store_true",
default=False,
help="Enable sequence parallel attention.",
)
parser.add_argument(
"--sync_mode",
type=str,
default="corrected_async_gn",
choices=[
"separate_gn",
"async_gn",
"corrected_async_gn",
"sync_gn",
"full_sync",
"no_sync",
],
help="Different GroupNorm synchronization modes",
)
parser.add_argument(
"--num_inference_steps",
type=int,
default=28,
)
parser.add_argument(
"--pp_num_patch", type=int, default=4, help="patch number in pipefusion."
)
parser.add_argument(
"--height",
type=int,
default=1024,
help="The height of image",
)
parser.add_argument(
"--width",
type=int,
default=1024,
help="The width of image",
)
parser.add_argument(
"--no_use_resolution_binning",
action="store_true",
)
parser.add_argument(
"--ulysses_degree",
type=int,
default=1,
)
parser.add_argument(
"--pipefusion_warmup_step",
type=int,
default=1,
)
parser.add_argument(
"--use_use_ulysses_low",
action="store_true",
)
parser.add_argument(
"--use_profiler",
action="store_true",
)
# parser.add_argument(
# "--use_cuda_graph",
# action="store_true",
# )
# parser.add_argument(
# "--use_parallel_vae",
# action="store_true",
# )
parser.add_argument(
"--output_type",
type=str,
default="latent",
choices=["latent", "pil"],
help="latent saves memory, pil will results a memory burst in vae",
)
parser.add_argument("--attn_num", default=None, nargs="*", type=int)
parser.add_argument(
"--scheduler",
"-s",
default="FM-ED",
type=str,
choices=["dpm-solver", "ddim", "FM-ED"],
help="Scheduler to use.",
)
parser.add_argument(
"--prompt",
type=str,
default="An astronaut riding a green horse",
)
parser.add_argument("--output_file", type=str, default=None)
args = parser.parse_args()
# torch.backends.cudnn.benchmark=True
torch.backends.cudnn.deterministic = True
# for DiT the height and width are fixed according to the model
distri_config = DistriConfig(
height=args.height,
width=args.width,
warmup_steps=args.pipefusion_warmup_step,
split_batch=False,
parallelism=args.parallelism,
mode=args.sync_mode,
# mode = "full_sync",
pp_num_patch=args.pp_num_patch,
attn_num=args.attn_num,
scheduler=args.scheduler,
)
pipeline = DistriSD3CNPipeline.from_pretrained(
distri_config=distri_config,
pretrained_model_name_or_path=args.model_id,
# variant="fp16",
# use_safetensors=True,
)
pipeline.set_progress_bar_config(disable=distri_config.rank != 0)
# warmup
# output = pipeline(
# prompt=args.prompt,
# generator=torch.Generator(device="cuda").manual_seed(42),
# output_type=args.output_type,
# )
torch.cuda.reset_peak_memory_stats()
case_name = f"{args.parallelism}_hw_{args.height}_sync_{args.sync_mode}_sp_{args.use_seq_parallel_attn}_u{args.ulysses_degree}_w{distri_config.world_size}_mb{args.pp_num_patch if args.parallelism=='pipefusion' else 0}"
if args.output_file:
case_name = args.output_file + "_" + case_name
# avg_elapsed_time = 0
# for i in range(10):
control_image = load_image("https://huggingface.co/InstantX/SD3-Controlnet-Canny/resolve/main/canny.jpg")
# print(control_image)
# exit()
# control_image.save('cn.png')
prompt = "Taylor Swift"
if args.use_profiler:
start_time = time.time()
with profile(
activities=[ProfilerActivity.CUDA],
on_trace_ready=torch.profiler.tensorboard_trace_handler(
f"./profile/{case_name}"
),
profile_memory=True,
with_stack=True,
record_shapes=True,
) as prof:
output = pipeline(
prompt=prompt,
control_image=control_image,
controlnet_conditioning_scale=0.7,
generator=torch.manual_seed(21),
# generator=torch.Generator(device="cuda").manual_seed(42),
num_inference_steps=args.num_inference_steps,
output_type=args.output_type,
)
# if distri_config.rank == 0:
# prof.export_memory_timeline(
# f"{distri_config.mode}_{args.height}_{distri_config.world_size}_mem.html"
# )
end_time = time.time()
else:
# MAX_NUM_OF_MEM_EVENTS_PER_SNAPSHOT = 100000
# torch.cuda.memory._record_memory_history(
# max_entries=MAX_NUM_OF_MEM_EVENTS_PER_SNAPSHOT
# )
start_time = time.time()
output = pipeline(
prompt=prompt,
control_image=control_image,
controlnet_conditioning_scale=0.7,
generator=torch.manual_seed(21),
# generator=torch.Generator(device="cuda").manual_seed(42),
num_inference_steps=args.num_inference_steps,
output_type=args.output_type,
)
end_time = time.time()
# torch.cuda.memory._dump_snapshot(
# f"{distri_config.mode}_{distri_config.world_size}.pickle"
# )
torch.cuda.memory._record_memory_history(enabled=None)
elapsed_time = end_time - start_time
# avg_elapsed_time +=elapsed_time
peak_memory = torch.cuda.max_memory_allocated(device="cuda")
if distri_config.rank == 0:
print(
f"{case_name} epoch time: {elapsed_time:.2f} sec, memory: {peak_memory/1e9} GB"
)
if args.output_type == "pil":
print(f"save images to ./{case_name}.png")
output.images[0].save(f"./{case_name}.png")
# print(f"Average Elapsed Time {avg_elapsed_time/10}")
if __name__ == "__main__":
main()