-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuning.py
More file actions
125 lines (99 loc) · 3.28 KB
/
tuning.py
File metadata and controls
125 lines (99 loc) · 3.28 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
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import torch
import torch.nn as nn
import bitsandbytes as bnb
import transformers
from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM
import json
import pandas as pd
from datasets import load_dataset
access_token = os.getenv("HUGGINGFACE_TOKEN")
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
model = AutoModelForCausalLM.from_pretrained(
"mistralai/Mistral-7B-v0.3",
load_in_8bit=True,
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.3")
if tokenizer.pad_token is None:
tokenizer.add_special_tokens({"pad_token": "[PAD]"})
model.resize_token_embeddings(len(tokenizer))
for param in model.parameters():
param.requires_grad = False # freeze the model - train adapters later
if param.ndim == 1:
# cast the small parameters (e.g. layernorm) to fp32 for stability
param.data = param.data.to(torch.float32)
model.gradient_checkpointing_enable() # reduce number of stored activations
model.enable_input_require_grads()
class CastOutputToFloat(nn.Sequential):
def forward(self, x):
return super().forward(x).to(torch.float32)
model.lm_head = CastOutputToFloat(model.lm_head)
def print_trainable_parameters(model):
"""
Prints the number of trainable parameters in the model.
"""
trainable_params = 0
all_param = 0
for _, param in model.named_parameters():
all_param += param.numel()
if param.requires_grad:
trainable_params += param.numel()
print(
f"trainable params: {trainable_params} || all params: {all_param} || trainable%: {100 * trainable_params / all_param}"
)
from peft import LoraConfig, get_peft_model
config = LoraConfig(
r=16, # attention heads
lora_alpha=32, # alpha scaling
# target_modules=["q_proj", "v_proj"], #if you know the
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM", # set this for CLM or Seq2Seq
)
model = get_peft_model(model, config)
print_trainable_parameters(model)
# import transformers
# from datasets import load_dataset
# data = load_dataset("Abirate/english_quotes")
# json_parsed = []
# with open('./dev.jsonl', 'r') as json_file:
# json_list = list(json_file)
# # print(json_list)
# for json_str in json_list:
# result = json.loads(json_str)
# json_parsed.append(result)
# for ds_number in range(0,3):
data = load_dataset("csv", data_files={"train": [f"train/bird_train.csv"]})
data = data.map(
lambda samples: tokenizer(samples["train_example"]), batched=True
)
trainer = transformers.Trainer(
model=model,
train_dataset=data["train"],
args=transformers.TrainingArguments(
per_device_train_batch_size=2,
per_device_eval_batch_size=2,
gradient_accumulation_steps=2,
warmup_steps=100,
max_steps=100,
learning_rate=2e-4,
fp16=True,
logging_steps=1,
output_dir="outputs",
),
data_collator=transformers.DataCollatorForLanguageModeling(
tokenizer, mlm=False
),
)
model.config.use_cache = (
False # silence the warnings. Please re-enable for inference!
)
trainer.train()
model.push_to_hub(
"ruandocini/Mistral-7B-v0.3-sql",
use_auth_token=True,
commit_message="basic training",
private=True,
)