-
Notifications
You must be signed in to change notification settings - Fork 1
/
llama_seq_clf.py
159 lines (134 loc) · 4.9 KB
/
llama_seq_clf.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
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
# -*- coding: utf-8 -*-
import sys
import json
from typing import List, Any, Dict
from datasets import load_dataset, Dataset, DatasetDict
from transformers import AutoTokenizer
from transformers.data import *
from transformers import TrainingArguments, Trainer
from peft import get_peft_model, LoraConfig, TaskType
import torch
torch.manual_seed(3407)
import random
random.seed(3407)
import numpy as np
np.random.seed(3407)
import evaluate
from modeling_llama import LlamaForSequenceClassification
def load_subtaskA_mono():
ret = {}
for split_name in ['train', 'dev', 'test']:
data = []
with open(f'./data/SubtaskA/subtaskA_{split_name}_monolingual.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
return DatasetDict(ret)
def load_subtaskA_mul():
ret = {}
for split_name in ['train', 'dev', 'test']:
data = []
with open(f'./data/SubtaskA/subtaskA_{split_name}_multilingual.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
return DatasetDict(ret)
def load_subtaskB():
ret = {}
for split_name in ['train', 'dev', 'test']:
data = []
with open(f'./data/SubtaskB/subtaskB_{split_name}.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
return DatasetDict(ret)
if len(sys.argv) != 3:
print('usage python %.py dataset model_size')
sys.exit()
dataset, model_size = sys.argv[1], sys.argv[2]
epochs = 10
batch_size = 16
learning_rate = 5e-5
lora_r = 12
max_length = 128
if model_size.lower() == '7b':
model_id = 'NousResearch/Llama-2-7b-hf'
elif model_size.lower() == '13b':
model_id = 'NousResearch/Llama-2-13b-hf'
test_name = 'test'
text_name = None
##############################################################################################################################################################################
if dataset == 'subtaskA_mono':
id2label = {0: "human", 1: "machine"}
label2id = {v: k for k, v in id2label.items()}
ds = load_subtaskA_mono()
dev_name = 'dev'
text_name = 'text'
elif dataset == 'subtaskA_mul':
id2label = {0: "human-written", 1: "machine-generated"}
label2id = {v: k for k, v in id2label.items()}
ds = load_subtaskA_mul()
dev_name = 'dev'
text_name = 'text'
elif dataset == 'subtaskB':
id2label = {0: "human", 1: "chatGPT", 2: "cohere", 3: 'davinci', 4: 'bloomz', 5: 'dolly'}
label2id = {v: k for k, v in id2label.items()}
ds = load_subtaskB()
dev_name = 'dev'
text_name = 'text'
else:
raise NotImplementedError
##############################################################################################################################################################################
accuracy = evaluate.load("accuracy")
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = LlamaForSequenceClassification.from_pretrained(
model_id, num_labels=len(label2id), id2label=id2label, label2id=label2id
).bfloat16()
peft_config = LoraConfig(task_type=TaskType.SEQ_CLS, inference_mode=False, r=lora_r, lora_alpha=32, lora_dropout=0.1)
model = get_peft_model(model, peft_config)
model.print_trainable_parameters()
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
return accuracy.compute(predictions=predictions, references=labels)
def preprocess_function(examples):
global text_name
if isinstance(text_name, str):
d = examples[text_name]
else:
d = examples[text_name[0]]
for n in text_name[1:]:
nd = examples[n]
assert len(d) == len(nd)
for i, t in enumerate(nd):
d[i] += '\n' + t
return tokenizer(d, padding='longest', max_length=max_length, truncation=True)
tokenized_ds = ds.map(preprocess_function, batched=True)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
training_args = TrainingArguments(
output_dir="clf",
learning_rate=learning_rate,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
num_train_epochs=epochs,
weight_decay=0.01,
evaluation_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True,
push_to_hub=False,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_ds["train"],
eval_dataset=tokenized_ds[dev_name],
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics,
)
trainer.train()
# Predictions on the test set
predictions = trainer.predict(tokenized_ds[test_name])
# Save the predictions to a file
with open(f"{dataset}_{model_size}_test_predictions.json", "w") as f:
json.dump(predictions.predictions.tolist(), f)