Add training script
Browse files
train.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Train a US Architectural Floor Plan LLM using SFT with LoRA.
|
| 3 |
+
|
| 4 |
+
Base model: Qwen/Qwen2.5-3B-Instruct
|
| 5 |
+
Dataset: Nithins03/us-architectural-floorplan-sft
|
| 6 |
+
Method: SFT with LoRA (rank=128, all-linear) following "LoRA Without Regret" recipe
|
| 7 |
+
Output: Nithins03/us-architectural-floorplan-llm
|
| 8 |
+
|
| 9 |
+
Reference implementations:
|
| 10 |
+
- TRL SFT docs: https://huggingface.co/docs/trl/sft_trainer
|
| 11 |
+
- LoRA Without Regret: https://huggingface.co/docs/trl/lora_without_regret
|
| 12 |
+
- OptiScene (arxiv:2506.07570): LoRA r=16, alpha=32, lr=5e-6, 10 epochs
|
| 13 |
+
- DStruct2Design (arxiv:2407.15723): LLaMA3-8B + 8-bit + LoRA
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
+
import torch
|
| 18 |
+
from datasets import load_dataset
|
| 19 |
+
from peft import LoraConfig
|
| 20 |
+
from trl import SFTTrainer, SFTConfig
|
| 21 |
+
import trackio
|
| 22 |
+
|
| 23 |
+
# ============================================================================
|
| 24 |
+
# Configuration
|
| 25 |
+
# ============================================================================
|
| 26 |
+
|
| 27 |
+
MODEL_NAME = "Qwen/Qwen2.5-3B-Instruct"
|
| 28 |
+
DATASET_NAME = "Nithins03/us-architectural-floorplan-sft"
|
| 29 |
+
OUTPUT_DIR = "./floorplan-llm-output"
|
| 30 |
+
HUB_MODEL_ID = "Nithins03/us-architectural-floorplan-llm"
|
| 31 |
+
|
| 32 |
+
peft_config = LoraConfig(
|
| 33 |
+
r=128,
|
| 34 |
+
lora_alpha=32,
|
| 35 |
+
lora_dropout=0.05,
|
| 36 |
+
bias="none",
|
| 37 |
+
task_type="CAUSAL_LM",
|
| 38 |
+
target_modules="all-linear",
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
training_args = SFTConfig(
|
| 42 |
+
output_dir=OUTPUT_DIR,
|
| 43 |
+
num_train_epochs=5,
|
| 44 |
+
learning_rate=1e-4,
|
| 45 |
+
lr_scheduler_type="cosine",
|
| 46 |
+
warmup_ratio=0.05,
|
| 47 |
+
weight_decay=0.01,
|
| 48 |
+
max_grad_norm=1.0,
|
| 49 |
+
per_device_train_batch_size=2,
|
| 50 |
+
gradient_accumulation_steps=4,
|
| 51 |
+
max_length=4096,
|
| 52 |
+
gradient_checkpointing=True,
|
| 53 |
+
bf16=True,
|
| 54 |
+
eval_strategy="steps",
|
| 55 |
+
eval_steps=500,
|
| 56 |
+
per_device_eval_batch_size=2,
|
| 57 |
+
logging_strategy="steps",
|
| 58 |
+
logging_steps=25,
|
| 59 |
+
logging_first_step=True,
|
| 60 |
+
disable_tqdm=True,
|
| 61 |
+
report_to=["trackio"],
|
| 62 |
+
save_strategy="steps",
|
| 63 |
+
save_steps=500,
|
| 64 |
+
save_total_limit=3,
|
| 65 |
+
load_best_model_at_end=True,
|
| 66 |
+
metric_for_best_model="eval_loss",
|
| 67 |
+
push_to_hub=True,
|
| 68 |
+
hub_model_id=HUB_MODEL_ID,
|
| 69 |
+
hub_strategy="every_save",
|
| 70 |
+
packing=False,
|
| 71 |
+
assistant_only_loss=True,
|
| 72 |
+
seed=42,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
def main():
|
| 76 |
+
print("=" * 60)
|
| 77 |
+
print("US Architectural Floor Plan LLM Training")
|
| 78 |
+
print("=" * 60)
|
| 79 |
+
|
| 80 |
+
trackio.init(project="us-floorplan-llm", name="qwen2.5-3b-lora-sft")
|
| 81 |
+
|
| 82 |
+
dataset = load_dataset(DATASET_NAME)
|
| 83 |
+
print(f"Train: {len(dataset['train'])} | Test: {len(dataset['test'])}")
|
| 84 |
+
|
| 85 |
+
trainer = SFTTrainer(
|
| 86 |
+
model=MODEL_NAME,
|
| 87 |
+
args=training_args,
|
| 88 |
+
train_dataset=dataset["train"],
|
| 89 |
+
eval_dataset=dataset["test"],
|
| 90 |
+
peft_config=peft_config,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
model = trainer.model
|
| 94 |
+
trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 95 |
+
total = sum(p.numel() for p in model.parameters())
|
| 96 |
+
print(f"Trainable: {trainable:,} / {total:,} ({100*trainable/total:.2f}%)")
|
| 97 |
+
|
| 98 |
+
train_result = trainer.train()
|
| 99 |
+
|
| 100 |
+
metrics = train_result.metrics
|
| 101 |
+
print(f"Train loss: {metrics.get('train_loss', 'N/A')}")
|
| 102 |
+
|
| 103 |
+
eval_metrics = trainer.evaluate()
|
| 104 |
+
print(f"Eval loss: {eval_metrics.get('eval_loss', 'N/A')}")
|
| 105 |
+
|
| 106 |
+
trainer.save_model()
|
| 107 |
+
trainer.push_to_hub(commit_message="Final model after SFT training on US floor plans")
|
| 108 |
+
print(f"Model pushed to: https://huggingface.co/{HUB_MODEL_ID}")
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
main()
|