-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_seq2seq.py
165 lines (126 loc) · 5.14 KB
/
sample_seq2seq.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
"""
Generate a large batch of image samples from a model and save them as a large
numpy array. This can be used to produce samples for FID evaluation.
"""
import argparse
import os, json
from tracemalloc import start
import numpy as np
import torch
import torch as th
import torch.distributed as dist
import torch.nn.functional as F
from transformers import set_seed
from diffuseq.rounding import denoised_fn_round, get_weights
from diffuseq.text_datasets import load_data_text
from diffuseq.gaussian_diffusion import GaussianDiffusion, index_to_log_onehot
# from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
import time
from diffuseq.utils import dist_util, logger
from functools import partial
from basic_utils import (
load_defaults_config,
create_model_and_diffusion,
add_dict_to_argparser,
args_to_dict,
load_model_emb,
load_tokenizer
)
def create_argparser():
defaults = dict(model_path='', step=0, out_dir='', top_p=0, hardcoded_pseudo_diralpha=5)
decode_defaults = dict(split='valid', clamp_step=0, seed2=105, clip_denoised=False)
defaults.update(load_defaults_config())
defaults.update(decode_defaults)
parser = argparse.ArgumentParser()
add_dict_to_argparser(parser, defaults)
return parser
def main():
args = create_argparser().parse_args()
dist_util.setup_dist()
logger.configure()
# load configurations.
config_path = os.path.join(os.path.split(args.model_path)[0], "training_args.json")
print(config_path)
# sys.setdefaultencoding('utf-8')
with open(config_path, 'rb', ) as f:
training_args = json.load(f)
training_args['batch_size'] = args.batch_size
args.__dict__.update(training_args)
logger.log("### Creating model and diffusion...")
model, _ = create_model_and_diffusion(
**args_to_dict(args, load_defaults_config().keys())
)
model.load_state_dict(
dist_util.load_state_dict(args.model_path, map_location="cpu")
)
pytorch_total_params = sum(p.numel() for p in model.parameters())
logger.log(f'### The parameter count is {pytorch_total_params}')
model.to(dist_util.dev())
model.eval()
tokenizer = load_tokenizer(args)
set_seed(args.seed2)
print("### Sampling...on", args.split)
## load data
data_valid = load_data_text(
batch_size=args.batch_size,
seq_len=args.seq_len,
src_seq_len=args.src_seq_len,
deterministic=True,
data_args=args,
split=args.split,
loaded_vocab=tokenizer,
model_emb=None, # using the same embedding wight with training data
loop=False
)
start_t = time.time()
diffusion = GaussianDiffusion(Timestep=200)
# batch, cond = next(data_valid)
# print(batch.shape)
model_base_name = os.path.basename(os.path.split(args.model_path)[0]) + f'.{os.path.split(args.model_path)[1]}'
out_dir = os.path.join(args.out_dir, f"{model_base_name.split('.ema')[0]}")
if not os.path.isdir(out_dir):
os.mkdir(out_dir)
out_path = os.path.join(out_dir, f"ema{model_base_name.split('.ema')[1]}.samples")
if not os.path.isdir(out_path):
os.mkdir(out_path)
out_path = os.path.join(out_path, f"seed{args.seed2}_step{args.clamp_step}.json")
# fout = open(out_path, 'a')
all_test_data = []
try:
while True:
cond = next(data_valid)
# print(batch.shape)
all_test_data.append(cond)
except StopIteration:
print('### End of reading iteration...')
from tqdm import tqdm
for cond in tqdm(all_test_data):
decoder_input_ids = cond.pop('decoder_input_ids').to(dist_util.dev())
input_ids = cond.pop('input_ids').to(dist_util.dev())
input_mask = cond.pop('input_mask').to(dist_util.dev())
sample = diffusion.sample(model, decoder_input_ids, input_ids, input_mask) # [batch, len]
gathered_samples = [th.zeros_like(sample) for _ in range(dist.get_world_size())]
dist.all_gather(gathered_samples, sample)
all_sentence = [sample.cpu().numpy() for sample in gathered_samples]
# print('sampling takes {:.2f}s .....'.format(time.time() - start_t))
word_lst_recover = []
word_lst_ref = []
word_lst_source = []
sample = np.concatenate(all_sentence, axis=0)
# tokenizer = load_tokenizer(args)
for seq in sample:
tokens = tokenizer.decode_token(seq)
word_lst_recover.append(tokens)
print("Recover:{}".format(word_lst_recover))
for seq, input_seq in zip(decoder_input_ids, input_ids):
word_lst_source.append(tokenizer.decode_token(input_seq))
word_lst_ref.append(tokenizer.decode_token(seq))
print("Source:{}".format(word_lst_source))
fout = open(out_path, 'a')
for (recov, ref, src) in zip(word_lst_recover, word_lst_ref, word_lst_source):
print(json.dumps({"recover": recov, "reference": ref, "source": src}), file=fout)
fout.close()
print('### Total takes {:.2f}s .....'.format(time.time() - start_t))
print(f'### Written the decoded output to {out_path}')
if __name__ == "__main__":
main()