-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_samples.py
executable file
·163 lines (142 loc) · 6.42 KB
/
generate_samples.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
# -*- encoding: utf-8 -*-
'''
@File : inference_glm.py
@Time : 2021/10/22 19:41:58
@Author : Ming Ding
@Contact : [email protected]
'''
# here put the import lib
import os
import sys
import random
import time
from datetime import datetime
import torch
import torch.nn.functional as F
import argparse
import stat
from functools import partial
from arguments import get_args
from model import GLMFPrefixModel
from finetune_glm import load_pretrained
from SwissArmyTransformer.model import GLMModel
from SwissArmyTransformer.model.mixins import CachedAutoregressiveMixin
from SwissArmyTransformer.generation.autoregressive_sampling import filling_sequence
from SwissArmyTransformer.generation.sampling_strategies import BeamSearchStrategy, BaseStrategy
from SwissArmyTransformer.generation.utils import timed_name, generate_continually
from SwissArmyTransformer.tokenization import get_tokenizer
from SwissArmyTransformer.training.deepspeed_training import initialize_distributed, set_random_seed
def get_masks_and_position_ids_glm(seq, mask_position, context_length):
tokens = seq.unsqueeze(0)
attention_mask = torch.ones((1, len(seq), len(seq)), device=tokens.device)
attention_mask.tril_()
attention_mask[..., :context_length] = 1
attention_mask.unsqueeze_(1)
position_ids = torch.zeros(2, len(seq), device=tokens.device, dtype=torch.long)
torch.arange(0, context_length, out=position_ids[0, :context_length])
position_ids[0, context_length:] = mask_position
torch.arange(1, len(seq) - context_length + 1, out=position_ids[1, context_length:])
position_ids = position_ids.unsqueeze(0)
return tokens, attention_mask, position_ids
def main(args):
os.environ['MASTER_PORT'] = '9901'
args.do_train = False
initialize_distributed(args)
tokenizer = get_tokenizer(args)
# build model
model = GLMFPrefixModel(args) if args.prefix_prompt > 0 else GLMModel(args)
model.add_mixin('auto-regressive', CachedAutoregressiveMixin())
if args.fp16:
model = model.half()
model = model.to(args.device)
load_pretrained(model, args.load_pretrained, args)
set_random_seed(args.seed)
model.eval()
end_tokens = [tokenizer.get_command('eop').Id, tokenizer.get_command('eos').Id]
# define function for each query
if args.num_beams == 1:
strategy = BaseStrategy(temperature=args.temperature, top_k=args.top_k, end_tokens=end_tokens)
else:
strategy = BeamSearchStrategy(args.num_beams, length_penalty=args.length_penalty, consider_end=True,
end_tokens=end_tokens, no_repeat_ngram_size=args.no_repeat_ngram_size,
min_tgt_length=args.min_tgt_length)
def process(raw_text):
if args.with_id:
query_id, raw_text = raw_text.split('\t')
# add MASK
generation_mask = '[gMASK]' if args.task_mask else '[MASK]'
if 'MASK]' not in raw_text:
raw_text += ' ' + generation_mask
seq = tokenizer.EncodeAsIds(raw_text).tokenization
seq = [tokenizer.get_command('ENC').Id] + seq
if not raw_text.endswith('MASK]'):
seq = seq + [tokenizer.get_command('eos').Id]
print('raw text: {}\n'.format(raw_text))
if len(seq) > args.max_sequence_length:
raise ValueError('text too long.')
# generation
mbz = args.max_inference_batch_size
assert args.batch_size < mbz or args.batch_size % mbz == 0
output_list = [seq]
# continually detect the first mark position
while True:
seq = output_list[0] # TODO find the best one
# detect
mask_tokens = ['MASK', 'sMASK', 'gMASK'] if args.task_mask else ['MASK']
mask_tokens = [tokenizer.get_command(token).Id for token in mask_tokens]
mask_position = len(seq)
for token in mask_tokens:
try:
mask_position = min(mask_position, seq.index(token))
except ValueError:
pass
if mask_position == len(seq):
break
get_func = partial(get_masks_and_position_ids_glm, mask_position=mask_position, context_length=len(seq))
output_list = []
for tim in range(max(args.batch_size // mbz, 1)):
input_seq = torch.cuda.LongTensor(
seq + [tokenizer.get_command('sop').Id] + [-1] * args.out_seq_length,
device=args.device)
output = filling_sequence(model, input_seq,
batch_size=args.num_beams,
strategy=strategy,
log_attention_weights=None,
get_masks_and_position_ids=get_func
)[0] # we don't use mems, fill back
if isinstance(output, torch.Tensor): # different strategies
output = list(output)
output_list.extend(output)
# clip -1s and fill back generated things into seq
for i in range(len(output_list)):
output = output_list[i].tolist()
try:
unfinished = output.index(-1)
except ValueError:
unfinished = len(output)
if output[unfinished - 1] in end_tokens:
unfinished -= 1
bog = output.index(tokenizer.get_command('sop').Id)
output_list[i] = output[:mask_position] + output[bog + 1:unfinished] + output[mask_position + 1:bog]
# decoding
txts = []
for seq in output_list:
decode_tokens = tokenizer.DecodeIds(seq)
txts.append(decode_tokens)
# save
if args.with_id:
full_path = os.path.join(args.output_path, query_id + '.txt')
else:
prefix = raw_text.replace('/', '')[:20]
full_path = timed_name(prefix, '.txt', args.output_path)
print(txts[0]) # print the first.
with open(full_path, 'w') as fout:
for txt in txts:
fout.write(txt + '\n')
os.chmod(full_path, stat.S_IRWXO + stat.S_IRWXG + stat.S_IRWXU)
os.makedirs(args.output_path, exist_ok=True)
generate_continually(process, args.input_source)
if __name__ == "__main__":
args = get_args()
with torch.no_grad():
main(args)