djarun / stable-diffusion-3.5-large /MULTI_GPU_GUIDE.md
hyperman12's picture
Upload 9 files
be6ad40 verified
|
Raw History Blame Contribute Delete
7.36 kB
# ๐Ÿš€ 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!** ๐Ÿš€