forked from HabanaAI/Model-References
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
470 lines (413 loc) · 17.4 KB
/
utils.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# coding=utf-8
# Copyright 2022 The HuggingFace Team. 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.
###############################################################################
# Copyright (C) 2020-2024 Habana Labs, Ltd. an Intel Company
###############################################################################
import copy
import glob
import os
import shutil
import tempfile
import time
from pathlib import Path
import logging
import torch
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
from transformers.utils import check_min_version
from optimum.habana.checkpoint_utils import (
get_ds_injection_policy,
get_repo_root,
model_is_optimized,
model_on_meta,
write_checkpoints_json,
)
from optimum.habana.utils import check_habana_frameworks_min_version, check_optimum_habana_min_version, set_seed
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__)
def override_print(enable):
import builtins as __builtin__
builtin_print = __builtin__.print
def print(*args, **kwargs):
force = kwargs.pop("force", False)
if force or enable:
builtin_print(*args, **kwargs)
__builtin__.print = print
def override_logger(logger, enable):
logger_info = logger.info
def info(*args, **kwargs):
force = kwargs.pop("force", False)
if force or enable:
logger_info(*args, **kwargs)
logger.info = info
def count_hpu_graphs():
return len(glob.glob(".graph_dumps/*PreGraph*"))
def override_prints(enable, logger):
override_print(enable)
override_logger(logger, enable)
def setup_distributed(args):
args.local_rank = int(os.getenv("LOCAL_RANK", "0"))
args.world_size = int(os.getenv("WORLD_SIZE", "0"))
args.global_rank = int(os.getenv("RANK", "0"))
def setup_quantization(args, model):
import habana_frameworks.torch.core as htcore
from habana_frameworks.torch.core.quantization import _check_params_as_const, _mark_params_as_const
from habana_frameworks.torch.hpu import hpu
print("Initializing inference with quantization")
_mark_params_as_const(model)
_check_params_as_const(model)
if not args.quant_config:
hpu.enable_quantization()
htcore.hpu_initialize(model)
return model
def setup_env(args):
# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("4.34.0")
check_optimum_habana_min_version("1.9.0.dev0")
os.environ.setdefault('EXPERIMENTAL_WEIGHT_SHARING', 'FALSE')
if args.global_rank == 0:
os.environ.setdefault("GRAPH_VISUALIZATION", "true")
shutil.rmtree(".graph_dumps", ignore_errors=True)
if args.world_size > 0:
os.environ.setdefault("PT_HPU_LAZY_ACC_PAR_MODE", "0")
os.environ.setdefault("PT_HPU_ENABLE_LAZY_COLLECTIVES", "true")
# Tweak generation so that it runs faster on Gaudi
from optimum.habana.transformers.modeling_utils import adapt_transformers_to_gaudi
adapt_transformers_to_gaudi()
def setup_device(args):
if args.device == "hpu":
import habana_frameworks.torch.core as htcore
if args.fp8:
htcore.hpu_set_env()
return torch.device(args.device)
def setup_model(args, model_dtype, model_kwargs, logger):
logger.info("Single-device run.")
model = AutoModelForCausalLM.from_pretrained(args.model_name_or_path, torch_dtype=model_dtype, **model_kwargs)
if args.quant_config:
import habana_quantization_toolkit
habana_quantization_toolkit.prep_model(model)
model = model.eval().to(args.device)
if args.use_hpu_graphs:
from habana_frameworks.torch.hpu import wrap_in_hpu_graph
model = wrap_in_hpu_graph(model)
return model
# patching LinearAllreduce to use ScopedLinearAllReduce
def patch_scoped_linear_all_reduce(model):
from deepspeed.module_inject.layers import LinearAllreduce
from optimum.habana.transformers.models.modeling_all_models import ScopedLinearAllReduce
for name, module in model.named_children():
if type(module) is LinearAllreduce:
SL = ScopedLinearAllReduce(mod=module)
setattr(model, name, SL)
patch_scoped_linear_all_reduce(module)
def setup_distributed_model(args, model_dtype, model_kwargs, logger):
import deepspeed
logger.info("DeepSpeed is enabled.")
deepspeed.init_distributed(dist_backend="hccl")
config = AutoConfig.from_pretrained(args.model_name_or_path, torch_dtype=model_dtype, **model_kwargs)
load_to_meta = model_on_meta(config)
if load_to_meta:
# Construct model with fake meta tensors, later will be replaced on devices during ds-inference ckpt load
with deepspeed.OnDevice(dtype=model_dtype, device="meta"):
model = AutoModelForCausalLM.from_config(config, torch_dtype=model_dtype)
# Model loaded to meta is managed differently
checkpoints_json = tempfile.NamedTemporaryFile(suffix=".json", mode="+w")
write_checkpoints_json(args.model_name_or_path, args.local_rank, checkpoints_json, token=args.token)
else:
# TODO: revisit placement on CPU when auto-injection is possible
with deepspeed.OnDevice(dtype=model_dtype, device="cpu"):
model = AutoModelForCausalLM.from_pretrained(
args.model_name_or_path, torch_dtype=model_dtype, **model_kwargs
)
model.eval()
# Initialize the model
ds_inference_kwargs = {"dtype": model_dtype}
ds_inference_kwargs["tensor_parallel"] = {"tp_size": args.world_size}
ds_inference_kwargs["enable_cuda_graph"] = args.use_hpu_graphs
ds_inference_kwargs["injection_policy"] = get_ds_injection_policy(config)
if load_to_meta:
ds_inference_kwargs["checkpoint"] = checkpoints_json.name
model = deepspeed.init_inference(model, **ds_inference_kwargs)
model = model.module
if model.config.model_type == "llama":
patch_scoped_linear_all_reduce(model)
if args.quant_config:
import habana_quantization_toolkit
habana_quantization_toolkit.prep_model(model)
return model
def setup_tokenizer(args, model):
tokenizer_kwargs = {
"revision": args.model_revision,
"token": args.token,
}
if args.bad_words is not None or args.force_words is not None:
tokenizer_kwargs["add_prefix_space"] = True
tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, **tokenizer_kwargs)
if not model.config.is_encoder_decoder:
tokenizer.padding_side = "left"
# Some models like GPT2 do not have a PAD token so we have to set it if necessary
if model.config.model_type == "llama":
# unwind broken decapoda-research config
model.generation_config.pad_token_id = 0
model.generation_config.bos_token_id = 1
model.generation_config.eos_token_id = 2
tokenizer.bos_token_id = model.generation_config.bos_token_id
tokenizer.eos_token_id = model.generation_config.eos_token_id
tokenizer.pad_token_id = model.generation_config.pad_token_id
tokenizer.pad_token = tokenizer.decode(tokenizer.pad_token_id)
tokenizer.eos_token = tokenizer.decode(tokenizer.eos_token_id)
tokenizer.bos_token = tokenizer.decode(tokenizer.bos_token_id)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model.generation_config.pad_token_id = model.generation_config.eos_token_id
return tokenizer, model
def setup_generation_config(args, model, tokenizer):
bad_words_ids = None
force_words_ids = None
if args.bad_words is not None:
bad_words_ids = [tokenizer.encode(bad_word, add_special_tokens=False) for bad_word in args.bad_words]
if args.force_words is not None:
force_words_ids = [tokenizer.encode(force_word, add_special_tokens=False) for force_word in args.force_words]
is_optimized = model_is_optimized(model.config)
# Generation configuration
generation_config = copy.deepcopy(model.generation_config)
generation_config.max_new_tokens = args.max_new_tokens
generation_config.use_cache = args.use_kv_cache
generation_config.static_shapes = is_optimized
generation_config.bucket_size = args.bucket_size if is_optimized else -1
generation_config.do_sample = args.do_sample
generation_config.num_beams = args.num_beams
generation_config.bad_words_ids = bad_words_ids
generation_config.force_words_ids = force_words_ids
generation_config.num_return_sequences = args.num_return_sequences
generation_config.trim_logits = args.trim_logits
generation_config.attn_softmax_bf16 = args.attn_softmax_bf16
generation_config.limit_hpu_graphs = args.limit_hpu_graphs
generation_config.reuse_cache = args.reuse_cache
return generation_config
def initialize_model(args, logger):
init_start = time.perf_counter()
setup_distributed(args)
override_prints(args.global_rank == 0 or args.verbose_workers, logger)
setup_env(args)
setup_device(args)
set_seed(args.seed)
get_repo_root(args.model_name_or_path, local_rank=args.local_rank, token=args.token)
use_deepspeed = args.world_size > 0
if use_deepspeed or args.bf16 or args.fp8:
model_dtype = torch.bfloat16
else:
model_dtype = torch.float
args.attn_softmax_bf16 = False
model_kwargs = {
"revision": args.model_revision,
"token": args.token,
}
model = (
setup_model(args, model_dtype, model_kwargs, logger)
if not use_deepspeed
else setup_distributed_model(args, model_dtype, model_kwargs, logger)
)
tokenizer, model = setup_tokenizer(args, model)
generation_config = setup_generation_config(args, model, tokenizer)
if args.fp8:
model = setup_quantization(args, model)
init_end = time.perf_counter()
logger.info(f"Args: {args}")
logger.info(f"device: {args.device}, n_hpu: {args.world_size}, bf16: {model_dtype == torch.bfloat16}")
logger.info(f"Model initialization took {(init_end - init_start):.3f}s")
return model, tokenizer, generation_config
def setup_parser(parser):
# Arguments management
parser.add_argument("--device", "-d", type=str, choices=["hpu"], help="Device to run", default="hpu")
parser.add_argument(
"--model_name_or_path",
default=None,
type=str,
required=True,
help="Path to pre-trained model (on the HF Hub or locally).",
)
parser.add_argument(
"--bf16",
action="store_true",
help="Whether to perform generation in bf16 precision.",
)
parser.add_argument("--max_new_tokens", type=int, default=1024, help="Number of tokens to generate.")
parser.add_argument(
"--max_input_tokens",
type=int,
default=1024,
help="If > 0 then pad and truncate the input sequences to this specified length of tokens. \
if == 0, then truncate to 16 (original default) \
if < 0, then do not truncate, use full input prompt",
)
parser.add_argument("--batch_size", type=int, default=128, help="Input batch size.")
parser.add_argument("--warmup", type=int, default=3, help="Number of warmup iterations for benchmarking.")
parser.add_argument("--n_iterations", type=int, default=0, help="Number of inference iterations for benchmarking. 0 = whole dataset")
parser.add_argument("--local_rank", type=int, default=0, metavar="N", help="Local process rank.")
parser.add_argument(
"--use_kv_cache",
action="store_true",
help="Whether to use the key/value cache for decoding. It should speed up generation.",
)
parser.add_argument(
"--use_hpu_graphs",
action="store_true",
help="Whether to use HPU graphs or not. Using HPU graphs should give better latencies.",
)
parser.add_argument(
"--dataset",
default="/mnt/weka/data/mlperf_inference/llama2/processed-data.pkl",
type=str,
help="Path to the dataset.",
)
parser.add_argument(
"--do_sample",
action="store_true",
help="Whether to use sampling for generation.",
)
parser.add_argument(
"--num_beams",
default=1,
type=int,
help="Number of beams used for beam search generation. 1 means greedy search will be performed.",
)
parser.add_argument(
"--trim_logits",
action="store_true",
help="Calculate logits only for the last token to save memory in the first step.",
)
parser.add_argument(
"--seed",
default=27,
type=int,
help="Seed to use for random generation. Useful to reproduce your runs with `--do_sample`.",
)
parser.add_argument(
"--profiling_warmup_steps",
default=0,
type=int,
help="Number of steps to ignore for profiling.",
)
parser.add_argument(
"--profiling_steps",
default=0,
type=int,
help="Number of steps to capture for profiling.",
)
parser.add_argument(
"--profiling_scope",
default="batch",
type=str,
help="Profile target: batch or token",
)
parser.add_argument(
"--prompt",
default=None,
type=str,
nargs="*",
help='Optional argument to give a prompt of your choice as input. Can be a single string (eg: --prompt "Hello world"), or a list of space-separated strings (eg: --prompt "Hello world" "How are you?")',
)
parser.add_argument(
"--bad_words",
default=None,
type=str,
nargs="+",
help="Optional argument list of words that are not allowed to be generated.",
)
parser.add_argument(
"--force_words",
default=None,
type=str,
nargs="+",
help="Optional argument list of words that must be generated.",
)
parser.add_argument("--num_return_sequences", type=int, default=1)
parser.add_argument(
"--token",
default=None,
type=str,
help="The token to use as HTTP bearer authorization for remote files. If not specified, will use the token "
"generated when running `huggingface-cli login` (stored in `~/.huggingface`).",
)
parser.add_argument(
"--model_revision",
default="main",
type=str,
help="The specific model version to use (can be a branch name, tag name or commit id).",
)
parser.add_argument(
"--attn_softmax_bf16",
action="store_true",
help="Whether to run attention softmax layer in lower precision provided that the model supports it and "
"is also running in lower precision.",
)
parser.add_argument(
"--output_dir",
default="./results",
type=str,
help="Output directory to store results in.",
)
parser.add_argument(
"--output_file",
default="out.pkl",
type=str,
help="Output file to store results in.",
)
parser.add_argument("--log_path", default="build/logs")
parser.add_argument(
"--bucket_size",
default=-1,
type=int,
help="Bucket size to maintain static shapes. If this number is negative (default is -1) \
then we use `shape = prompt_length + max_new_tokens`. If a positive number is passed \
we increase the bucket in steps of `bucket_size` instead of allocating to max (`prompt_length + max_new_tokens`).",
)
parser.add_argument(
"--limit_hpu_graphs",
action="store_true",
help="Skip HPU Graph usage for first token to save memory",
)
parser.add_argument(
"--reuse_cache",
action="store_true",
help="Whether to reuse key/value cache for decoding. It should save memory.",
)
parser.add_argument(
"--skip_hash_with_views",
action="store_true",
help="Whether to skip hash with views for HPU graphs. When skip_hash_with_views is not used, the input to HPU graphs includes both view and base tensors.",
)
parser.add_argument("--verbose_workers", action="store_true", help="Enable output from non-master workers")
parser.add_argument("--fp8", action="store_true", help="Enable Quantization to fp8")
parser.add_argument("--accuracy", action="store_true")
parser.add_argument("--scenario", default="Offline")
args = parser.parse_args()
if not args.use_hpu_graphs:
args.limit_hpu_graphs = False
args.reuse_cache = False
args.quant_config = os.getenv("QUANT_CONFIG", "")
return args
def print_stats(durations, args, n_iterations):
if int(os.getenv("LOCAL_RANK", "0")) == 0:
from optimum.habana.utils import get_hpu_memory_stats
import pprint
result = get_hpu_memory_stats()
result["throughput (TPS)"] = (n_iterations * args.batch_size * args.max_new_tokens)/sum(durations)
result["generation time"] = f"[{min(durations):.4f}-{max(durations):.4f}] avg: {(sum(durations)/len(durations)):.4f}"
pprint.pprint(result)