File size: 9,106 Bytes
619e569 | 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | import os
from pathlib import Path
import torch
import pandas as pd
from datasets import load_dataset, Dataset, load_from_disk
from sentence_transformers import (
SentenceTransformer,
SentenceTransformerTrainer,
SentenceTransformerTrainingArguments,
)
from sentence_transformers.losses import CachedMultipleNegativesRankingLoss
from sentence_transformers.training_args import BatchSamplers
from sentence_transformers.util import mine_hard_negatives
# =========================
# CONFIG
# =========================
DATASET_NAME = "phamson02/large-vi-legal-queries"
# Stage 1 model dir
BASE_MODEL_DIR = "./embeddinggemma-300m-vilegal"
BASE_MODEL_CHECKPOINT_DIR = "./embeddinggemma-300m-vilegal"
# Stage 2 output dir
STAGE2_OUTPUT_DIR = "./embeddinggemma-300m-vilegal-stage2-hardneg"
# Cache dir
CACHE_ROOT = Path("./cache_vilegal_stage2")
CACHE_ROOT.mkdir(parents=True, exist_ok=True)
# Cached artifacts
CLEAN_DF_PATH = CACHE_ROOT / "clean_df.parquet"
PAIR_DATASET_PATH = CACHE_ROOT / "pair_dataset"
HARD_NEGATIVE_DATA_DIR = CACHE_ROOT / "hardneg_dataset"
TASK_NAME = "Retrieval"
# Data control
LIMIT_ROWS = None # set e.g. 20000 for quick tests
# Cache toggles
USE_CACHE_CLEAN_DF = True
USE_CACHE_PAIR_DATASET = True
USE_CACHE_HARDNEG = True
FORCE_REBUILD_CLEAN_DF = False
FORCE_REBUILD_PAIR_DATASET = False
FORCE_REBUILD_HARDNEG = False
# Training control
RESUME_IF_POSSIBLE = True
# =========================
# HELPERS
# =========================
def clean_text(x):
if x is None:
return ""
x = str(x).strip()
x = " ".join(x.split())
return x
def build_positive_document(row):
return f"text: {row['context']}"
def path_exists_and_nonempty(path: Path) -> bool:
return path.exists() and any(path.iterdir()) if path.is_dir() else path.exists()
def get_last_checkpoint(output_dir: str):
output_path = Path(output_dir)
if not output_path.exists():
return None
checkpoints = []
for p in output_path.iterdir():
if p.is_dir() and p.name.startswith("checkpoint-"):
try:
step = int(p.name.split("-")[-1])
checkpoints.append((step, p))
except ValueError:
continue
if not checkpoints:
return None
checkpoints.sort(key=lambda x: x[0])
return str(checkpoints[-1][1])
# =========================
# DATA PREP
# =========================
def load_and_prepare_dataframe(limit_rows=None):
if (
USE_CACHE_CLEAN_DF
and not FORCE_REBUILD_CLEAN_DF
and CLEAN_DF_PATH.exists()
):
print(f"✅ Loading cached clean dataframe from: {CLEAN_DF_PATH}")
df = pd.read_parquet(CLEAN_DF_PATH)
print("Cached clean rows:", len(df))
return df
print("📥 Loading raw dataset from hub...")
ds = load_dataset(DATASET_NAME, split="train")
if limit_rows is not None:
ds = ds.select(range(min(limit_rows, len(ds))))
df = ds.to_pandas()
print("Raw shape:", df.shape)
for col in ["domain", "title", "header", "aspect", "context", "query"]:
if col not in df.columns:
df[col] = ""
df[col] = df[col].apply(clean_text)
df = df[(df["query"] != "") & (df["context"] != "")]
df = df.drop_duplicates(subset=["query", "context"]).reset_index(drop=True)
print("Cleaned rows:", len(df))
if USE_CACHE_CLEAN_DF:
print(f"💾 Saving clean dataframe cache to: {CLEAN_DF_PATH}")
df.to_parquet(CLEAN_DF_PATH, index=False)
return df
def build_pair_dataset(df):
if (
USE_CACHE_PAIR_DATASET
and not FORCE_REBUILD_PAIR_DATASET
and path_exists_and_nonempty(PAIR_DATASET_PATH)
):
print(f"✅ Loading cached pair dataset from: {PAIR_DATASET_PATH}")
dataset = load_from_disk(str(PAIR_DATASET_PATH))
print("Cached pair dataset:", dataset)
return dataset
print("🛠 Building pair dataset...")
pair_df = pd.DataFrame(
{
"query": df["query"].tolist(),
"positive": df.apply(build_positive_document, axis=1).tolist(),
}
)
dataset = Dataset.from_pandas(pair_df, preserve_index=False)
if USE_CACHE_PAIR_DATASET:
print(f"💾 Saving pair dataset cache to: {PAIR_DATASET_PATH}")
dataset.save_to_disk(str(PAIR_DATASET_PATH))
return dataset
# =========================
# HARD NEGATIVE MINING
# =========================
def mine_hard_negative_dataset(pair_dataset, model_dir):
if (
USE_CACHE_HARDNEG
and not FORCE_REBUILD_HARDNEG
and path_exists_and_nonempty(HARD_NEGATIVE_DATA_DIR)
):
print(f"✅ Loading cached hard negative dataset from: {HARD_NEGATIVE_DATA_DIR}")
hn_dataset = load_from_disk(str(HARD_NEGATIVE_DATA_DIR))
print("Cached hard negative dataset:", hn_dataset)
return hn_dataset
print("⛏ Mining hard negatives...")
miner_model = SentenceTransformer(model_dir)
miner_model.max_seq_length = 512
hn_dataset = mine_hard_negatives(
dataset=pair_dataset,
model=miner_model,
positive_column_name="positive",
range_min=10,
range_max=50,
relative_margin=0.05,
num_negatives=3,
sampling_strategy="random",
batch_size=128,
use_faiss=True,
query_prompt_name="query",
corpus_prompt_name="document",
output_format="n-tuple",
use_multi_process=True,
)
if USE_CACHE_HARDNEG:
print(f"💾 Saving hard negative dataset cache to: {HARD_NEGATIVE_DATA_DIR}")
hn_dataset.save_to_disk(str(HARD_NEGATIVE_DATA_DIR))
return hn_dataset
def preview_hard_negatives(hn_dataset, sample_size=10):
if len(hn_dataset) == 0:
print("No hard negatives to preview.")
return
sample_df = hn_dataset.to_pandas().sample(
min(sample_size, len(hn_dataset)),
random_state=42,
)
for _, row in sample_df.iterrows():
print("=" * 100)
print("QUERY:\n", row["query"])
print("\nPOSITIVE:\n", row["positive"][:700])
for i in range(1, 10):
neg_col = f"negative_{i}"
if neg_col in row and isinstance(row[neg_col], str):
print(f"\n{neg_col.upper()}:\n", row[neg_col][:700])
print()
# =========================
# TRAINING
# =========================
def train_stage2_with_hardneg(hn_dataset, model_checkpoint_dir, output_dir):
# IMPORTANT: không dùng .to("cuda") khi chạy torchrun
train_model = SentenceTransformer(model_checkpoint_dir)
train_model.max_seq_length = 512
loss = CachedMultipleNegativesRankingLoss(
train_model,
mini_batch_size=32,
gather_across_devices=True,
)
training_args = SentenceTransformerTrainingArguments(
prompts=train_model.prompts[TASK_NAME],
torch_compile=False,
output_dir=output_dir,
num_train_epochs=1,
per_device_train_batch_size=1024,
gradient_accumulation_steps=1,
learning_rate=1e-5,
warmup_ratio=0.1,
bf16=torch.cuda.is_available(),
logging_steps=50,
save_strategy="epoch",
report_to="none",
remove_unused_columns=False,
batch_sampler=BatchSamplers.NO_DUPLICATES,
dataloader_num_workers=8,
dataloader_persistent_workers=True,
dataloader_drop_last=True,
ddp_find_unused_parameters=False,
)
trainer = SentenceTransformerTrainer(
model=train_model,
args=training_args,
train_dataset=hn_dataset,
loss=loss,
)
resume_checkpoint = None
if RESUME_IF_POSSIBLE:
resume_checkpoint = get_last_checkpoint(output_dir)
if resume_checkpoint is not None:
print(f"🔁 Resuming from checkpoint: {resume_checkpoint}")
else:
print("ℹ️ No checkpoint found. Training from scratch.")
trainer.train(resume_from_checkpoint=resume_checkpoint)
print(f"💾 Saving final model to: {output_dir}")
trainer.save_model(output_dir)
# =========================
# MAIN
# =========================
def main():
# 1) Load + clean
df = load_and_prepare_dataframe(limit_rows=LIMIT_ROWS)
# 2) Build query-positive dataset
pair_dataset = build_pair_dataset(df)
print("Pair dataset:", pair_dataset)
if len(pair_dataset) > 0:
print("Sample pair:", pair_dataset[0])
# 3) Mine hard negatives từ stage 1 model
hn_dataset = mine_hard_negative_dataset(
pair_dataset=pair_dataset,
model_dir=BASE_MODEL_DIR,
)
print("Hard negative dataset:", hn_dataset)
if len(hn_dataset) > 0:
print("Sample n-tuple:", hn_dataset[0])
# 4) Preview vài sample hard negative
preview_hard_negatives(hn_dataset, sample_size=10)
# 5) Train stage 2 từ checkpoint stage 1
train_stage2_with_hardneg(
hn_dataset=hn_dataset,
model_checkpoint_dir=BASE_MODEL_CHECKPOINT_DIR,
output_dir=STAGE2_OUTPUT_DIR,
)
if __name__ == "__main__":
main()
|