-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt.py
182 lines (153 loc) · 6.27 KB
/
opt.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
import transformers
import torch
import os
import random
import pickle
from lm_eval.base import BaseLM
# from transformers.deepspeed import HfDeepSpeedConfig
# import deepspeed
class HFLM(BaseLM):
def __init__(
self,
device="cuda",
pretrained="facebook/opt-125m",
revision="main",
subfolder=None,
tokenizer=None,
batch_size=1,
model_cache_dir = None,
tokenizer_cache_dir = None,
mask_single_head=0,
mask_heads=0,
mask_fc=0,
mask_iterative_fc=0,
head_percent_mask=0,
head_importance_path=None,
fc_percent_mask=0,
fc_importance_path=None,
):
super().__init__()
assert isinstance(device, str)
assert isinstance(pretrained, str)
assert isinstance(batch_size, int)
if device:
if device not in ["cuda", "cpu"]:
device = int(device)
self._device = torch.device(device)
print(f"Using device '{device}'")
else:
print("Device not specified")
print(f"Cuda Available? {torch.cuda.is_available()}")
self._device = (
torch.device("cuda")
if torch.cuda.is_available()
else torch.device("cpu")
)
device_map = 'auto'
if '66b' in pretrained:
device_map = eval(open('66b_device_map.txt', 'r').readlines()[0])
for key in device_map:
if 'layers' in key:
layer_num = int(key.split('.')[-1])
if layer_num <= 3:
device_map[key] = 0
elif layer_num >= 60:
device_map[key] = 'cpu'
else:
device_map[key] = ((layer_num - 4) // 8) + 1
self.opt = transformers.AutoModelForCausalLM.from_pretrained(
pretrained,
device_map = device_map,
cache_dir = model_cache_dir,
torch_dtype=torch.float16
)
self.opt.get_decoder().embed_tokens.weight.requires_grad = False
self.opt.get_decoder().embed_positions.weight.requires_grad = False
# self.opt = transformers.AutoModelForCausalLM.from_pretrained(
# pretrained,
# cache_dir = model_cache_dir,
# )
# self.ds_engine = deepspeed.initialize(model=self.opt, model_parameters = self.opt.parameters(), config_params=ds_config)[0]
# self.ds_engine.module.eval() # inference
self.tokenizer = transformers.AutoTokenizer.from_pretrained(
pretrained,
cache_dir = tokenizer_cache_dir if tokenizer_cache_dir else 'tokenizer_cache/',
use_fast = False
) if tokenizer is None else tokenizer
assert isinstance(
self.tokenizer,
(
transformers.GPT2Tokenizer,
transformers.GPT2TokenizerFast,
transformers.T5Tokenizer,
transformers.T5TokenizerFast,
),
), "this tokenizer has not been checked for compatibility yet!"
self.vocab_size = self.tokenizer.vocab_size
# multithreading and batching
self.batch_size_per_gpu = batch_size # todo: adaptive batch size
num_hidden_layers = self.opt.config.num_hidden_layers
num_heads = self.opt.config.num_attention_heads
self.head_mask = torch.ones(num_hidden_layers * num_heads, dtype = torch.half)
self.fc_mask = torch.ones(num_hidden_layers, dtype = torch.half)
if int(mask_heads):
with open(head_importance_path, 'rb') as f:
importance = pickle.load(f)
_, head_indices = torch.sort(importance.view(-1))
head_indices = list(head_indices.numpy())
head_indices = head_indices[: int(head_percent_mask) * len(head_indices) // 100]
self.head_mask[head_indices] = 0.
elif int(mask_single_head): #Only performing it on OPT125M
self.head_mask[int(mask_single_head)-1] = 0.
self.head_mask = self.head_mask.view(num_hidden_layers, num_heads).contiguous()
if mask_fc:
self.fc_mask[int(mask_fc)] = 0.
elif int(mask_iterative_fc):
with open(fc_importance_path, 'rb') as f:
fc_indices = list(pickle.load(f))
fc_indices = fc_indices[: int(fc_percent_mask) * len(fc_indices) // 100]
self.fc_mask[fc_indices] = 0.
@property
def eot_token_id(self):
return self.tokenizer.eos_token_id
@property
def max_length(self):
return self.opt.config.max_position_embeddings
@property
def max_gen_toks(self):
return 256
@property
def batch_size(self):
# TODO: fix multi-gpu
return self.batch_size_per_gpu # * gpus
@property
def device(self):
# TODO: fix multi-gpu
return self._device
def get_tokenizer(self):
return self.tokenizer
def tok_encode(self, string: str):
return self.tokenizer.encode(string, add_special_tokens=False)
def tok_decode(self, tokens):
return self.tokenizer.decode(tokens)
def _model_call(self, inps, attn_mask = None, labels = None):
"""
inps: a torch tensor of shape [batch, sequence]
the size of sequence may vary from call to call
returns: a torch tensor of shape [batch, sequence, vocab] with the
logits returned from the model
"""
if labels == None:
with torch.no_grad():
return self.opt(input_ids = inps, head_mask = self.head_mask, fc_mask = self.fc_mask)[0][:, :, :50265]
# rank = int(os.getenv("LOCAL_RANK", "0"))
# return self.ds_engine.module(input_ids = inps, head_mask = self.head_mask.to(rank))[0][:, :, :50265]
else:
return self.opt(input_ids = inps, attention_mask = attn_mask, labels = labels).loss
# return self.ds_engine.module(input_ids = inps, attention_mask = attn_mask, labels = labels).loss
def _model_generate(self, context, max_length, eos_token_id):
return self.opt.generate(
context, max_length=max_length, eos_token_id=eos_token_id, do_sample=False
)
# for backwards compatibility
OPTLM = HFLM