File size: 6,646 Bytes
c9e8126 | 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 | #!/usr/bin/env python3
"""
GPU-Optimized ZRIA Training Script
"""
import os
import sys
import torch
import time
# We're now in the ae directory
def check_gpu():
"""Check GPU availability and capabilities."""
print("π Checking GPU availability...")
if torch.cuda.is_available():
gpu_count = torch.cuda.device_count()
print(f"β
GPU detected: {gpu_count} device(s)")
for i in range(gpu_count):
gpu_name = torch.cuda.get_device_name(i)
gpu_memory = torch.cuda.get_device_properties(i).total_memory / 1e9
print(f" GPU {i}: {gpu_name} ({gpu_memory:.1f} GB)")
device = torch.device('cuda:0')
print(f"π Using GPU: {torch.cuda.get_device_name(0)}")
return device
else:
print("βΉοΈ No GPU detected, using CPU")
return torch.device('cpu')
def optimize_for_gpu(device):
"""Optimize training parameters based on GPU capabilities."""
if device.type == 'cuda':
gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1e9
if gpu_memory >= 20: # 20GB+ (RTX 4090, A100)
config = {
'dim': 256,
'max_seq_len': 128,
'batch_size': 64,
'epochs': 150,
'learning_rate': 3e-4,
'weight_decay': 0.01
}
print("β‘ Using high-performance configuration (20GB+ GPU)")
elif gpu_memory >= 12: # 12GB+ (RTX 4070 Ti, RTX 3060)
config = {
'dim': 192,
'max_seq_len': 96,
'batch_size': 32,
'epochs': 150,
'learning_rate': 3e-4,
'weight_decay': 0.01
}
print("β‘ Using medium-performance configuration (12GB+ GPU)")
else: # 8GB (RTX 4060 Ti)
config = {
'dim': 128,
'max_seq_len': 64,
'batch_size': 16,
'epochs': 150,
'learning_rate': 3e-4,
'weight_decay': 0.01
}
print("β‘ Using standard configuration (8GB GPU)")
else:
# CPU configuration
config = {
'dim': 128,
'max_seq_len': 64,
'batch_size': 8,
'epochs': 150,
'learning_rate': 3e-4,
'weight_decay': 0.01
}
print("β‘ Using CPU configuration")
return config
def train_with_gpu(file_path, model_name="gpu_trained_model"):
"""Train the ZRIA model with GPU acceleration."""
# Check GPU
device = check_gpu()
# Optimize configuration
config = optimize_for_gpu(device)
print(f"\nπ Training ZRIA model on: {file_path}")
print(f"π File size: {os.path.getsize(file_path)} bytes")
print(f"π§ Device: {device}")
print(f"βοΈ Configuration: {config}")
# Import the text processor
from text_processor import HierarchicalTextProcessor
# Initialize processor
processor = HierarchicalTextProcessor()
# Process the text file
print("\nπ Processing text into structured knowledge...")
start_time = time.time()
knowledge = processor.process_large_text_file(file_path, chunk_size=5000)
process_time = time.time() - start_time
print(f"β
Processing completed in {process_time:.1f} seconds")
# Create training data
print("\nπ Creating training data...")
training_data = processor.create_training_data(knowledge)
print(f"β
Created {len(training_data)} training examples")
# Train the model with GPU optimization
print(f"\nπ§ Training ZRIA model with GPU acceleration...")
from persistent_model import PersistentZRIAModel
# Create model with GPU-optimized config
model = PersistentZRIAModel(model_name)
model.device = device
# Train with optimized parameters
model.train_and_save(training_data, config)
print(f"\nβ
Training complete! Model saved as '{model_name}'")
return model
def benchmark_training(file_path):
"""Benchmark CPU vs GPU performance."""
print("π Running training benchmark...")
# CPU training
print("\nπ CPU Training Benchmark:")
start_time = time.time()
try:
# Temporarily force CPU
original_cuda_available = torch.cuda.is_available
torch.cuda.is_available = lambda: False
train_with_gpu(file_path, "benchmark_cpu")
cpu_time = time.time() - start_time
print(f"β±οΈ CPU training time: {cpu_time:.1f} seconds")
# Restore CUDA
torch.cuda.is_available = original_cuda_available
except Exception as e:
print(f"β CPU benchmark failed: {e}")
cpu_time = None
# GPU training
print("\nπ GPU Training Benchmark:")
start_time = time.time()
try:
train_with_gpu(file_path, "benchmark_gpu")
gpu_time = time.time() - start_time
print(f"β±οΈ GPU training time: {gpu_time:.1f} seconds")
if cpu_time and gpu_time:
speedup = cpu_time / gpu_time
print(f"π GPU speedup: {speedup:.1f}x faster")
except Exception as e:
print(f"β GPU benchmark failed: {e}")
def main():
"""Main function."""
print("π GPU-Optimized ZRIA Training")
print("=" * 50)
# Use the sample file
file_path = "sample_text_file.txt"
if not os.path.exists(file_path):
print(f"β File not found: {file_path}")
print("Please make sure the sample_text_file.txt exists.")
return
# Check if user wants to benchmark
benchmark_choice = input("\nWould you like to run a performance benchmark? (y/n): ").strip().lower()
if benchmark_choice == 'y':
benchmark_training(file_path)
else:
# Regular training
try:
model = train_with_gpu(file_path, "my_gpu_trained_model")
print(f"\nπ Success! Your GPU-accelerated model is ready!")
print(f"You can load it anytime using: load_and_use_model('my_gpu_trained_model')")
except Exception as e:
print(f"β Error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main() |