File size: 7,355 Bytes
be6ad40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ๐Ÿš€ 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!** ๐Ÿš€