Upload .\scripts\train.py with huggingface_hub
Browse files- .//scripts//train.py +120 -0
.//scripts//train.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Main training script for BwengeAi."""
|
| 2 |
+
|
| 3 |
+
import logging
|
| 4 |
+
import sys
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import yaml
|
| 8 |
+
|
| 9 |
+
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
| 10 |
+
|
| 11 |
+
sys.path.insert(0, str(PROJECT_ROOT / "src"))
|
| 12 |
+
|
| 13 |
+
from models.bwenge_model import BwengeModel
|
| 14 |
+
from training.trainer import BwengeTrainer
|
| 15 |
+
from evaluation.metrics import BwengeEvaluator
|
| 16 |
+
|
| 17 |
+
logging.basicConfig(
|
| 18 |
+
level=logging.INFO,
|
| 19 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
| 20 |
+
)
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def load_config(config_path: str = None) -> dict:
|
| 25 |
+
"""Load configuration."""
|
| 26 |
+
if config_path is None:
|
| 27 |
+
config_path = str(PROJECT_ROOT / "configs/default.yaml")
|
| 28 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 29 |
+
return yaml.safe_load(f)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def main():
|
| 33 |
+
"""Run training pipeline."""
|
| 34 |
+
logger.info("=" * 60)
|
| 35 |
+
logger.info("BwengeAi Training Pipeline")
|
| 36 |
+
logger.info("=" * 60)
|
| 37 |
+
|
| 38 |
+
config = load_config()
|
| 39 |
+
|
| 40 |
+
logger.info("\n" + "=" * 40)
|
| 41 |
+
logger.info("Step 1: Loading model")
|
| 42 |
+
logger.info("=" * 40)
|
| 43 |
+
|
| 44 |
+
bwenge = BwengeModel(config)
|
| 45 |
+
|
| 46 |
+
training_type = config.get("training", {}).get("training_type", "full")
|
| 47 |
+
|
| 48 |
+
use_lora = training_type in ("lora", "qlora", "full")
|
| 49 |
+
use_quantization = training_type in ("qlora",)
|
| 50 |
+
|
| 51 |
+
logger.info(f"Training type: {training_type} (LoRA={use_lora}, quantization={use_quantization})")
|
| 52 |
+
|
| 53 |
+
model_name = config.get("model", {}).get("finetune", {}).get("base_models", [None])[0]
|
| 54 |
+
if not model_name:
|
| 55 |
+
model_name = config.get("model", {}).get("base_model", "meta-llama/Llama-3.2-1B")
|
| 56 |
+
|
| 57 |
+
model, tokenizer = bwenge.load_base_model(
|
| 58 |
+
model_name=model_name,
|
| 59 |
+
use_quantization=use_quantization,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
if use_lora:
|
| 63 |
+
logger.info("Setting up LoRA for efficient fine-tuning...")
|
| 64 |
+
model = bwenge.setup_lora(model)
|
| 65 |
+
|
| 66 |
+
logger.info("\n" + "=" * 40)
|
| 67 |
+
logger.info("Step 2: Preparing training data")
|
| 68 |
+
logger.info("=" * 40)
|
| 69 |
+
|
| 70 |
+
trainer = BwengeTrainer(config)
|
| 71 |
+
|
| 72 |
+
processed_dir = str(PROJECT_ROOT / config.get("data", {}).get("processed_dir", "data/processed"))
|
| 73 |
+
training_data_path = f"{processed_dir}/training_data.jsonl"
|
| 74 |
+
instruction_data_path = f"{processed_dir}/instruction_data.jsonl"
|
| 75 |
+
|
| 76 |
+
if Path(instruction_data_path).exists():
|
| 77 |
+
data_path = instruction_data_path
|
| 78 |
+
logger.info(f"Using instruction dataset: {data_path}")
|
| 79 |
+
elif Path(training_data_path).exists():
|
| 80 |
+
data_path = training_data_path
|
| 81 |
+
logger.info(f"Using training dataset: {data_path}")
|
| 82 |
+
else:
|
| 83 |
+
logger.error("No training data found! Run collect_all.py first.")
|
| 84 |
+
return
|
| 85 |
+
|
| 86 |
+
logger.info("\n" + "=" * 40)
|
| 87 |
+
logger.info("Step 3: Training")
|
| 88 |
+
logger.info("=" * 40)
|
| 89 |
+
|
| 90 |
+
trainer.train_from_config(
|
| 91 |
+
model=model,
|
| 92 |
+
tokenizer=tokenizer,
|
| 93 |
+
data_path=data_path,
|
| 94 |
+
lora=use_lora,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
logger.info("\n" + "=" * 40)
|
| 98 |
+
logger.info("Step 4: Evaluation")
|
| 99 |
+
logger.info("=" * 40)
|
| 100 |
+
|
| 101 |
+
evaluator = BwengeEvaluator(config)
|
| 102 |
+
|
| 103 |
+
if Path(instruction_data_path).exists():
|
| 104 |
+
eval_results = evaluator.evaluate_model(
|
| 105 |
+
model=model,
|
| 106 |
+
tokenizer=tokenizer,
|
| 107 |
+
eval_data_path=instruction_data_path,
|
| 108 |
+
output_dir=str(trainer.output_dir),
|
| 109 |
+
)
|
| 110 |
+
logger.info(f"Evaluation results: {eval_results}")
|
| 111 |
+
|
| 112 |
+
logger.info("\n" + "=" * 60)
|
| 113 |
+
logger.info("Training Pipeline Complete!")
|
| 114 |
+
logger.info("=" * 60)
|
| 115 |
+
|
| 116 |
+
logger.info(f"Model saved to: {trainer.output_dir / 'final'}")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
main()
|