-
Notifications
You must be signed in to change notification settings - Fork 129
/
train_t2i_adapter_trainer.py
100 lines (87 loc) · 3.65 KB
/
train_t2i_adapter_trainer.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
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
# http://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 math
import os
import paddle
from adapter import (
AdapterLDM,
AdapterLDMTrainer,
DataArguments,
ModelArguments,
TextImagePair,
)
from paddlenlp.trainer import PdArgumentParser, TrainingArguments, get_last_checkpoint
from paddlenlp.utils.log import logger
def unfreeze_params(params):
for param in params:
param.stop_gradient = False
def main():
parser = PdArgumentParser((ModelArguments, DataArguments, TrainingArguments))
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
# report to custom_visualdl
training_args.report_to = ["custom_visualdl"]
training_args.resolution = data_args.resolution
training_args.image_logging_steps = model_args.image_logging_steps = (
math.ceil(model_args.image_logging_steps / training_args.logging_steps) * training_args.logging_steps
)
training_args.print_config(model_args, "Model")
training_args.print_config(data_args, "Data")
paddle.set_device(training_args.device)
# Detecting last checkpoint.
last_checkpoint = None
if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
last_checkpoint = get_last_checkpoint(training_args.output_dir)
if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
raise ValueError(
f"Output directory ({training_args.output_dir}) already exists and is not empty. "
"Use --overwrite_output_dir to overcome."
)
elif last_checkpoint is not None and training_args.resume_from_checkpoint is None:
logger.info(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
)
model = AdapterLDM(model_args)
train_dataset = TextImagePair(
file_list=data_args.file_list,
size=data_args.resolution,
num_records=data_args.num_records,
buffer_size=data_args.buffer_size,
shuffle_every_n_samples=data_args.shuffle_every_n_samples,
interpolation="lanczos",
tokenizer=model.tokenizer,
control_image_processor=model.control_image_processor,
data_format=data_args.data_format,
)
trainer = AdapterLDMTrainer(
model=model,
args=training_args,
train_dataset=train_dataset,
tokenizer=model.tokenizer,
)
# must set recompute after trainer init
trainer.model.set_recompute(training_args.recompute)
params_to_train = trainer.model.adapter.parameters()
trainer.set_optimizer_grouped_parameters(params_to_train)
checkpoint = None
if training_args.resume_from_checkpoint is not None:
checkpoint = training_args.resume_from_checkpoint
elif last_checkpoint is not None:
checkpoint = last_checkpoint
# Training
trainer.train(resume_from_checkpoint=checkpoint)
trainer.save_model()
trainer.save_state()
if __name__ == "__main__":
main()