forked from xdit-project/xDiT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
228 lines (204 loc) · 7.34 KB
/
generate.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
import argparse
import os
import torch
from datasets import load_dataset
from diffusers import DDIMScheduler, DPMSolverMultistepScheduler, EulerDiscreteScheduler
from tqdm import trange
from legacy.pipefuser.pipelines import (
DistriSDXLPipeline,
DistriDiTPipeline,
DistriPixArtAlphaPipeline,
)
from legacy.pipefuser.utils import DistriConfig
from legacy.pipefuser.logger import init_logger
logger = init_logger(__name__)
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
# Diffuser specific arguments
parser.add_argument("--output_root", type=str, default=None)
parser.add_argument(
"--num_inference_steps", type=int, default=20, help="Number of inference steps"
)
parser.add_argument(
"--image_size",
type=int,
nargs="*",
default=1024,
help="Image size of generation",
)
parser.add_argument("--guidance_scale", type=float, default=5.0)
parser.add_argument(
"--scheduler", type=str, default="ddim", choices=["euler", "dpm-solver", "ddim"]
)
# pipefuser specific arguments
parser.add_argument(
"--pipeline", type=str, default="dit", choices=["sdxl", "dit", "pixart"]
)
parser.add_argument("--model_path", type=str, default="facebook/DiT-XL-2-256")
parser.add_argument(
"--no_split_batch",
action="store_true",
help="Disable the batch splitting for classifier-free guidance",
)
parser.add_argument(
"--warmup_steps", type=int, default=4, help="Number of warmup steps"
)
parser.add_argument(
"--sync_mode",
type=str,
default="corrected_async_gn",
choices=[
"separate_gn",
"stale_gn",
"corrected_async_gn",
"sync_gn",
"full_sync",
"no_sync",
],
help="Different GroupNorm synchronization modes",
)
parser.add_argument(
"--parallelism",
type=str,
default="patch",
choices=["patch", "tensor", "naive_patch", "pipefusion", "sequence"],
help="patch parallelism, tensor parallelism or naive patch",
)
parser.add_argument(
"--split_scheme",
type=str,
default="alternate",
choices=["row", "col", "alternate"],
help="Split scheme for naive patch",
)
parser.add_argument(
"--no_cuda_graph", action="store_true", help="Disable CUDA graph"
)
parser.add_argument(
"--split", nargs=2, type=int, default=None, help="Split the dataset into chunks"
)
parser.add_argument(
"--pp_num_patch",
type=int,
default=2,
help="Number of patch number in PipeFusion",
)
# Dataset specific arguments
parser.add_argument(
"--dataset", type=str, default="imagenet", choices=["coco", "imagenet"]
)
parser.add_argument(
"--start_idx", type=int, default=0, help="Start index of the dataset"
)
parser.add_argument(
"--end_idx", type=int, default=5000, help="End index of the dataset"
)
args = parser.parse_args()
return args
def main():
args = get_args()
if isinstance(args.image_size, int):
args.image_size = [args.image_size, args.image_size]
else:
if len(args.image_size) == 1:
args.image_size = [args.image_size[0], args.image_size[0]]
else:
assert len(args.image_size) == 2
distri_config = DistriConfig(
height=args.image_size[0],
width=args.image_size[1],
do_classifier_free_guidance=(
args.guidance_scale > 1 if args.pipeline != "dit" else False
),
split_batch=not args.no_split_batch if args.pipeline != "dit" else False,
warmup_steps=args.warmup_steps,
mode=args.sync_mode,
use_cuda_graph=not args.no_cuda_graph,
parallelism=args.parallelism,
split_scheme=args.split_scheme,
pp_num_patch=args.pp_num_patch,
scheduler=args.scheduler,
)
pretrained_model_name_or_path = args.model_path
if args.scheduler == "euler":
scheduler = EulerDiscreteScheduler.from_pretrained(
pretrained_model_name_or_path, subfolder="scheduler"
)
elif args.scheduler == "dpm-solver":
scheduler = DPMSolverMultistepScheduler.from_pretrained(
pretrained_model_name_or_path, subfolder="scheduler"
)
elif args.scheduler == "ddim":
scheduler = DDIMScheduler.from_pretrained(
pretrained_model_name_or_path, subfolder="scheduler"
)
else:
raise NotImplementedError
if args.pipeline == "dit":
pipeline = DistriDiTPipeline.from_pretrained(
pretrained_model_name_or_path=pretrained_model_name_or_path,
distri_config=distri_config,
scheduler=scheduler,
)
elif args.pipeline == "sdxl":
pipeline = DistriSDXLPipeline.from_pretrained(
pretrained_model_name_or_path=pretrained_model_name_or_path,
distri_config=distri_config,
variant="fp16",
use_safetensors=True,
scheduler=scheduler,
)
elif args.pipeline == "pixart":
pipeline = DistriPixArtAlphaPipeline.from_pretrained(
pretrained_model_name_or_path=pretrained_model_name_or_path,
distri_config=distri_config,
output_type="pil",
# scheduler=args.scheduler
)
pipeline.set_progress_bar_config(
disable=distri_config.rank != 0, position=1, leave=False
)
if args.output_root is None:
args.output_root = os.path.join(
"results",
f"{args.dataset}",
f"{args.scheduler}-{args.num_inference_steps}",
f"gpus{distri_config.world_size if args.no_split_batch else distri_config.world_size // 2}-"
f"warmup{args.warmup_steps}-{args.sync_mode}-{args.pp_num_patch}-{args.parallelism}",
)
if distri_config.rank == 0:
os.makedirs(args.output_root, exist_ok=True)
if args.dataset == "coco":
dataset = load_dataset(
"HuggingFaceM4/COCO", name="2014_captions", split="validation"
)
elif args.dataset == "imagenet":
dataset = load_dataset("evanarlian/imagenet_1k_resized_256", split="val")
# dataset = dataset.shuffle(seed=42)
if args.split is not None:
assert args.split[0] < args.split[1]
chunk_size = (5000 + args.split[1] - 1) // args.split[1]
start_idx = args.split[0] * chunk_size
end_idx = min((args.split[0] + 1) * chunk_size, 5000)
else:
start_idx = args.start_idx
end_idx = args.end_idx
for i in trange(
start_idx, end_idx, disable=distri_config.rank != 0, position=0, leave=False
):
if args.pipeline == "dit":
prompt = dataset["label"][i]
elif args.pipeline in ["sdxl", "pixart"]:
prompt = dataset["sentences_raw"][i][i % len(dataset["sentences_raw"][i])]
seed = i
image = pipeline(
prompt,
generator=torch.Generator(device="cuda").manual_seed(seed),
num_inference_steps=args.num_inference_steps,
guidance_scale=args.guidance_scale,
)
if distri_config.rank == 0:
output_path = os.path.join(args.output_root, f"{i:05d}.png")
image.images[0].save(output_path)
if __name__ == "__main__":
main()