Download patch_comfyui_mxfp8.py from FP8Enjoyer/FLUX.3-Klein-Step-Distilled-MXFP8: direct link, hf CLI and curl.
- Browser
- Download file 5.21 kB
-
https://huggingface.co/FP8Enjoyer/FLUX.3-Klein-Step-Distilled-MXFP8/resolve/main/patch_comfyui_mxfp8.py
- Command line
-
hf download hf://FP8Enjoyer/FLUX.3-Klein-Step-Distilled-MXFP8/patch_comfyui_mxfp8.py
-
curl -L -o patch_comfyui_mxfp8.py https://huggingface.co/FP8Enjoyer/FLUX.3-Klein-Step-Distilled-MXFP8/resolve/main/patch_comfyui_mxfp8.py
5.21 kB
| #!/usr/bin/env python3 | |
| """Apply or revert the FLUX.3 Klein MXFP8 contiguous-input fix to ComfyUI. | |
| Standard-library only. Does not edit model checkpoints or install packages. | |
| """ | |
| import argparse | |
| import ast | |
| import os | |
| from pathlib import Path | |
| import sys | |
| import tempfile | |
| OLD = "ck.quantize_mxfp8(tensor, pad_32x=needs_padding)" | |
| NEW = "ck.quantize_mxfp8(tensor.contiguous(), pad_32x=needs_padding)" | |
| def locate_comfyui(value: str) -> Path: | |
| value = value.strip().strip('"').strip("'") | |
| if not value: | |
| raise ValueError("No ComfyUI folder provided.") | |
| folder = Path(value).expanduser().resolve() | |
| if folder.is_file() and folder.name.lower() == "quant_ops.py": | |
| target = folder | |
| elif (folder / "comfy" / "quant_ops.py").is_file(): | |
| target = folder / "comfy" / "quant_ops.py" | |
| else: | |
| raise FileNotFoundError( | |
| f"Cannot find 'comfy/quant_ops.py' under: {folder}\n" | |
| "Enter the ComfyUI source folder (not the models folder)." | |
| ) | |
| return target | |
| def atomic_write(path: Path, raw: bytes) -> None: | |
| """Write to sibling temp file; replace the destination only after success.""" | |
| name = None | |
| try: | |
| with tempfile.NamedTemporaryFile( | |
| mode="wb", prefix=path.name + ".tmp.", dir=path.parent, | |
| delete=False | |
| ) as output: | |
| name = output.name | |
| output.write(raw) | |
| output.flush() | |
| os.fsync(output.fileno()) | |
| os.replace(name, path) | |
| finally: | |
| if name and os.path.exists(name): | |
| os.unlink(name) | |
| def run(target: Path, *, check: bool = False, restore: bool = False) -> int: | |
| backup = target.with_name(target.name + ".mxfp8_backup") | |
| raw = target.read_bytes() | |
| # ComfyUI source should be valid UTF-8; keeping the original newline style. | |
| source = raw.decode("utf-8") | |
| old_count = source.count(OLD) | |
| new_count = source.count(NEW) | |
| print(f"ComfyUI file: {target}") | |
| print(f"Backup file: {backup}") | |
| if check: | |
| if old_count == 1 and new_count == 0: | |
| print("STATUS: Unpatched, expected kernel call found.") | |
| return 0 | |
| if old_count == 0 and new_count == 1: | |
| print("STATUS: Patch already applied.") | |
| return 0 | |
| print("STATUS: Unsupported or ambiguous source layout; no changes made.") | |
| return 1 | |
| if restore: | |
| if not backup.is_file(): | |
| print("ERROR: No backup found; refusing to guess original contents.") | |
| return 1 | |
| original = backup.read_bytes() | |
| original_source = original.decode("utf-8") | |
| if original_source.count(OLD) != 1 or original_source.count(NEW): | |
| print("ERROR: Backup does not contain the expected unpatched code.") | |
| return 1 | |
| if old_count != 0 or new_count != 1: | |
| print("ERROR: Current file is not in the expected patched state.") | |
| return 1 | |
| if source.replace(NEW, OLD) != original_source: | |
| print("ERROR: File changed since patching; refusing to erase other edits.") | |
| return 1 | |
| atomic_write(target, original) | |
| print("RESTORED original quant_ops.py; restart ComfyUI.") | |
| return 0 | |
| if old_count == 0 and new_count == 1: | |
| print("ALREADY PATCHED. No changes needed.") | |
| return 0 | |
| if old_count != 1 or new_count: | |
| print("ERROR: Cannot safely identify the MXFP8 call; no changes made.") | |
| print(f"Original call occurrences: {old_count}; patched: {new_count}.") | |
| return 1 | |
| patched = source.replace(OLD, NEW, 1) | |
| # Ensure we have not introduced a syntax error. | |
| ast.parse(patched, filename=str(target)) | |
| if backup.exists(): | |
| if backup.read_bytes() != raw: | |
| print("ERROR: An older backup already exists with different content.") | |
| print("Refusing to replace it or patch a different ComfyUI revision.") | |
| return 1 | |
| else: | |
| with backup.open("xb") as output: | |
| output.write(raw) | |
| output.flush() | |
| os.fsync(output.fileno()) | |
| atomic_write(target, patched.encode("utf-8")) | |
| print("PATCH APPLIED: MXFP8 activation input is now contiguous.") | |
| print("No weights or model files were changed. Restart ComfyUI.") | |
| return 0 | |
| def main() -> int: | |
| parser = argparse.ArgumentParser( | |
| description="Patch ComfyUI MXFP8 activation contiguity, with backup and rollback." | |
| ) | |
| parser.add_argument( | |
| "--comfyui", help="ComfyUI root folder (prompts for it if omitted)" | |
| ) | |
| operation = parser.add_mutually_exclusive_group() | |
| operation.add_argument("--check", action="store_true", help="Read-only status check") | |
| operation.add_argument("--restore", action="store_true", help="Restore original backup") | |
| args = parser.parse_args() | |
| try: | |
| location = args.comfyui or input("Enter your ComfyUI folder path: ") | |
| target = locate_comfyui(location) | |
| return run(target, check=args.check, restore=args.restore) | |
| except (OSError, UnicodeError, SyntaxError, ValueError, EOFError) as exc: | |
| print(f"ERROR: {exc}", file=sys.stderr) | |
| return 1 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |