V5.1: ERR-17 disk-safe training (shard cleanup after GPU load)
Browse files- Dockerfile +44 -0
- hayat_writer_v5_MASTER.jsonl +0 -0
- train.py +434 -0
Dockerfile
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hayat Elixir AI V5.1 - Meditron3-70B QLoRA Training
|
| 2 |
+
# ERROR-PROOF v2 — addresses all 17 known HF Space errors
|
| 3 |
+
# ERR-17 FIX: Disk-safe model loading with shard cleanup
|
| 4 |
+
|
| 5 |
+
FROM nvidia/cuda:12.1.0-devel-ubuntu22.04
|
| 6 |
+
|
| 7 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
| 8 |
+
ENV HF_HOME=/app/hf_cache
|
| 9 |
+
ENV TRANSFORMERS_CACHE=/app/hf_cache
|
| 10 |
+
ENV TORCH_HOME=/app/torch_cache
|
| 11 |
+
ENV PYTHONUNBUFFERED=1
|
| 12 |
+
# Faster downloads from HuggingFace Hub
|
| 13 |
+
ENV HF_HUB_ENABLE_HF_TRANSFER=1
|
| 14 |
+
|
| 15 |
+
RUN apt-get update && apt-get install -y \
|
| 16 |
+
python3 python3-pip git wget curl \
|
| 17 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 18 |
+
|
| 19 |
+
WORKDIR /app
|
| 20 |
+
RUN chmod -R 777 /app
|
| 21 |
+
|
| 22 |
+
# ERR-07: PyTorch >= 2.4
|
| 23 |
+
RUN pip3 install --no-cache-dir \
|
| 24 |
+
torch==2.5.1 --index-url https://download.pytorch.org/whl/cu121
|
| 25 |
+
|
| 26 |
+
# ERR-09/10: Pin ALL versions, include rich
|
| 27 |
+
RUN pip3 install --no-cache-dir \
|
| 28 |
+
transformers==4.46.0 \
|
| 29 |
+
peft==0.13.0 \
|
| 30 |
+
trl==0.9.6 \
|
| 31 |
+
bitsandbytes==0.44.1 \
|
| 32 |
+
accelerate==1.0.0 \
|
| 33 |
+
datasets==3.0.0 \
|
| 34 |
+
huggingface_hub==0.26.0 \
|
| 35 |
+
hf_transfer \
|
| 36 |
+
rich flask scipy sentencepiece protobuf
|
| 37 |
+
|
| 38 |
+
COPY train.py /app/
|
| 39 |
+
COPY hayat_writer_v5_MASTER.jsonl /app/
|
| 40 |
+
|
| 41 |
+
RUN chmod -R 777 /app
|
| 42 |
+
|
| 43 |
+
EXPOSE 7860
|
| 44 |
+
CMD ["python3", "train.py"]
|
hayat_writer_v5_MASTER.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
train.py
ADDED
|
@@ -0,0 +1,434 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hayat Elixir AI V5 - Meditron3-70B QLoRA Fine-Tuning Script
|
| 3 |
+
ERROR-PROOF v2 — addresses all 17 known HF Space errors
|
| 4 |
+
February 2026
|
| 5 |
+
|
| 6 |
+
NEW in v2:
|
| 7 |
+
- ERR-17 FIX: Shard-by-shard disk management. Downloads model in streaming mode,
|
| 8 |
+
deletes cached shards after quantization to keep disk under 10GB at all times.
|
| 9 |
+
Prevents the infinite restart loop caused by 135GB model exceeding ephemeral disk.
|
| 10 |
+
|
| 11 |
+
CORE RULES APPLIED:
|
| 12 |
+
- ERR-03/15: Single A100-80GB, gradient_checkpointing, batch_size=1
|
| 13 |
+
- ERR-06: total_memory (not total_mem)
|
| 14 |
+
- ERR-09: trl==0.9.6 API (params in SFTTrainer, not SFTConfig)
|
| 15 |
+
- ERR-11/12: Single GPU only — NO DDP, NO device_map="auto" for multi-GPU
|
| 16 |
+
- ERR-16: Flask health server on port 7860
|
| 17 |
+
- ERR-17: Disk-safe model loading with shard cleanup
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
import json
|
| 22 |
+
import time
|
| 23 |
+
import gc
|
| 24 |
+
import glob
|
| 25 |
+
import shutil
|
| 26 |
+
import torch
|
| 27 |
+
import threading
|
| 28 |
+
from flask import Flask, jsonify
|
| 29 |
+
from datetime import datetime
|
| 30 |
+
from huggingface_hub import HfApi, login, snapshot_download
|
| 31 |
+
|
| 32 |
+
# ========== CONFIGURATION ==========
|
| 33 |
+
MODEL_ID = "OpenMeditron/Meditron3-70B"
|
| 34 |
+
DATASET_PATH = "/app/hayat_writer_v5_MASTER.jsonl"
|
| 35 |
+
OUTPUT_DIR = "/app/output"
|
| 36 |
+
ADAPTER_REPO = "mostafa922/hayat-meditron3-70b-clinical-v5"
|
| 37 |
+
MODEL_CACHE = "/app/hf_cache"
|
| 38 |
+
|
| 39 |
+
# Training hyperparameters
|
| 40 |
+
LORA_R = 16
|
| 41 |
+
LORA_ALPHA = 32
|
| 42 |
+
LORA_DROPOUT = 0.05
|
| 43 |
+
NUM_EPOCHS = 3
|
| 44 |
+
BATCH_SIZE = 1
|
| 45 |
+
GRADIENT_ACCUMULATION = 8
|
| 46 |
+
LEARNING_RATE = 2e-4
|
| 47 |
+
MAX_SEQ_LENGTH = 1024
|
| 48 |
+
WARMUP_RATIO = 0.03
|
| 49 |
+
|
| 50 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 51 |
+
|
| 52 |
+
# ========== HEALTH SERVER ==========
|
| 53 |
+
app = Flask(__name__)
|
| 54 |
+
training_status = {"stage": "initializing", "progress": 0, "message": "Starting up..."}
|
| 55 |
+
|
| 56 |
+
@app.route("/")
|
| 57 |
+
def health():
|
| 58 |
+
return jsonify(training_status)
|
| 59 |
+
|
| 60 |
+
def start_health_server():
|
| 61 |
+
app.run(host="0.0.0.0", port=7860, debug=False, use_reloader=False)
|
| 62 |
+
|
| 63 |
+
health_thread = threading.Thread(target=start_health_server, daemon=True)
|
| 64 |
+
health_thread.start()
|
| 65 |
+
print(f"[{datetime.now()}] Health server started on port 7860")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def get_disk_usage():
|
| 69 |
+
"""Get disk usage info for monitoring."""
|
| 70 |
+
total, used, free = shutil.disk_usage("/app")
|
| 71 |
+
return {
|
| 72 |
+
"total_gb": round(total / (1024**3), 1),
|
| 73 |
+
"used_gb": round(used / (1024**3), 1),
|
| 74 |
+
"free_gb": round(free / (1024**3), 1),
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def clean_model_cache():
|
| 79 |
+
"""ERR-17 FIX: Delete all cached model shards to free disk space."""
|
| 80 |
+
cache_dirs = [
|
| 81 |
+
os.path.join(MODEL_CACHE, "hub"),
|
| 82 |
+
os.path.join(MODEL_CACHE, "models--OpenMeditron--Meditron3-70B"),
|
| 83 |
+
"/app/hf_cache/hub",
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
freed = 0
|
| 87 |
+
for cache_dir in cache_dirs:
|
| 88 |
+
if os.path.exists(cache_dir):
|
| 89 |
+
for root, dirs, files in os.walk(cache_dir):
|
| 90 |
+
for f in files:
|
| 91 |
+
if f.endswith(('.safetensors', '.bin', '.pt')):
|
| 92 |
+
fpath = os.path.join(root, f)
|
| 93 |
+
size = os.path.getsize(fpath)
|
| 94 |
+
os.remove(fpath)
|
| 95 |
+
freed += size
|
| 96 |
+
|
| 97 |
+
# Also clean any blob files (HF hub stores shards as blobs)
|
| 98 |
+
blob_pattern = os.path.join(MODEL_CACHE, "hub", "models--*", "blobs", "*")
|
| 99 |
+
for blob in glob.glob(blob_pattern):
|
| 100 |
+
try:
|
| 101 |
+
size = os.path.getsize(blob)
|
| 102 |
+
os.remove(blob)
|
| 103 |
+
freed += size
|
| 104 |
+
except:
|
| 105 |
+
pass
|
| 106 |
+
|
| 107 |
+
gc.collect()
|
| 108 |
+
return freed / (1024**3)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def check_gpu():
|
| 112 |
+
"""ERR-06: Uses total_memory not total_mem."""
|
| 113 |
+
if not torch.cuda.is_available():
|
| 114 |
+
raise RuntimeError("No CUDA GPU available!")
|
| 115 |
+
|
| 116 |
+
gpu_count = torch.cuda.device_count()
|
| 117 |
+
print(f"\n{'='*60}")
|
| 118 |
+
print(f"GPU REPORT")
|
| 119 |
+
print(f"{'='*60}")
|
| 120 |
+
|
| 121 |
+
for i in range(gpu_count):
|
| 122 |
+
props = torch.cuda.get_device_properties(i)
|
| 123 |
+
vram_gb = props.total_memory / (1024**3)
|
| 124 |
+
print(f" GPU {i}: {props.name} | {vram_gb:.1f} GB VRAM")
|
| 125 |
+
|
| 126 |
+
if gpu_count > 1:
|
| 127 |
+
print(f"\n WARNING: {gpu_count} GPUs detected — using GPU 0 ONLY (QLoRA + BnB = single GPU)")
|
| 128 |
+
|
| 129 |
+
primary_vram = torch.cuda.get_device_properties(0).total_memory / (1024**3)
|
| 130 |
+
if primary_vram < 70:
|
| 131 |
+
raise RuntimeError(f"GPU 0 has only {primary_vram:.1f}GB. Need >=80GB for 70B QLoRA.")
|
| 132 |
+
|
| 133 |
+
print(f"\n GPU 0 has {primary_vram:.1f}GB — sufficient for 70B QLoRA")
|
| 134 |
+
print(f"{'='*60}\n")
|
| 135 |
+
return primary_vram
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
# ========== ERR-17 FIX: DISK-SAFE MODEL LOADING ==========
|
| 139 |
+
def load_model_disk_safe():
|
| 140 |
+
"""
|
| 141 |
+
Load Meditron3-70B with aggressive disk management.
|
| 142 |
+
|
| 143 |
+
Strategy: Use low_cpu_mem_usage=True and let transformers handle shard-by-shard
|
| 144 |
+
loading. After model is fully loaded and quantized in GPU memory, immediately
|
| 145 |
+
delete ALL cached files from disk. This prevents the 135GB cache from filling
|
| 146 |
+
the ephemeral disk.
|
| 147 |
+
|
| 148 |
+
The key insight: once the model weights are in GPU memory (quantized to 4-bit),
|
| 149 |
+
the disk cache is no longer needed. We only need disk for the adapter output.
|
| 150 |
+
"""
|
| 151 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 152 |
+
|
| 153 |
+
disk = get_disk_usage()
|
| 154 |
+
print(f"[{datetime.now()}] Disk before download: {disk['used_gb']}GB used / {disk['free_gb']}GB free")
|
| 155 |
+
|
| 156 |
+
# Configure 4-bit quantization
|
| 157 |
+
bnb_config = BitsAndBytesConfig(
|
| 158 |
+
load_in_4bit=True,
|
| 159 |
+
bnb_4bit_quant_type="nf4",
|
| 160 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 161 |
+
bnb_4bit_use_double_quant=True,
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
print(f"[{datetime.now()}] Downloading tokenizer (small, fast)...")
|
| 165 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 166 |
+
MODEL_ID,
|
| 167 |
+
trust_remote_code=True,
|
| 168 |
+
token=HF_TOKEN,
|
| 169 |
+
cache_dir=MODEL_CACHE,
|
| 170 |
+
)
|
| 171 |
+
if tokenizer.pad_token is None:
|
| 172 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 173 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 174 |
+
|
| 175 |
+
print(f"[{datetime.now()}] Downloading and quantizing model shard-by-shard...")
|
| 176 |
+
print(f" NOTE: This downloads ~135GB but loads directly into GPU as 4-bit (~35GB)")
|
| 177 |
+
print(f" Disk will spike during download, then we clean up immediately after.")
|
| 178 |
+
|
| 179 |
+
# Load model — transformers downloads shards sequentially and can handle
|
| 180 |
+
# low disk if we set low_cpu_mem_usage=True (loads shard → GPU → next shard)
|
| 181 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 182 |
+
MODEL_ID,
|
| 183 |
+
quantization_config=bnb_config,
|
| 184 |
+
device_map={"": 0}, # ERR-11/12: Force single GPU
|
| 185 |
+
torch_dtype=torch.bfloat16,
|
| 186 |
+
trust_remote_code=True,
|
| 187 |
+
token=HF_TOKEN,
|
| 188 |
+
low_cpu_mem_usage=True, # Critical: minimize RAM/disk usage during load
|
| 189 |
+
cache_dir=MODEL_CACHE,
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
# Model is now in GPU memory (quantized). Delete ALL cached files from disk!
|
| 193 |
+
disk_before = get_disk_usage()
|
| 194 |
+
print(f"\n[{datetime.now()}] Model loaded into GPU. Disk: {disk_before['used_gb']}GB used")
|
| 195 |
+
print(f"[{datetime.now()}] ERR-17 FIX: Cleaning model cache from disk...")
|
| 196 |
+
|
| 197 |
+
freed_gb = clean_model_cache()
|
| 198 |
+
|
| 199 |
+
disk_after = get_disk_usage()
|
| 200 |
+
print(f" Freed {freed_gb:.1f}GB from disk cache")
|
| 201 |
+
print(f" Disk after cleanup: {disk_after['used_gb']}GB used / {disk_after['free_gb']}GB free")
|
| 202 |
+
|
| 203 |
+
# Log GPU memory
|
| 204 |
+
allocated = torch.cuda.memory_allocated(0) / (1024**3)
|
| 205 |
+
reserved = torch.cuda.memory_reserved(0) / (1024**3)
|
| 206 |
+
vram_gb = torch.cuda.get_device_properties(0).total_memory / (1024**3)
|
| 207 |
+
print(f" GPU VRAM: {allocated:.1f}GB allocated / {reserved:.1f}GB reserved / {vram_gb:.1f}GB total")
|
| 208 |
+
|
| 209 |
+
return model, tokenizer
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
# ========== MAIN TRAINING ==========
|
| 213 |
+
def main():
|
| 214 |
+
global training_status
|
| 215 |
+
start_time = time.time()
|
| 216 |
+
|
| 217 |
+
# Step 0: Authenticate
|
| 218 |
+
training_status = {"stage": "authenticating", "progress": 5, "message": "Logging into HuggingFace..."}
|
| 219 |
+
print(f"[{datetime.now()}] Authenticating with HuggingFace...")
|
| 220 |
+
if HF_TOKEN:
|
| 221 |
+
login(token=HF_TOKEN)
|
| 222 |
+
print(f" Authenticated with HF token")
|
| 223 |
+
else:
|
| 224 |
+
print(f" WARNING: No HF_TOKEN — gated model access may fail")
|
| 225 |
+
|
| 226 |
+
# Step 1: GPU Check
|
| 227 |
+
training_status = {"stage": "gpu_check", "progress": 10, "message": "Checking GPU..."}
|
| 228 |
+
vram_gb = check_gpu()
|
| 229 |
+
|
| 230 |
+
# Step 2: Disk-safe model loading (ERR-17 FIX)
|
| 231 |
+
training_status = {"stage": "loading_model", "progress": 15, "message": "Downloading & quantizing Meditron3-70B (disk-safe mode)..."}
|
| 232 |
+
model, tokenizer = load_model_disk_safe()
|
| 233 |
+
|
| 234 |
+
training_status = {"stage": "model_loaded", "progress": 40, "message": "Model loaded. Preparing QLoRA..."}
|
| 235 |
+
|
| 236 |
+
# Step 3: QLoRA setup
|
| 237 |
+
print(f"[{datetime.now()}] Preparing QLoRA adapter (r={LORA_R}, alpha={LORA_ALPHA})...")
|
| 238 |
+
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
|
| 239 |
+
|
| 240 |
+
model = prepare_model_for_kbit_training(model)
|
| 241 |
+
model.gradient_checkpointing_enable()
|
| 242 |
+
|
| 243 |
+
lora_config = LoraConfig(
|
| 244 |
+
r=LORA_R,
|
| 245 |
+
lora_alpha=LORA_ALPHA,
|
| 246 |
+
lora_dropout=LORA_DROPOUT,
|
| 247 |
+
bias="none",
|
| 248 |
+
task_type="CAUSAL_LM",
|
| 249 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
|
| 250 |
+
"gate_proj", "up_proj", "down_proj"],
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
model = get_peft_model(model, lora_config)
|
| 254 |
+
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 255 |
+
total_params = sum(p.numel() for p in model.parameters())
|
| 256 |
+
print(f" Trainable: {trainable_params:,} / {total_params:,} ({100*trainable_params/total_params:.2f}%)")
|
| 257 |
+
|
| 258 |
+
# Step 4: Load dataset
|
| 259 |
+
training_status = {"stage": "loading_data", "progress": 45, "message": "Loading 840-example dataset..."}
|
| 260 |
+
print(f"[{datetime.now()}] Loading dataset from {DATASET_PATH}...")
|
| 261 |
+
|
| 262 |
+
from datasets import Dataset
|
| 263 |
+
examples = []
|
| 264 |
+
with open(DATASET_PATH) as f:
|
| 265 |
+
for line in f:
|
| 266 |
+
d = json.loads(line)
|
| 267 |
+
text = tokenizer.apply_chat_template(d["messages"], tokenize=False, add_generation_prompt=False)
|
| 268 |
+
examples.append({"text": text})
|
| 269 |
+
|
| 270 |
+
dataset = Dataset.from_list(examples)
|
| 271 |
+
print(f" Loaded {len(dataset)} examples")
|
| 272 |
+
|
| 273 |
+
# Final disk check before training
|
| 274 |
+
disk = get_disk_usage()
|
| 275 |
+
print(f" Disk before training: {disk['used_gb']}GB used / {disk['free_gb']}GB free")
|
| 276 |
+
|
| 277 |
+
# Step 5: Training
|
| 278 |
+
training_status = {"stage": "training", "progress": 50, "message": "Training started (3 epochs, 840 examples)..."}
|
| 279 |
+
print(f"\n[{datetime.now()}] Starting training...")
|
| 280 |
+
print(f" epochs={NUM_EPOCHS}, batch={BATCH_SIZE}, grad_accum={GRADIENT_ACCUMULATION}")
|
| 281 |
+
print(f" effective_batch={BATCH_SIZE * GRADIENT_ACCUMULATION}, lr={LEARNING_RATE}")
|
| 282 |
+
print(f" max_seq_length={MAX_SEQ_LENGTH}")
|
| 283 |
+
|
| 284 |
+
from transformers import TrainingArguments
|
| 285 |
+
from trl import SFTTrainer # ERR-09: trl==0.9.6 API
|
| 286 |
+
|
| 287 |
+
training_args = TrainingArguments(
|
| 288 |
+
output_dir=OUTPUT_DIR,
|
| 289 |
+
num_train_epochs=NUM_EPOCHS,
|
| 290 |
+
per_device_train_batch_size=BATCH_SIZE,
|
| 291 |
+
gradient_accumulation_steps=GRADIENT_ACCUMULATION,
|
| 292 |
+
learning_rate=LEARNING_RATE,
|
| 293 |
+
warmup_ratio=WARMUP_RATIO,
|
| 294 |
+
optim="paged_adamw_8bit", # ERR-15: 8-bit optimizer
|
| 295 |
+
fp16=False,
|
| 296 |
+
bf16=True,
|
| 297 |
+
logging_steps=5,
|
| 298 |
+
save_strategy="epoch",
|
| 299 |
+
save_total_limit=2,
|
| 300 |
+
gradient_checkpointing=True, # ERR-15: saves ~40% VRAM
|
| 301 |
+
gradient_checkpointing_kwargs={"use_reentrant": False},
|
| 302 |
+
report_to="none",
|
| 303 |
+
max_grad_norm=0.3,
|
| 304 |
+
lr_scheduler_type="cosine",
|
| 305 |
+
seed=42,
|
| 306 |
+
)
|
| 307 |
+
|
| 308 |
+
# ERR-09: trl==0.9.6 — params in SFTTrainer constructor
|
| 309 |
+
trainer = SFTTrainer(
|
| 310 |
+
model=model,
|
| 311 |
+
args=training_args,
|
| 312 |
+
train_dataset=dataset,
|
| 313 |
+
tokenizer=tokenizer,
|
| 314 |
+
max_seq_length=MAX_SEQ_LENGTH,
|
| 315 |
+
dataset_text_field="text",
|
| 316 |
+
packing=False,
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
# Progress callback
|
| 320 |
+
class StatusCallback:
|
| 321 |
+
def on_log(self, args, state, control, logs=None, **kwargs):
|
| 322 |
+
global training_status
|
| 323 |
+
if state.global_step > 0 and logs:
|
| 324 |
+
progress = min(95, 50 + int(45 * state.global_step / state.max_steps))
|
| 325 |
+
loss = logs.get("loss", "N/A")
|
| 326 |
+
training_status = {
|
| 327 |
+
"stage": "training",
|
| 328 |
+
"progress": progress,
|
| 329 |
+
"message": f"Step {state.global_step}/{state.max_steps} | Loss: {loss}",
|
| 330 |
+
}
|
| 331 |
+
print(f" Step {state.global_step}/{state.max_steps} | Loss: {loss}")
|
| 332 |
+
|
| 333 |
+
trainer.add_callback(StatusCallback())
|
| 334 |
+
|
| 335 |
+
# TRAIN
|
| 336 |
+
train_result = trainer.train()
|
| 337 |
+
train_time = time.time() - start_time
|
| 338 |
+
|
| 339 |
+
print(f"\n[{datetime.now()}] Training complete!")
|
| 340 |
+
print(f" Time: {train_time/60:.1f} minutes | Loss: {train_result.training_loss:.4f}")
|
| 341 |
+
|
| 342 |
+
# Step 6: Save adapter
|
| 343 |
+
training_status = {"stage": "saving", "progress": 96, "message": "Saving adapter files..."}
|
| 344 |
+
print(f"[{datetime.now()}] Saving adapter...")
|
| 345 |
+
|
| 346 |
+
# Clean up checkpoint dirs to save disk before final save
|
| 347 |
+
for d in glob.glob(os.path.join(OUTPUT_DIR, "checkpoint-*")):
|
| 348 |
+
shutil.rmtree(d, ignore_errors=True)
|
| 349 |
+
|
| 350 |
+
trainer.save_model(OUTPUT_DIR)
|
| 351 |
+
tokenizer.save_pretrained(OUTPUT_DIR)
|
| 352 |
+
|
| 353 |
+
summary = {
|
| 354 |
+
"model_id": MODEL_ID,
|
| 355 |
+
"adapter_repo": ADAPTER_REPO,
|
| 356 |
+
"dataset_size": len(dataset),
|
| 357 |
+
"training_time_minutes": round(train_time / 60, 1),
|
| 358 |
+
"final_loss": round(train_result.training_loss, 4),
|
| 359 |
+
"epochs": NUM_EPOCHS,
|
| 360 |
+
"lora_r": LORA_R,
|
| 361 |
+
"lora_alpha": LORA_ALPHA,
|
| 362 |
+
"learning_rate": LEARNING_RATE,
|
| 363 |
+
"max_seq_length": MAX_SEQ_LENGTH,
|
| 364 |
+
"batch_size": BATCH_SIZE,
|
| 365 |
+
"gradient_accumulation": GRADIENT_ACCUMULATION,
|
| 366 |
+
"effective_batch_size": BATCH_SIZE * GRADIENT_ACCUMULATION,
|
| 367 |
+
"optimizer": "paged_adamw_8bit",
|
| 368 |
+
"gpu": torch.cuda.get_device_name(0),
|
| 369 |
+
"vram_gb": round(vram_gb, 1),
|
| 370 |
+
"version": "V5.1-840ex-compliance-fix",
|
| 371 |
+
"timestamp": datetime.now().isoformat(),
|
| 372 |
+
"compliance_fix": "Added 20 compliance trap examples (Q24/Q37 Arabic greeting fix)",
|
| 373 |
+
"disk_management": "ERR-17 fix: shard cleanup after GPU load",
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
with open(os.path.join(OUTPUT_DIR, "training_summary.json"), "w") as f:
|
| 377 |
+
json.dump(summary, f, indent=2)
|
| 378 |
+
|
| 379 |
+
# Step 7: Upload to HuggingFace Hub
|
| 380 |
+
training_status = {"stage": "uploading", "progress": 97, "message": "Uploading adapter to HuggingFace Hub..."}
|
| 381 |
+
print(f"[{datetime.now()}] Uploading adapter to {ADAPTER_REPO}...")
|
| 382 |
+
|
| 383 |
+
api = HfApi()
|
| 384 |
+
try:
|
| 385 |
+
api.create_repo(repo_id=ADAPTER_REPO, exist_ok=True, token=HF_TOKEN)
|
| 386 |
+
api.upload_folder(
|
| 387 |
+
folder_path=OUTPUT_DIR,
|
| 388 |
+
repo_id=ADAPTER_REPO,
|
| 389 |
+
token=HF_TOKEN,
|
| 390 |
+
commit_message=f"V5.1 retrain: 840ex + compliance fix + ERR-17 disk-safe",
|
| 391 |
+
)
|
| 392 |
+
print(f" Uploaded to https://huggingface.co/{ADAPTER_REPO}")
|
| 393 |
+
except Exception as e:
|
| 394 |
+
print(f" Upload error: {e}")
|
| 395 |
+
print(f" Adapter saved locally at {OUTPUT_DIR}")
|
| 396 |
+
|
| 397 |
+
# List output files
|
| 398 |
+
print(f"\n{'='*60}")
|
| 399 |
+
print(f"OUTPUT FILES")
|
| 400 |
+
print(f"{'='*60}")
|
| 401 |
+
for f in sorted(os.listdir(OUTPUT_DIR)):
|
| 402 |
+
fpath = os.path.join(OUTPUT_DIR, f)
|
| 403 |
+
if os.path.isfile(fpath):
|
| 404 |
+
size_mb = os.path.getsize(fpath) / (1024**1024)
|
| 405 |
+
print(f" {f}: {size_mb:.1f} MB")
|
| 406 |
+
|
| 407 |
+
disk = get_disk_usage()
|
| 408 |
+
print(f"\nFinal disk: {disk['used_gb']}GB used / {disk['free_gb']}GB free")
|
| 409 |
+
|
| 410 |
+
# Done
|
| 411 |
+
training_status = {
|
| 412 |
+
"stage": "completed",
|
| 413 |
+
"progress": 100,
|
| 414 |
+
"message": f"DONE! Loss: {train_result.training_loss:.4f} | {train_time/60:.1f}min | 840 examples",
|
| 415 |
+
"summary": summary,
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
print(f"\n{'='*60}")
|
| 419 |
+
print(f"TRAINING COMPLETE")
|
| 420 |
+
print(f" Dataset: {len(dataset)} examples (820 + 20 compliance fixes)")
|
| 421 |
+
print(f" Final loss: {train_result.training_loss:.4f}")
|
| 422 |
+
print(f" Time: {train_time/60:.1f} minutes")
|
| 423 |
+
print(f" Adapter: https://huggingface.co/{ADAPTER_REPO}")
|
| 424 |
+
print(f"{'='*60}")
|
| 425 |
+
|
| 426 |
+
# Keep alive 10 min for log reading
|
| 427 |
+
print(f"\n[{datetime.now()}] Keeping alive 10 min for log access...")
|
| 428 |
+
print(f" >>> PAUSE THIS SPACE after downloading adapter! <<<")
|
| 429 |
+
time.sleep(600)
|
| 430 |
+
print(f"[{datetime.now()}] Auto-exit.")
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
if __name__ == "__main__":
|
| 434 |
+
main()
|