File size: 11,382 Bytes
f206b57 | 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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | """
Comprehensive TorchForge Examples
Demonstrates all major features of TorchForge framework.
"""
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
from torchforge import ForgeModel, ForgeConfig
from torchforge.governance import ComplianceChecker, NISTFramework
from torchforge.monitoring import ModelMonitor
from torchforge.deployment import DeploymentManager
# Example 1: Basic Classification Model
def example_basic_classification():
"""Basic classification with TorchForge."""
print("\n" + "="*60)
print("Example 1: Basic Classification")
print("="*60)
# Define PyTorch model
class Classifier(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(20, 64)
self.fc2 = nn.Linear(64, 32)
self.fc3 = nn.Linear(32, 3)
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
return self.fc3(x)
# Wrap with TorchForge
config = ForgeConfig(
model_name="simple_classifier",
version="1.0.0",
enable_monitoring=True,
enable_governance=True
)
base_model = Classifier()
model = ForgeModel(base_model, config=config)
# Generate synthetic data
X_train = torch.randn(1000, 20)
y_train = torch.randint(0, 3, (1000,))
# Train
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
print("\nTraining model...")
for epoch in range(5):
model.train()
optimizer.zero_grad()
output = model(X_train)
loss = criterion(output, y_train)
loss.backward()
optimizer.step()
# Track predictions
model.track_prediction(output, y_train, metadata={"epoch": epoch})
print(f"Epoch {epoch+1}/5, Loss: {loss.item():.4f}")
# Get metrics
print("\nModel Metrics:")
metrics = model.get_metrics_summary()
for key, value in metrics.items():
print(f" {key}: {value}")
print("\n✓ Example 1 completed successfully!")
# Example 2: Governance & Compliance
def example_governance():
"""Demonstrate governance and compliance features."""
print("\n" + "="*60)
print("Example 2: Governance & Compliance")
print("="*60)
# Create model with full governance
class SimpleNet(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(10, 2)
def forward(self, x):
return self.fc(x)
config = ForgeConfig(
model_name="compliant_model",
version="1.0.0",
enable_governance=True,
enable_monitoring=True,
)
config.governance.bias_detection = True
config.governance.audit_logging = True
config.governance.lineage_tracking = True
model = ForgeModel(SimpleNet(), config=config)
# Check compliance
print("\nRunning NIST AI RMF compliance check...")
checker = ComplianceChecker(framework=NISTFramework.RMF_1_0)
report = checker.assess_model(model)
print(f"\nCompliance Results:")
print(f" Overall Score: {report.overall_score:.1f}/100")
print(f" Risk Level: {report.risk_level}")
print(f"\nCompliance Checks:")
for check in report.checks:
status = "✓" if check.passed else "✗"
print(f" {status} {check.check_name}: {check.score:.1f}/100")
print(f"\nRecommendations:")
for i, rec in enumerate(report.recommendations, 1):
print(f" {i}. {rec}")
# Export report
print("\nExporting compliance report...")
report.export_json("compliance_report.json")
report.export_pdf("compliance_report.pdf")
print(" - compliance_report.json")
print(" - compliance_report.html")
print("\n✓ Example 2 completed successfully!")
# Example 3: Production Deployment
def example_deployment():
"""Demonstrate deployment features."""
print("\n" + "="*60)
print("Example 3: Production Deployment")
print("="*60)
# Create production-ready model
class ProductionModel(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(10, 64),
nn.ReLU(),
nn.Linear(64, 2)
)
def forward(self, x):
return self.net(x)
config = ForgeConfig(
model_name="production_model",
version="2.0.0",
enable_monitoring=True,
enable_governance=True,
enable_optimization=True
)
model = ForgeModel(ProductionModel(), config=config)
# Deploy to AWS
print("\nDeploying to AWS SageMaker...")
deployment = DeploymentManager(
model=model,
cloud_provider="aws",
instance_type="ml.g4dn.xlarge"
)
info = deployment.deploy(
enable_autoscaling=True,
min_instances=2,
max_instances=10,
health_check_path="/health"
)
print(f"\nDeployment Information:")
print(f" Status: {info['status']}")
print(f" Endpoint: {info['endpoint_url']}")
print(f" Cloud Provider: {info['cloud_provider']}")
print(f" Instance Type: {info['instance_type']}")
print(f" Autoscaling: {info['autoscaling_enabled']}")
print(f" Min Instances: {info['min_instances']}")
print(f" Max Instances: {info['max_instances']}")
# Get metrics
print("\nDeployment Metrics (1h window):")
metrics = deployment.get_metrics(window="1h")
print(f" P95 Latency: {metrics.latency_p95:.2f}ms")
print(f" P99 Latency: {metrics.latency_p99:.2f}ms")
print(f" Requests/sec: {metrics.requests_per_second:.1f}")
print(f" Error Rate: {metrics.error_rate:.3%}")
print("\n✓ Example 3 completed successfully!")
# Example 4: Monitoring & Observability
def example_monitoring():
"""Demonstrate monitoring features."""
print("\n" + "="*60)
print("Example 4: Monitoring & Observability")
print("="*60)
# Create monitored model
class MonitoredNet(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(10, 2)
def forward(self, x):
return self.fc(x)
config = ForgeConfig(
model_name="monitored_model",
version="1.0.0",
enable_monitoring=True
)
config.monitoring.drift_detection = True
config.monitoring.fairness_tracking = True
config.monitoring.prometheus_enabled = True
model = ForgeModel(MonitoredNet(), config=config)
# Setup monitor
print("\nSetting up model monitor...")
monitor = ModelMonitor(model)
monitor.enable_drift_detection()
monitor.enable_fairness_tracking()
# Simulate production traffic
print("\nSimulating production traffic...")
for i in range(100):
x = torch.randn(1, 10)
_ = model(x)
# Get health status
print("\nModel Health Status:")
health = monitor.get_health_status()
print(f" Status: {health['status']}")
print(f" Drift Detection: {health['drift_detection']}")
print(f" Fairness Tracking: {health['fairness_tracking']}")
metrics = health['metrics']
print(f"\nPerformance Metrics:")
print(f" Total Inferences: {metrics['inference_count']}")
print(f" Mean Latency: {metrics['latency_mean_ms']:.2f}ms")
print(f" P95 Latency: {metrics['latency_p95_ms']:.2f}ms")
print(f" Error Rate: {metrics['error_rate']:.3%}")
print("\n✓ Example 4 completed successfully!")
# Example 5: Complete ML Pipeline
def example_complete_pipeline():
"""Demonstrate complete ML pipeline."""
print("\n" + "="*60)
print("Example 5: Complete ML Pipeline")
print("="*60)
# 1. Define Model
class MLPipeline(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(20, 128),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 2)
)
def forward(self, x):
return self.net(x)
# 2. Configure
print("\n1. Configuring model...")
config = ForgeConfig(
model_name="ml_pipeline",
version="1.0.0",
description="Complete ML pipeline with all features",
author="Anil Prasad",
tags=["production", "classification"],
enable_monitoring=True,
enable_governance=True,
enable_optimization=True
)
model = ForgeModel(MLPipeline(), config=config)
# 3. Train
print("\n2. Training model...")
X = torch.randn(1000, 20)
y = torch.randint(0, 2, (1000,))
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
model.train()
for epoch in range(10):
optimizer.zero_grad()
output = model(X)
loss = criterion(output, y)
loss.backward()
optimizer.step()
if (epoch + 1) % 2 == 0:
print(f" Epoch {epoch+1}/10, Loss: {loss.item():.4f}")
# 4. Evaluate
print("\n3. Evaluating model...")
model.eval()
with torch.no_grad():
output = model(X)
predictions = output.argmax(dim=1)
accuracy = (predictions == y).float().mean()
print(f" Accuracy: {accuracy:.2%}")
# 5. Check Compliance
print("\n4. Checking compliance...")
checker = ComplianceChecker()
report = checker.assess_model(model)
print(f" Compliance Score: {report.overall_score:.1f}/100")
print(f" Risk Level: {report.risk_level}")
# 6. Save
print("\n5. Saving checkpoint...")
model.save_checkpoint("ml_pipeline_checkpoint.pt")
print(" ✓ Checkpoint saved")
# 7. Deploy
print("\n6. Deploying to production...")
deployment = DeploymentManager(model=model)
info = deployment.deploy(enable_autoscaling=True)
print(f" ✓ Deployed to {info['endpoint_url']}")
# 8. Monitor
print("\n7. Setting up monitoring...")
monitor = ModelMonitor(model)
monitor.enable_drift_detection()
monitor.enable_fairness_tracking()
print(" ✓ Monitoring enabled")
print("\n✓ Example 5 completed successfully!")
print("\nComplete ML pipeline executed end-to-end!")
if __name__ == "__main__":
print("\n" + "="*60)
print("TorchForge - Comprehensive Examples")
print("Author: Anil Prasad")
print("="*60)
# Run all examples
example_basic_classification()
example_governance()
example_deployment()
example_monitoring()
example_complete_pipeline()
print("\n" + "="*60)
print("All examples completed successfully! 🎉")
print("="*60)
|