-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep2_train_action_calvin.py
More file actions
277 lines (234 loc) · 10.3 KB
/
step2_train_action_calvin.py
File metadata and controls
277 lines (234 loc) · 10.3 KB
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
import torch.nn as nn
# import cv2
from torchvision.utils import save_image
import logging
from pathlib import Path
import sys
from typing import List, Union
import os
import wandb
from time import time
# This is for using the locally installed repo clone when using slurm
sys.path.insert(0, Path(__file__).absolute().parents[1].as_posix())
import hydra
from omegaconf import DictConfig, ListConfig, OmegaConf
from accelerate import Accelerator
import torch
from glob import glob
from copy import deepcopy
from collections import OrderedDict
from pytorch_lightning import Callback, LightningModule, seed_everything, Trainer
from policy_models.utils.utils import (
get_git_commit_hash,
get_last_checkpoint,
initialize_pretrained_weights,
print_system_env_info,
)
#from torch.nn.parallel import DistributedDataParallel as DDP
#################################################################################
# Training Helper Functions #
#################################################################################
@torch.no_grad()
def update_ema(ema_model, model, decay=0.9999):
"""
Step the EMA model towards the current model.
"""
ema_params = OrderedDict(ema_model.named_parameters())
model_params = OrderedDict(model.named_parameters())
for name, param in model_params.items():
name = name.replace("module.", "")
# TODO: Consider applying only to params that require_grad to avoid small numerical changes of pos_embed
ema_params[name].mul_(decay).add_(param.data, alpha=1 - decay)
def requires_grad(model, flag=True):
"""
Set requires_grad flag for all parameters in a model.
"""
for p in model.parameters():
p.requires_grad = flag
def create_logger(logging_dir):
"""
Create a logger that writes to a log file and stdout.
"""
logging.basicConfig(
level=logging.INFO,
format='[\033[34m%(asctime)s\033[0m] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[logging.StreamHandler(), logging.FileHandler(f"{logging_dir}/log.txt")]
)
logger = logging.getLogger(__name__)
return logger
#################################################################################
# Training Loop #
#################################################################################
#@hydra.main(config_path="./policy_conf", config_name="VPP_Calvinabc_train")
def train(cfg: DictConfig) -> None:
os.environ['HYDRA_FULL_ERROR'] = '1'
accelerator = Accelerator()
assert torch.cuda.is_available(), "Training currently requires at least one GPU."
device = accelerator.device
# new added
torch.set_float32_matmul_precision('medium')
if accelerator.is_main_process:
os.makedirs(cfg.log_dir, exist_ok=True) # Make results folder (holds all experiment subfolders)
from datetime import datetime
uuid = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
#experiment_dir = f"{cfg.log_dir}/{uuid}" # Create an experiment folder
experiment_dir = f"output/policy/{uuid}"
checkpoint_dir = f"{experiment_dir}/checkpoints" # Stores saved model checkpoints
eval_dir = f"{experiment_dir}/eval"
os.makedirs(checkpoint_dir, exist_ok=True)
os.makedirs(eval_dir, exist_ok=True)
logger = create_logger(experiment_dir)
logger.info(f"Experiment directory created at {experiment_dir}")
logger.info(f"Training with the following config:\n{OmegaConf.to_yaml(cfg)}")
datamodule = hydra.utils.instantiate(cfg.datamodule)
datamodule.setup()
if accelerator.is_main_process:
logger.info(f"Global batch size {cfg.batch_size:,} num_processes ({accelerator.num_processes})")
chk = get_last_checkpoint(Path.cwd())
train_loader = datamodule.train_dataloader()["lang"]
test_loader = datamodule.val_dataloader()["lang"]
# chk = get_last_checkpoint(Path('/home/temp_store/code/calvin_d/logs/runs/2023-09-10/17-52-50/saved_models/epoch=09_eval_lh/avg_seq_len=2.62.ckpt'))
# Load Model
model = hydra.utils.instantiate(cfg.model)
if "pretrain_chk" in cfg:
initialize_pretrained_weights(model, cfg)
if cfg.use_ckpt_path:
state_dict = torch.load(cfg.ckpt_path, map_location='cpu')
# print('state_dict_key:', state_dict['model'].keys())
print('load_from_ckpt:',cfg.ckpt_path)
# c = []
# hydra.initialize(config_path="../../conf")
# hydra.main(config_name="config_abc.yaml")(lambda x: c.append(x))()
model = hydra.utils.instantiate(cfg.model)
model.load_state_dict(state_dict['model'])
model = model.to(device)
model.process_device()
if accelerator.is_main_process:
logger.info(f"DiT Parameters: {sum(p.numel() for p in model.parameters()):,}")
opt = model.configure_optimizers()["optimizer"]
Ir_scheduler = model.configure_optimizers()["lr_scheduler"]["scheduler"]
model.on_train_start()
if accelerator.is_main_process:
logger.info(f"model parameter init")
ema = deepcopy(model).to(device) # Create an EMA of the model for use after training
requires_grad(ema, False)
update_ema(ema, model, decay=0) # Ensure EMA is initialized with synced weights
ema.eval()
model.train()
model, opt, loader, test_loader = accelerator.prepare(model, opt, train_loader, test_loader)
# model = DDP(model, find_unused_parameters=True)
train_steps = 0
log_steps = 0
running_loss = 0
start_time = time()
eval_batch = None
best_eval_loss = 1e8
if accelerator.is_main_process:
logger.info(f"Training for {cfg.max_epochs} epochs...")
for epoch in range(cfg.max_epochs):
if accelerator.is_main_process:
logger.info(f"Beginning epoch {epoch}...")
running_loss = 0
for idx,data_batch in enumerate(loader):
with accelerator.autocast():
loss = model(data_batch)
opt.zero_grad()
accelerator.backward(loss)
opt.step()
Ir_scheduler.step()
update_ema(ema, model)
running_loss += loss
log_steps += 1
train_steps += 1
if train_steps % cfg.log_every == 0:
torch.cuda.synchronize()
end_time = time()
steps_per_sec = log_steps / (end_time - start_time)
# Reduce loss history over all processes:
avg_loss = torch.tensor(running_loss / log_steps, device=device)
# avg_loss = avg_loss.item() / accelerator.num_processes # why divide?
avg_loss = avg_loss.item()
if accelerator.is_main_process:
logger.info(
f"(step={train_steps:07d}) Train total Loss : {avg_loss:.6f}, Train Steps/Sec: {steps_per_sec:.2f}")
# Reset monitoring variables:
running_loss = 0
log_steps = 0
start_time = time()
model.eval()
if accelerator.is_main_process:
logger.info(f"Finished training epoch {epoch}")
logger.info(f"started validation epoch {epoch}")
val_losses = []
for test_batch in test_loader:
with torch.no_grad():
val_loss = model.module.validation_step(test_batch)
val_losses.append(val_loss["validation_loss"])
val_losses = torch.tensor(val_losses, device=device)
gathered_val_losses = accelerator.gather(val_losses)
accelerator.wait_for_everyone()
if accelerator.is_main_process:
if gathered_val_losses.numel() > 0:
total_val_loss = torch.mean(gathered_val_losses).item()
else:
total_val_loss = 0.0
checkpoint = {
"model": model.module.state_dict() if accelerator.num_processes > 1 else model.state_dict(),
# "ema": ema.state_dict(),
# "opt": opt.state_dict(),
"args": cfg,
}
# if not args.without_ema:
# checkpoint["ema"] = ema.state_dict()
checkpoint_path = f"{checkpoint_dir}/{train_steps:07d}_{total_val_loss:.3f}.pt"
torch.save(checkpoint, checkpoint_path)
logger.info(f"Saved checkpoint to {checkpoint_path}")
best_eval_loss = total_val_loss
last_path = f"{checkpoint_dir}/last.pt"
torch.save(checkpoint, last_path)
accelerator.wait_for_everyone()
model.train()
# Setup accelerator:
def setup_logger(cfg: DictConfig, model: LightningModule):
"""
Set up the logger (tensorboard or wandb) from hydra config.
Args:
cfg: Hydra config
model: LightningModule
Returns:
logger
"""
pathlib_cwd = Path.cwd()
if "group" in cfg.logger:
cfg.logger.group = pathlib_cwd.parent.name
cfg.logger.name = pathlib_cwd.parent.name + "/" + pathlib_cwd.name
cfg.logger.id = cfg.logger.name.replace("/", "_")
train_logger = hydra.utils.instantiate(cfg.logger)
# train_logger.watch(model)
else:
train_logger = hydra.utils.instantiate(cfg.logger)
return train_logger
if __name__ == "__main__":
# os.environ["PL_TORCH_DISTRIBUTED_BACKEND"] = "gloo"
# Set CUDA device IDs
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
# os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
print(torch.cuda.is_available())
print(torch.cuda.device_count())
os.environ["TOKENIZERS_PARALLELISM"] = 'True'
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--video_model_path", type=str, default="")
parser.add_argument("--text_encoder_path", type=str, default="")
parser.add_argument("--root_data_dir", type=str, default="")
args = parser.parse_args()
from hydra import compose, initialize
with initialize(config_path="./policy_conf", job_name="VPP_Calvinabc_train"):
cfg = compose(config_name="VPP_Calvinabc_train")
cfg.model.pretrained_model_path = args.video_model_path
cfg.model.text_encoder_path = args.text_encoder_path
cfg.root_data_dir = args.root_data_dir
cfg.datamodule.root_data_dir = args.root_data_dir
train(cfg)