-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels.py
More file actions
117 lines (94 loc) · 3.07 KB
/
Copy pathmodels.py
File metadata and controls
117 lines (94 loc) · 3.07 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
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 abba import ABBAConfig, get_abba_model
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_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
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_abba(model, args):
abba_config = ABBAConfig(
r1=args.lora_r,
r2=args.lora_r,
alpha1=args.lora_alpha,
alpha2=args.lora_alpha,
dropout=args.lora_dropout,
target_modules=["q_proj", "o_proj", "k_proj", "v_proj", "gate_proj", "up_proj", "down_proj"],
)
model = get_abba_model(model, abba_config)
return model, abba_config
def create_peft_model_cr_abba(model, args):
abba_config = ABBAConfig(
r1=args.lora_r,
r2=args.lora_r,
alpha1=args.lora_alpha,
alpha2=args.lora_alpha,
dropout=args.lora_dropout,
target_modules=["q_proj", "o_proj", "k_proj", "v_proj", "gate_proj", "up_proj", "down_proj"],
)
model = get_abba_model(model, abba_config)
return model, abba_config