Fix ZeroGPU - Deshabilitar CPU offload y mixed precision para evitar errores
Browse files
app.py
CHANGED
|
@@ -142,7 +142,7 @@ def load_model(model_id):
|
|
| 142 |
else:
|
| 143 |
print(f"📦 Cargando modelo público: {model_id}")
|
| 144 |
pipe = DiffusionPipeline.from_pretrained(
|
| 145 |
-
|
| 146 |
torch_dtype=torch_dtype,
|
| 147 |
variant="fp16" if use_fp16_variant else None
|
| 148 |
)
|
|
@@ -162,10 +162,10 @@ def load_model(model_id):
|
|
| 162 |
pipe.enable_attention_slicing()
|
| 163 |
print("✅ Attention slicing habilitado")
|
| 164 |
|
| 165 |
-
#
|
| 166 |
-
if hasattr(pipe, 'enable_model_cpu_offload') and "sdxl" in model_id.lower():
|
| 167 |
-
|
| 168 |
-
|
| 169 |
|
| 170 |
if hasattr(pipe, 'enable_vae_slicing'):
|
| 171 |
pipe.enable_vae_slicing()
|
|
@@ -184,7 +184,7 @@ def load_model(model_id):
|
|
| 184 |
print(f"⚠️ XFormers no disponible: {e}")
|
| 185 |
print("🔄 Usando atención estándar")
|
| 186 |
|
| 187 |
-
|
| 188 |
print(f"✅ Modelo {model_id} cargado exitosamente")
|
| 189 |
|
| 190 |
if torch.cuda.is_available():
|
|
@@ -461,58 +461,57 @@ def infer(
|
|
| 461 |
# Limpiar cache de GPU antes de la inferencia
|
| 462 |
torch.cuda.empty_cache()
|
| 463 |
|
| 464 |
-
#
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
|
|
|
|
|
|
|
|
|
| 479 |
|
| 480 |
-
# Verificar que la imagen
|
| 481 |
-
if
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
height=height,
|
| 502 |
-
generator=generator
|
| 503 |
-
)
|
| 504 |
-
image = result.images[0]
|
| 505 |
-
else:
|
| 506 |
-
print("✅ Imagen generada correctamente")
|
| 507 |
else:
|
| 508 |
-
print("
|
| 509 |
-
raise Exception("Imagen vacía generada")
|
| 510 |
else:
|
| 511 |
-
print("❌ Error: Imagen
|
| 512 |
-
raise Exception("Imagen
|
| 513 |
else:
|
| 514 |
-
print("❌ Error:
|
| 515 |
-
raise Exception("
|
|
|
|
|
|
|
|
|
|
| 516 |
else:
|
| 517 |
# Fallback para CPU
|
| 518 |
result = pipe(
|
|
|
|
| 142 |
else:
|
| 143 |
print(f"📦 Cargando modelo público: {model_id}")
|
| 144 |
pipe = DiffusionPipeline.from_pretrained(
|
| 145 |
+
model_id,
|
| 146 |
torch_dtype=torch_dtype,
|
| 147 |
variant="fp16" if use_fp16_variant else None
|
| 148 |
)
|
|
|
|
| 162 |
pipe.enable_attention_slicing()
|
| 163 |
print("✅ Attention slicing habilitado")
|
| 164 |
|
| 165 |
+
# Deshabilitar CPU offload temporalmente (causa problemas con ZeroGPU)
|
| 166 |
+
# if hasattr(pipe, 'enable_model_cpu_offload') and "sdxl" in model_id.lower():
|
| 167 |
+
# pipe.enable_model_cpu_offload()
|
| 168 |
+
# print("✅ CPU offload habilitado (modelo grande)")
|
| 169 |
|
| 170 |
if hasattr(pipe, 'enable_vae_slicing'):
|
| 171 |
pipe.enable_vae_slicing()
|
|
|
|
| 184 |
print(f"⚠️ XFormers no disponible: {e}")
|
| 185 |
print("🔄 Usando atención estándar")
|
| 186 |
|
| 187 |
+
current_model_id = model_id
|
| 188 |
print(f"✅ Modelo {model_id} cargado exitosamente")
|
| 189 |
|
| 190 |
if torch.cuda.is_available():
|
|
|
|
| 461 |
# Limpiar cache de GPU antes de la inferencia
|
| 462 |
torch.cuda.empty_cache()
|
| 463 |
|
| 464 |
+
# Generar la imagen (sin mixed precision para evitar problemas)
|
| 465 |
+
print("⚡ Generando imagen con H200...")
|
| 466 |
+
|
| 467 |
+
# Generar la imagen
|
| 468 |
+
result = pipe(
|
| 469 |
+
prompt=prompt,
|
| 470 |
+
negative_prompt=negative_prompt,
|
| 471 |
+
guidance_scale=final_guidance_scale,
|
| 472 |
+
num_inference_steps=final_inference_steps,
|
| 473 |
+
width=width,
|
| 474 |
+
height=height,
|
| 475 |
+
generator=generator,
|
| 476 |
+
**additional_params
|
| 477 |
+
)
|
| 478 |
+
|
| 479 |
+
# Verificar que la imagen se generó correctamente
|
| 480 |
+
if hasattr(result, 'images') and len(result.images) > 0:
|
| 481 |
+
image = result.images[0]
|
| 482 |
|
| 483 |
+
# Verificar que la imagen no sea completamente negra
|
| 484 |
+
if image is not None:
|
| 485 |
+
# Convertir a numpy para verificar
|
| 486 |
+
img_array = np.array(image)
|
| 487 |
+
if img_array.size > 0:
|
| 488 |
+
# Verificar si la imagen es completamente negra
|
| 489 |
+
if np.all(img_array == 0) or np.all(img_array < 10):
|
| 490 |
+
print("⚠️ ADVERTENCIA: Imagen generada es completamente negra")
|
| 491 |
+
print("🔄 Reintentando con parámetros ajustados...")
|
| 492 |
+
|
| 493 |
+
# Reintentar con parámetros más conservadores
|
| 494 |
+
result = pipe(
|
| 495 |
+
prompt=prompt,
|
| 496 |
+
negative_prompt=negative_prompt,
|
| 497 |
+
guidance_scale=max(1.0, final_guidance_scale * 0.8),
|
| 498 |
+
num_inference_steps=max(10, final_inference_steps),
|
| 499 |
+
width=width,
|
| 500 |
+
height=height,
|
| 501 |
+
generator=generator
|
| 502 |
+
)
|
| 503 |
+
image = result.images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
else:
|
| 505 |
+
print("✅ Imagen generada correctamente")
|
|
|
|
| 506 |
else:
|
| 507 |
+
print("❌ Error: Imagen vacía")
|
| 508 |
+
raise Exception("Imagen vacía generada")
|
| 509 |
else:
|
| 510 |
+
print("❌ Error: Imagen es None")
|
| 511 |
+
raise Exception("Imagen es None")
|
| 512 |
+
else:
|
| 513 |
+
print("❌ Error: No se generaron imágenes")
|
| 514 |
+
raise Exception("No se generaron imágenes")
|
| 515 |
else:
|
| 516 |
# Fallback para CPU
|
| 517 |
result = pipe(
|