#!/usr/bin/env python3 """manju T2I:内网 MiniMax H3-Image 文生图提交器。 零第三方依赖。读 config/pipeline.json 的 imageModel,输出 PNG 到 work//images/.png —— 正是 tools/submit_i2v.py 期望的首帧路径。 服务端明文 HTTP,必须绕过本机代理(见 references/models.md §3.2)。 用法: python3 tools/submit_t2i.py --ep EP01 --shot S01 --prompt-file 40_prompts/S01.t2i.txt python3 tools/submit_t2i.py --prompt "A lone figure on a misty pier, ..." --out assets/characters/ling.png python3 tools/submit_t2i.py --ep EP01 --shot S03 --width 4096 --height 4096 --force 约定:给 --ep/--shot 时,未给 --prompt-file 则读 projects//40_prompts/.t2i.txt(出图提示词,与 .i2v.txt 运动提示词分开留档)。 ★ 权重自适应(2026-11-13) ------------------------ 不同端点的 H3-Image 量化权重名不同,写死必然踩坑: DCU 192.168.1.24:30120 -> minimax_h3_image_pruned_w4a8.safetensors K8s 192.168.1.30:30113 -> minimax_h3_image_pruned_int8_convrot.safetensors 所以本脚本**不写死权重名**:提交前读目标端点的 /object_info/, 按优先级挑一个真正存在的(见 UNET_PRIORITY)。端点换权重、或将来新增第三个 端点,不用改代码就能跑。 ⚠️ 两条线的坑完全不同,别互相照搬: - DCU(ROCm gfx928):**不要用 int8_convrot** —— convrot 的 rint 有 bug → NaN → 纯黑图。 - K8s(CUDA sm_75):**没有 w4a8**(w4a8 的 CUDA 加速路径要求 sm_80),只有 int8_convrot。 该端点跑在 4Gi cgroup 硬限里,靠 22 处内存墙补丁 + 显存换入换出才跑得动, 详见 references/models.md §2.1。 """ import argparse import hashlib import json import os import sys import time import uuid import urllib.parse import urllib.request DEFAULTS = { "endpoint": "http://192.168.1.30:30113", "width": 2048, "height": 2048, "steps": 6, "seed": 42, "prefix": "H3IMG", "timeout": 3600, } # 权重候选,按优先级。实际用哪个由目标端点 /object_info 决定。 UNET_PRIORITY = [ "minimax_h3_image_pruned_w4a8.safetensors", # DCU 30120 "minimax_h3_image_pruned_int8_convrot.safetensors", # K8s 30113 ] CLIP_PRIORITY = [ "qwen3vl_32b_minimax_h3_nvfp4_awq.safetensors", ] VAE_PRIORITY = [ "minimax_h3_image_vae_fp16.safetensors", ] # 本机代理会把明文 HTTP 响应改写成 text/html,必须显式绕过 opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) def pick_weight(host, node_type, priority): """从端点的 /object_info/ 里挑一个真实存在的权重名。 端点换权重不用改代码 —— 这是本脚本能在 DCU / K8s 两套基础设施上通吃的关键。 探不到就回退到 priority[0],让服务端报 node_errors(比静默猜错好)。 """ try: d = get(host, "/object_info/" + node_type, timeout=20) node = d.get(node_type) or {} req = ((node.get("input") or {}).get("required") or {}) available = [] for key in ("unet_name", "clip_name", "vae_name"): v = req.get(key) if isinstance(v, list) and v and isinstance(v[0], list): available = list(v[0]) break for name in priority: if name in available: return name # 端点上有别的 H3-Image 权重(将来新增档位)—— 认前缀兜底 for name in available: if name.startswith("minimax_h3_image"): return name return priority[0] except Exception: return priority[0] def find_root(): """工作空间根:MANJU_ROOT > cwd 向上找 config/pipeline.json > 脚本上级。""" env = os.environ.get("MANJU_ROOT") if env: return os.path.abspath(os.path.expanduser(env)) d = os.getcwd() while True: if os.path.isfile(os.path.join(d, "config", "pipeline.json")): return d nd = os.path.dirname(d) if nd == d: break d = nd return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def load_cfg(root): cfg = dict(DEFAULTS) p = os.path.join(root, "config", "pipeline.json") if os.path.isfile(p): cfg.update(json.load(open(p, encoding="utf-8")).get("imageModel", {})) return cfg def get(host, path, timeout=60): return json.loads(opener.open(host + path, timeout=timeout).read()) def post(host, path, payload, timeout=60): req = urllib.request.Request(host + path, data=json.dumps(payload).encode(), headers={"Content-Type": "application/json"}) return json.loads(opener.open(req, timeout=timeout).read()) def build(prompt, width, height, steps, seed, prefix, unet, clip, vae): """实机 /object_info 核对:width/height step=32;vae、images 为 optional。 unet/clip/vae 由 pick_weight() 从目标端点探出来,不写死。 """ return { "1": {"class_type": "UNETLoader", "inputs": {"unet_name": unet, "weight_dtype": "default"}}, "2": {"class_type": "CLIPLoader", "inputs": {"clip_name": clip, "type": "minimax", "device": "default"}}, "3": {"class_type": "VAELoader", "inputs": {"vae_name": vae}}, "4": {"class_type": "MiniMaxH3ImageConditioning", "inputs": {"clip": ["2", 0], "vae": ["3", 0], "prompt": prompt, "width": width, "height": height}}, "5": {"class_type": "ConditioningZeroOut", "inputs": {"conditioning": ["4", 0]}}, "6": {"class_type": "KSampler", "inputs": {"model": ["1", 0], "seed": seed, "steps": steps, "cfg": 1.0, "sampler_name": "euler", "scheduler": "simple", "positive": ["4", 0], "negative": ["5", 0], "latent_image": ["4", 1], "denoise": 1.0}}, "7": {"class_type": "VAEDecode", "inputs": {"samples": ["6", 0], "vae": ["3", 0]}}, "8": {"class_type": "SaveImage", "inputs": {"images": ["7", 0], "filename_prefix": prefix}}, } def wait(host, pid, timeout): t0 = time.time() gaps = 0 while True: try: d = get(host, "/history/" + urllib.parse.quote(pid), timeout=30) except Exception as e: gaps += 1 print(" ...服务暂不可达 (%d): %s" % (gaps, str(e)[:90]), flush=True) if gaps >= 8 or time.time() - t0 > timeout: raise TimeoutError("等待出图超时或服务重启") time.sleep(8) continue gaps = 0 item = d.get(pid) if item: st = item.get("status", {}) errs = [m for m in st.get("messages", []) if m[0] == "execution_error"] if errs: raise RuntimeError("渲染失败: " + json.dumps(errs, ensure_ascii=False)[:2000]) if st.get("completed") and item.get("outputs"): return item if time.time() - t0 > timeout: raise TimeoutError("等待出图超时 %s 秒" % timeout) time.sleep(5) def find_image(item): for out in (item.get("outputs") or {}).values(): for f in out.get("images", []) or []: if isinstance(f, dict) and f.get("filename"): return f raise RuntimeError("未找到图片输出: " + json.dumps(item.get("outputs"), ensure_ascii=False)[:1200]) def download(host, f, out): q = urllib.parse.urlencode({"filename": f["filename"], "subfolder": f.get("subfolder", ""), "type": f.get("type", "output")}) raw = opener.open(host + "/view?" + q, timeout=300).read() os.makedirs(os.path.dirname(os.path.abspath(out)) or ".", exist_ok=True) with open(out, "wb") as fh: fh.write(raw) return len(raw) def sanity_check(path): """出图自检:全黑 / 全白 / 单色 一律判失败。 为什么必须有这一步(2026-11-13 实测踩过): H3-Image 在量化档位不对时会**出图成功但内容是纯黑/单色** —— ComfyUI 侧 status.completed=True、outputs 有文件,不报错。 不自检就会把废图当成成品记进台账,后面审片才发现。 只用标准库读 PNG:解 IHDR 拿尺寸,采样像素算颜色数/亮度/标准差。 """ try: with open(path, "rb") as fh: data = fh.read() if data[:8] != b"\x89PNG\r\n\x1a\n": return "not-png" # PNG IHDR: 8 字节签名 + 4 长度 + 4 类型 + 8 宽高 w = int.from_bytes(data[16:20], "big") h = int.from_bytes(data[20:24], "big") # 解 IDAT(PNG 用 zlib deflate) import zlib idat = b"" i = 8 while i < len(data) - 8: ln = int.from_bytes(data[i:i + 4], "big") typ = data[i + 4:i + 8] if typ == b"IDAT": idat += data[i + 8:i + 8 + ln] elif typ == b"IEND": break i += 12 + ln if not idat: return "no-idat" raw = zlib.decompress(idat) # 假设 8bit RGB/RGBA,逐行反滤波(filter 0..4) bpp = 4 if len(raw) >= h * (1 + w * 4) else 3 stride = w * bpp prev = bytearray(stride) colors = set() bright_sum = 0.0 bright_sq = 0.0 cnt = 0 pos = 0 for _y in range(h): ft = raw[pos] pos += 1 line = bytearray(raw[pos:pos + stride]) pos += stride if ft == 1: for x in range(bpp, stride): line[x] = (line[x] + line[x - bpp]) & 0xFF elif ft == 2: for x in range(stride): line[x] = (line[x] + prev[x]) & 0xFF elif ft == 3: for x in range(stride): a = line[x - bpp] if x >= bpp else 0 line[x] = (line[x] + ((a + prev[x]) >> 1)) & 0xFF elif ft == 4: for x in range(stride): a = line[x - bpp] if x >= bpp else 0 c = prev[x - bpp] if x >= bpp else 0 b = prev[x] p = a + b - c pa, pb, pc = abs(p - a), abs(p - b), abs(p - c) pr = a if (pa <= pb and pa <= pc) else (b if pb <= pc else c) line[x] = (line[x] + pr) & 0xFF prev = line # 采样:每 8 行取一行,避免大图太慢 for x in range(0, stride, bpp * 7): px = (line[x], line[x + 1], line[x + 2]) colors.add(px) v = (px[0] * 299 + px[1] * 587 + px[2] * 114) / 1000.0 bright_sum += v bright_sq += v * v cnt += 1 if cnt == 0: return "empty" mean = bright_sum / cnt std = (max(0.0, bright_sq / cnt - mean * mean)) ** 0.5 # 判定:颜色数极少 或 标准差极小 → 近乎纯色(黑/白/单色) if len(colors) <= 8: return "single-color(%d colors)" % len(colors) if std < 3.0: return "flat(std=%.2f, %d colors)" % (std, len(colors)) return "ok(%dx%d, %d colors, mean=%.1f, std=%.1f)" % (w, h, len(colors), mean, std) except Exception as e: return "check-failed(%s)" % str(e)[:60] def main(): ap = argparse.ArgumentParser() ap.add_argument("--ep", default="", help="集号,如 EP01") ap.add_argument("--shot", default="", help="镜号,如 S01") ap.add_argument("--prompt", default="") ap.add_argument("--prompt-file", default="") ap.add_argument("--out", default="", help="输出 PNG;默认 work//images/.png") ap.add_argument("--width", type=int, default=0) ap.add_argument("--height", type=int, default=0) ap.add_argument("--steps", type=int, default=0) ap.add_argument("--seed", type=int, default=-1) ap.add_argument("--timeout", type=int, default=0) ap.add_argument("--force", action="store_true", help="已存在也重跑") ap.add_argument("--cleanup", action="store_true", help="导出后清理 ComfyUI 的 history/queue 记录与产物(等价于 cleanup.auto=true)") ap.add_argument("--no-cleanup", action="store_true", help="本次强制不清理,覆盖 config 里的 cleanup.auto") a = ap.parse_args() root = find_root() cfg = load_cfg(root) host = str(cfg["endpoint"]).rstrip("/") full = json.load(open(os.path.join(root, "config", "pipeline.json"), encoding="utf-8")) \ if os.path.isfile(os.path.join(root, "config", "pipeline.json")) else {} do_cleanup = (a.cleanup or full.get("cleanup", {}).get("auto", False)) and not a.no_cleanup pf = a.prompt_file if not pf and a.ep and a.shot and not a.prompt: pf = os.path.join(root, "projects", a.ep, "40_prompts", a.shot + ".t2i.txt") prompt = open(pf, encoding="utf-8").read().strip() if pf else a.prompt.strip() if not prompt: return "需要 --prompt / --prompt-file,或备好 projects//40_prompts/.t2i.txt" width = a.width or int(cfg["width"]) height = a.height or int(cfg["height"]) steps = a.steps or int(cfg["steps"]) seed = a.seed if a.seed >= 0 else int(cfg["seed"]) timeout = a.timeout or int(cfg["timeout"]) if width % 32 or height % 32: return "宽高必须 32 对齐(当前 %dx%d)" % (width, height) if a.out: out = a.out elif a.ep and a.shot: out = os.path.join(root, "work", a.ep, "images", a.shot + ".png") else: out = os.path.join(root, "h3_image_out.png") h8 = hashlib.sha256(prompt.encode("utf-8")).hexdigest()[:8] if os.path.exists(out) and os.path.getsize(out) > 100000 and not a.force: print("SKIP 已存在 %s(--force 可重跑)" % out, flush=True) return 0 sysinfo = get(host, "/system_stats", timeout=20).get("system", {}) unet = pick_weight(host, "UNETLoader", UNET_PRIORITY) clip = pick_weight(host, "CLIPLoader", CLIP_PRIORITY) vae = pick_weight(host, "VAELoader", VAE_PRIORITY) print("ComfyUI %s @ %s | %dx%d %d步 seed=%d | promptHash=%s" % (sysinfo.get("comfyui_version", "?"), host, width, height, steps, seed, h8), flush=True) print("weights unet=%s clip=%s vae=%s" % (unet, clip, vae), flush=True) graph = build(prompt, width, height, steps, seed, str(cfg["prefix"]), unet, clip, vae) t0 = time.time() # client_id 会被 ComfyUI 持久化在 history 项 prompt[3],是清理时的归属凭证 cid = "manju-t2i-" + uuid.uuid4().hex[:8] r = post(host, "/prompt", {"prompt": graph, "client_id": cid}) if r.get("node_errors"): raise RuntimeError("节点错误: " + json.dumps(r["node_errors"], ensure_ascii=False)[:3000]) pid = r["prompt_id"] print("[submit] prompt_id=%s -> %s" % (pid, out), flush=True) item = wait(host, pid, timeout) size = download(host, find_image(item), out) verdict = sanity_check(out) bad = not verdict.startswith("ok") if bad: # 废图(纯黑/单色)照样落地,但要显式报警 —— 不能让「completed」等于「可用」 print("[SANITY] FAIL %s -> %s" % (out, verdict), file=sys.stderr, flush=True) print("[SANITY] 出图成功但内容异常。H3-Image 在量化档位不对时会静默出纯色图:" "DCU 禁用 int8_convrot;K8s(30113) 只有 int8_convrot 可用。" "先看 models.md §2.1 的量化档位表,别急着重跑。", file=sys.stderr, flush=True) else: print("[SANITY] %s -> %s" % (out, verdict), flush=True) log_path = os.path.join(os.path.dirname(os.path.abspath(out)), "t2i_runs.json") runs = json.load(open(log_path, encoding="utf-8")) if os.path.exists(log_path) else {} key = (a.ep + "/" + a.shot) if (a.ep and a.shot) else os.path.basename(out) runs[key] = {"out": os.path.relpath(out, root), "promptHash": h8, "prompt": prompt, "model": unet, "endpoint": host, "width": width, "height": height, "steps": steps, "seed": seed, "promptId": pid, "clientId": cid, "bytes": size, "sanity": verdict, "sanityOK": not bad, "seconds": round(time.time() - t0, 1), "at": time.strftime("%Y-%m-%d %H:%M:%S")} with open(log_path, "w", encoding="utf-8") as f: json.dump(runs, f, ensure_ascii=False, indent=2) print("[ok] %.2f MB in %.1fs" % (size / 1048576, time.time() - t0), flush=True) if do_cleanup and bad: # 废图不清服务端 —— 保留 history 与产物当排查证据(量化档位/权重问题要看原始产物) print("[cleanup] SKIP 自检未通过,保留服务端记录与产物供排查", flush=True) elif do_cleanup: try: sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from cleanup_comfyui import after_export print("[cleanup] " + after_export(root, pid, host, log_path, key, out, cid), flush=True) except Exception as e: # 成片已安全落地;清理失败只提示,绝不影响主流程 print("[cleanup] FAILED %s(图片已落地,可稍后跑 cleanup_comfyui.py 补清)" % e, file=sys.stderr, flush=True) return 1 if bad else 0 if __name__ == "__main__": try: sys.exit(main()) except Exception as e: print("ERROR: %s" % e, file=sys.stderr, flush=True) sys.exit(1)