Spaces:
Sleeping
Sleeping
Delete utilities/modeling.py
Browse files- utilities/modeling.py +0 -90
utilities/modeling.py
DELETED
@@ -1,90 +0,0 @@
|
|
1 |
-
from unsloth import FastLanguageModel
|
2 |
-
import torch
|
3 |
-
from trl import SFTTrainer
|
4 |
-
from transformers import TrainingArguments
|
5 |
-
from unsloth import is_bfloat16_supported
|
6 |
-
|
7 |
-
|
8 |
-
def load_model(model_name, max_seq_length):
|
9 |
-
dtype = None
|
10 |
-
load_in_4bit = True
|
11 |
-
|
12 |
-
model, tokenizer = FastLanguageModel.from_pretrained(
|
13 |
-
model_name = model_name,
|
14 |
-
max_seq_length = max_seq_length,
|
15 |
-
dtype = dtype,
|
16 |
-
load_in_4bit = load_in_4bit,
|
17 |
-
# token = ""
|
18 |
-
)
|
19 |
-
return model, tokenizer
|
20 |
-
|
21 |
-
|
22 |
-
def get_peft(model, peft, max_seq_length, random_seed):
|
23 |
-
|
24 |
-
model = FastLanguageModel.get_peft_model(
|
25 |
-
model,
|
26 |
-
r = peft['r',]
|
27 |
-
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
|
28 |
-
"gate_proj", "up_proj", "down_proj",],
|
29 |
-
lora_alpha = peft['alpha'],
|
30 |
-
lora_dropout = peft['dropout'],
|
31 |
-
bias = peft['bias'],
|
32 |
-
use_gradient_checkpointing = "unsloth",
|
33 |
-
random_state = random_seed,
|
34 |
-
use_rslora = peft['rslora'], # We support rank stabilized LoRA
|
35 |
-
loftq_config = peft['loftq_config'], # And LoftQ
|
36 |
-
)
|
37 |
-
return model
|
38 |
-
|
39 |
-
|
40 |
-
def get_trainer(model, tokenizer, dataset, sft,
|
41 |
-
data_field, max_seq_length, random_seed,
|
42 |
-
num_epochs, max_steps):
|
43 |
-
|
44 |
-
trainer = SFTTrainer(
|
45 |
-
model = model,
|
46 |
-
tokenizer = tokenizer,
|
47 |
-
train_dataset = dataset,
|
48 |
-
dataset_text_field = data_field,
|
49 |
-
max_seq_length = max_seq_length,
|
50 |
-
dataset_num_proc = 2,
|
51 |
-
packing = False,
|
52 |
-
args = TrainingArguments(
|
53 |
-
per_device_train_batch_size = sft['per_device_train_batch_size'],
|
54 |
-
gradient_accumulation_steps = sft['gradient_accumulation_steps'],
|
55 |
-
warmup_steps = sft['warmup_steps'],
|
56 |
-
num_train_epochs = num_epochs,
|
57 |
-
max_steps = max_steps,
|
58 |
-
learning_rate = sft['learning_rate'],
|
59 |
-
fp16 = not is_bfloat16_supported(),
|
60 |
-
bf16 = is_bfloat16_supported(),
|
61 |
-
logging_steps = sft['logging_steps'],
|
62 |
-
optim = sft['optim'],
|
63 |
-
weight_decay = sft['weight_decay'],
|
64 |
-
lr_scheduler_type = sft['lr_scheduler_type'],
|
65 |
-
seed = random_seed,
|
66 |
-
output_dir = "outputs",
|
67 |
-
),
|
68 |
-
)
|
69 |
-
return trainer
|
70 |
-
|
71 |
-
|
72 |
-
def prepare_trainer(model_name, max_seq_length, random_seed,
|
73 |
-
num_epochs, max_steps,
|
74 |
-
peft, sft, dataset, data_field):
|
75 |
-
|
76 |
-
print("Loading Model")
|
77 |
-
model, tokenizer = load_model(model_name, max_seq_length)
|
78 |
-
|
79 |
-
print("Preparing for PEFT")
|
80 |
-
model = get_peft(model, peft, max_seq_length, random_seed)
|
81 |
-
|
82 |
-
print("Getting Trainer Model")
|
83 |
-
trainer = get_trainer(model, tokenizer, dataset, data_field, max_seq_length, random_seed,
|
84 |
-
num_epochs, max_steps)
|
85 |
-
|
86 |
-
return trainer
|
87 |
-
|
88 |
-
if __name__ == "__main__":
|
89 |
-
trainer = prepare_trainer()
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|