-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
171 lines (151 loc) · 5.69 KB
/
training.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
160
161
162
163
164
165
166
167
168
169
170
171
import json
import torch
from torch.utils.data import Dataset
from transformers import (
MarkupLMProcessor,
MarkupLMForQuestionAnswering,
TrainingArguments,
Trainer
)
from peft import LoraConfig, get_peft_model
import os
class AmazonProductTokenDataset(Dataset):
"""
Token-level extractive QA dataset.
For each JSON item, we extract the HTML, the query (question), and the first non-null answer.
We then compute token-level start/end positions from character offsets.
"""
def __init__(self, json_file, processor, max_length=512):
with open(json_file, 'r') as f:
self.data = json.load(f)
self.processor = processor
self.max_length = max_length
self.qa_items = [] # List of tuples: (html, question, answer)
for item in self.data:
html = item["input"]["html"]
question = item["input"]["text"].strip()
answer = self._find_answer(item["output"])
if answer is None:
answer = ""
self.qa_items.append((html, question, answer))
def _find_answer(self, output_obj):
# Traverse the output dictionary/list and return the first non-null "value"
if isinstance(output_obj, dict):
if "value" in output_obj and output_obj["value"]:
return output_obj["value"].strip()
for v in output_obj.values():
ans = self._find_answer(v)
if ans is not None:
return ans
elif isinstance(output_obj, list):
for v in output_obj:
ans = self._find_answer(v)
if ans is not None:
return ans
return None
def __len__(self):
return len(self.qa_items)
def __getitem__(self, idx):
html, question, answer = self.qa_items[idx]
# Lowercase for matching.
lower_html = html.lower()
lower_answer = answer.lower()
answer_start_char = lower_html.find(lower_answer)
if answer_start_char == -1:
answer_start_char, answer_end_char = 0, 0
else:
answer_end_char = answer_start_char + len(answer)
# Encoding with offset mapping (so we can map char positions to tokens)
encoding = self.processor(
html_strings=html,
questions=question,
max_length=self.max_length,
padding="max_length",
truncation=True,
return_offsets_mapping=True,
return_tensors="pt"
)
# Remove batch dimension
for key, value in encoding.items():
encoding[key] = value.squeeze(0)
offsets = encoding["offset_mapping"] # shape: [seq_length, 2]
start_index, end_index = None, None
for i, (start, end) in enumerate(offsets.tolist()):
if start_index is None and start <= answer_start_char < end:
start_index = i
if end_index is None and end > answer_end_char >= start:
end_index = i
break
if start_index is None or end_index is None:
start_index, end_index = 0, 0
encoding["start_positions"] = torch.tensor(start_index, dtype=torch.long)
encoding["end_positions"] = torch.tensor(end_index, dtype=torch.long)
# Remove offsets from inputs
del encoding["offset_mapping"]
return encoding
def main():
# Create output directory if it doesn't exist
output_dir = "./markuplm_amazon_qa_token_lora_final"
os.makedirs(output_dir, exist_ok=True)
print("Loading processor and base model...")
processor = MarkupLMProcessor.from_pretrained("microsoft/markuplm-base")
processor.parse_html = True
base_model = MarkupLMForQuestionAnswering.from_pretrained("microsoft/markuplm-base")
print("Configuring LoRA...")
lora_config = LoraConfig(
r=8,
lora_alpha=32,
lora_dropout=0.1,
target_modules=["query", "value"],
bias="none",
task_type="QUESTION_ANSWERING"
)
model = get_peft_model(base_model, lora_config)
model.print_trainable_parameters()
print("Preparing dataset...")
dataset = AmazonProductTokenDataset("dataset.json", processor, max_length=512)
total_items = len(dataset)
train_size = int(0.8 * total_items)
train_dataset = torch.utils.data.Subset(dataset, range(train_size))
val_dataset = torch.utils.data.Subset(dataset, range(train_size, total_items))
training_args = TrainingArguments(
output_dir="./checkpoints",
num_train_epochs=3,
per_device_train_batch_size=2,
per_device_eval_batch_size=2,
warmup_steps=500,
weight_decay=0.01,
logging_dir="./logs",
logging_steps=10,
evaluation_strategy="steps",
eval_steps=100,
save_steps=100,
save_total_limit=3,
seed=42,
dataloader_pin_memory=True,
gradient_accumulation_steps=1,
fp16=True,
optim="adamw_torch",
learning_rate=2e-5,
max_grad_norm=1.0,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
)
print("Starting training...")
trainer.train()
print("Training complete. Saving model...")
# Merge LoRA weights and save the complete model
merged_model = model.merge_and_unload()
merged_model.save_pretrained(output_dir)
# Save processor configuration
processor.save_pretrained(output_dir)
# Save additional model files
config = merged_model.config
config.save_pretrained(output_dir)
print(f"Model and processor saved to {output_dir}")
if __name__ == "__main__":
main()