comfyui-colab-flash / bootstrap_remote.py
ramusgill's picture
Switch tunnel to HTTP/2 + add RAM-gzip & CDN/Browser Cache-Control middleware
64bd70b verified
Raw History Blame Contribute Delete
38.9 kB
#!/usr/bin/env python3
# Auto-generated remote bootstrap script (HF + aria2c Ultra-Flash Mode, sanitized)
# @title 1. Arranque Flash: Modelos Tier 0/1 (aria2c) + Entorno Limpio (zstd) + Cachés Triton/LoRA
import os, sys, re, time, json, math, struct, shutil, hashlib, subprocess, urllib.parse, urllib.request, concurrent.futures
from pathlib import Path
import torch
WITH_NODES = True # @param {type:"boolean"}
INFERENCE_MODE = "reference" # @param ["reference", "first_frame"]
CUSTOM_MODEL = "10Eros_Max_h3_TURBO-hybrid_beta5_int8" # @param ["10Eros_Max_h3_TURBO-hybrid_beta5_int8", "10Eros_Max_h3_TURBO-hybrid_beta5_w4a8_14gb_optimized", "10Eros_Max_h3_8stepTURBO_beta7_fix1-native_int8_protected_edges_HQ", "minimax_h3_ref2va_pruned_w4a8_mixed", "None"]
TURBO_LORA_MODE = "resized_rank21_4step" # @param ["resized_rank21_4step", "pdmd_2step_ultrafast", "full_bf16_4step"]
VIDEO_VAE_MODE = "video_vae_int8" # @param ["video_vae_int8", "lynnreal_light_int8"]
DOWNLOAD_QWEN_NSFW = True # @param {type:"boolean"}
QWEN_IN_BACKGROUND = True # @param {type:"boolean"}
COPY_MY_LORAS = True # @param {type:"boolean"}
HF_ASSETS_REPO = "ramusgill/comfyui-colab-flash" # @param {type:"string"}
PREBUILT_ENV_ARCHIVE = "h3_env_py313_cu130_flash.tar.zst" # @param {type:"string"}
REBUILD_ENV = False # @param {type:"boolean"}
_env_mode = os.environ.get("H3_INFERENCE_MODE", "").strip().lower()
if _env_mode:
INFERENCE_MODE = _env_mode
os.environ["HF_XET_HIGH_PERFORMANCE"] = "1"
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
T0 = time.time()
def lap(msg): print(f"[{time.time()-T0:6.1f}s] {msg}", flush=True)
def sh(cmd, check=True, **kw): return subprocess.run(cmd, shell=isinstance(cmd, str), check=check, **kw)
assert torch.cuda.is_available(), "Selecciona una GPU en 'Entorno de ejecución > Cambiar tipo de entorno de ejecución'."
vram_gib = round(torch.cuda.get_device_properties(0).total_memory / 2**30, 1)
GPU_CAPABILITY = torch.cuda.get_device_capability(0)
CUDA_MAJOR = int((torch.version.cuda or "0").split(".", 1)[0])
print(f"🚀 GPU: {torch.cuda.get_device_name(0)} (CC {GPU_CAPABILITY[0]}.{GPU_CAPABILITY[1]}) | VRAM: {vram_gib} GiB | CUDA: {torch.version.cuda}")
if not (shutil.which("aria2c") and shutil.which("zstd")):
sh("apt-get install -y -qq aria2 zstd >/dev/null 2>&1", check=False)
COMFY = Path("/content/ComfyUI"); VENV = Path("/content/venv"); VPY = VENV / "bin" / "python"
CACHE = Path("/content/h3_cache"); CACHE.mkdir(exist_ok=True)
DRIVE_DIR = Path("/content/drive/MyDrive/MiniMax_H3_Colab")
if Path("/content/drive/MyDrive").exists():
DRIVE_DIR.mkdir(parents=True, exist_ok=True)
MODELS_ROOT = COMFY / "models"
for folder in ["checkpoints", "diffusion_models", "loras", "text_encoders", "vae", "controlnet", "model_patches"]:
(MODELS_ROOT / folder).mkdir(parents=True, exist_ok=True)
# ---------- 1) Selección de Modelos SOTA Benchmarkeados ----------
BASE_REPO = "Comfy-Org/MiniMax-H3"
HF = "https://huggingface.co/{}/resolve/main/{}"
TEXT_ENCODER = "qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors"
AUDIO_VAE = "minimax_h3_audio_vae_fp32.safetensors"
if VIDEO_VAE_MODE == "lynnreal_light_int8" and CUDA_MAJOR >= 13:
VIDEO_VAE = "minimax_h3_lynnreal_light_vae_int8_convrot.safetensors"
video_vae_url = HF.format("Kijai/MiniMax-H3-experimental", VIDEO_VAE)
elif CUDA_MAJOR >= 13 and GPU_CAPABILITY >= (8, 0):
VIDEO_VAE = "minimax_h3_video_vae_int8_convrot.safetensors"
video_vae_url = HF.format(BASE_REPO, "vae/" + VIDEO_VAE)
else:
VIDEO_VAE = "minimax_h3_video_vae_fp16.safetensors"
video_vae_url = HF.format(BASE_REPO, "vae/" + VIDEO_VAE)
if CUSTOM_MODEL == "minimax_h3_ref2va_pruned_w4a8_mixed":
DIFFUSION = "minimax_h3_ref2va_pruned_w4a8_mixed.safetensors"
diffusion_url = HF.format("Kijai/MiniMax-H3-experimental", DIFFUSION)
elif CUSTOM_MODEL != "None":
DIFFUSION = f"{CUSTOM_MODEL}.safetensors"
diffusion_url = HF.format("TenStrip/10Eros-Max", DIFFUSION)
else:
if INFERENCE_MODE == "reference":
variant = "int8_convrot" if (CUDA_MAJOR >= 13 and GPU_CAPABILITY >= (8, 0)) else ("fp8_scaled" if GPU_CAPABILITY >= (8, 9) else "bf16")
DIFFUSION = f"minimax_h3_ref2va_pruned_{variant}.safetensors"
else:
variant = "int8_convrot" if CUDA_MAJOR >= 13 else "fp8_scaled"
DIFFUSION = f"minimax_h3_fl2va_pruned_{variant}.safetensors"
diffusion_url = HF.format(BASE_REPO, "diffusion_models/" + DIFFUSION)
if TURBO_LORA_MODE == "pdmd_2step_ultrafast":
PRIMARY_TURBO_LORA = "minimax_h3_pdmd_2step_lora_avg_rank_38_bf16.safetensors"
turbo_lora_url = HF.format(HF_ASSETS_REPO, "loras/" + PRIMARY_TURBO_LORA)
elif TURBO_LORA_MODE == "full_bf16_4step":
PRIMARY_TURBO_LORA = "minimax_h3_ref2v_turbo_4step_v0.1_comfyui_bf16.safetensors"
turbo_lora_url = HF.format(BASE_REPO, "loras/" + PRIMARY_TURBO_LORA)
else:
PRIMARY_TURBO_LORA = "minimax_h3_ref2v_turbo_4step_v0.1_comfyui_resized_avg_rank_21_bf16.safetensors"
turbo_lora_url = HF.format(HF_ASSETS_REPO, "loras/" + PRIMARY_TURBO_LORA)
TIER0_JOBS = [
(HF.format(BASE_REPO, "text_encoders/" + TEXT_ENCODER), "text_encoders", TEXT_ENCODER),
(video_vae_url, "vae", VIDEO_VAE),
(HF.format(BASE_REPO, "vae/" + AUDIO_VAE), "vae", AUDIO_VAE),
(diffusion_url, "diffusion_models", DIFFUSION),
(turbo_lora_url, "loras", PRIMARY_TURBO_LORA),
]
QWEN_JOBS = []
if DOWNLOAD_QWEN_NSFW:
QWEN_JOBS = [
("https://huggingface.co/Phr00t/Qwen-Image-Edit-Rapid-AIO/resolve/main/v23/Qwen-Rapid-AIO-NSFW-v23.safetensors", "checkpoints", "Qwen-Rapid-AIO-NSFW-v23.safetensors"),
("https://huggingface.co/RunningHubAI/rh-wuji-qwen2511-aio-nsfw-2603-checkpoint/resolve/main/WuJi_Qwen2511-AIONSFW%E8%81%9A%E5%90%88%E7%89%88_2603.safetensors", "checkpoints", "WuJi_Qwen2511-AIONSFW.safetensors"),
("https://huggingface.co/Comfy-Org/Qwen-Image-Edit_ComfyUI/resolve/main/split_files/diffusion_models/qwen_image_edit_2511_int8_convrot.safetensors", "diffusion_models", "qwen_image_edit_2511_int8_convrot.safetensors"),
(HF.format(HF_ASSETS_REPO, "vae/qwen_image_vae.safetensors"), "vae", "qwen_image_vae.safetensors"),
(HF.format(HF_ASSETS_REPO, "text_encoders/qwen_2.5_vl_7b_fp8_scaled.safetensors"), "text_encoders", "qwen_2.5_vl_7b_fp8_scaled.safetensors"),
(HF.format(HF_ASSETS_REPO, "loras/Qwen-Image-Edit-2511-Lightning-4steps-V1.0-bf16.safetensors"), "loras", "Qwen-Image-Edit-2511-Lightning-4steps-V1.0-bf16.safetensors"),
("https://huggingface.co/fal/Qwen-Image-Edit-2511-Multiple-Angles-LoRA/resolve/main/qwen-image-edit-2511-multiple-angles-lora.safetensors", "loras", "qwen-image-edit-2511-multiple-angles-lora.safetensors"),
("https://huggingface.co/Rumcajs9000/SexGod_NSFW_Female_Nudes_QWEN_Image_Edit_2511/resolve/main/SEXGOD_FemaleNudity_QwenEdit_2511_v2.safetensors", "loras", "SEXGOD_FemaleNudity_QwenEdit_2511_v2.safetensors"),
("https://huggingface.co/prithivMLmods/Qwen-Image-Edit-2511-Midnight-Noir-Eyes-Spotlight/resolve/main/Qwen-Image-Edit-2511-Midnight-Noir-Eyes-Spotlight.safetensors", "loras", "Qwen-Image-Edit-2511-Midnight-Noir-Eyes-Spotlight.safetensors"),
("https://huggingface.co/MatheeshaNxtDoor/kaia/resolve/main/bfs_head_v5_2511_merged_version_rank_16_fp16.safetensors", "loras", "bfs_head_v5_2511_merged_version_rank_16_fp16.safetensors"),
("https://huggingface.co/metncelik/qwen-edit-2511-consistence-lora/resolve/main/qe2511_consis_alpha_patched.safetensors", "loras", "qe2511_consis_alpha_patched.safetensors"),
("https://huggingface.co/Cofiejunkie81/Au_naturale/resolve/main/SexGod_CouplesNudity_QwenEdit_2511_v1.safetensors", "loras", "SexGod_CouplesNudity_QwenEdit_2511_v1.safetensors"),
("https://huggingface.co/Gotlayeh/loras/resolve/main/Qwen_Image_Edit_2511_All_included_with_extra_gay_v2.0.safetensors", "loras", "Qwen_Image_Edit_2511_All_included_with_extra_gay_v2.0.safetensors"),
("https://huggingface.co/prithivMLmods/Qwen-Image-Edit-2511-Ultra-Realistic-Portrait/resolve/main/URP_20.safetensors", "loras", "URP_20.safetensors"),
("https://huggingface.co/mickmumpitz/QWEN-EDIT_360/resolve/main/251018_MICKMUMPITZ_QWEN-EDIT_360_03.safetensors", "loras", "251018_MICKMUMPITZ_QWEN-EDIT_360_03.safetensors"),
]
if not QWEN_IN_BACKGROUND:
TIER0_JOBS += QWEN_JOBS
def _valid_safetensors(p: Path) -> bool:
try:
if not p.exists() or Path(str(p) + ".aria2").exists() or p.stat().st_size < 1024:
return False
with open(p, "rb") as f:
hdr_len = struct.unpack("<Q", f.read(8))[0]
return 0 < hdr_len < 100_000_000 and f.read(1) == b"{"
except Exception:
return False
claimed_paths = set()
MODEL_TARGETS = []
tier0_lines = []
for url, folder, fname in TIER0_JOBS:
d = MODELS_ROOT / folder; d.mkdir(parents=True, exist_ok=True)
t = (d / fname).resolve()
if t in claimed_paths:
continue
claimed_paths.add(t)
MODEL_TARGETS.append(t)
if _valid_safetensors(t):
continue
t.unlink(missing_ok=True)
tier0_lines += [url, f" dir={d}", f" out={fname}"]
ARIA_LOG = Path("/content/aria2_models.log")
ARIA_INPUT = CACHE / "models_tier0.aria2"
ARIA_INPUT.write_text("\n".join(tier0_lines) + "\n", encoding="utf-8")
ARIA_CMD = [
"aria2c", "-i", str(ARIA_INPUT), "-j", "6", "-x", "16", "-s", "16", "-k", "1M", "-c",
"--file-allocation=none", "--auto-file-renaming=false", "--max-tries=10", "--retry-wait=3",
"--console-log-level=warn", "--summary-interval=0", "--disk-cache=128M"
]
ARIA_PROC = subprocess.Popen(ARIA_CMD, stdout=open(ARIA_LOG, "a"), stderr=subprocess.STDOUT) if tier0_lines else None
lap(f"⚡ Tier 0 (núcleo bloqueante): {len(tier0_lines)//3} archivos descargándose a máxima prioridad")
# ---------- 2) Tier 1 Asíncrono Deduplicado (87 LoRAs + Qwen en segundo plano sin colisiones) ----------
TIER1_JOBS = list(QWEN_JOBS) if (DOWNLOAD_QWEN_NSFW and QWEN_IN_BACKGROUND) else []
if COPY_MY_LORAS:
lora_manifest_url = f"https://huggingface.co/{HF_ASSETS_REPO}/resolve/main/loras_manifest.json"
try:
with urllib.request.urlopen(lora_manifest_url, timeout=10) as resp:
lora_items = json.loads(resp.read().decode("utf-8"))
for item in lora_items:
fname = item["name"]
encoded_name = urllib.parse.quote(fname)
TIER1_JOBS.append((f"https://huggingface.co/{HF_ASSETS_REPO}/resolve/main/loras/{encoded_name}", "loras", fname))
except Exception as e:
print(f"⚠️ Aviso consultando loras_manifest.json en HF: {e}")
tier1_lines = []
for url, folder, fname in TIER1_JOBS:
d = MODELS_ROOT / folder; d.mkdir(parents=True, exist_ok=True)
t = (d / fname).resolve()
if t in claimed_paths:
continue
claimed_paths.add(t)
if _valid_safetensors(t):
continue
tier1_lines += [url, f" dir={d}", f" out={fname}"]
ARIA_LORAS_PROC = None
if tier1_lines:
LORA_ARIA_INPUT = CACHE / "tier1_background.aria2"
LORA_ARIA_INPUT.write_text("\n".join(tier1_lines) + "\n", encoding="utf-8")
LORA_SYNC_LOG = Path("/content/lora_sync.log")
LORA_SYNC_LOG.write_text(f"[{time.strftime('%X')}] Descargando {len(tier1_lines)//3} archivos Tier 1 en background...\n", encoding="utf-8")
LORA_ARIA_CMD = [
"aria2c", "-i", str(LORA_ARIA_INPUT), "-j", "8", "-x", "16", "-s", "16", "-k", "1M", "-c",
"--file-allocation=none", "--auto-file-renaming=false", "--max-tries=5", "--retry-wait=2",
"--console-log-level=notice", "--summary-interval=5", "--disk-cache=64M"
]
ARIA_LORAS_PROC = subprocess.Popen(LORA_ARIA_CMD, stdout=open(LORA_SYNC_LOG, "a", encoding="utf-8"), stderr=subprocess.STDOUT)
lap(f"⚡ Tier 1 (background sin bloqueo): {len(tier1_lines)//3} modelos/LoRAs descargándose en segundo plano.")
else:
lap("✅ Todos los modelos y 87 LoRAs de Tier 1 ya están presentes en el NVMe.")
if Path("/content/drive/MyDrive").exists():
extra_yaml = f"""colab_drive:
base_path: {DRIVE_DIR}
loras: |
models/loras
loras
.
checkpoints: models/checkpoints
diffusion_models: models/diffusion_models
vae: models/vae
"""
(COMFY / "extra_model_paths.yaml").write_text(extra_yaml, encoding="utf-8")
# ---------- 3) Restauración Paralela ZSTD (Entorno Limpio + 84 Nodos + Cachés LoRA SHA256 y Triton Blackwell) ----------
def is_zstd(p):
try:
with open(p, "rb") as f: return f.read(4) == b"\x28\xb5\x2f\xfd"
except OSError:
return False
def fetch_archive(name):
for local_p in [Path("/content") / name, CACHE / name, DRIVE_DIR / name]:
if is_zstd(local_p) and not Path(str(local_p) + ".aria2").exists():
return local_p
dst = CACHE / name
hf_url = f"https://huggingface.co/{HF_ASSETS_REPO}/resolve/main/{urllib.parse.quote(name)}"
r = sh(["aria2c", "-x", "16", "-s", "16", "-k", "1M", "-c", "--file-allocation=none",
"--auto-file-renaming=false", "--console-log-level=warn", "--summary-interval=0",
"-d", str(CACHE), "-o", name, hf_url], check=False)
if r.returncode == 0 and is_zstd(dst):
return dst
dst.unlink(missing_ok=True)
return None
def extract(archive):
r = sh(["tar", "-I", "zstd -d -T0", "-xf", str(archive), "-C", "/"], check=False)
return r.returncode == 0
MARKER = VENV / ".h3_env_key"
def env_ok():
return VPY.exists() and (COMFY / "main.py").exists() and MARKER.exists()
def _setup_env():
if not REBUILD_ENV and env_ok():
lap("⚡ Entorno Python ya activo en NVMe.")
return True
if not REBUILD_ENV:
ea = fetch_archive(PREBUILT_ENV_ARCHIVE)
if ea and extract(ea):
MARKER.parent.mkdir(parents=True, exist_ok=True)
MARKER.write_text("py313_cu130_flash")
lap(f"⚡ Entorno limpio restaurado desde {ea.name}")
return True
return False
def _setup_nodes():
if not WITH_NODES:
return False
na = fetch_archive("custom_nodes.tar.zst")
if na and extract(na):
lap("✅ 84 custom_nodes optimizados extraídos en NVMe.")
return True
return False
def _setup_lora_meta():
ma = fetch_archive("loras_metadata.tar.zst")
return bool(ma and extract(ma))
def _setup_triton_cache():
if GPU_CAPABILITY[0] == 12 and CUDA_MAJOR >= 13:
ta = fetch_archive("triton_cache_cu130_blackwell.tar.zst")
return bool(ta and extract(ta))
return False
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:
fut_env = pool.submit(_setup_env)
fut_nodes = pool.submit(_setup_nodes)
fut_meta = pool.submit(_setup_lora_meta)
fut_triton = pool.submit(_setup_triton_cache)
env_ready = fut_env.result()
nodes_ready = fut_nodes.result()
meta_ready = fut_meta.result()
triton_ready = fut_triton.result()
if not env_ready or not env_ok():
lap("Construyendo entorno Python acelerado de respaldo con uv...")
sh([sys.executable, "-m", "pip", "install", "-q", "uv"])
shutil.rmtree(VENV, ignore_errors=True)
sh([sys.executable, "-m", "uv", "venv", "--system-site-packages", "--python", sys.executable, str(VENV)])
if not (COMFY / "main.py").exists():
tmp = Path("/content/_comfy_src"); shutil.rmtree(tmp, ignore_errors=True)
sh(["git", "clone", "--depth", "1", "https://github.com/Comfy-Org/ComfyUI.git", str(tmp)])
shutil.rmtree(tmp / ".git", ignore_errors=True)
COMFY.mkdir(exist_ok=True); sh(["cp", "-a", f"{tmp}/.", f"{COMFY}/"])
def clean_reqs(path):
lines = path.read_text(encoding="utf-8", errors="ignore").splitlines()
filtered = [l for l in lines if not re.match(r"^\s*(torch|torchvision|torchaudio|triton|transformers|diffusers|protobuf|opencv)([>=<~!\s-]|$)", l, re.I)]
dst = CACHE / f"filtered_{path.name}"
dst.write_text("\n".join(filtered) + "\n", encoding="utf-8")
return str(dst)
uv = [sys.executable, "-m", "uv", "pip", "install", "--python", str(VPY), "-q", "--index-strategy", "unsafe-best-match"]
sh(uv + ["-r", clean_reqs(COMFY / "requirements.txt"), "onnxruntime-gpu", "nvidia-ml-py", "av>=17.0.0", "onnx"])
MARKER.write_text("py313_cu130_uv_fallback")
(COMFY / "input").mkdir(exist_ok=True); (COMFY / "output").mkdir(exist_ok=True)
lap(f"✅ Setup completado (LoRA-Meta={'OK' if meta_ready else 'skip'}, Triton-Cache={'OK' if triton_ready else 'skip'}).")
# @title 2. Esperar modelos núcleo Tier 0 (aria2c)
def _done(): return all(_valid_safetensors(t) for t in MODEL_TARGETS)
for attempt in range(3):
if ARIA_PROC is not None:
while ARIA_PROC.poll() is None:
got = sum(t.stat().st_size for t in MODEL_TARGETS if t.exists())
loras_count = len(list((MODELS_ROOT / "loras").glob("*.safetensors")))
print(f"\r⬇️ Tier 0 Core: {got/2**30:6.2f} GiB | LoRAs en NVMe: {loras_count}...", end="", flush=True)
time.sleep(1.5)
print()
if _done():
break
print(f"Reanudando descarga Tier 0 intento {attempt+1}/3...")
ARIA_PROC = subprocess.Popen(ARIA_CMD, stdout=open(ARIA_LOG, "a"), stderr=subprocess.STDOUT)
assert _done(), "Descarga Tier 0 incompleta; revisa " + str(ARIA_LOG) + "\n" + ARIA_LOG.read_text()[-3000:]
for t in MODEL_TARGETS:
print(f"✅ {t.name}: {t.stat().st_size/2**30:.2f} GiB")
loras_ready = len(list((MODELS_ROOT / "loras").glob("*.safetensors")))
lap(f"🎉 Núcleo Tier 0 verificado ({loras_ready} LoRAs ya disponibles en NVMe).")
# @title 3. Iniciar ComfyUI + Túnel HTTP/2 Zero Trust + Caché RAM/CDN + Auto-Warmup GPU
import json, time, uuid, threading, requests, sys, subprocess, os, re, shutil
from pathlib import Path
import torch
ATTENTION_BACKEND = "ck_int8" # @param ["ck_int8", "sage"]
PREVIEW_METHOD = "none" # @param ["none", "latent2rgb", "auto", "taesd"]
TUNNEL_PROTOCOL = "http2" # @param ["http2", "quic"]
USE_ZERO_TRUST = True # @param {type:"boolean"}
WARMUP_ON_BOOT = True # @param {type:"boolean"}
KEEP_CELL_ALIVE = True # @param {type:"boolean"}
CLOUDFLARE_TOKEN = '' # @param {type:"string"}
if not CLOUDFLARE_TOKEN.strip():
CLOUDFLARE_TOKEN = os.environ.get("CLOUDFLARE_TOKEN", "").strip()
if not CLOUDFLARE_TOKEN.strip():
try:
from google.colab import userdata
CLOUDFLARE_TOKEN = (userdata.get("CLOUDFLARE_TOKEN") or "").strip()
except Exception:
pass
PORT = 8188
BASE_URL = f"http://127.0.0.1:{PORT}"
LOG_PATH = Path("/content/comfyui_h3.log")
SHM_TEMP = Path("/dev/shm/comfy_temp"); SHM_TEMP.mkdir(parents=True, exist_ok=True)
os.system("pkill -9 -f 'main.py --listen' 2>/dev/null; pkill -9 -f 'comfy_watchdog.py' 2>/dev/null; pkill -9 cloudflared 2>/dev/null")
time.sleep(1)
lap("Verificando parches FP8 TensorCore, RoPE y SageAttention...")
# 1) Parche FP8 dinámico (scale="recalculate") en comfy/ops.py
_ops_file = COMFY / "comfy" / "ops.py"
if _ops_file.exists():
_ops_txt = _ops_file.read_text(encoding="utf-8")
_old_fp8_block = (
" scale_weight = torch.ones((), device=input.device, dtype=torch.float32)\n\n"
" scale_input = torch.ones((), device=input.device, dtype=torch.float32)\n"
" input = torch.clamp(input, min=-448, max=448, out=input)\n"
" input_fp8 = input.to(dtype).contiguous()\n"
" layout_params_input = TensorCoreFP8Layout.Params(scale=scale_input, orig_dtype=input_dtype, orig_shape=tuple(input_fp8.shape))\n"
" quantized_input = QuantizedTensor(input_fp8, \"TensorCoreFP8Layout\", layout_params_input)"
)
_new_fp8_block = (
" scale_weight = getattr(self, \"scale_weight\", None)\n"
" if scale_weight is None:\n"
" scale_weight = torch.ones((), device=input.device, dtype=torch.float32)\n\n"
" quantized_input = QuantizedTensor.from_float(input, \"TensorCoreFP8E4M3Layout\", scale=\"recalculate\")"
)
if _old_fp8_block in _ops_txt:
_ops_file.write_text(_ops_txt.replace(_old_fp8_block, _new_fp8_block), encoding="utf-8")
# 2) Parche RoPE dtype en minimax/model.py
_mm_model = COMFY / "comfy" / "ldm" / "minimax" / "model.py"
if _mm_model.exists():
_mm_txt = _mm_model.read_text(encoding="utf-8")
_mm_patched = _mm_txt.replace(
"qw = comfy.model_management.cast_to(self.q_norm.weight, device=x.device)\n kw = comfy.model_management.cast_to(self.k_norm.weight, device=x.device)",
"qw = comfy.model_management.cast_to(self.q_norm.weight, dtype=q.dtype, device=x.device)\n kw = comfy.model_management.cast_to(self.k_norm.weight, dtype=k.dtype, device=x.device)"
)
if _mm_patched != _mm_txt:
_mm_model.write_text(_mm_patched, encoding="utf-8")
# 3) Ruedas precompiladas de sageattn3 y sageattention (fallback si se reconstruyó con uv)
if subprocess.run([str(VPY), "-c", "import sageattn3, sageattention; getattr(sageattention, 'sageattn_qk_int8_pv_fp8_cuda_sm90')"], capture_output=True).returncode != 0:
_whls = [
f"https://huggingface.co/{HF_ASSETS_REPO}/resolve/main/wheels/sageattn3-1.0.0-cp313-cp313-linux_x86_64.whl",
f"https://huggingface.co/{HF_ASSETS_REPO}/resolve/main/wheels/sageattention-2.2.0-py3-none-any.whl",
]
subprocess.run([str(VPY), "-m", "pip", "install", "-q", "--no-deps", "--force-reinstall"] + _whls, check=False)
# 3b) Parche RAM-Gzip + Cache-Control CDN/Browser en middleware/cache_middleware.py (elimina 'no-store' en /assets y /extensions)
_cache_mw = COMFY / "middleware" / "cache_middleware.py"
if _cache_mw.parent.exists():
_cache_mw.write_text('''"""Ultra-fast RAM-gzipped + Edge/Browser Cache-Control middleware for remote ComfyUI"""
import gzip, mimetypes, time
from pathlib import Path
from typing import Callable, Awaitable, Dict, Tuple
from aiohttp import web
ONE_HOUR, ONE_DAY, ONE_YEAR = 3600, 86400, 31536000
IMG_EXTENSIONS = (".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp", ".ico")
COMPRESSIBLE_EXTS = (".js", ".mjs", ".css", ".svg", ".json", ".html", ".txt", ".xml")
FONT_EXTS = (".woff2", ".woff", ".ttf", ".otf", ".eot")
MIME_MAP = {
".js": "application/javascript; charset=utf-8", ".mjs": "application/javascript; charset=utf-8",
".css": "text/css; charset=utf-8", ".html": "text/html; charset=utf-8",
".json": "application/json; charset=utf-8", ".svg": "image/svg+xml",
".woff2": "font/woff2", ".woff": "font/woff", ".ttf": "font/ttf", ".otf": "font/otf", ".ico": "image/x-icon",
}
_STATIC_CACHE: Dict[str, Tuple[int, int, bytes, bytes, str, str]] = {}
_OBJ_INFO_CACHE: Dict[str, Tuple[float, bytes, str, str]] = {}
def _cache_policy_for_path(req_path: str) -> str:
low = req_path.lower()
fname = low.rsplit("/", 1)[-1]
if req_path == "/" or fname == "index.html" or (fname.startswith("index") and fname.endswith(".json")) or fname.startswith("manifest-"):
return "no-cache"
if req_path.startswith("/assets/") or low.endswith(FONT_EXTS):
return f"public, max-age={ONE_YEAR}, immutable"
if req_path.startswith(("/extensions/", "/scripts/", "/lib/")) or low.endswith((".js", ".mjs", ".css", ".svg")):
return f"public, max-age={ONE_DAY}, stale-while-revalidate={ONE_DAY * 7}"
return ""
@web.middleware
async def cache_control(request: web.Request, handler: Callable[[web.Request], Awaitable[web.Response]]) -> web.Response:
req_path = request.path
if request.method == "GET" and req_path in ("/object_info", "/api/object_info") and not request.query_string:
want_gz = "gzip" in request.headers.get("Accept-Encoding", "")
ckey = "gz" if want_gz else "raw"
cached_oi = _OBJ_INFO_CACHE.get(ckey)
now = time.monotonic()
if cached_oi and (now - cached_oi[0]) < 20.0:
_, body_b, ctype, enc = cached_oi
hdrs = {"Content-Type": ctype, "Cache-Control": "private, max-age=15", "Vary": "Accept-Encoding"}
if enc: hdrs["Content-Encoding"] = enc
return web.Response(body=body_b, status=200, headers=hdrs)
resp = await handler(request)
if isinstance(resp, web.Response) and resp.status == 200 and resp.body is not None:
raw_b = bytes(resp.body)
enc = resp.headers.get("Content-Encoding", "")
ctype = resp.headers.get("Content-Type", "application/json; charset=utf-8")
if want_gz and not enc and len(raw_b) > 1024:
raw_b = gzip.compress(raw_b, compresslevel=6); enc = "gzip"
_OBJ_INFO_CACHE[ckey] = (now, raw_b, ctype, enc)
hdrs = dict(resp.headers); hdrs.pop("Transfer-Encoding", None); hdrs.pop("Content-Length", None)
hdrs["Cache-Control"] = "private, max-age=15"; hdrs["Vary"] = "Accept-Encoding"
if enc: hdrs["Content-Encoding"] = enc
return web.Response(body=raw_b, status=200, headers=hdrs)
return resp
response: web.Response = await handler(request)
if req_path.startswith(("/view", "/api/view", "/userdata", "/api/userdata")):
return response
policy = _cache_policy_for_path(req_path)
if request.method == "GET" and isinstance(response, web.FileResponse) and hasattr(response, "_path"):
try:
fpath = Path(response._path); ext = fpath.suffix.lower()
if ext in COMPRESSIBLE_EXTS or ext in FONT_EXTS:
st = fpath.stat()
if st.st_size <= 25 * 1024 * 1024:
key = str(fpath); entry = _STATIC_CACHE.get(key)
if entry is None or entry[0] != st.st_mtime_ns or entry[1] != st.st_size:
raw_b = fpath.read_bytes()
gz_b = gzip.compress(raw_b, compresslevel=6) if (ext in COMPRESSIBLE_EXTS and len(raw_b) > 256) else raw_b
ctype = MIME_MAP.get(ext) or mimetypes.guess_type(key)[0] or "application/octet-stream"
etag = f'W/"{st.st_mtime_ns:x}-{st.st_size:x}"'
if len(_STATIC_CACHE) > 2500: _STATIC_CACHE.clear()
entry = (st.st_mtime_ns, st.st_size, raw_b, gz_b, ctype, etag); _STATIC_CACHE[key] = entry
_, _, raw_b, gz_b, ctype, etag = entry
hdrs = {"ETag": etag, "Cache-Control": policy or f"public, max-age={ONE_DAY}", "Vary": "Accept-Encoding"}
for hk in ("Access-Control-Allow-Origin", "Access-Control-Allow-Methods", "Access-Control-Allow-Headers"):
if hk in response.headers: hdrs[hk] = response.headers[hk]
inm = request.headers.get("If-None-Match")
if inm and etag in inm:
return web.Response(status=304, headers=hdrs)
hdrs["Content-Type"] = ctype
if "gzip" in request.headers.get("Accept-Encoding", "") and gz_b is not raw_b and len(gz_b) < len(raw_b):
hdrs["Content-Encoding"] = "gzip"
return web.Response(body=gz_b, status=200, headers=hdrs)
return web.Response(body=raw_b, status=200, headers=hdrs)
except OSError:
pass
if policy:
response.headers["Cache-Control"] = policy
return response
if not req_path.lower().endswith(IMG_EXTENSIONS):
return response
if response.status == 404:
response.headers.setdefault("Cache-Control", f"public, max-age={ONE_HOUR}")
elif response.status in (200, 201, 202, 203, 204, 205, 206, 301, 308):
response.headers.setdefault("Cache-Control", f"public, max-age={ONE_DAY}")
elif response.status in (302, 303, 307):
response.headers.setdefault("Cache-Control", "no-cache")
return response
''', encoding="utf-8")
help_txt = subprocess.run([str(VPY), "main.py", "--help"], cwd=COMFY, capture_output=True, text=True).stdout
launch = [
str(VPY), "-u", "main.py",
"--listen", "127.0.0.1",
"--port", str(PORT),
"--disable-auto-launch",
]
for flag in ("--fast", "--fast-disk", "--dont-upcast-attention", "--supports-fp8-compute",
"--enable-triton-backend", "--enable-cors-header", "--enable-compress-response-body",
"--disable-partner-nodes"):
if flag in help_txt:
launch.append(flag)
attn_cli = "--use-sage-attention" if ATTENTION_BACKEND == "sage" else "--use-ck-attention"
if attn_cli in help_txt:
launch.append(attn_cli)
if "--preview-method" in help_txt:
launch += ["--preview-method", PREVIEW_METHOD]
if "--temp-directory" in help_txt:
launch += ["--temp-directory", str(SHM_TEMP)]
if "--default-hashing-function" in help_txt:
launch += ["--default-hashing-function", "md5"]
vram = torch.cuda.get_device_properties(0).total_memory
if vram < 16 * 2**30:
launch.append("--lowvram")
elif vram >= 24 * 2**30 and "--highvram" in help_txt:
launch.append("--highvram")
if "--reserve-vram" in help_txt:
launch += ["--reserve-vram", "2.0"]
if "--cache-lru" in help_txt:
launch += ["--cache-lru", "32"]
env = dict(
os.environ,
PYTHONUNBUFFERED="1",
HF_XET_HIGH_PERFORMANCE="1",
HF_HUB_ENABLE_HF_TRANSFER="1",
PYTORCH_CUDA_ALLOC_CONF="expandable_segments:True",
)
lap(f"Lanzando ComfyUI optimizado ({ATTENTION_BACKEND})...")
LOG_PATH.write_text("")
COMFY_PROCESS = subprocess.Popen(launch, cwd=COMFY, env=env, stdout=open(LOG_PATH, "a"), stderr=subprocess.STDOUT, start_new_session=True)
for _ in range(1200):
if COMFY_PROCESS.poll() is not None:
log_content = LOG_PATH.read_text(errors="ignore")
raise RuntimeError("ComfyUI falló al iniciar:\n" + log_content[-2000:])
try:
requests.get(BASE_URL + "/system_stats", timeout=3).raise_for_status()
break
except requests.RequestException:
time.sleep(0.4)
else:
raise TimeoutError(f"ComfyUI no respondió a tiempo; revisa {LOG_PATH}")
lap("🔥 ¡ComfyUI activo en modo HIGH_VRAM + fast_disk=True!")
# 4) Túnel Cloudflare QUIC + TCP BBR
os.system("sysctl -w net.core.default_qdisc=fq net.ipv4.tcp_congestion_control=bbr net.core.rmem_max=33554432 net.core.wmem_max=33554432 >/dev/null 2>&1")
CF_BIN = Path("/usr/local/bin/cloudflared")
if not CF_BIN.exists():
sh(["aria2c", "-q", "-x", "8", "-s", "8", "-d", "/usr/local/bin", "-o", "cloudflared",
"https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64"])
CF_BIN.chmod(0o755)
CF_LOG = Path("/content/cloudflared.log"); CF_LOG.write_text("")
CF_ZT_LOG = Path("/content/cloudflared_zt.log"); CF_ZT_LOG.write_text("")
cf_base = [str(CF_BIN), "tunnel", "--no-autoupdate", "--protocol", TUNNEL_PROTOCOL, "--edge-ip-version", "auto"]
if USE_ZERO_TRUST and CLOUDFLARE_TOKEN.strip():
cf_zt = cf_base + ["run", "--token", CLOUDFLARE_TOKEN.strip()]
subprocess.Popen(cf_zt, stdout=open(CF_ZT_LOG, "a"), stderr=subprocess.STDOUT, start_new_session=True)
print(f"🌐 Túnel Zero Trust ({TUNNEL_PROTOCOL.upper()}) activo: https://colab-comfy.ntic.cl")
Path("/content/comfy_url.txt").write_text("https://colab-comfy.ntic.cl")
cf_quick = cf_base + ["--url", BASE_URL]
subprocess.Popen(cf_quick, stdout=open(CF_LOG, "a"), stderr=subprocess.STDOUT, start_new_session=True)
public_url = None
for _ in range(40):
m = re.search(r"https://[a-z0-9-]+\.trycloudflare\.com", CF_LOG.read_text(errors="ignore"))
if m:
public_url = m.group(0)
print("\n=======================================================")
if USE_ZERO_TRUST and CLOUDFLARE_TOKEN.strip():
print("🚀 ENLACE PRINCIPAL ZERO TRUST: https://colab-comfy.ntic.cl")
print(f"🚀 ENLACE RESPALDO TRYCLOUDFLARE: {public_url}")
print("=======================================================\n")
if not (USE_ZERO_TRUST and CLOUDFLARE_TOKEN.strip()):
Path("/content/comfy_url.txt").write_text(public_url)
break
time.sleep(0.4)
# 5) Auto-Warmup asíncrono en GPU (pre-carga los 37.6 GiB en VRAM para que la 1ª petición web tarde ~3.6s en vez de 14s)
def _bg_gpu_warmup():
try:
from PIL import Image
w_img = COMFY / "input" / "_warmup_ref.png"
if not w_img.exists():
Image.new("RGB", (256, 256), (64, 72, 96)).save(w_img)
if INFERENCE_MODE != "reference":
return
g = {
"1": {"class_type": "UNETLoader", "inputs": {"unet_name": DIFFUSION, "weight_dtype": "default"}},
"2": {"class_type": "LoraLoaderModelOnly", "inputs": {"model": ["1", 0], "lora_name": PRIMARY_TURBO_LORA, "strength_model": 1.0}},
"3": {"class_type": "CLIPLoader", "inputs": {"clip_name": TEXT_ENCODER, "type": "minimax", "device": "default"}},
"4": {"class_type": "VAELoader", "inputs": {"vae_name": VIDEO_VAE}},
"5": {"class_type": "VAELoader", "inputs": {"vae_name": AUDIO_VAE}},
"6": {"class_type": "LoadImage", "inputs": {"image": "_warmup_ref.png"}},
"7": {"class_type": "MiniMaxH3ReferenceToVideo", "inputs": {"clip": ["3", 0], "prompt": "Warmup <Picture 1>.", "width": 256, "height": 256, "length": 5, "ref_image_size": "match", "vae": ["4", 0], "audio_vae": ["5", 0], "ref_images.ref_image_1": ["6", 0]}},
"8": {"class_type": "BasicGuider", "inputs": {"model": ["2", 0], "conditioning": ["7", 0]}},
"9": {"class_type": "RandomNoise", "inputs": {"noise_seed": 1}},
"10": {"class_type": "KSamplerSelect", "inputs": {"sampler_name": "euler"}},
"11": {"class_type": "BasicScheduler", "inputs": {"scheduler": "beta", "steps": 1, "denoise": 1.0, "model": ["2", 0]}},
"12": {"class_type": "SamplerCustomAdvanced", "inputs": {"noise": ["9", 0], "guider": ["8", 0], "sampler": ["10", 0], "sigmas": ["11", 0], "latent_image": ["7", 1]}},
"13": {"class_type": "VAEDecode", "inputs": {"samples": ["12", 0], "vae": ["4", 0]}},
"14": {"class_type": "PreviewImage", "inputs": {"images": ["13", 0]}},
}
requests.post(BASE_URL + "/prompt", json={"prompt": g, "client_id": "boot-warmup"}, timeout=10)
except Exception as _we:
print("Warmup aviso:", _we)
if WARMUP_ON_BOOT and vram >= 40 * 2**30:
threading.Thread(target=_bg_gpu_warmup, daemon=True).start()
lap("⚡ Auto-Warmup GPU disparado en background (anclando UNET + Qwen3VL-32B + VAEs en VRAM).")
def _bg_http_ram_prewarm():
try:
requests.get(BASE_URL + "/api/object_info", headers={"Accept-Encoding": "gzip"}, timeout=20)
exts = requests.get(BASE_URL + "/extensions", headers={"Accept-Encoding": "gzip"}, timeout=10).json()
for ep in exts:
requests.get(BASE_URL + ep, headers={"Accept-Encoding": "gzip"}, timeout=5)
except Exception:
pass
threading.Thread(target=_bg_http_ram_prewarm, daemon=True).start()
# 6) Demonio Watchdog Anti-Caídas + Keep-Alive GPU (/content/comfy_watchdog.py)
watchdog_code = f'''import os, time, subprocess, urllib.request
import torch
LAUNCH_CMD = {launch!r}
CF_ZT_CMD = {[str(CF_BIN), "tunnel", "--no-autoupdate", "--protocol", TUNNEL_PROTOCOL, "--edge-ip-version", "auto", "run", "--token", CLOUDFLARE_TOKEN.strip()]!r}
USE_ZT = {bool(USE_ZERO_TRUST and CLOUDFLARE_TOKEN.strip())!r}
COMFY_DIR = "/content/ComfyUI"
LOG_PATH = "/content/comfyui_h3.log"
CF_ZT_LOG = "/content/cloudflared_zt.log"
def is_comfy_up():
try:
urllib.request.urlopen("http://127.0.0.1:8188/system_stats", timeout=5)
return True
except Exception:
return False
def is_proc_running(pattern):
return subprocess.run(["pgrep", "-f", pattern], capture_output=True).returncode == 0
last_gpu_pulse = 0
fail_count = 0
while True:
try:
if not is_comfy_up():
fail_count += 1
if not is_proc_running("[m]ain.py --listen") or fail_count >= 6:
os.system("pkill -9 -f '[m]ain.py --listen' 2>/dev/null")
time.sleep(1)
env = dict(os.environ, PYTHONUNBUFFERED="1", HF_XET_HIGH_PERFORMANCE="1", PYTORCH_CUDA_ALLOC_CONF="expandable_segments:True")
subprocess.Popen(LAUNCH_CMD, cwd=COMFY_DIR, env=env, stdout=open(LOG_PATH, "a"), stderr=subprocess.STDOUT, start_new_session=True)
fail_count = 0
time.sleep(15)
else:
fail_count = 0
if USE_ZT and not is_proc_running("cloudflared.*--token"):
subprocess.Popen(CF_ZT_CMD, stdout=open(CF_ZT_LOG, "a"), stderr=subprocess.STDOUT, start_new_session=True)
now = time.time()
if now - last_gpu_pulse > 40 and torch.cuda.is_available():
_x = torch.ones((64, 64), device="cuda", dtype=torch.float16)
_y = _x @ _x
del _x, _y
last_gpu_pulse = now
except Exception as e:
print("[Watchdog] Error:", e, flush=True)
time.sleep(8)
'''
Path("/content/comfy_watchdog.py").write_text(watchdog_code, encoding="utf-8")
subprocess.Popen([str(VPY), "-u", "/content/comfy_watchdog.py"], stdout=open("/content/watchdog.log", "a"), stderr=subprocess.STDOUT, start_new_session=True)
lap("🛡️ Watchdog Anti-Caídas y GPU Keep-Alive activo (/content/comfy_watchdog.py).")
lap("🎉 ¡Servidor listo para inferencia remota de ultra-baja latencia!")
if KEEP_CELL_ALIVE and ("ipykernel" in sys.modules) and os.environ.get("COLAB_BACKGROUND_BOOTSTRAP") != "1":
print("🔒 Modo Anti-Desconexión Activo en celda...")
last_pos = LOG_PATH.stat().st_size if LOG_PATH.exists() else 0
try:
while True:
if LOG_PATH.exists():
with open(LOG_PATH, "r", encoding="utf-8", errors="ignore") as _f:
_f.seek(0, 2)
curr_pos = _f.tell()
if curr_pos < last_pos:
last_pos = 0
if curr_pos > last_pos:
_f.seek(last_pos)
print(_f.read(), end="", flush=True)
last_pos = curr_pos
time.sleep(2.0)
except KeyboardInterrupt:
print("\nℹ️ Streaming pausado (ComfyUI + Watchdog siguen activos en background).")