-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
71 lines (48 loc) · 2.42 KB
/
main.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
import os
import argparse
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from src.args_train import parse_train
from src.args_peft import parse_peft
DIR = os.path.dirname(os.path.realpath(__file__))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--mode", default='train', type=str, required=True, choices=['post_train', 'train', 'infer'])
parser.add_argument("--seed", default=42, type=int, required=False)
parser.add_argument("--model", default=None, type=str, required=True)
parser.add_argument("--post_train_path", default=None, type=str, required=False)
parser.add_argument("--train_path", default=None, type=str, required=False)
parser.add_argument("--data", default=None, type=str, required=True, choices=['multi_train', 'all_text', 'train', 'test'])
parser.add_argument("--peft", default='lora', required=True, choices=['lora', 'none', 'ia3'])
parser.add_argument("--bnb", default=True, required=True)
parser.add_argument("--train", default=None, required=True)
#########################
parser.add_argument("--local_rank", default=None, required=False)
parser.add_argument("--ds_config", default=None, required=False)
args = parser.parse_args()
parse_train(args, os.path.join(DIR, 'args', 'train'))
parse_peft(args, os.path.join(DIR, 'args', 'peft'))
# set seed
args.train['seed'] = args.seed
bnb_config=None
if args.bnb:
bnb_config = BitsAndBytesConfig(load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16
)
model = AutoModelForCausalLM.from_pretrained(args.model, quantization_config=bnb_config)
tokenizer = AutoTokenizer.from_pretrained(args.model)
if tokenizer.pad_token is None:
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
model.resize_token_embeddings(len(tokenizer))
if args.mode in ['train', 'post_train']:
from src.train import train
dataset = os.path.join(DIR, 'data', f"{args.data}.txt")
train(args, model, tokenizer, dataset)
elif args.mode == 'infer':
from src.infer import infer
dataset = os.path.join(DIR, 'data', 'test.txt')
infer(args, model, tokenizer, dataset)
else:
raise ValueError('Invalid mode')