Chatgpt fp8 version for comfyui
I asked chatgpt to do a script for the fp8 (on a windows / nvidia 5090 machine) version for use in comfyui. The fp8 seems to work as a replacement for the original fp8 file in comfyui, but use at you own risk:
import os, json, sys, math
from pathlib import Path
import torch
from huggingface_hub import snapshot_download
from safetensors.torch import load_file, save_file
=== Config ===
REPO_ID = "Qwen/Qwen-Image-Edit-2509"
SUBFOLDER = "transformer" # where the shards live
OUT_BF16 = "qwen_image_edit_bf16_2509_merged.safetensors"
OUT_FP8 = "qwen_image_edit_fp8_e4m3fn.safetensors"
def human_mb(x): return f"{x/1024/1024:.1f} MB"
def main():
print(f"Downloading '{REPO_ID}/{SUBFOLDER}' shards from Hugging Face…")
local_dir = snapshot_download(repo_id=REPO_ID, allow_patterns=[f"{SUBFOLDER}/*"])
tdir = Path(local_dir) / SUBFOLDER
index_path = tdir / "diffusion_pytorch_model.safetensors.index.json"
if not index_path.exists():
sys.exit(f"ERROR: index file not found at {index_path}")
with open(index_path, "r", encoding="utf-8") as f:
index = json.load(f)
shard_files = sorted({ (tdir / p).as_posix() for p in index["weight_map"].values() })
print(f"Found {len(shard_files)} shard(s). Merging…")
merged = {}
total_params = 0
for i, shard in enumerate(shard_files, 1):
part = load_file(shard, device="cpu")
for k, v in part.items():
merged[k] = v
if torch.is_floating_point(v):
total_params += v.numel()
print(f" [{i}/{len(shard_files)}] loaded {Path(shard).name} "
f"({len(part)} tensors)")
# Normalize to BF16 first (safer baseline & matches Comfy/community practice)
casted = {}
for k, v in merged.items():
if torch.is_floating_point(v):
casted[k] = v.to(torch.bfloat16)
else:
casted[k] = v # ints, bools, etc.
merged.clear()
print(f"Saving BF16 single-file → {OUT_BF16} …")
save_file(casted, OUT_BF16)
print(f" wrote: {OUT_BF16} (size: {Path(OUT_BF16).stat().st_size/1e9:.2f} GB)")
# Try FP8 cast (weight-only). Comfy’s existing fp8_e4m3fn diffusion files are
# plain float8 weights in safetensors; loaders understand them. :contentReference[oaicite:2]{index=2}
fp8_supported = hasattr(torch, "float8_e4m3fn")
if not fp8_supported:
print("\nWARNING: Your PyTorch build lacks torch.float8_e4m3fn "
"(common if CUDA build is too old). Keeping BF16 file.")
return
print("Casting eligible tensors to FP8 e4m3fn (weight-only)…")
casted_fp8 = {}
# Only cast floating tensors; keep non-floats as-is.
# We *don’t* change tensor names/keys; Comfy expects original keys.
for k, v in casted.items():
if torch.is_floating_point(v):
# Weight-only cast; activations are handled at runtime
casted_fp8[k] = v.to(torch.float8_e4m3fn)
else:
casted_fp8[k] = v
print(f"Saving FP8 single-file → {OUT_FP8} …")
save_file(casted_fp8, OUT_FP8)
print(f" wrote: {OUT_FP8} (size: {Path(OUT_FP8).stat().st_size/1e9:.2f} GB)")
# Small sanity echo
print("\nDone.")
print(f"Place '{OUT_FP8}' in: ComfyUI\\models\\diffusion_models\\")
print("If Comfy outputs are weird/blank on your setup, use the BF16 file instead.")
if name == "main":
main()
Could you upload the fp8 file somewhere for testing?
The normal version is available here: https://huggingface.co/Comfy-Org/Qwen-Image-Edit_ComfyUI/tree/main/split_files/diffusion_models