-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
305 lines (268 loc) · 11.2 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
import functools
import os
import subprocess
import time
from dataclasses import field, dataclass
from typing import Optional, List, Union, Any, Dict
import torch
from transformers import AutoTokenizer
from trl import DataCollatorForCompletionOnlyLM
from trl.core import flatten_dict
from peft import (
LoraConfig,
PeftConfig,
PromptEncoderConfig,
PromptTuningConfig,
PromptTuningInit,
PrefixTuningConfig
)
LORA_TARGET_MODULES = {
"Phi-3-mini-128k-instruct": ["o_proj", "qkv_proj"],
"deepseek-coder-6.7b-instruct": ["q_proj", "v_proj", "o_proj", "k_proj"],
"CodeQwen1.5-7B-Chat": ["q_proj", "k_proj", "v_proj", "o_proj", "up_proj", "down_proj", "gate_proj"],
"Meta-Llama-3.1-8B-Instruct": ["q_proj", "k_proj", "v_proj", "o_proj", "up_proj", "down_proj", "gate_proj"],
"Qwen2.5-Coder-7B-Instruct": ["q_proj", "k_proj", "v_proj", "o_proj", "up_proj", "down_proj", "gate_proj"],
"Qwen2.5-Coder-1.5B": ["q_proj", "k_proj", "v_proj", "o_proj", "up_proj", "down_proj", "gate_proj"],
"Qwen2.5-Coder-1.5B-Instruct": ["q_proj", "k_proj", "v_proj", "o_proj", "up_proj", "down_proj", "gate_proj"],
}
_MAGIC_SPLITTER_ = "-[[]]-this-is-really-our-highest-priority-[[]]-"
INSTRUCTION_PREFIX = {
"conala": (
"Provide a self-contained Python script that solves the following problem in a markdown code block. "
"Your solution should most likely contain a single line of code, or only a few ones."
),
"mbpp": (
"Provide a self-contained Python script that solves the following problem in a markdown code block. "
"You are given example test cases from which you can infer the function signature."
),
"apps": (
"Provide a self-contained Python script that solves the following problem in a markdown code block. "
"Make sure the solution obeys the constraints and passes the example test cases."
)
}
def encode_chat_template(chat_template, tokenizer):
prompt = tokenizer.apply_chat_template(chat_template, tokenize=False).split(_MAGIC_SPLITTER_)[0]
return tokenizer(prompt, return_attention_mask=True, return_tensors="pt")
def make_chat_template_prompt(instruction, response, instruction_prefix):
# https://github.com/evalplus/evalplus/blob/master/evalplus/provider/utility.py#L25
user_content = f"{instruction_prefix}\n```\n{instruction.strip()}\n```"
if response is None:
assistant_content = f"```python\n{_MAGIC_SPLITTER_}\n```"
else:
assistant_content = f"```python\n{response.strip()}\n```"
return [
{"role": "user", "content": user_content},
{"role": "assistant", "content": assistant_content}
]
class CustomDataCollatorForCompletionOnlyLM(DataCollatorForCompletionOnlyLM):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def torch_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]:
batch = super().torch_call(examples)
return batch
def get_gpu_memory_usage():
try:
visible_devices = os.environ.get("CUDA_VISIBLE_DEVICES", None)
if visible_devices is None:
# select all available GPUs
gpu_ids = range(torch.cuda.device_count())
else:
# select active GPUs for the current script
gpu_ids = list(map(int, visible_devices.split(',')))
result = subprocess.check_output(
['nvidia-smi', '--query-gpu=memory.used', '--format=csv,nounits,noheader'],
encoding='utf-8'
)
# sum GPU memory usage from active GPUs
gpu_memory = [int(x) for x in result.strip().split('\n')]
total_memory_used = sum(gpu_memory[gpu_id] for gpu_id in gpu_ids)
return total_memory_used
except Exception:
return -1
def track_gpu_usage(func):
@functools.wraps(func)
def wrapper_track_gpu_usage(*args, **kwargs):
initial_gpu_memory = get_gpu_memory_usage()
max_gpu_memory_usage = initial_gpu_memory
start_time = time.time()
result = []
try:
gen = func(*args, **kwargs)
for sample_index, output in enumerate(gen):
current_gpu_memory = get_gpu_memory_usage()
max_gpu_memory_usage = max(max_gpu_memory_usage, current_gpu_memory)
result.append(output)
except Exception as e:
print(f"Error during execution: {e}")
finally:
end_time = time.time()
return result, initial_gpu_memory, max_gpu_memory_usage, end_time - start_time
return wrapper_track_gpu_usage
@dataclass
class SFTScriptArguments:
dataset_name: str = field(
default="timdettmers/openassistant-guanaco",
metadata={"help": "the dataset name"},
)
dataset_train_split: str = field(default="train", metadata={"help": "The dataset split to train on"})
dataset_test_split: str = field(default="validation", metadata={"help": "The dataset split to evaluate on"})
config: str = field(default=None, metadata={"help": "Path to the optional config file"})
gradient_checkpointing_use_reentrant: bool = field(
default=False,
metadata={"help": "Whether to apply `use_reentrant` for gradient_checkpointing"},
)
completion_only: bool = field(
default=False,
metadata={"help": "Whether to only consider the assistant's response in the loss calculation"}
)
response_template: str = field(
default=None,
metadata={"help": "Response template when setting `completion_only`"}
)
@dataclass
class ModelConfig:
"""
Arguments which define the model and tokenizer to load.
"""
model_name_or_path: Optional[str] = field(
default=None,
metadata={"help": ("The model checkpoint for weights initialization.")},
)
model_revision: str = field(
default="main",
metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."},
)
torch_dtype: Optional[str] = field(
default=None,
metadata={
"help": (
"Override the default `torch.dtype` and load the model under this dtype. If `auto` is passed, the "
"dtype will be automatically derived from the model's weights."
),
"choices": ["auto", "bfloat16", "float16", "float32"],
},
)
trust_remote_code: bool = field(default=True, metadata={"help": "Trust remote code when loading a model."})
attn_implementation: Optional[str] = field(
default=None,
metadata={
"help": (
"Which attention implementation to use; you can run --attn_implementation=flash_attention_2, in which case you must install this manually by running `pip install flash-attn --no-build-isolation`"
)
},
)
use_peft: bool = field(
default=False,
metadata={"help": ("Whether to use PEFT or not for training.")},
)
# LoRA methods
use_lora: bool = field(
default=False,
metadata={"help": ("Whether to use LoRA.")},
)
lora_r: Optional[int] = field(
default=16,
metadata={"help": ("LoRA R value.")},
)
lora_alpha: Optional[int] = field(
default=32,
metadata={"help": ("LoRA alpha.")},
)
lora_dropout: Optional[float] = field(
default=0.05,
metadata={"help": ("LoRA dropout.")},
)
lora_target_modules: Optional[List[str]] = field(
default=None,
metadata={"help": ("LoRA target modules.")},
)
lora_modules_to_save: Optional[List[str]] = field(
default=None,
metadata={"help": ("Model layers to unfreeze & train")},
)
task_type: str = field(
default="CAUSAL_LM", metadata={"help": "The task_type to pass for LoRA (use SEQ_CLS for reward modeling)"}
)
# QLoRA
load_in_8bit: bool = field(
default=False, metadata={"help": "use 8 bit precision for the base model - works only with LoRA"}
)
load_in_4bit: bool = field(
default=False, metadata={"help": "use 4 bit precision for the base model - works only with LoRA"}
)
bnb_4bit_quant_type: Optional[str] = field(
default="nf4", metadata={"help": "precise the quantization type (fp4 or nf4)"}
)
use_bnb_nested_quant: bool = field(default=False, metadata={"help": "use nested quantization"})
# Prompt methods
use_p_tuning: bool = field(
default=False,
metadata={"help": ("Whether to use p-tuning.")},
)
use_prefix_tuning: bool = field(
default=False,
metadata={"help": ("Whether to use prefix tuning.")},
)
use_prompt_tuning: bool = field(
default=False,
metadata={"help": ("Whether to use prompt tuning.")},
)
num_virtual_tokens: int = field(
default=20,
metadata={"help": ("Number of virtual tokens for p-tuning or prefix tuning.")},
)
encoder_hidden_size: int = field(
default=128,
metadata={"help": ("Encoder hidden size for p-tuning.")},
)
active_gpu: int = field(
default=-1,
metadata={"help": ("The index of the active GPU used for training.")}
)
def to_dict(self):
output_dict = {}
for key, value in self.__dict__.items():
output_dict[key] = value
return flatten_dict(output_dict)
def __post_init__(self):
if self.load_in_8bit and self.load_in_4bit:
raise ValueError("You can't use 8 bit and 4 bit precision at the same time")
if isinstance(self.lora_target_modules, list) and len(self.lora_target_modules) == 1:
self.lora_target_modules = self.lora_target_modules[0]
def get_peft_config(model_config: ModelConfig, tokenizer: AutoTokenizer) -> "Optional[PeftConfig]":
if model_config.use_peft is False:
return None
model_name = model_config.model_name_or_path.split("/")[-1]
if model_config.use_lora:
peft_config = LoraConfig(
r=model_config.lora_r,
lora_alpha=model_config.lora_alpha,
lora_dropout=model_config.lora_dropout,
bias="none",
task_type=model_config.task_type,
target_modules=LORA_TARGET_MODULES[model_name],
modules_to_save=model_config.lora_modules_to_save,
)
elif model_config.use_p_tuning:
peft_config = PromptEncoderConfig(
task_type=model_config.task_type,
num_virtual_tokens=model_config.num_virtual_tokens,
encoder_hidden_size=model_config.encoder_hidden_size,
)
elif model_config.use_prompt_tuning:
prompt_tuning_init_text = "Generate a Python code that solves the given problem.\n"
peft_config = PromptTuningConfig(
task_type=model_config.task_type,
prompt_tuning_init=PromptTuningInit.TEXT,
num_virtual_tokens=len(tokenizer(prompt_tuning_init_text)["input_ids"]),
prompt_tuning_init_text=prompt_tuning_init_text,
tokenizer_name_or_path=model_config.model_name_or_path,
)
elif model_config.use_prefix_tuning:
peft_config = PrefixTuningConfig(
task_type=model_config.task_type,
num_virtual_tokens=model_config.num_virtual_tokens
)
else:
peft_config = None
return peft_config