-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdemo_training.py
More file actions
319 lines (273 loc) · 10.7 KB
/
demo_training.py
File metadata and controls
319 lines (273 loc) · 10.7 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
"""
Full end-to-end training demo with real data upload, epoch logs, and CIDs.
Visualizes the complete PyTorch + Akave O3 workflow.
"""
import sys
import os
from pathlib import Path
from datetime import datetime
# Load AKAVE_PRIVATE_KEY from .env
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import logging
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
datefmt='%H:%M:%S'
)
logger = logging.getLogger(__name__)
from pytorch_o3 import O3Client, O3CheckpointManager
# ============================================================================
# DEMO SETUP
# ============================================================================
print("\n" + "="*70)
print(" 🚀 PyTorch + Akave O3 Training Demo - Complete Workflow")
print("="*70 + "\n")
# Check for API key
api_key = os.getenv("AKAVE_PRIVATE_KEY")
if not api_key:
print("⚠️ AKAVE_PRIVATE_KEY not set. Skipping O3 uploads (training will run locally).\n")
USE_O3 = False
else:
USE_O3 = True
print(f"✅ AKAVE_PRIVATE_KEY configured (length: {len(api_key)})\n")
# ============================================================================
# SIMPLE CNN MODEL
# ============================================================================
class SimpleCNN(nn.Module):
def __init__(self, in_channels=1, num_classes=10):
super().__init__()
self.conv1 = nn.Conv2d(in_channels, 16, 3, padding=1)
self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
self.pool = nn.MaxPool2d(2, 2)
self.adaptive = nn.AdaptiveAvgPool2d(4)
self.fc1 = nn.Linear(32 * 4 * 4, 64)
self.fc2 = nn.Linear(64, num_classes)
def forward(self, x):
x = self.pool(torch.relu(self.conv1(x)))
x = self.pool(torch.relu(self.conv2(x)))
x = self.adaptive(x)
x = x.view(x.size(0), -1)
x = torch.relu(self.fc1(x))
return self.fc2(x)
# ============================================================================
# LOAD SAMPLE DATA
# ============================================================================
print("📦 Loading sample dataset (MNIST)...\n")
sample_dir = Path("data/samples/mnist")
if not sample_dir.exists():
print("❌ Sample dataset not found. Did you run data generation?")
sys.exit(1)
train_data = torch.load(sample_dir / "train.pt", map_location="cpu", weights_only=True)
test_data = torch.load(sample_dir / "test.pt", map_location="cpu", weights_only=True)
train_images = train_data["images"].float() / 255.0
train_labels = train_data["labels"].long()
test_images = test_data["images"].float() / 255.0
test_labels = test_data["labels"].long()
# Add channel dimension if needed
if train_images.ndim == 3:
train_images = train_images.unsqueeze(1)
if test_images.ndim == 3:
test_images = test_images.unsqueeze(1)
print(f" Training: {train_images.shape[0]} samples, shape {list(train_images.shape[1:])}")
print(f" Test: {test_images.shape[0]} samples, shape {list(test_images.shape[1:])}\n")
# ============================================================================
# SETUP O3 (Optional)
# ============================================================================
o3_client = None
ckpt_manager = None
ckpt_bucket = "local-checkpoints" # Default fallback
if USE_O3:
try:
print("🔌 Connecting to Akave O3...\n")
o3_client = O3Client()
# List available buckets
buckets = o3_client.list_buckets()
print(f" Available buckets: {len(buckets)}")
bucket_names = []
for b in buckets[:5]:
name = b.name if hasattr(b, 'name') else str(b)
bucket_names.append(name)
print(f" - {name}")
if len(buckets) > 5:
print(f" ... and {len(buckets) - 5} more")
for b in buckets[5:]:
name = b.name if hasattr(b, 'name') else str(b)
bucket_names.append(name)
print()
# Setup checkpoint manager - use first available bucket
if bucket_names:
ckpt_bucket = bucket_names[0]
else:
ckpt_bucket = "pytorch-democheckpoints"
try:
# Use existing bucket for checkpoints
ckpt_manager = O3CheckpointManager(o3_client, bucket_name=ckpt_bucket)
print(f"✅ O3 Checkpoint Manager ready (bucket: {ckpt_bucket})\n")
except Exception as e:
print(f"⚠️ Could not setup checkpoint manager: {e}\n")
ckpt_manager = None
except Exception as e:
print(f"⚠️ O3 connection failed: {e}")
print(" Continuing with local training only...\n")
o3_client = None
# ============================================================================
# TRAINING LOOP
# ============================================================================
print("="*70)
print(" 🤖 Training Phase")
print("="*70 + "\n")
batch_size = 32
epochs = 3
learning_rate = 0.001
device = torch.device("cpu")
train_loader = DataLoader(TensorDataset(train_images, train_labels), batch_size=batch_size, shuffle=True)
model = SimpleCNN(in_channels=train_images.shape[1], num_classes=10).to(device)
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
criterion = nn.CrossEntropyLoss()
total_params = sum(p.numel() for p in model.parameters())
print(f"📊 Model: SimpleCNN with {total_params:,} parameters")
print(f"⚙️ Config: epochs={epochs}, batch={batch_size}, lr={learning_rate}\n")
# Create checkpoint directory
ckpt_dir = Path("data/checkpoints")
ckpt_dir.mkdir(parents=True, exist_ok=True)
checkpoints_saved = []
for epoch in range(1, epochs + 1):
print("-" * 70)
print(f"EPOCH {epoch}/{epochs}")
print("-" * 70)
model.train()
running_loss = 0.0
correct = 0
total = 0
batch_count = 0
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
running_loss += loss.item() * data.size(0)
pred = output.argmax(dim=1)
correct += pred.eq(target).sum().item()
total += data.size(0)
batch_count += 1
if (batch_idx + 1) % max(1, len(train_loader) // 3) == 0 or batch_idx == len(train_loader) - 1:
avg_loss = running_loss / total
train_acc = 100.0 * correct / total
print(f" Batch {batch_idx+1}/{len(train_loader)} | Loss: {avg_loss:.4f} | Acc: {train_acc:.2f}%")
train_loss = running_loss / total
train_acc = 100.0 * correct / total
# Evaluate on test set
model.eval()
with torch.no_grad():
outputs = model(test_images.to(device))
preds = outputs.argmax(dim=1)
test_acc = 100.0 * preds.eq(test_labels.to(device)).sum().item() / len(test_labels)
print(f"\n✅ Epoch {epoch} Summary:")
print(f" Training Loss: {train_loss:.4f}")
print(f" Training Accuracy: {train_acc:.2f}%")
print(f" Test Accuracy: {test_acc:.2f}%")
# ── SAVE CHECKPOINT LOCALLY ──
ckpt_path = ckpt_dir / f"epoch_{epoch:03d}.pt"
ckpt_payload = {
"epoch": epoch,
"model_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
"loss": train_loss,
"accuracy": test_acc,
}
torch.save(ckpt_payload, ckpt_path)
ckpt_size = ckpt_path.stat().st_size
print(f"\n 📁 Local checkpoint saved: {ckpt_path.name} ({ckpt_size / 1024:.1f} KB)")
# ── UPLOAD TO O3 ──
if ckpt_manager:
try:
print(f"\n 📤 Uploading to Akave O3...")
import io
buf = io.BytesIO()
torch.save(ckpt_payload, buf)
data_bytes = buf.getvalue()
file_meta = o3_client.upload_object(
ckpt_bucket,
f"checkpoint_epoch_{epoch:03d}.pt",
data_bytes
)
# Extract CID
cid = None
if hasattr(file_meta, 'root_cid'):
cid = file_meta.root_cid
elif hasattr(file_meta, 'RootCid'):
cid = file_meta.RootCid
elif isinstance(file_meta, dict):
cid = file_meta.get('root_cid', file_meta.get('RootCid', file_meta.get('cid')))
if cid:
print(f" ✅ O3 Upload Complete!")
print(f" 🔗 CID (Content Identifier): {cid}")
checkpoints_saved.append({
"epoch": epoch,
"cid": cid,
"accuracy": test_acc,
"loss": train_loss,
})
else:
print(f" ⚠️ Upload successful but CID extraction pending")
checkpoints_saved.append({
"epoch": epoch,
"cid": "pending-cid",
"accuracy": test_acc,
"loss": train_loss,
})
except Exception as e:
print(f" ⚠️ O3 upload failed: {e}")
checkpoints_saved.append({
"epoch": epoch,
"cid": "local-only",
"accuracy": test_acc,
"loss": train_loss,
})
else:
checkpoints_saved.append({
"epoch": epoch,
"cid": "local-only",
"accuracy": test_acc,
"loss": train_loss,
})
print()
# ============================================================================
# SUMMARY
# ============================================================================
print("\n" + "="*70)
print(" ✨ Training Complete!")
print("="*70 + "\n")
print("📋 Checkpoint Summary:\n")
print(f"{'Epoch':<8} {'Accuracy':<12} {'Loss':<10} {'CID / Location':<50}")
print("-" * 80)
for ckpt in checkpoints_saved:
epoch = ckpt["epoch"]
acc = ckpt["accuracy"]
loss = ckpt["loss"]
cid = ckpt["cid"]
cid_display = cid[:47] + "..." if len(cid) > 50 else cid
print(f"{epoch:<8} {acc:<12.2f}% {loss:<10.4f} {cid_display:<50}")
print()
best = max(checkpoints_saved, key=lambda c: c["accuracy"])
print(f"🏆 Best checkpoint: Epoch {best['epoch']} with {best['accuracy']:.2f}% accuracy")
print(f" CID: {best['cid']}\n")
if ckpt_manager:
print("✅ All checkpoints saved to:")
print(f" - Local: {ckpt_dir}/")
print(f" - O3 Bucket: {ckpt_bucket}/")
else:
print("✅ Checkpoints saved locally to:", ckpt_dir)
print("\n" + "="*70 + "\n")