forked from mindspore-lab/mindone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs_train.py
247 lines (229 loc) · 9.93 KB
/
args_train.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
import argparse
import logging
import os
import yaml
from utils.model_utils import _check_cfgs_in_parser, str2bool
logger = logging.getLogger()
def parse_train_args(parser):
parser.add_argument(
"--config",
"-c",
default="",
type=str,
help="path to load a config yaml file that describes the training recipes which will override the default arguments",
)
# the following args's defualt value will be overrided if specified in config yaml
parser.add_argument("--data_config_file", default="", type=str, help="data configuration file path")
parser.add_argument("--dataset_name", default="", type=str, help="dataset name")
parser.add_argument("--output_path", default="output/", type=str, help="output directory to save training results")
parser.add_argument(
"--pretrained_model_path",
default="",
type=str,
help="Specify the pretrained model path, either a pretrained " "DiT model or a pretrained Latte model.",
)
# ms
parser.add_argument("--device_target", type=str, default="Ascend", help="Ascend or GPU")
parser.add_argument("--max_device_memory", type=str, default=None, help="e.g. `30GB` for 910a, `59GB` for 910b")
parser.add_argument("--mode", default=0, type=int, help="Specify the mode: 0 for graph mode, 1 for pynative mode")
parser.add_argument("--use_parallel", default=False, type=str2bool, help="use parallel")
# modelarts
parser.add_argument("--enable_modelarts", default=False, type=str2bool, help="run codes in ModelArts platform")
parser.add_argument("--num_workers", default=1, type=int, help="the number of modelarts workers")
parser.add_argument(
"--json_data_path",
default="mindone/examples/stable_diffusion_v2/ldm/data/num_samples_64_part.json",
type=str,
help="the path of num_samples.json containing a dictionary with 64 parts. "
"Each part is a large dictionary containing counts of samples of 533 tar packages.",
)
parser.add_argument(
"--resume",
default=False,
type=str,
help="It can be a string for path to resume checkpoint, or a bool False for not resuming.(default=False)",
)
# training hyper-params
parser.add_argument("--optim", default="adamw", type=str, help="optimizer")
parser.add_argument(
"--betas",
type=float,
default=[0.9, 0.999],
help="Specify the [beta1, beta2] parameter for the AdamW optimizer.",
)
parser.add_argument(
"--optim_eps", type=float, default=1e-6, help="Specify the eps parameter for the AdamW optimizer."
)
parser.add_argument(
"--group_strategy",
type=str,
default="norm_and_bias",
help="Grouping strategy for weight decay. If `norm_and_bias`, weight decay filter list is [beta, gamma, bias]. \
If None, filter list is [layernorm, bias]. Default: norm_and_bias",
)
parser.add_argument("--weight_decay", default=1e-6, type=float, help="Weight decay.")
parser.add_argument("--seed", default=3407, type=int, help="data path")
parser.add_argument("--warmup_steps", default=1000, type=int, help="warmup steps")
parser.add_argument("--train_batch_size", default=10, type=int, help="batch size")
parser.add_argument("--start_learning_rate", default=1e-5, type=float, help="The initial learning rate for Adam.")
parser.add_argument("--end_learning_rate", default=1e-7, type=float, help="The end learning rate for Adam.")
parser.add_argument("--decay_steps", default=0, type=int, help="lr decay steps.")
parser.add_argument("--scheduler", default="cosine_decay", type=str, help="scheduler.")
# dataloader params
parser.add_argument("--dataset_sink_mode", default=False, type=str2bool, help="sink mode")
parser.add_argument("--sink_size", default=-1, type=int, help="dataset sink size. If -1, sink size = dataset size.")
parser.add_argument(
"--epochs",
default=10,
type=int,
help="epochs. If dataset_sink_mode is on, epochs is with respect to dataset sink size. Otherwise, it's w.r.t the dataset size.",
)
parser.add_argument("--init_loss_scale", default=65536, type=float, help="loss scale")
parser.add_argument("--loss_scale_factor", default=2, type=float, help="loss scale factor")
parser.add_argument("--scale_window", default=1000, type=float, help="scale window")
parser.add_argument("--gradient_accumulation_steps", default=1, type=int, help="gradient accumulation steps")
# parser.add_argument("--cond_stage_trainable", default=False, type=str2bool, help="whether text encoder is trainable")
parser.add_argument("--use_ema", default=False, type=str2bool, help="whether use EMA")
parser.add_argument("--clip_grad", default=False, type=str2bool, help="whether apply gradient clipping")
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(
"--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(
"--model_name",
"-m",
type=str,
default="Latte-XL/2",
help="Model name , such as Latte-XL/2, Latte-L/2",
)
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(
"--clip_checkpoint",
type=str,
default=None,
help="CLIP text encoder checkpoint (or sd checkpoint to only load the text encoder part.)",
)
parser.add_argument(
"--sd_scale_factor", type=float, default=0.18215, help="VAE scale factor of Stable Diffusion model."
)
parser.add_argument("--image_size", default=256, type=int, help="the image size used to initiate model")
parser.add_argument("--num_frames", default=16, type=int, help="the num of frames used to initiate model")
parser.add_argument(
"--num_classes",
type=int,
default=1000,
help="number of classes, applies only when condition is `class`",
)
parser.add_argument(
"--enable_flash_attention",
default=None,
type=str2bool,
help="whether to enable flash attention.",
)
parser.add_argument("--drop_overflow_update", default=True, type=str2bool, help="drop overflow update")
parser.add_argument("--loss_scaler_type", default="dynamic", type=str, help="dynamic or static")
parser.add_argument(
"--max_grad_norm",
default=1.0,
type=float,
help="max gradient norm for clipping, effective when `clip_grad` enabled.",
)
parser.add_argument("--ckpt_save_interval", default=1, type=int, help="save checkpoint every this epochs or steps")
parser.add_argument("--ckpt_max_keep", default=10, type=int, help="Maximum number of checkpoints to keep")
parser.add_argument(
"--step_mode",
default=False,
type=str2bool,
help="whether save ckpt by steps. If False, save ckpt by epochs.",
)
parser.add_argument("--profile", default=False, type=str2bool, help="Profile or not")
parser.add_argument(
"--log_level",
type=str,
default="logging.INFO",
help="log level, options: logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR",
)
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("--log_interval", type=int, default=1, help="log interval")
return parser
def parse_embedding_cache_args(parser):
parser.add_argument(
"--cache_file_type",
default="mindrecord",
type=str,
choices=["numpy", "mindrecord"],
help="type of cached dataset file",
)
parser.add_argument(
"--save_data_type",
default="float32",
type=str,
choices=["float16", "float32"],
help="data type when saving embedding cache",
)
parser.add_argument("--cache_folder", default="", type=str, help="directory to save embedding cache")
parser.add_argument(
"--max_page_size",
default=256,
type=int,
choices=[64, 128, 256],
help="The maximum page size for the MindRecord File Writer. Should be one of [64, 128, 256]",
)
parser.add_argument(
"--resume_cache_index", default=None, type=int, help="If provided, will resume cache from this video index."
)
parser.add_argument(
"--dump_every_n_lines",
type=int,
default=1,
help="The number of data items (videos) saved every time calling mindrecord writer.",
)
return parser
def parse_args():
parser = argparse.ArgumentParser()
parser = parse_train_args(parser)
parser = parse_embedding_cache_args(parser)
abs_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ""))
default_args = parser.parse_args()
if 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()
print(args)
return args