-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_text.py
104 lines (89 loc) · 4.53 KB
/
generate_text.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
# coding=utf-8
# Copyright (c) 2020, 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.
"""Sample Generate GPT"""
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
os.path.pardir)))
from megatron import get_args
from megatron import print_rank_0
from megatron import mpu
from megatron.checkpointing import load_checkpoint
from megatron.initialize import initialize_megatron
from megatron.model import GPTModel
from megatron.training import get_model
from megatron.text_generation import generate_and_post_process
import numpy as np
import time
import torch
def model_provider(pre_process=True, post_process=True):
"""Build the model."""
print_rank_0('building GPT model ...')
model = GPTModel(num_tokentypes=0, parallel_output=False, pre_process=pre_process, post_process=post_process)
return model
def add_text_generate_args(parser):
group = parser.add_argument_group(title='text generation')
group.add_argument("--prompts-file", type=str, required=True,
help='File with prompt (that can then be truncated)')
group.add_argument("--all-num-input-tokens", type=int, nargs='+',
help='Number of input tokens (must be > 0)')
group.add_argument("--all-num-output-tokens", type=int, nargs='+',
help='Number of tokens to generate')
group.add_argument("--temperature", type=float, default=1.0,
help='Sampling temperature.')
group.add_argument("--top-p", type=float, default=0.0,
help='Top p sampling.')
group.add_argument("--top-k", type=int, default=0,
help='Top k sampling.')
return parser
if __name__ == "__main__":
initialize_megatron(extra_args_provider=add_text_generate_args,
args_defaults={'tokenizer_type': 'GPT2BPETokenizer',
'no_load_rng': True,
'no_load_optim': True})
args = get_args()
if args.num_layers_per_virtual_pipeline_stage is not None:
print("Interleaved pipeline schedule is not yet supported for text generation.")
exit()
# Set up model and load checkpoint
model = get_model(model_provider, wrap_with_ddp=False)
if args.load is not None:
_ = load_checkpoint(model, None, None)
assert len(model) == 1, "Above condition should have caught this"
model = model[0]
num_warmup_iterations = 10
num_iterations = 50
for num_input_tokens in args.all_num_input_tokens:
with open(args.prompts_file, 'r') as f:
prompts = [f.read()[:10*num_input_tokens]] # Hack to prevent tokenization from taking too long.
for num_output_tokens in args.all_num_output_tokens:
times = []
last_start_time = time.time()
for i in range(num_warmup_iterations + num_iterations):
if i == num_warmup_iterations:
start_time = time.time()
output = generate_and_post_process(model, prompts=prompts,
num_input_tokens=num_input_tokens,
tokens_to_generate=num_output_tokens,
temperature=args.temperature,
top_k_sampling=args.top_k,
top_p_sampling=args.top_p,
use_eod_token_for_early_termination=False)
times.append(time.time() - last_start_time)
last_start_time = time.time()
runtime = (time.time() - start_time) / num_iterations
if torch.distributed.get_rank() == 0:
label = {'num_input_tokens': num_input_tokens, 'num_output_tokens': num_output_tokens}
print(f"Runtime for {label}: {runtime:.3f} seconds +- {np.std(times[num_warmup_iterations:]):.3f}")