forked from mindspore-lab/mindone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
285 lines (258 loc) · 9.37 KB
/
sample.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import argparse
import datetime
import logging
import os
import sys
import time
import yaml
from pipelines import InferPipeline
from utils.model_utils import _check_cfgs_in_parser, count_params, remove_pname_prefix, str2bool
import mindspore as ms
from mindspore import Tensor, ops
# TODO: remove in future when mindone is ready for install
__dir__ = os.path.dirname(os.path.abspath(__file__))
mindone_lib_path = os.path.abspath(os.path.join(__dir__, "../../"))
sys.path.insert(0, mindone_lib_path)
from modules.autoencoder import SD_CONFIG, AutoencoderKL
from mindone.models.latte import Latte_models
from mindone.utils.amp import auto_mixed_precision
from mindone.utils.logger import set_logger
from mindone.utils.seed import set_random_seed
from mindone.visualize.videos import save_videos
logger = logging.getLogger(__name__)
def init_env(args):
# no parallel mode currently
ms.set_context(mode=args.mode) # needed for MS2.0
device_id = int(os.getenv("DEVICE_ID", 0))
ms.set_context(
mode=args.mode,
device_target=args.device_target,
device_id=device_id,
)
if args.precision_mode is not None:
ms.set_context(ascend_config={"precision_mode": args.precision_mode})
return device_id
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
"-c",
default="",
type=str,
help="path to load a config yaml file that describes the setting which will override the default arguments",
)
parser.add_argument(
"--image_size",
type=int,
default=256,
help="image size in [256, 512]",
)
parser.add_argument(
"--num_frames",
type=int,
default=16,
help="number of frames",
)
parser.add_argument(
"--num_classes",
type=int,
default=1000,
help="number of classes, applies only when condition is `class`",
)
parser.add_argument(
"--num_samples",
type=int,
default=3,
help="number of videos to be generated",
)
parser.add_argument(
"--model_name",
"-m",
type=str,
default="Latte-XL/2",
help="Model name ",
)
parser.add_argument(
"--condition",
default=None,
type=str,
help="the condition types: `None` means using no conditions; `text` means using text embedding as conditions;"
" `class` means using class labels as conditions.",
)
parser.add_argument(
"--checkpoint",
type=str,
default="",
help="latte checkpoint path. If specified, will load from it, otherwise, will use random initialization",
)
parser.add_argument(
"--vae_checkpoint",
type=str,
default="models/sd-vae-ft-mse.ckpt",
help="VAE checkpoint file path which is used to load vae weight.",
)
parser.add_argument(
"--sd_scale_factor", type=float, default=0.18215, help="VAE scale factor of Stable Diffusion model."
)
parser.add_argument("--sampling_steps", type=int, default=50, help="Diffusion Sampling Steps")
parser.add_argument("--guidance_scale", type=float, default=8.5, help="the scale for classifier-free guidance")
# MS new args
parser.add_argument("--device_target", type=str, default="Ascend", help="Ascend or GPU")
parser.add_argument("--mode", type=int, default=0, help="Running in GRAPH_MODE(0) or PYNATIVE_MODE(1) (default=0)")
parser.add_argument("--seed", type=int, default=4, help="Inference seed")
parser.add_argument(
"--enable_flash_attention",
default=False,
type=str2bool,
help="whether to enable flash attention. Default is False",
)
parser.add_argument(
"--dtype",
default="fp16",
type=str,
choices=["bf16", "fp16", "fp32"],
help="what data type to use for latte. Default is `fp16`, which corresponds to ms.float16",
)
parser.add_argument(
"--precision_mode",
default=None,
type=str,
help="If specified, set the precision mode for Ascend configurations.",
)
parser.add_argument(
"--use_recompute",
default=False,
type=str2bool,
help="whether use recompute.",
)
parser.add_argument(
"--patch_embedder",
type=str,
default="conv",
choices=["conv", "linear"],
help="Whether to use conv2d layer or dense (linear layer) as Patch Embedder.",
)
parser.add_argument("--ddim_sampling", type=str2bool, default=True, help="Whether to use DDIM for sampling")
default_args = parser.parse_args()
abs_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ""))
if default_args.config:
logger.info(f"Overwrite default arguments with configuration file {default_args.config}")
default_args.config = os.path.join(abs_path, default_args.config)
with open(default_args.config, "r") as f:
cfg = yaml.safe_load(f)
_check_cfgs_in_parser(cfg, parser)
parser.set_defaults(**cfg)
args = parser.parse_args()
return args
if __name__ == "__main__":
time_str = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
save_dir = f"samples/{time_str}"
os.makedirs(save_dir, exist_ok=True)
set_logger(name="", output_dir=save_dir)
# 1. init env
args = parse_args()
init_env(args)
set_random_seed(args.seed)
# 2. model initiate and weight loading
# 2.1 latte
logger.info(f"{args.model_name}-{args.image_size}x{args.image_size} init")
latent_size = args.image_size // 8
latte_model = Latte_models[args.model_name](
input_size=latent_size,
num_classes=args.num_classes,
block_kwargs={"enable_flash_attention": args.enable_flash_attention},
condition=args.condition,
num_frames=args.num_frames,
use_recompute=args.use_recompute,
patch_embedder=args.patch_embedder,
)
if args.dtype == "fp16":
model_dtype = ms.float16
latte_model = auto_mixed_precision(latte_model, amp_level="O2", dtype=model_dtype)
elif args.dtype == "bf16":
model_dtype = ms.bfloat16
latte_model = auto_mixed_precision(latte_model, amp_level="O2", dtype=model_dtype)
else:
model_dtype = ms.float32
if len(args.checkpoint) > 0:
param_dict = ms.load_checkpoint(args.checkpoint)
logger.info(f"Loading ckpt {args.checkpoint} into Latte")
# in case a save ckpt with "network." prefix, removing it before loading
param_dict = remove_pname_prefix(param_dict, prefix="network.")
latte_model.load_params_from_ckpt(param_dict)
else:
logger.warning("Latte uses random initialization!")
latte_model = latte_model.set_train(False)
for param in latte_model.get_parameters(): # freeze latte_model
param.requires_grad = False
# 2.2 vae
logger.info("vae init")
vae = AutoencoderKL(
SD_CONFIG,
4,
ckpt_path=args.vae_checkpoint,
use_fp16=False, # disable amp for vae
)
vae = vae.set_train(False)
for param in vae.get_parameters(): # freeze vae
param.requires_grad = False
# Labels to condition the model with (feel free to change):
# Create sampling noise:
if args.condition == "class":
class_labels = [1, 13, 100]
n = len(class_labels)
y = Tensor(class_labels)
y_null = ops.ones_like(y) * args.num_classes
elif args.condition == "text":
# tokenizer
pass
else:
y, y_null = None, None
n = args.num_samples
z = ops.randn((n, args.num_frames, 4, latent_size, latent_size), dtype=ms.float32)
# 3. build inference pipeline
pipeline = InferPipeline(
latte_model,
vae,
scale_factor=args.sd_scale_factor,
num_inference_steps=args.sampling_steps,
guidance_rescale=args.guidance_scale,
ddim_sampling=args.ddim_sampling,
)
# 4. print key info
num_params_vae, num_params_vae_trainable = count_params(vae)
num_params_latte, num_params_latte_trainable = count_params(latte_model)
num_params = num_params_vae + num_params_latte
num_params_trainable = num_params_vae_trainable + num_params_latte_trainable
key_info = "Key Settings:\n" + "=" * 50 + "\n"
key_info += "\n".join(
[
f"MindSpore mode[GRAPH(0)/PYNATIVE(1)]: {args.mode}",
f"Num of samples: {n}",
f"Num params: {num_params:,} (latte: {num_params_latte:,}, vae: {num_params_vae:,})",
f"Num trainable params: {num_params_trainable:,}",
f"Use model dtype: {model_dtype}",
f"Sampling steps {args.sampling_steps}",
f"DDIM sampling: {args.ddim_sampling}",
f"CFG guidance scale: {args.guidance_scale}",
]
)
key_info += "\n" + "=" * 50
logger.info(key_info)
# init inputs
inputs = {}
inputs["noise"] = z
inputs["y"] = y
inputs["y_null"] = y_null
inputs["scale"] = args.guidance_scale
logger.info(f"Sampling for {n} samples with condition {args.condition}")
start_time = time.time()
# infer
x_samples = pipeline(inputs)
x_samples = x_samples.asnumpy()
end_time = time.time()
# save result
for i in range(n):
save_fp = f"{save_dir}/{i}.gif"
save_videos(x_samples[i : i + 1], save_fp, loop=0)
logger.info(f"save to {save_fp}")