John2386 commited on
Commit
3dd4cd8
·
verified ·
1 Parent(s): f5df6ba

Add Colab int8 (ConvRot) quantization pipeline script

Browse files
Files changed (1) hide show
  1. tools/fullgreed_int8_quantize.py +124 -0
tools/fullgreed_int8_quantize.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ End-to-end INT8 (ConvRot) quantization of John2386/fullgreed for ComfyUI.
4
+
5
+ RUN THIS ON A CUDA GPU (Colab / RunPod / any NVIDIA box with a terminal).
6
+ It will NOT work on a Mac — it needs CUDA + Triton.
7
+
8
+ Recommended GPU: 24 GB+ VRAM (3090/4090/L4/A100). A 16 GB T4 may OOM on a ~6B model
9
+ because on-the-fly quant keeps an extra INT8 copy in memory (see the node's README).
10
+
11
+ What it does:
12
+ 1. installs ComfyUI + ComfyUI-INT8-Fast + Triton
13
+ 2. downloads fullgreed_f16.safetensors from your HF repo
14
+ 3. runs the OTUNetLoaderW8A8 -> INT8ModelSave quantize workflow headlessly (ConvRot, z-image)
15
+ 4. converts the result to ComfyUI's native int8 format (convert_to_comfy.py)
16
+ 5. uploads fullgreed_i8_comfy.safetensors back to John2386/fullgreed
17
+
18
+ Set your HF write token below (or `export HF_TOKEN=...`).
19
+ """
20
+ import os, sys, subprocess, time, glob, json
21
+
22
+ HF_TOKEN = os.environ.get("HF_TOKEN", "PASTE_HF_WRITE_TOKEN_HERE")
23
+ HF_REPO = "John2386/fullgreed"
24
+ BASE_FILE = "fullgreed_f16.safetensors"
25
+ OUT_NAME = "fullgreed_i8_comfy.safetensors"
26
+
27
+ WORK = "/content" if os.path.isdir("/content") else os.getcwd()
28
+ COMFY = os.path.join(WORK, "ComfyUI")
29
+ NODE = os.path.join(COMFY, "custom_nodes", "ComfyUI-INT8-Fast")
30
+
31
+ def run(cmd):
32
+ print("\n+ " + cmd, flush=True)
33
+ subprocess.run(cmd, shell=True, check=True)
34
+
35
+ # ---- 0) GPU sanity + Triton version (T4/20-series are sm75: need triton==3.2.0) ----
36
+ import torch
37
+ assert torch.cuda.is_available(), "No CUDA GPU visible — set Runtime > Change runtime type > GPU"
38
+ cap = torch.cuda.get_device_capability(0)
39
+ name = torch.cuda.get_device_name(0)
40
+ vram = torch.cuda.get_device_properties(0).total_memory / 1e9
41
+ print(f"GPU: {name} sm{cap[0]}{cap[1]} {vram:.1f} GB VRAM", flush=True)
42
+ if vram < 20:
43
+ print("WARNING: <20 GB VRAM — a ~6B model may OOM during on-the-fly quantization.", flush=True)
44
+ TRITON = "triton==3.2.0" if cap == (7, 5) else "triton" # sm75 support dropped in triton 3.3
45
+
46
+ # ---- 1) ComfyUI + node + deps -------------------------------------------------
47
+ if not os.path.isdir(COMFY):
48
+ run(f"git clone --depth 1 https://github.com/comfyanonymous/ComfyUI '{COMFY}'")
49
+ run(f"pip install -q -r '{COMFY}/requirements.txt'")
50
+ run(f"pip install -q {TRITON} safetensors huggingface_hub requests")
51
+ if not os.path.isdir(NODE):
52
+ run(f"git clone --depth 1 https://github.com/BobJohnson24/ComfyUI-INT8-Fast '{NODE}'")
53
+ if os.path.exists(f"{NODE}/requirements.txt"):
54
+ run(f"pip install -q -r '{NODE}/requirements.txt'")
55
+
56
+ # ---- 2) download the fp16 base model -----------------------------------------
57
+ from huggingface_hub import hf_hub_download, upload_file
58
+ dst = os.path.join(COMFY, "models", "diffusion_models")
59
+ os.makedirs(dst, exist_ok=True)
60
+ print("downloading base model from HF...", flush=True)
61
+ hf_hub_download(HF_REPO, BASE_FILE, local_dir=dst)
62
+
63
+ # ---- 3) start ComfyUI headless + queue the quantize prompt --------------------
64
+ import requests
65
+ print("starting ComfyUI (headless)...", flush=True)
66
+ server = subprocess.Popen(
67
+ [sys.executable, "main.py", "--listen", "127.0.0.1", "--port", "8188",
68
+ "--disable-auto-launch"], cwd=COMFY)
69
+ for _ in range(180): # wait up to ~6 min for startup
70
+ try:
71
+ if requests.get("http://127.0.0.1:8188/system_stats", timeout=2).ok:
72
+ break
73
+ except Exception:
74
+ pass
75
+ time.sleep(2)
76
+ else:
77
+ raise SystemExit("ComfyUI did not come up — check the log above for node/Triton errors.")
78
+
79
+ # API-format graph: loader (on-the-fly int8 + convrot, z-image) -> save
80
+ prompt = {
81
+ "1": {"class_type": "OTUNetLoaderW8A8", "inputs": {
82
+ "unet_name": BASE_FILE,
83
+ "weight_dtype": "default",
84
+ "model_type": "z-image",
85
+ "on_the_fly_quantization": True,
86
+ "enable_convrot": True,
87
+ "lora_mode": "None"}},
88
+ "2": {"class_type": "INT8ModelSave", "inputs": {
89
+ "model": ["1", 0],
90
+ "filename_prefix": "int8_models/fullgreed_i8"}},
91
+ }
92
+ r = requests.post("http://127.0.0.1:8188/prompt", json={"prompt": prompt})
93
+ if not r.ok:
94
+ raise SystemExit(f"/prompt rejected: {r.status_code} {r.text}")
95
+ pid = r.json()["prompt_id"]
96
+ print("queued prompt", pid, "- quantizing (this is the slow part)...", flush=True)
97
+
98
+ while True: # poll until the job leaves the queue
99
+ h = requests.get(f"http://127.0.0.1:8188/history/{pid}", timeout=10).json()
100
+ if pid in h:
101
+ st = h[pid].get("status", {})
102
+ print("job finished:", st.get("status_str", st), flush=True)
103
+ if st.get("status_str") == "error":
104
+ raise SystemExit("Quantization errored — see ComfyUI log above.")
105
+ break
106
+ time.sleep(5)
107
+
108
+ # ---- 4) convert I8Fast -> native ComfyUI int8 --------------------------------
109
+ cands = sorted(glob.glob(os.path.join(COMFY, "output", "int8_models", "fullgreed_i8*.safetensors")))
110
+ if not cands:
111
+ raise SystemExit("No int8 output file was produced.")
112
+ i8fast = cands[-1]
113
+ out_path = os.path.join(COMFY, "output", OUT_NAME)
114
+ print("produced:", i8fast)
115
+ run(f"python '{NODE}/convert_to_comfy.py' '{i8fast}' '{out_path}'")
116
+
117
+ # ---- 5) upload back to HF -----------------------------------------------------
118
+ print("uploading to HF...", flush=True)
119
+ url = upload_file(path_or_fileobj=out_path, path_in_repo=OUT_NAME,
120
+ repo_id=HF_REPO, repo_type="model", token=HF_TOKEN,
121
+ commit_message="Add INT8 (ConvRot) ComfyUI quant of fullgreed")
122
+ print("\nDONE ->", url)
123
+ print("Load it in ComfyUI with the native INT8 loader (or the INT8-Fast loader with "
124
+ "on_the_fly_quantization OFF), plus Qwen3-4B text encoder + Flux 16ch VAE.")