-
Notifications
You must be signed in to change notification settings - Fork 41
/
run_helm.py
234 lines (192 loc) · 8.59 KB
/
run_helm.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
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
#!/usr/bin/env python
# coding=utf-8
# Copyright 2018 Google AI, Google Brain and Carnegie Mellon University Authors and the HuggingFace Inc. team.
# Copyright (c) 2018, NVIDIA CORPORATION. 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.
""" Conditional text generation with the auto-regressive models of the library (GPT/GPT-2/CTRL/Transformer-XL/XLNet)
"""
import argparse
import logging
import numpy as np
import torch
import json
import tqdm
import copy
from transformers import (
CTRLLMHeadModel,
CTRLTokenizer,
GPT2LMHeadModel,
GPT2Tokenizer,
OpenAIGPTLMHeadModel,
OpenAIGPTTokenizer,
TransfoXLLMHeadModel,
TransfoXLTokenizer,
XLMTokenizer,
XLMWithLMHeadModel,
XLNetLMHeadModel,
XLNetTokenizer,
)
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
from utils_hh.modify_llama import convert_kvcache_llama_heavy_recent, LlamaAttention_heavy_hitter
from utils_hh.modify_gptneox import convert_kvcache_gpt_neox_heavy_recent, GPTNeoXAttention_Mask
from utils_hh.modify_opt import convert_kvcache_opt_heavy_recent, OPTAttention_Mask
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
MAX_LENGTH = int(10000) # Hardcoded max length to avoid infinite loop
MODEL_CLASSES = {
"gpt2": (GPT2LMHeadModel, GPT2Tokenizer),
"ctrl": (CTRLLMHeadModel, CTRLTokenizer),
"openai-gpt": (OpenAIGPTLMHeadModel, OpenAIGPTTokenizer),
"xlnet": (XLNetLMHeadModel, XLNetTokenizer),
"transfo-xl": (TransfoXLLMHeadModel, TransfoXLTokenizer),
"xlm": (XLMWithLMHeadModel, XLMTokenizer),
}
# Padding text to help Transformer-XL and XLNet with short prompts as proposed by Aman Rusia
# in https://github.com/rusiaaman/XLNet-gen#methodology
# and https://medium.com/@amanrusia/xlnet-speaks-comparison-to-gpt-2-ea1a4e9ba39e
PREFIX = """In 1991, the remains of Russian Tsar Nicholas II and his family
(except for Alexei and Maria) are discovered.
The voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the
remainder of the story. 1883 Western Siberia,
a young Grigori Rasputin is asked by his father and a group of men to perform magic.
Rasputin has a vision and denounces one of the men as a horse thief. Although his
father initially slaps him for making such an accusation, Rasputin watches as the
man is chased outside and beaten. Twenty years later, Rasputin sees a vision of
the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous,
with people, even a bishop, begging for his blessing. <eod> </s> <eos>"""
def set_seed(args):
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
ENABLE_Heavy_Hitter_FUNCTIONS = {
"llama": convert_kvcache_llama_heavy_recent,
"opt": convert_kvcache_opt_heavy_recent,
"gpt_neox": convert_kvcache_gpt_neox_heavy_recent,
}
TAGET_MODULE = {
"llama": LlamaAttention_heavy_hitter,
"opt": OPTAttention_Mask,
"gpt_neox": GPTNeoXAttention_Mask,
}
def adjust_length_to_model(length, max_sequence_length):
if length < 0 and max_sequence_length > 0:
length = max_sequence_length
elif 0 < max_sequence_length < length:
length = max_sequence_length # No generation bigger than model size
elif length < 0:
length = MAX_LENGTH # avoid infinite loop
return length
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--input_path", type=str, default="")
parser.add_argument("--output_path", type=str, default="")
parser.add_argument("--model_name", type=str, default="")
parser.add_argument('--model_arch', type=str, default='opt')
parser.add_argument("--cache_dir", type=str, default="../../checkpoint/")
parser.add_argument("--heavy_ratio", type=float, default=0.1)
parser.add_argument("--recent_ratio", type=float, default=0.1)
parser.add_argument('--enable_small_cache', action='store_true')
parser.add_argument("--sample_num", type=int, default=1000)
parser.add_argument("--k", type=int, default=0)
parser.add_argument("--seed", type=int, default=42, help="random seed for initialization")
parser.add_argument("--no_cuda", action="store_true", help="Avoid using CUDA when available")
parser.add_argument(
"--fp16",
action="store_true",
help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit",
)
args = parser.parse_args()
args.device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
args.n_gpu = 0 if args.no_cuda else torch.cuda.device_count()
logger.warning(f"device: {args.device}, n_gpu: {args.n_gpu}, 16-bits training: {args.fp16}")
set_seed(args)
model_name = args.model_name
input_path = args.input_path
output_path = args.output_path
config = AutoConfig.from_pretrained(model_name, cache_dir=args.cache_dir)
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, cache_dir=args.cache_dir)
model = AutoModelForCausalLM.from_pretrained(model_name, cache_dir=args.cache_dir)
if args.enable_small_cache:
print('Enable Small Cache Size')
config.heavy_ratio = args.heavy_ratio
config.recent_ratio = args.recent_ratio
checkpoint = copy.deepcopy(model.state_dict())
model = ENABLE_Heavy_Hitter_FUNCTIONS[args.model_arch](model, config)
model.load_state_dict(checkpoint)
model.half().eval().cuda()
logger.info(args)
requests = []
with open(input_path, 'r') as f:
for line in f:
if line.strip() != '':
requests.append(json.loads(line))
print(len(requests))
if args.sample_num < len(requests):
print('Sample {} Examples'.format(args.sample_num))
requests = requests[:args.sample_num]
results = []
with torch.no_grad():
for request in tqdm.tqdm(requests):
request = request['request']
result = {'request': request, 'result': {}}
prompt = request['prompt']
temperature = request['temperature']
stop = request['stop']
input_ids = tokenizer(prompt, add_special_tokens=False, return_tensors='pt').input_ids.to(model.device)
output_sequences = model.generate(
input_ids=input_ids,
max_length=request['max_tokens'] + len(input_ids[0]),
temperature=temperature,
top_k=args.k,
top_p=request['top_p'],
do_sample=True,
num_return_sequences=request['n'],
return_dict_in_generate=True, output_scores=True,
)
for name, m in model.named_modules():
if isinstance(m, TAGET_MODULE[args.model_arch]):
m._reset_masks()
tokens = tokenizer.convert_ids_to_tokens(output_sequences['sequences'].squeeze(0))[len(input_ids[0]):]
logprobs = [logits.log_softmax(dim=-1).max().item() for logits in output_sequences['scores']]
top_logprobs = [{i: v for i, v in zip(tokens, logprobs)}]
generate_text = tokenizer.decode(output_sequences['sequences'].squeeze(0)[len(input_ids[0]):])
generate_text = generate_text[: generate_text.find(stop[0])]
result['result'] = {
"choices": [
{
"text": generate_text,
"logprobs": {
"tokens": tokens,
"token_logprobs": logprobs,
"top_logprobs": top_logprobs,
"text_offset": []
},
"finish_reason": "length"
}
],
"request_time": {
"batch_time": 0,
"batch_size": 1}
}
results.append(result)
with open(output_path, 'w') as f:
for result in results:
f.write(json.dumps(result) + '\n')
if __name__ == "__main__":
main()