-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
executable file
·162 lines (119 loc) · 3.95 KB
/
Copy pathmodels.py
File metadata and controls
executable file
·162 lines (119 loc) · 3.95 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
import torch
from torch.utils.data import DataLoader
from transformers import RobertaTokenizer, RobertaForSequenceClassification, AdamW
from transformers import (
AutoTokenizer,
LlamaTokenizer,
AutoModelForCausalLM,
TrainingArguments,
BitsAndBytesConfig,
AutoModelForSequenceClassification,
AutoModelForSeq2SeqLM,
)
from datasets import load_dataset
import numpy as np
from peft import (
get_peft_model,
AdaLoraModel,
AdaLoraConfig,
TaskType,
LoraConfig,
prepare_model_for_kbit_training,
)
from utils.data_utils import *
import argparse
from copy import deepcopy
from tqdm import tqdm
from peft.utils import _get_submodules
def create_model_tokenizer(num_labels, args):
if 'roberta' in args.model:
model = AutoModelForSequenceClassification.from_pretrained(args.model, num_labels=num_labels)
tokenizer = AutoTokenizer.from_pretrained(args.model)
model.to(args.device)
return model, tokenizer
def create_peft_model(model, args):
if 'roberta' in args.model:
peft_config = LoraConfig(
task_type=TaskType.SEQ_CLS,
r=args.lora_r,
lora_alpha=args.lora_alpha,
lora_dropout=args.lora_dropout,
target_modules=["query", "value", "attention.output.dense", "output.dense"],
)
elif 't5' in args.model:
peft_config = LoraConfig(
task_type=TaskType.SEQ_2_SEQ_LM,
r=args.lora_r,
lora_alpha=args.lora_alpha,
lora_dropout=args.lora_dropout,
target_modules=["q", "v", "k", "o", "wi", "wo"],
)
model = get_peft_model(model, peft_config)
model.to(args.device)
return model, peft_config
def create_model_tokenizer_it(args):
model = AutoModelForCausalLM.from_pretrained(
args.model,
device_map="auto",
torch_dtype = torch.bfloat16
)
tokenizer = AutoTokenizer.from_pretrained(
args.model,
use_fast=True,
model_max_length=args.max_seq_length,
padding="max_length",
)
tokenizer.pad_token_id = tokenizer.eos_token_id
#model.to(args.device)
return model, tokenizer
def create_model_tokenizer_cr(args):
model = AutoModelForCausalLM.from_pretrained(
args.model,
device_map="auto",
torch_dtype = torch.bfloat16)
if "llama" in args.model:
if "Llama-3" in args.model:
tokenizer = AutoTokenizer.from_pretrained(
args.model,
use_fast=True,
model_max_length=args.max_seq_length,
padding="max_length",
)
else:
tokenizer = LlamaTokenizer.from_pretrained(
args.model,
use_fast=True,
model_max_length=args.max_seq_length,
padding="max_length",
)
else:
tokenizer = AutoTokenizer.from_pretrained(
args.model,
use_fast=True,
model_max_length=args.max_seq_length,
padding="max_length",
)
tokenizer.pad_token_id = (0)
tokenizer.padding_side = "left"
return model, tokenizer
def create_peft_model_it(model, args):
peft_config = LoraConfig(
r=args.lora_r,
lora_alpha=args.lora_alpha,
target_modules=["q_proj", "o_proj", "k_proj", "v_proj", "gate_proj", "up_proj", "down_proj"],
lora_dropout=0,
task_type="CAUSAL_LM",
)
model = get_peft_model(model, peft_config)
return model, peft_config
def create_peft_model_cr(model, args):
peft_config = LoraConfig(
r=args.lora_r,
lora_alpha=args.lora_alpha,
target_modules=["q_proj", "o_proj", "k_proj", "v_proj", "gate_proj", "up_proj", "down_proj"],
lora_dropout=args.lora_dropout,
bias="none",
task_type="CAUSAL_LM",
)
model = get_peft_model(model, peft_config)
return model, peft_config