forked from foundation-model-stack/foundation-model-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
250 lines (220 loc) · 7.8 KB
/
inference.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import argparse
import itertools
import os
import random
import numpy as np
import torch
import torch._inductor.config
from torch import distributed as dist
from fms.models import get_model
from fms.utils import fusion, generation, tokenizers
from fms.utils.generation import generate, pad_input_ids
# This example script validates the LLaMA implementation by running inference on a couple of prompts.
#
# Example usage with single-GPU 7B model on slurm, with torch.compile and determinstic behavior:
# CUBLAS_WORKSPACE_CONFIG=:4096:8 srun -N 1 --gres=gpu:1 python scripts/inference.py --model_path=~/models/7B-F/ --tokenizer=~/models/tokenizer.model --compile --deterministic
# Example usage of 13B model on 2 GPUs with Tensor Parallel:
# srun -N 1 --gres=gpu:2 torchrun --nproc_per_node=2 scripts/inference.py --model_path=~/models/13B-F --tokenizer=~/models/tokenizer.model --distributed
parser = argparse.ArgumentParser(
description="Script to run inference on a causal model"
)
parser.add_argument("--device_type", type=str, default="cuda")
parser.add_argument(
"--architecture",
type=str,
default="llama",
help="The model architecture to benchmark",
)
parser.add_argument(
"--variant",
type=str,
default="7b",
help="The model variant (configuration) to benchmark. E.g. 7b, 13b, 70b.",
)
parser.add_argument(
"--model_path",
type=str,
help="Path to the directory containing LLaMa weights (.pth files sharded by tensor parallel rank, not HF weights)",
)
parser.add_argument(
"--model_source",
type=str,
help="Source of the checkpoint. E.g. 'meta', 'hf', None",
)
parser.add_argument(
"--tokenizer",
type=str,
required=True,
help="Path to the tokenizer (e.g. ~/tokenizer.model)",
)
parser.add_argument(
"--no_use_cache",
action="store_false",
help="Disable the kv-cache (on by default)",
)
parser.add_argument(
"--unfuse_weights",
action="store_true",
help="If set to True, this will unfuse any fused weight modules that support the unfuse_weights method",
)
parser.add_argument(
"--compile",
action="store_true",
help="Use torch.compile (slow for first inference pass)",
)
parser.add_argument(
"--compile_mode",
type=str,
help="Mode for compilation",
default="default",
choices=["default", "reduce-overhead"],
)
parser.add_argument(
"--deterministic",
action="store_true",
help="Set torch.use_deterministic_algorithms? Requires env variable `CUBLAS_WORKSPACE_CONFIG=:4096:8`",
)
parser.add_argument(
"--distributed",
action="store_true",
help="This is a distributed job (multiple instances run with RANK+WORLD_SIZE)",
)
parser.add_argument(
"--batch_input",
action="store_true",
help="use a batch of prompts as input",
)
parser.add_argument(
"--min_pad_length",
type=int,
help="Pad inputs to a minimum specified length. If any prompt is larger than the specified length, padding will be determined by the largest prompt",
default=0,
)
parser.add_argument("--context_file", type=str, default=None, help="File to summarize")
args = parser.parse_args()
local_rank = int(os.getenv("LOCAL_RANK", 0))
world_size = int(os.getenv("WORLD_SIZE", 1))
if args.device_type == "cuda":
device = torch.device(args.device_type, local_rank)
torch.cuda.set_device(device)
else:
device = torch.device(args.device_type)
torch.set_default_dtype(torch.float16)
# requires setting environment variable: `CUBLAS_WORKSPACE_CONFIG=:4096:8`
if args.deterministic:
SEED = 42
random.seed(SEED)
torch.manual_seed(SEED) # pytorch random seed
np.random.seed(SEED) # numpy random seed
torch.use_deterministic_algorithms(True)
if args.distributed:
dist.init_process_group()
# Fix until PT 2.3
torch._C._distributed_c10d._register_process_group("default", dist.group.WORLD)
print("loading model")
if args.distributed:
distr_param = "tp"
else:
if torch.cuda.device_count() > 1 and world_size == 1:
distr_param = "mp"
else:
distr_param = None
model = get_model(
args.architecture,
args.variant,
model_path=args.model_path,
device_type=args.device_type,
source=args.model_source,
distributed_strategy=distr_param,
group=dist.group.WORLD,
)
if args.unfuse_weights:
print("unfusing weights")
model = fusion.apply_unfuse_weights(model)
tokenizer = tokenizers.get_tokenizer(args.tokenizer)
model.eval()
torch.set_grad_enabled(False)
print("loading complete on rank", local_rank)
if args.compile:
print("compiling model")
# compiling can make first inference pass slow
model.compile(mode=args.compile_mode)
def ids_for_prompt(prompt):
tokens = tokenizer.tokenize(prompt)
ids = tokenizer.convert_tokens_to_ids(tokens)
ids = [tokenizer.bos_token_id] + ids
ids = torch.tensor(ids, dtype=torch.long, device=device)
return ids
if args.context_file is not None:
# during testing, the context_file used was a copy/paste of the text of:
# https://arxiv.org/pdf/2306.15595.pdf
with open(args.context_file) as file:
long_prompt = file.read()
prompt1 = (
long_prompt
+ "\nPlease give me a brief summary of this research paper in a few bullet points."
)
# prompt1 = long_prompt + "\nDescribe work that was done concurrently with the research in this paper."
prompt2 = long_prompt + "\nPlease write me the abstract for this paper."
else:
template = "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{}\n\n### Response:"
prompt1 = template.format(
"Provide a list of instructions for preparing chicken soup."
)
prompt2 = template.format("Explain some popular greetings in Spanish.")
prompt1 = ids_for_prompt(prompt1)
prompt2 = ids_for_prompt(prompt2)
max_len = max([len(prompt) for prompt in [prompt1, prompt2]])
if args.batch_input:
ids = [prompt1, prompt2]
ids, padding_kwargs = pad_input_ids(ids, min_pad_length=args.min_pad_length)
else:
ids = prompt1
if args.min_pad_length != 0:
ids, padding_kwargs = pad_input_ids([ids], min_pad_length=args.min_pad_length)
else:
padding_kwargs = None
def print_result(result):
if local_rank != 0:
return
# stop at EOS token if present
result = generation.truncate_after_eos(result, tokenizer.eos_token_id)
# print(result)
# print(tokenizer.convert_ids_to_tokens(result))
print(tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(result)))
print()
def infer(use_cache, do_sample):
# With greedy generation (do_sample=False) we _should_ always get the same results.
# There is currently a bug in start_pos for batched rotary embeddings that can lead
# varying results for the same prompt.
if local_rank == 0:
print("use_cache", use_cache, ";; do_sample", do_sample)
print("==================")
if (
getattr(model.config, "ntk_scaling", None) is not None
and model.config.ntk_scaling
):
max_seq_len = max(max_len, model.config.max_expected_seq_len)
else:
# without ntk scaling, extending the seq length too far gives bogus results.
max_seq_len = model.config.max_expected_seq_len
result = generate(
model,
ids,
max_new_tokens=100,
use_cache=use_cache,
do_sample=do_sample,
max_seq_len=max_seq_len,
extra_kwargs=padding_kwargs,
)
if len(result.shape) == 1:
result = result.unsqueeze(0)
for i in range(result.shape[0]):
print_result(result[i])
print("generating output", local_rank)
do_sample = [False]
use_cache = [
args.no_use_cache
] # True/False are identical with greedy iff `torch.use_deterministic_algorithms(True)`
for sample, cache in itertools.product(do_sample, use_cache):
infer(cache, sample)