# 🚀 Multi-GPU Setup Guide - 8 GPU Configuration ## ✨ What Just Changed: Your app now **automatically detects and uses ALL available GPUs**! ### With 8 GPUs: - ✅ **8 simultaneous image generations** (one per GPU) - ✅ **8x faster throughput** than single GPU - ✅ **Round-robin GPU assignment** for load balancing - ✅ **~12 images per minute** total capacity --- ## 📊 Performance Comparison: | Setup | Concurrent Users | Images/Minute | Queue Wait (10 users) | |-------|-----------------|---------------|----------------------| | **1 GPU** | 1 at a time | ~1.5 | 6-7 minutes | | **8 GPUs** ✨ | 8 at a time | ~12 | < 1 minute | --- ## 🔧 How It Works: ### 1. **Auto-Detection** ```python num_gpus = torch.cuda.device_count() # Detects: 8 ``` ### 2. **Model Loading** (happens at startup) ``` 📥 Loading model on GPU 0... ✅ 📥 Loading model on GPU 1... ✅ 📥 Loading model on GPU 2... ✅ ... 📥 Loading model on GPU 7... ✅ ``` ### 3. **Round-Robin Assignment** ``` User 1 → GPU 0 🎨 User 2 → GPU 1 🎨 User 3 → GPU 2 🎨 ... User 8 → GPU 7 🎨 User 9 → GPU 0 🔄 (cycles back) ``` ### 4. **Concurrent Processing** All 8 GPUs work **simultaneously** - not queued! --- ## 💾 Memory Requirements: ### Per GPU: - **VRAM**: ~12-16 GB per GPU - **System RAM**: ~4 GB per GPU (32 GB total recommended) ### Recommended GPUs: - ✅ **NVIDIA A100 (40GB)** - Ideal - ✅ **NVIDIA A6000 (48GB)** - Ideal - ✅ **RTX 4090 (24GB)** - Works well - ✅ **RTX 3090 (24GB)** - Works - ⚠️ **RTX 3080 (10GB)** - Tight, may need lower resolution --- ## 🎮 Testing Your Setup: ### Check GPU Detection: ```bash python -c "import torch; print(f'GPUs: {torch.cuda.device_count()}')" ``` Expected output: ``` GPUs: 8 ``` ### Monitor GPU Usage: ```bash # In a separate terminal, run continuously: watch -n 1 nvidia-smi ``` You'll see all 8 GPUs at ~90-100% utilization when busy! ### Load Test: 1. Open **8 browser tabs** 2. Submit different prompts in each 3. All should start generating **immediately** (not queued) --- ## 📈 Capacity Planning: ### With 8 GPUs (40 steps, ~40 seconds per image): | Concurrent Users | Experience | |-----------------|------------| | **1-8 users** | ✅ Instant generation, no wait | | **9-16 users** | ✅ Half wait ~40 seconds | | **17-24 users** | ⚠️ Some wait ~80 seconds | | **25-40 users** | ⚠️ Average wait ~2-3 minutes | | **50+ users** | ⚠️ Queue fills up, 5+ min waits | **Sweet spot: 8-16 simultaneous users for best experience** --- ## ⚙️ Optimization Tips: ### 1. **Reduce Steps for Faster Throughput** ```python num_inference_steps = 30 # Instead of 40 # Slightly lower quality, but 25% faster # Throughput: ~16 images/minute instead of 12 ``` ### 2. **Enable Model Offloading** (if VRAM limited) ```python for pipe in pipes: pipe.enable_model_cpu_offload() # Saves ~6GB VRAM per GPU ``` ### 3. **Use xFormers** (faster attention) ```python for pipe in pipes: pipe.enable_xformers_memory_efficient_attention() ``` ### 4. **Compile with torch.compile** (PyTorch 2.0+) ```python for pipe in pipes: pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead") # First generation slow, then ~20% faster ``` --- ## 🔍 Monitoring & Debugging: ### Check Which GPU is Processing: The app prints: ``` 🎨 Generating on GPU 3... ✅ GPU 3 completed! ``` ### Verify All GPUs Are Used: Run `nvidia-smi` while 8 users generate: ``` +-----------------------------------------------------------------------------+ | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | 0 NVIDIA A100... On | 00000000:00:04.0 Off | 0 | | 95% 72C P0 250W / 400W | 15234MiB / 40960MiB | 98% Default | +-------------------------------+----------------------+----------------------+ | 1 NVIDIA A100... On | 00000000:00:05.0 Off | 0 | | 95% 71C P0 248W / 400W | 15198MiB / 40960MiB | 99% Default | ... | 7 NVIDIA A100... On | 00000000:00:0C.0 Off | 0 | | 96% 73C P0 252W / 400W | 15267MiB / 40960MiB | 97% Default | +-----------------------------------------------------------------------------+ ``` All 8 should show high utilization! --- ## 🚨 Common Issues: ### Issue 1: "CUDA out of memory" **Solution**: Reduce image size or steps: ```python MAX_IMAGE_SIZE = 768 # Instead of 1024 ``` ### Issue 2: Only GPU 0 is used **Solution**: Check the code has `default_concurrency_limit=num_gpus` ### Issue 3: Slow startup (loading 8 models) **Expected**: Takes 2-5 minutes to load all 8 models at startup **Solution**: This is normal, one-time cost ### Issue 4: Uneven GPU usage **Solution**: The round-robin should balance automatically, but you can verify with: ```bash watch -n 0.5 nvidia-smi --query-gpu=utilization.gpu --format=csv ``` --- ## 💰 Cost Analysis: ### Cloud Pricing (estimated): | Provider | 8x A100 40GB | Cost/Hour | Cost/Day (24h) | |----------|--------------|-----------|----------------| | **AWS** | p4d.24xlarge | ~$32 | ~$768 | | **GCP** | a2-highgpu-8g | ~$28 | ~$672 | | **Lambda Labs** | 8x A100 | ~$12 | ~$288 | | **RunPod** | 8x A100 | ~$10-15 | ~$240-360 | ### Throughput Economics: - **Images/day**: ~17,280 (at 12/min × 24h) - **Cost per image**: $0.014 - $0.044 Compare to Stability AI API: ~$0.02 per image --- ## 🎯 Production Checklist: Before deploying with 8 GPUs: - [ ] Verify all 8 GPUs detected: `torch.cuda.device_count() == 8` - [ ] Test with 8 simultaneous users - [ ] Monitor VRAM usage on all GPUs - [ ] Set up logging to track GPU assignment - [ ] Configure auto-restart on crashes - [ ] Set up monitoring (Prometheus + Grafana) - [ ] Test failover if a GPU fails - [ ] Load test with 50+ concurrent users --- ## 📊 Expected Startup Output: ``` 🔥 Detected 8 GPU(s) 📥 Loading model on GPU 0... ✅ GPU 0 ready! 📥 Loading model on GPU 1... ✅ GPU 1 ready! 📥 Loading model on GPU 2... ✅ GPU 2 ready! 📥 Loading model on GPU 3... ✅ GPU 3 ready! 📥 Loading model on GPU 4... ✅ GPU 4 ready! 📥 Loading model on GPU 5... ✅ GPU 5 ready! 📥 Loading model on GPU 6... ✅ GPU 6 ready! 📥 Loading model on GPU 7... ✅ GPU 7 ready! ============================================================ 🚀 Starting Multi-GPU Server ============================================================ 📊 GPUs Available: 8 ⚡ Concurrent Generations: 8 📈 Theoretical Max Throughput: 12.0 images/minute ============================================================ Running on local URL: http://0.0.0.0:7860 Running on public URL: https://xxxxx.gradio.live ``` --- ## 🎉 Summary: ✅ **Automatic multi-GPU detection and usage** ✅ **8x performance increase** over single GPU ✅ **Round-robin load balancing** across all GPUs ✅ **Production-ready** for 8-16 concurrent users ✅ **Scales automatically** - works with 1, 2, 4, or 8 GPUs Your app is now a **high-performance image generation server!** 🚀