Darin Leonhart commited on
Upload finetune_setup.py with huggingface_hub
Browse files- finetune_setup.py +104 -181
finetune_setup.py
CHANGED
|
@@ -1,181 +1,104 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
from
|
| 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 |
-
# Optimizer optimized
|
| 106 |
-
learning_rate=1e-4, # Higher LR untuk faster convergence
|
| 107 |
-
weight_decay=0.01,
|
| 108 |
-
warmup_steps=10, # Minimal warmup
|
| 109 |
-
lr_scheduler_type="linear", # Faster scheduler
|
| 110 |
-
|
| 111 |
-
# Logging minimal
|
| 112 |
-
logging_steps=20,
|
| 113 |
-
save_steps=500, # Less frequent saves
|
| 114 |
-
eval_steps=500,
|
| 115 |
-
|
| 116 |
-
# Memory optimization
|
| 117 |
-
dataloader_pin_memory=False,
|
| 118 |
-
bf16=True,
|
| 119 |
-
tf32=True,
|
| 120 |
-
dataloader_num_workers=4, # Parallel loading
|
| 121 |
-
|
| 122 |
-
# Speed settings
|
| 123 |
-
remove_unused_columns=False,
|
| 124 |
-
push_to_hub=True,
|
| 125 |
-
hub_model_id="Desorden1337/d1337-cipher-v1",
|
| 126 |
-
hub_private_repo=True,
|
| 127 |
-
report_to=None,
|
| 128 |
-
|
| 129 |
-
# Additional speed
|
| 130 |
-
max_steps=100, # Ultra fast dengan 8x A100
|
| 131 |
-
ddp_find_unused_parameters=False, # DDP optimization
|
| 132 |
-
dataloader_persistent_workers=True # Keep workers alive
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
return training_args
|
| 136 |
-
|
| 137 |
-
def train(self):
|
| 138 |
-
"""Execute training"""
|
| 139 |
-
print("="*60)
|
| 140 |
-
print("D1337 CIPHER - TRAINING INITIATED")
|
| 141 |
-
print("="*60)
|
| 142 |
-
|
| 143 |
-
# Setup
|
| 144 |
-
self.setup_model()
|
| 145 |
-
dataset = self.prepare_dataset()
|
| 146 |
-
training_args = self.setup_training()
|
| 147 |
-
|
| 148 |
-
# Data collator
|
| 149 |
-
data_collator = DataCollatorForLanguageModeling(
|
| 150 |
-
tokenizer=self.tokenizer,
|
| 151 |
-
mlm=False
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
# Trainer
|
| 155 |
-
trainer = Trainer(
|
| 156 |
-
model=self.model,
|
| 157 |
-
args=training_args,
|
| 158 |
-
train_dataset=dataset,
|
| 159 |
-
data_collator=data_collator
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
# Train!
|
| 163 |
-
print("[+] TRAINING STARTED...")
|
| 164 |
-
trainer.train()
|
| 165 |
-
|
| 166 |
-
# Save final model
|
| 167 |
-
trainer.save_model()
|
| 168 |
-
self.tokenizer.save_pretrained(training_args.output_dir)
|
| 169 |
-
|
| 170 |
-
# Push to hub
|
| 171 |
-
trainer.push_to_hub()
|
| 172 |
-
|
| 173 |
-
print("="*60)
|
| 174 |
-
print("D1337 CIPHER TRAINING COMPLETE!")
|
| 175 |
-
print(f"Model saved: Desorden1337/d1337-cipher-v1")
|
| 176 |
-
print("="*60)
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
if __name__ == "__main__":
|
| 180 |
-
trainer = D1337Training(base_model="huihui-ai/Huihui-GLM-4.7-Flash-abliterated")
|
| 181 |
-
trainer.train()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import threading
|
| 5 |
+
import sys
|
| 6 |
+
import time
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# Training log file
|
| 10 |
+
TRAINING_LOG = "/tmp/training.log"
|
| 11 |
+
|
| 12 |
+
def start_training():
|
| 13 |
+
"""Start D1337 CIPHER training"""
|
| 14 |
+
|
| 15 |
+
# Clear log file
|
| 16 |
+
Path(TRAINING_LOG).write_text("")
|
| 17 |
+
|
| 18 |
+
def run_training():
|
| 19 |
+
try:
|
| 20 |
+
# Redirect output to log file
|
| 21 |
+
import sys
|
| 22 |
+
log_file = open(TRAINING_LOG, "w", encoding="utf-8")
|
| 23 |
+
sys.stdout = log_file
|
| 24 |
+
sys.stderr = log_file
|
| 25 |
+
|
| 26 |
+
print("="*60)
|
| 27 |
+
print("D1337 CIPHER - TRAINING INITIATED")
|
| 28 |
+
print("="*60)
|
| 29 |
+
print(f"[+] Starting at {time.strftime('%Y-%m-%d %H:%M:%S')}")
|
| 30 |
+
print()
|
| 31 |
+
|
| 32 |
+
# Import training script
|
| 33 |
+
from finetune_setup import D1337Training
|
| 34 |
+
|
| 35 |
+
print("[+] Loading training script...")
|
| 36 |
+
print("[+] Initializing trainer...")
|
| 37 |
+
# Initialize and train
|
| 38 |
+
trainer = D1337Training(base_model="huihui-ai/Huihui-GLM-4.7-Flash-abliterated")
|
| 39 |
+
print("[+] Trainer initialized!")
|
| 40 |
+
print("[+] Starting training process...")
|
| 41 |
+
print()
|
| 42 |
+
trainer.train()
|
| 43 |
+
|
| 44 |
+
print()
|
| 45 |
+
print("="*60)
|
| 46 |
+
print("✅ TRAINING COMPLETE!")
|
| 47 |
+
print("Model: Desorden1337/d1337-cipher-v1")
|
| 48 |
+
print("="*60)
|
| 49 |
+
|
| 50 |
+
log_file.close()
|
| 51 |
+
sys.stdout = sys.__stdout__
|
| 52 |
+
sys.stderr = sys.__stderr__
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
import traceback
|
| 56 |
+
error_msg = f"❌ Error: {str(e)}\n\n{traceback.format_exc()}"
|
| 57 |
+
Path(TRAINING_LOG).write_text(error_msg, encoding="utf-8")
|
| 58 |
+
if 'log_file' in locals():
|
| 59 |
+
log_file.close()
|
| 60 |
+
sys.stdout = sys.__stdout__
|
| 61 |
+
sys.stderr = sys.__stderr__
|
| 62 |
+
|
| 63 |
+
# Run in background
|
| 64 |
+
thread = threading.Thread(target=run_training)
|
| 65 |
+
thread.daemon = True
|
| 66 |
+
thread.start()
|
| 67 |
+
|
| 68 |
+
return "🔥 D1337 CIPHER TRAINING STARTED!\n\nLoading model (31B) - this may take a few minutes...\n\nCheck logs below for progress..."
|
| 69 |
+
|
| 70 |
+
def get_training_log():
|
| 71 |
+
"""Get latest training log"""
|
| 72 |
+
try:
|
| 73 |
+
if Path(TRAINING_LOG).exists():
|
| 74 |
+
content = Path(TRAINING_LOG).read_text(encoding="utf-8")
|
| 75 |
+
if content.strip():
|
| 76 |
+
return content
|
| 77 |
+
# Also check if training is running by checking process
|
| 78 |
+
return "Training log will appear here once training starts...\n\nIf you clicked START TRAINING, wait a few seconds for model loading (31B model takes time)."
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"Error reading log: {str(e)}"
|
| 81 |
+
|
| 82 |
+
# UI
|
| 83 |
+
with gr.Blocks(title="D1337 CIPHER Training") as demo:
|
| 84 |
+
gr.Markdown("# 🔥 D1337 CIPHER C2 V.1 - TRAINING")
|
| 85 |
+
gr.Markdown("**Base**: GLM-4.7-Flash-abliterated (31B)")
|
| 86 |
+
gr.Markdown("**Dataset**: 92 samples")
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
train_btn = gr.Button("🚀 START TRAINING", variant="primary")
|
| 90 |
+
|
| 91 |
+
output = gr.Textbox(label="Training Output", lines=20, value="Click 'START TRAINING' to begin...")
|
| 92 |
+
|
| 93 |
+
train_btn.click(start_training, outputs=output)
|
| 94 |
+
|
| 95 |
+
# Auto-refresh log every 5 seconds
|
| 96 |
+
demo.load(
|
| 97 |
+
fn=get_training_log,
|
| 98 |
+
outputs=output,
|
| 99 |
+
every=5
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
gr.Markdown("**Expected time: 15-30 minutes on GPU**")
|
| 103 |
+
|
| 104 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, ssl_verify=False, show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|