-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
263 lines (203 loc) · 7.65 KB
/
utils.py
File metadata and controls
263 lines (203 loc) · 7.65 KB
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
import datetime
import hashlib
import logging
import os
import json
from typing import Union
import torch
from accelerate.utils import set_seed
from sentence_transformers import SentenceTransformer
def ensure_dir(dir_path):
os.makedirs(dir_path, exist_ok=True)
def config_for_log(config: dict) -> dict:
config = config.copy()
config.pop('device', None)
config.pop('accelerator', None)
for k, v in config.items():
if isinstance(v, list):
config[k] = str(v)
return config
def get_total_steps(config, train_dataloader):
if config['steps'] is not None:
return config['steps']
else:
return len(train_dataloader) * config['epochs']
def init_logger(config):
LOGROOT = config['log_dir']
os.makedirs(LOGROOT, exist_ok=True)
dataset_name = os.path.join(LOGROOT, config["dataset"])
os.makedirs(dataset_name, exist_ok=True)
logfilename = get_file_name(config, suffix='.log')
logfilepath = os.path.join(LOGROOT, config["dataset"], logfilename)
filefmt = "%(asctime)-15s %(levelname)s %(message)s"
filedatefmt = "%a %d %b %Y %H:%M:%S"
fileformatter = logging.Formatter(filefmt, filedatefmt)
fh = logging.FileHandler(logfilepath)
fh.setLevel(logging.INFO)
fh.setFormatter(fileformatter)
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO, handlers=[sh, fh])
def get_file_name(config: dict, suffix: str = ''):
config_str = "".join([str(value) for key, value in config.items() if (key != 'accelerator' and key != 'device') ])
md5 = hashlib.md5(config_str.encode(encoding="utf-8")).hexdigest()[:6]
logfilename = "{}-{}{}".format(config['run_local_time'], md5, suffix)
return logfilename
def load_json(file_path):
with open(file_path, 'r') as f:
data = json.load(f)
return data
def init_seed(seed, reproducibility):
r"""init random seed for random functions in numpy, torch, cuda and cudnn
This function is taken from https://github.com/RUCAIBox/RecBole/blob/2b6e209372a1a666fe7207e6c2a96c7c3d49b427/recbole/utils/utils.py#L188-L205
Args:
seed (int): random seed
reproducibility (bool): Whether to require reproducibility
"""
import random
import numpy as np
import torch
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
set_seed(seed)
if reproducibility:
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
else:
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = False
def convert_config_dict(config: dict) -> dict:
"""
Convert the values in a dictionary to their appropriate types.
Args:
config (dict): The dictionary containing the configuration values.
Returns:
dict: The dictionary with the converted values.
"""
for key in config:
v = config[key]
if not isinstance(v, str):
continue
try:
new_v = eval(v)
if new_v is not None and not isinstance(
new_v, (str, int, float, bool, list, dict, tuple)
):
new_v = v
except (NameError, SyntaxError, TypeError):
if isinstance(v, str) and v.lower() in ['true', 'false']:
new_v = (v.lower() == 'true')
else:
new_v = v
config[key] = new_v
return config
def init_device():
"""
Set the visible devices for training. Supports multiple GPUs.
Returns:
torch.device: The device to use for training.
"""
import torch
use_ddp = True if os.environ.get("WORLD_SIZE") else False # Check if DDP is enabled
if torch.cuda.is_available():
return torch.device('cuda'), use_ddp
else:
return torch.device('cpu'), use_ddp
def get_local_time():
r"""Get current time
Returns:
str: current time
"""
cur = datetime.datetime.now()
cur = cur.strftime("%b-%d-%Y_%H-%M")
return cur
def parse_command_line_args(unparsed: list[str]) -> dict:
args = {}
for text_arg in unparsed:
if '=' not in text_arg:
# print(text_arg)
raise ValueError(f"Invalid command line argument: {text_arg}, please add '=' to separate key and value.")
key, value = text_arg.split('=')
key = key[len('--'):]
try:
value = eval(value)
except:
pass
args[key] = value
return args
def log(message, accelerator, logger, level='info'):
if accelerator.is_main_process:
if level == 'info':
logger.info(message)
elif level == 'error':
logger.error(message)
elif level == 'warning':
logger.warning(message)
elif level == 'debug':
logger.debug(message)
else:
raise ValueError(f'Invalid log level: {level}')
def encode_sent_emb(config, item2meta, id_mapping, output_path):
sent_emb_model = SentenceTransformer(
config['sent_emb_model']
).to(config['device'])
meta_sentences = [] # 1-base, meta_sentences[0] -> item_id = 1
for i in range(1, len(id_mapping['id2item'])):
meta_sentences.append(item2meta[id_mapping['id2item'][i]])
sent_embs = sent_emb_model.encode(
meta_sentences,
convert_to_numpy=True,
batch_size=config['sent_emb_batch_size'],
show_progress_bar=True,
device=config['device']
)
# PCA
if config['sent_emb_pca'] > 0:
print(f'[TOKENIZER] Applying PCA to sentence embeddings...')
from sklearn.decomposition import PCA
pca = PCA(n_components=config['sent_emb_pca'], whiten=True)
sent_embs = pca.fit_transform(sent_embs)
sent_embs.tofile(output_path)
return sent_embs
def list_to_str(l: Union[list, str], remove_blank=False) -> str:
ret = ''
if isinstance(l, list):
ret = ', '.join(map(str, l))
else:
ret = l
if remove_blank:
ret = ret.replace(' ', '')
return ret
def selective_log_softmax(logits, index):
"""
A memory-efficient implementation of the common `log_softmax -> gather` operation.
This function is equivalent to the following naive implementation:
```python
logps = torch.gather(logits.log_softmax(-1), dim=-1, index=index.unsqueeze(-1)).squeeze(-1)
```
Args:
logits (`torch.Tensor`):
Logits tensor of shape `(..., num_classes)`.
index (`torch.Tensor`):
Index tensor of shape `(...)`, specifying the positions to gather from the log-softmax output.
Returns:
`torch.Tensor`:
Gathered log probabilities with the same shape as `index`.
"""
if logits.dtype in [torch.float32, torch.float64]:
selected_logits = torch.gather(logits, dim=-1, index=index.unsqueeze(-1)).squeeze(-1)
# loop to reduce peak mem consumption
logsumexp_values = torch.stack([torch.logsumexp(lg, dim=-1) for lg in logits])
per_token_logps = selected_logits - logsumexp_values # log_softmax(x_i) = x_i - logsumexp(x)
else:
# logsumexp approach is unstable with bfloat16, fall back to slightly less efficent approach
per_token_logps = []
for row_logits, row_labels in zip(logits, index): # loop to reduce peak mem consumption
row_logps = F.log_softmax(row_logits, dim=-1)
row_per_token_logps = row_logps.gather(dim=-1, index=row_labels.unsqueeze(-1)).squeeze(-1)
per_token_logps.append(row_per_token_logps)
per_token_logps = torch.stack(per_token_logps)
return per_token_logps