-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_util.py
59 lines (45 loc) · 2.04 KB
/
model_util.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
from accelerate import init_empty_weights, infer_auto_device_map, load_checkpoint_and_dispatch
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
import torch
def load_tokenizer_model(checkpoint, pad_trunc_right=True):
if checkpoint == 'opt175b':
return None, None
if 'opt' in checkpoint:
model = load_opt_model(checkpoint)
else:
model = AutoModelForCausalLM.from_pretrained(checkpoint)
if torch.cuda.is_available():
model.parallelize()
if pad_trunc_right:
print(checkpoint)
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
else:
tokenizer = AutoTokenizer.from_pretrained(checkpoint, padding_side='left', truncation_side='left')
tokenizer.pad_token = tokenizer.eos_token # original pad token id is None, not in embedding matrix
model.config.pad_token_id = tokenizer.eos_token_id
return tokenizer, model
def load_opt_model(checkpoint):
config = AutoConfig.from_pretrained(checkpoint)
# Initializes an empty shell with the model. This is instant and does not take any RAM.
with init_empty_weights():
model = AutoModelForCausalLM.from_config(config)
# Initialize the model under the previous context manager breaks the tied weights.
model.tie_weights()
# Infer device map automatically
device_map = infer_auto_device_map(model.model, no_split_module_classes=["OPTDecoderLayer"], dtype='float16')
print(device_map)
load_checkpoint_and_dispatch(
model.model,
checkpoint,
device_map=device_map,
offload_folder=None,
dtype='float16',
offload_state_dict=True
)
model.tie_weights()
return model
if __name__ == '__main__':
tokenizer, model = load_tokenizer_model('/home/sysuser/opt/30b')
inputs = tokenizer("Hugging Face is pushing the convention that a unicorn with two horns becomes a llama.", return_tensors="pt")
output = model.generate(inputs["input_ids"].to(0), max_length=50, do_sample=True)
print(tokenizer.decode(output[0].tolist()))