-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_diff_sample.py
182 lines (138 loc) · 5.91 KB
/
run_diff_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
import argparse
import os
import json
from json import JSONEncoder
from easydict import EasyDict as edict
import torch
import torch.distributed as dist
import numpy as np
import datetime
import time
from tqdm import tqdm
from diffusion.dataloader import load_data
from utils import dist_util, logger
from utils.utils import setup_seed, load_config, init_save_path, create_model
from time import perf_counter as timer
@torch.no_grad()
def main():
# load configs
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
type=str,
nargs="?",
help="Path to the config file.",
default="./config/diff_sample_geolife.yml",
)
args = parser.parse_args()
config = load_config(args.config)
config = edict(config)
# load configurations.
config_path = os.path.join(os.path.join(config.model_path, "training_args.json"))
print(config_path)
# sys.setdefaultencoding('utf-8')
with open(config_path, "rb") as f:
training_args = json.load(f)
config = {**training_args, **config}
config = edict(config)
setup_seed(config.seed)
dist_util.setup_dist()
world_size = dist.get_world_size() or 1
rank = dist.get_rank() or 0
time_now = int(datetime.datetime.now().timestamp())
log_dir = init_save_path(config, time_now=time_now)
logger.configure(dir=log_dir)
logger.log("### Creating model and diffusion...")
config.device = dist_util.get_device()
model = create_model(config)
checkpoint = dist_util.load_state_dict(
os.path.join(config.model_path, config.trained_model_name), map_location="cpu"
)
model.load_state_dict(checkpoint["ema"])
pytorch_total_params = sum(p.numel() for p in model.parameters())
logger.log(f"### The parameter count is {pytorch_total_params}")
model.eval().requires_grad_(False).to(dist_util.get_device())
print("### Sampling...on", config.split)
## load data
data_valid = load_data(
batch_size=config.batch_size,
shuffle=False,
data_args=config,
split=config.split,
)
data_valid = iter(data_valid)
start_t = time.time()
out_path = os.path.join(log_dir, f"seed{config.seed}_step{config.decoding_steps}.json")
all_test_data = []
try:
while True:
inputs = next(data_valid)
all_test_data.append(inputs)
except StopIteration:
print("### End of reading iteration...")
time_ls = []
device = dist_util.get_device()
for inputs in tqdm(all_test_data):
src, tgt, src_ctx, tgt_cxt = inputs
src = src.to(device).long()
tgt = tgt.to(device).long()
src_ctx = {k: v.to(device) for k, v in src_ctx.items()}
tgt_cxt = {k: v.to(device) for k, v in tgt_cxt.items()}
start = timer()
encoder_out = model.encoder(src, context=src_ctx)
# padding_mask B x T
mask = torch.ones_like(tgt) == 1
# initialize
z_0 = model.decoder.forward_embedding(tgt, tgt_cxt=tgt_cxt)
z_t = torch.randn_like(z_0) * config.decoding_rescaling_factor
z_t = z_t.to(encoder_out["encoder_out"])
# self-conditioning
prev_z_0_hat = torch.zeros_like(z_t)
for step in list(range(config.decoding_steps))[::-1]:
z_t, prev_z_0_hat = model.forward_decoder(z_t, step, mask, encoder_out, prev_z_0_hat)
return_dict = model.forward_output_layer(prev_z_0_hat)
time_ls.append((timer() - start) / len(return_dict["tokens"]))
res_dict_ls = []
for i, (seq_pred, seq_src, seq_tgt) in enumerate(zip(return_dict["tokens"], src, tgt)):
res_dict = {
"recover": seq_pred.detach().cpu().numpy(),
"target": seq_tgt.detach().cpu().numpy(),
"source": seq_src.detach().cpu().numpy(),
}
if config.if_include_duration:
res_dict["duration"] = np.round(return_dict["durations"][i].detach().cpu().numpy(), 3)
res_dict["time"] = np.round(return_dict["time"][i].detach().cpu().numpy(), 3)
tgt_dur = tgt_cxt["duration"][i].detach().cpu().numpy()
tgt_dur[tgt_dur != 0] = np.round(((tgt_dur[tgt_dur != 0] + 1) / 2 * 2880), 0)
res_dict["tgt_dur"] = tgt_dur
src_dur = src_ctx["duration"][i].detach().cpu().numpy()
src_dur[src_dur != 0] = np.round(((src_dur[src_dur != 0] + 1) / 2 * 2880), 0)
res_dict["src_dur"] = src_dur
tgt_time = tgt_cxt["time"][i].detach().cpu().numpy()
tgt_time[tgt_time != 0] = (tgt_time[tgt_time != 0] + 1) / 2 * 1440
res_dict["tgt_time"] = np.round(tgt_time, 0)
seq_time = src_ctx["time"][i].detach().cpu().numpy()
seq_time[seq_time != 0] = (seq_time[seq_time != 0] + 1) / 2 * 1440
res_dict["seq_time"] = np.round(seq_time, 0)
if config.if_include_mode:
res_dict["mode"] = return_dict["mode"][i].detach().cpu().numpy()
res_dict["tgt_mode"] = tgt_cxt["mode"][i].detach().cpu().numpy()
res_dict["src_mode"] = src_ctx["mode"][i].detach().cpu().numpy()
res_dict_ls.append(res_dict)
for i in range(world_size):
if i == rank: # Write files sequentially
fout = open(out_path, "a")
for res_dict in res_dict_ls:
print(json.dumps(res_dict, cls=NumpyArrayEncoder), file=fout)
fout.close()
dist.barrier()
print(np.mean(np.array(time_ls)), np.std(np.array(time_ls)))
print("### Total takes {:.2f}s .....".format(time.time() - start_t))
print(f"### Written the decoded output to {out_path}")
class NumpyArrayEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return JSONEncoder.default(self, obj)
if __name__ == "__main__":
main()