-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
472 lines (418 loc) · 12.5 KB
/
Copy pathutils.py
File metadata and controls
472 lines (418 loc) · 12.5 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
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
471
472
"""
Machine Learning Utilities Module
Author: Qianxi Li
Date: June 1, 2024
Description:
This module provides utility functions for machine learning operations including
model loading, tokenization, JSON handling, memory management, and metric calculations.
It also includes argument parsing and custom encoders for numpy types.
"""
import argparse
import json
import logging
import os
import functools
import gc
from typing import Any, Dict, List, Tuple, Optional
import numpy as np
import torch
from accelerate import infer_auto_device_map
from peft import PeftModel
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
BitsAndBytesConfig,
BertTokenizer,
BertModel
)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class NpEncoder(json.JSONEncoder):
"""
Custom JSON encoder for handling NumPy data types.
This encoder converts NumPy types to their Python equivalents for JSON serialization.
"""
def default(self, obj: Any) -> Any:
"""Convert NumPy types to Python types."""
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
return super(NpEncoder, self).default(obj)
class ClearCache:
"""
Context manager for clearing GPU and system memory.
Usage:
with ClearCache():
# Your memory-intensive operations here
"""
def __enter__(self) -> None:
"""Clear memory on entering context."""
gc.collect()
torch.cuda.empty_cache()
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
"""Clear memory on exiting context and reset CUDA stats."""
gc.collect()
torch.cuda.empty_cache()
if torch.cuda.is_available():
torch.cuda.reset_peak_memory_stats()
torch.cuda.empty_cache()
torch.cuda.synchronize()
def log_method(func: Any) -> Any:
"""
Decorator to log the start and end of method execution.
Args:
func: Function to be decorated
Returns:
Wrapped function with logging
"""
@functools.wraps(func)
def wrapper_log_method(*args: Any, **kwargs: Any) -> Any:
logger.info(f'Starting method {func.__name__}')
result = func(*args, **kwargs)
logger.info(f'Ending method {func.__name__}')
return result
return wrapper_log_method
def parse_arguments() -> argparse.Namespace:
"""
Parse command line arguments for the application.
Returns:
Parsed command line arguments
"""
parser = argparse.ArgumentParser(description="Fine-tuning LLM")
# LaFFi related arguments
parser.add_argument(
"--feedback_dataset_path",
type=str,
help="Path for the feedback prediction dataset"
)
parser.add_argument(
"--base_dataset_path",
type=str,
default="/home/qianxi/scratch/laffi/datasets/natural_instruction_v1/train",
help="Path for the base dataset"
)
parser.add_argument(
"--experiment_root_path",
type=str,
default="/home/qianxi/scratch/laffi/code/results/",
help="Root directory for storing results"
)
parser.add_argument(
"--baseline_only",
type=int,
default=0,
help="Run baseline evaluation only"
)
parser.add_argument(
"--enable_prompt_optimization",
type=int,
default=1,
help="Enable prompt optimization"
)
parser.add_argument(
"--enable_initial_human_examples",
type=int,
default=1,
help="Enable human examples"
)
parser.add_argument(
"--enable_mismatch_initial_human_examples",
type=int,
default=0,
help="Enable mismatched initial human examples"
)
parser.add_argument(
"--model_path",
type=str,
default="/home/qianxi/scratch/laffi/models/7b",
help="Path to the model"
)
parser.add_argument(
"--per_task_data_rows",
type=int,
default=10,
help="Number of training data rows per task file"
)
parser.add_argument(
"--num_return_seq",
type=int,
default=5,
help="Number of responses for major voting"
)
parser.add_argument(
"--contamination",
type=float,
default=0.3,
help="Outlier detection strength"
)
parser.add_argument(
"--eval_inference_batch_size",
type=int,
default=4,
help="Evaluation batch size"
)
parser.add_argument(
"--clusters",
type=int,
default=2,
help="Number of cluster centers for this task"
)
parser.add_argument(
"--cur_iteration",
type=int,
default=0,
help="Current iteration number"
)
parser.add_argument(
"--pos_example_amount",
type=int,
default=2,
help="Number of positive examples"
)
parser.add_argument(
"--neg_example_amount",
type=int,
default=0,
help="Number of negative examples"
)
parser.add_argument(
"--current_examples_path",
type=str,
default=None,
help="Path to current examples"
)
parser.add_argument(
"--adapter_path",
type=str,
default=None,
help="Path to adapter"
)
# Natural Instruction arguments
parser.add_argument(
"--enable_natural_ins",
type=int,
default=1,
help="Enable natural instruction evaluation"
)
parser.add_argument(
"--na_ins_evalset_path",
type=str,
default="/home/qianxi/scratch/laffi/datasets/natural_instruction_v1/converted/natural_ins_eval_official_converted.json",
help="Path to evaluation dataset"
)
# BoolQ related arguments
parser.add_argument(
"--enable_boolq_eval",
type=int,
default=0,
help="Enable BoolQ evaluation"
)
parser.add_argument(
"--boolq_eval_path",
type=str,
default="/home/qianxi/scratch/laffi/datasets/boolq/eval_boolq.json",
help="BoolQ evaluation set path"
)
parser.add_argument(
"--boolq_eval_result_path",
type=str,
default=None,
help="BoolQ evaluation result path"
)
# Squad related arguments
parser.add_argument(
"--enable_squad_eval",
type=int,
default=0,
help="Enable SQUAD evaluation"
)
parser.add_argument(
"--transformed_squad_eval_set_path",
type=str,
default="/home/qianxi/scratch/laffi/datasets/squad2/processed_eval_dataset.json",
help="Transformed SQUAD evaluation set path"
)
parser.add_argument(
"--original_squad_eval_set_path",
type=str,
default="/home/qianxi/scratch/laffi/datasets/squad2/squad_official_eval.json",
help="Original SQUAD evaluation set path"
)
parser.add_argument(
"--squad_response_gen_file",
type=str,
default=None,
help="SQUAD response generation file"
)
parser.add_argument(
"--squad_eval_result_path",
type=str,
default=None,
help="SQUAD evaluation result path"
)
# GSM8K related arguments
parser.add_argument(
"--enable_gsm8k_eval",
type=int,
default=0,
help="Enable GSM8K evaluation"
)
parser.add_argument(
"--gsm8k_testset",
type=str,
default="/home/qianxi/scratch/laffi/datasets/GSM8K/grade_school_math/data/test.json",
help="GSM8K test set path"
)
return parser.parse_args()
def load_bert() -> Tuple[BertModel, BertTokenizer]:
"""
Load BERT model and tokenizer.
Returns:
Tuple of (BERT model, BERT tokenizer)
"""
bert = BertModel.from_pretrained(
'bert-base-uncased',
output_hidden_states=True
).to("cuda:0")
bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
return bert, bert_tokenizer
def read_json(json_path: str) -> Dict:
"""
Read and parse JSON file.
Args:
json_path: Path to JSON file
Returns:
Parsed JSON content
"""
with open(json_path) as obj:
data = json.loads(obj.read())
return data
def write_json(json_path: str, content: Any) -> None:
"""
Write content to JSON file using custom NumPy encoder.
Args:
json_path: Path to save JSON file
content: Content to save
"""
with open(json_path, 'w') as obj:
json.dump(content, obj, cls=NpEncoder)
def calculate_classification_metrics(
predictions: np.ndarray,
labels: np.ndarray
) -> Dict[str, float]:
"""
Calculate classification metrics for boolean question answering.
Args:
predictions: Predicted labels
labels: True labels
Returns:
Dictionary of metrics (precision, recall, F1, accuracy)
"""
return {
"boolq_precision": precision_score(labels, predictions),
"boolq_recall": recall_score(labels, predictions),
"boolq_f1_score": f1_score(labels, predictions),
"boolq_accuracy": accuracy_score(labels, predictions)
}
@log_method
def load_model_with_adapters(
current_iter: int,
adapter_folder: str,
base_model: str
) -> Any:
"""
Load model with multiple adapters and combine them.
Args:
current_iter: Current iteration number
adapter_folder: Path to adapter folder
base_model: Path to base model
Returns:
Model with combined adapters
"""
model = load_model(base_model, four_bit_quant=True)
if current_iter >= 1:
full_adapter_path = os.path.join(adapter_folder, "model1")
model = PeftModel.from_pretrained(model, full_adapter_path, adapter_name="model1")
final_name = "model1"
if current_iter > 1:
adapter_name_list = ["model1"]
weight_list = [1.0]
for i in range(2, current_iter + 1):
full_adapter_path = os.path.join(adapter_folder, f"model{i}")
model.load_adapter(full_adapter_path, adapter_name=f"model{i}")
adapter_name_list.append(f"model{i}")
weight_list.append(1.0)
final_name = "combined"
model.add_weighted_adapter(
adapter_name_list,
weight_list,
final_name,
combination_type="ties",
density=0.2
)
model = model.merge_and_unload()
return model
def load_model(
model_path: str,
four_bit_quant: bool
) -> AutoModelForCausalLM:
"""
Load pre-trained model with optional quantization.
Args:
model_path: Path to model
four_bit_quant: Whether to use 4-bit quantization
Returns:
Loaded model
"""
quant_config = None
if four_bit_quant:
compute_dtype = getattr(torch, "float16")
quant_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=compute_dtype,
bnb_4bit_use_double_quant=False
)
model = AutoModelForCausalLM.from_pretrained(
model_path,
quantization_config=quant_config,
low_cpu_mem_usage=True,
device_map="auto"
)
return model
def load_tokenizer(model_path: str) -> AutoTokenizer:
"""
Load tokenizer for the model.
Args:
model_path: Path to model
Returns:
Loaded tokenizer
"""
tokenizer = AutoTokenizer.from_pretrained(model_path)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "left"
return tokenizer
def split_into_batches(lst: List[Any], batch_size: int) -> List[List[Any]]:
"""
Split a list into batches of specified size.
Args:
lst: List to split
batch_size: Size of each batch
Returns:
List of batches
Raises:
ValueError: If batch_size is not positive
"""
if batch_size <= 0:
raise ValueError("Batch size must be a positive integer")
if not lst:
return []
return [lst[i:i + batch_size] for i in range(0, len(lst), batch_size)]