-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_colab_safe.py
More file actions
124 lines (106 loc) Β· 4.57 KB
/
Copy pathmain_colab_safe.py
File metadata and controls
124 lines (106 loc) Β· 4.57 KB
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
from app.simulation import Simulation
import pandas as pd
import torch
if __name__ == "__main__":
# Safe Colab Configuration - Won't trigger termination
print("π‘οΈ SAFE COLAB CONFIGURATION - No Session Termination")
print("=" * 60)
# Check GPU availability
if torch.cuda.is_available():
gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1024**3
print(f"π GPU Available: {torch.cuda.get_device_name(0)}")
print(f"πΎ GPU Memory: {gpu_memory:.1f} GB")
print("π§ Using conservative settings to avoid termination")
else:
print("β οΈ GPU not available, using CPU configuration")
# Conservative configuration that won't trigger Colab's abuse detection
config = {
"dataset": "mnist", # 'mnist' or 'cifar10'
"num_clients": 15, # Conservative client count
"byzantine_pct": 0.2,
"attack_type": "sign_flipping",
"is_iid": False,
"num_rounds": 50,
"local_epochs": 3, # Safe epoch count
# Safe GPU utilization parameters
"client_lr": 0.001,
"client_optimizer": "adam",
"batch_size": 128, # Safe batch size (was 32, now 4x larger)
"weight_decay": 1e-4,
# Conservative data loading to avoid RAM issues
"num_workers": 2, # Conservative worker count
"pin_memory": True, # Keep this optimization
"prefetch_factor": 2,
# GPU optimizations that are safe
"use_amp": True, # Mixed precision is safe and helpful
"amp_dtype": "float16",
"grad_clip": 1.0,
# Conservative memory management
"empty_cache_every": 5, # More frequent cache clearing
"max_grad_norm": 1.0,
# Q-learning parameters
"learning_rate": 0.1,
"discount_factor": 0.9,
"epsilon_start": 1.0,
"epsilon_decay": 0.995,
"epsilon_min": 0.01,
# Trust mechanism parameters
"trust_beta": 0.5,
"trust_params": {
"w_sim": 0.4,
"w_loss": 0.4,
"w_norm": 0.2,
"norm_threshold": 5.0
},
# Model persistence options
"use_pretrained": True,
"save_model": True,
"force_retrain": False,
# Training enhancements
"use_scheduler": True,
"early_stopping": True,
"patience": 10,
}
print(f"π― Safe Configuration Summary:")
print(f" Clients: {config['num_clients']} (conservative)")
print(f" Batch Size: {config['batch_size']} (4x original)")
print(f" Local Epochs: {config['local_epochs']} (safe)")
print(f" Mixed Precision: {config['use_amp']} (memory efficient)")
print(f" Workers: {config['num_workers']} (conservative)")
print(f" Expected GPU Usage: 6-8GB (40-50%)")
print(f" Termination Risk: VERY LOW β
")
print(f" Expected Time: 20-25 minutes")
# --- Run Safe Simulation ---
print("\nπ Starting SAFE TARS training (won't terminate)...")
simulation = Simulation(config)
history = simulation.run()
# --- Save Results ---
if history:
df = pd.DataFrame(history)
df.to_csv("safe_colab_results.csv", index=False)
print(f"\nπΎ Results saved to safe_colab_results.csv")
# Print performance summary
final_acc = df['accuracy'].iloc[-1]
best_acc = df['accuracy'].max()
print(f"π Final Accuracy: {final_acc:.2f}%")
print(f"π Best Accuracy: {best_acc:.2f}%")
if best_acc >= 97.0:
print("π TARGET ACHIEVED: 97%+ accuracy reached!")
elif best_acc >= 95.0:
print("β
EXCELLENT: 95%+ accuracy achieved!")
else:
print("π GOOD: Training completed successfully")
# GPU utilization summary
if torch.cuda.is_available():
max_memory = torch.cuda.max_memory_allocated() / 1024**3
total_memory = torch.cuda.get_device_properties(0).total_memory / 1024**3
utilization = (max_memory / total_memory) * 100
print(f"πΎ Peak GPU Usage: {max_memory:.1f}GB / {total_memory:.1f}GB ({utilization:.1f}%)")
if utilization < 60:
print("β
SAFE: Low GPU usage - no termination risk")
elif utilization < 80:
print("β οΈ MODERATE: Monitor for potential issues")
else:
print("π¨ HIGH: Consider reducing batch size next time")
else:
print("β οΈ No training history available")