Tile VAE decode at high res; tighten GPU budgets; non-thinking PE
Browse files
app.py
CHANGED
|
@@ -71,6 +71,20 @@ TRANSPARENCY_INFO = (
|
|
| 71 |
print(f"[load] {MODEL_ID} …", flush=True)
|
| 72 |
pipe = QwenImage21Pipeline.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
|
| 73 |
pipe = pipe.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
print("[load] pipeline ready", flush=True)
|
| 75 |
|
| 76 |
# The guard is a 270M classifier and stays on the CPU in the main process, so a prompt can
|
|
@@ -171,8 +185,11 @@ def enhance_prompt(prompt: str, image_paths: list[str]) -> tuple[str, str]:
|
|
| 171 |
result = client.predict(
|
| 172 |
prompt=prompt,
|
| 173 |
image_paths=[handle_file(p) for p in image_paths],
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 176 |
seed=0,
|
| 177 |
randomize_seed=True,
|
| 178 |
api_name="/enhance",
|
|
@@ -228,7 +245,15 @@ def estimate_duration(
|
|
| 228 |
*args,
|
| 229 |
**kwargs,
|
| 230 |
):
|
| 231 |
-
"""Budget GPU seconds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
try:
|
| 233 |
steps = int(num_inference_steps)
|
| 234 |
res = int(resolution)
|
|
@@ -237,9 +262,9 @@ def estimate_duration(
|
|
| 237 |
steps, res, cfg = 40, 1024, 1.0
|
| 238 |
n_images = len(input_images or [])
|
| 239 |
tokens = (res / 16.0) ** 2
|
| 240 |
-
per_step =
|
| 241 |
-
|
| 242 |
-
return int(min(
|
| 243 |
|
| 244 |
|
| 245 |
# --------------------------------------------------------------------------------------
|
|
|
|
| 71 |
print(f"[load] {MODEL_ID} …", flush=True)
|
| 72 |
pipe = QwenImage21Pipeline.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
|
| 73 |
pipe = pipe.to("cuda")
|
| 74 |
+
|
| 75 |
+
# The VAE decoder upcasts to fp32 in its upsamplers, so a 2048x2048 decode allocates far
|
| 76 |
+
# more than the denoising loop that produced it: it died with an allocator assert
|
| 77 |
+
# ("NVML_SUCCESS == r INTERNAL ASSERT FAILED") *after* all 40 steps had completed, and
|
| 78 |
+
# left the ZeroGPU worker wedged so every later request reported "No CUDA GPUs are
|
| 79 |
+
# available" until the Space was restarted. Tiling the decode keeps peak VAE memory flat.
|
| 80 |
+
# The threshold is set above 1536 so the resolutions measured to decode safely in one
|
| 81 |
+
# shot still do, and only larger outputs pay the tiling overhead.
|
| 82 |
+
pipe.vae.enable_tiling(
|
| 83 |
+
tile_sample_min_height=1536,
|
| 84 |
+
tile_sample_min_width=1536,
|
| 85 |
+
tile_sample_stride_height=1152,
|
| 86 |
+
tile_sample_stride_width=1152,
|
| 87 |
+
)
|
| 88 |
print("[load] pipeline ready", flush=True)
|
| 89 |
|
| 90 |
# The guard is a 270M classifier and stays on the CPU in the main process, so a prompt can
|
|
|
|
| 185 |
result = client.predict(
|
| 186 |
prompt=prompt,
|
| 187 |
image_paths=[handle_file(p) for p in image_paths],
|
| 188 |
+
# Non-thinking is 3-4x faster (measured: ~16s t2i / ~45s i2i vs ~65s either way)
|
| 189 |
+
# and its answers are just as usable. 1536 tokens truncated the longer I2I
|
| 190 |
+
# rewrites mid-JSON, so the cap sits above the longest answer observed.
|
| 191 |
+
max_new_tokens=2048,
|
| 192 |
+
enable_thinking=False,
|
| 193 |
seed=0,
|
| 194 |
randomize_seed=True,
|
| 195 |
api_name="/enhance",
|
|
|
|
| 245 |
*args,
|
| 246 |
**kwargs,
|
| 247 |
):
|
| 248 |
+
"""Budget GPU seconds from times measured on this Space's own hardware.
|
| 249 |
+
|
| 250 |
+
Fitted to ``[generate]`` log timings: 1024px/40 steps = 22.3s, 1536px/40 steps = 54.6s,
|
| 251 |
+
1024px/40 steps with true CFG = 42.7s. That gives ~0.48 s/step at 1024px scaling as
|
| 252 |
+
``latent_tokens ** 1.21``, true CFG doubling the cost (it denoises the negative branch
|
| 253 |
+
too), plus a small fixed cost for text encoding and the VAE decode. The 1.12 factor is
|
| 254 |
+
the only slack, so the budget stays close to the real call instead of over-reserving
|
| 255 |
+
quota from every visitor.
|
| 256 |
+
"""
|
| 257 |
try:
|
| 258 |
steps = int(num_inference_steps)
|
| 259 |
res = int(resolution)
|
|
|
|
| 262 |
steps, res, cfg = 40, 1024, 1.0
|
| 263 |
n_images = len(input_images or [])
|
| 264 |
tokens = (res / 16.0) ** 2
|
| 265 |
+
per_step = 2.05e-5 * tokens**1.21 * (2.0 if cfg > 1.0 else 1.0)
|
| 266 |
+
fixed = 3.0 + 2.0 * n_images + 1.5e-6 * res * res
|
| 267 |
+
return int(min(340, math.ceil((fixed + steps * per_step) * 1.12)))
|
| 268 |
|
| 269 |
|
| 270 |
# --------------------------------------------------------------------------------------
|