# ComfyUI で Anima を CPU + INT8 で動かす導入ガイド GPU のない環境(Museの仮想マシン)で、画像生成モデル **Anima** を **INT8 量子化モデル + CPU** で動かすための手順書です。 通常、ComfyUI は CPU での INT8 計算を「非対応」として扱いますが、 末尾のパッチを入れることで、対応 CPU で INT8 経路が使えるようになります。 検証済み環境の実測(4 vCPU / RAM 8GB 級の VM、768×512、4ステップ): - サンプリング約 **76 秒/ステップ**、モデル読み込み時間込みで **約 8 分/枚** - メモリ使用量は約 7GB(OOM なし) --- ## 必要なもの - Python 3.12(検証環境) - RAM 8GB 以上、ディスク空き 10GB 程度 - CPU は AVX-512 VNNI 対応だと INT8 が効きます(無くても動きますが速くなりません) ## 手順 1:ComfyUI のインストール 仮想環境を作り、**CPU 版の PyTorch** と ComfyUI 本体を入れます。 GPU 版は不要です。 ```bash python3 -m venv venv source venv/bin/activate pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu git clone https://github.com/comfyanonymous/ComfyUI cd ComfyUI pip install -r requirements.txt ``` (検証時の torch は 2.15.0+cpu でした) ## 手順 2:モデルのダウンロードと配置 Anima は「DiT 本体・テキストエンコーダ(TE)・VAE」が別ファイルです。 INT8 化されたものを揃えます。配置先は ComfyUI フォルダ内の `models/` 配下です。 | ファイル | 配置先 | サイズ | 入手先 | |---|---|---|---| | `anima-base-v1.0.int8.safetensors` | `models/diffusion_models/` | 2,423,696,296 B | sm079/anima-studio | | `qwen3-0.6b.int8.safetensors` | `models/clip/` | 613,869,013 B | sm079/anima-studio | | `qwen_image_vae.safetensors` | `models/vae/` | 253,806,246 B | vanes430/Anima-Turbo-V1.1-GGUF の `split_files/vae/` | | `anima_turbo_4step_v3.safetensors` | `models/loras/` | 200,839,056 B | sorryhyun/anima-turbo-4step | ダウンロード URL(Hugging Face の resolve 形式): ``` https://huggingface.co/sm079/anima-studio/resolve/main/anima-base-v1.0.int8.safetensors https://huggingface.co/sm079/anima-studio/resolve/main/qwen3-0.6b.int8.safetensors https://huggingface.co/vanes430/Anima-Turbo-V1.1-GGUF/resolve/main/split_files/vae/qwen_image_vae.safetensors https://huggingface.co/sorryhyun/anima-turbo-4step/resolve/main/anima_turbo_4step_v3.safetensors ``` 補足: - 本体と TE は **comfy_quant 形式(int8_tensorwise + ConvRot)** の量子化済みファイルが必要です。 通常の bf16 モデルを読み込むときに自動で INT8 化されるわけではありません。またconvrotでは動作しません。 - TE も INT8 版を使うと、メモリが約 1.19GB → 0.61GB に半減します(速度への効果は小さく、主にメモリ節約)。 - VAE は sm079 側にある decoder 専用の特殊版ではなく、上記の通常版を使ってください。 - `anima_turbo_4step_v3` は「ベースモデルを 4 ステップで回すための Turbo 化 LoRA」です。 これを当てると、ベースモデルでも CFG 1.0 / 4 ステップで生成できます。 ## 手順 3:CPU INT8 パッチの設置 **これが要点です。** ComfyUI 本体には「そのデバイスで INT8 計算ができるか」を 判定する関数 `supports_int8_compute` があり、CPU は一律で「不可」と返します。 そのため、通常は INT8 量子化モデルを読み込んでも INT8 経路が使われません。 末尾のパッチは、この判定を **実物テスト付き** に差し替えます: - CPU のときだけ、PyTorch の INT8 行列積(`torch.int8_mm` / `torch._int_mm`)を 小さい行列で実際に実行し、int32 の参照結果と一致するか確認する - 一致したら「対応あり(True)」、失敗したら元の挙動(False)に安全に戻る - GPU のときは元の判定をそのまま使う 設置は、末尾のコードを以下の名前で保存するだけです。 ``` ComfyUI/custom_nodes/comfy_cpu_int8/__init__.py ``` ノードは追加されません(`NODE_CLASS_MAPPINGS` は空)。判定の差し替えだけを行うパッチです。 ## 手順 4:起動と確認 ```bash cd ComfyUI ../venv/bin/python main.py --cpu --port 8188 ``` 起動ログに次の2行が出れば、パッチが効いています。 ``` [CPU-INT8] ComfyUI CPU INT8 capability patch loaded. [CPU-INT8] INT8 GEMM capability detected. ``` さらに、モデル読み込み時に表示される native ops の一覧に `int8_tensorwise` が含まれていれば、INT8 経路で計算されています。 ## 生成ワークフロー ノード構成と設定値(API 形式でも GUI でも同じです): | ノード | 設定 | |---|---| | UNETLoader | `anima-base-v1.0.int8.safetensors` | | CLIPLoader | `qwen3-0.6b.int8.safetensors`、type = **lumina2** | | LoraLoader | `anima_turbo_4step_v3.safetensors`、strength_model = **1.0**、strength_clip = 0.0 | | ModelSamplingAuraFlow | shift = **3.0**(LoRA の後に接続) | | CLIPTextEncode | ポジティブプロンプト(下記) | | EmptyLatentImage | 768×512 など | | KSampler | sampler = **er_sde**、scheduler = **beta**(simple でも可)、steps = **4**、cfg = **1.0**、denoise = 1.0 | | VAELoader | `qwen_image_vae.safetensors` | | VAEDecode → SaveImage | — | 注意: - **CFG 1.0 ではネガティブプロンプトは効きません。** ネガティブ側は空で構いません。 - Turbo 化 LoRA を使う前提の設定です。LoRA なしのベースモデル単体では CFG 4〜5 / 30 ステップ程度が本来の使い方になります(CPU では重いです)。 ## プロンプトの書き方(Anima 公式の並び) - 並び順:クォリティ系 → 人数 → 外見(上から下へ)→ 服装 → ポーズ → 背景 → 光 - タグは小文字、カンマ区切り、アンダースコアではなくスペース区切り (`score_7` のような score タグだけはアンダースコア) サンプル ``` masterpiece, highres, score_7, newest, 1girl, solo, cat ears, gray hair, long hair, yellow eyes, maid, maid dress, frilled apron, cat tail, standing, full body, flower garden, blue sky, sunlight, pastel colors ``` --- ## 付録:CPU INT8 パッチ(`__init__.py` 全文) ```python import logging import torch import comfy.model_management as mm _original_supports_int8_compute = mm.supports_int8_compute _cpu_int8_supported = None def cpu_int8_probe(): """Test whether the exact INT8 GEMM entry point used by comfy-kitchen works.""" global _cpu_int8_supported if _cpu_int8_supported is not None: return _cpu_int8_supported try: # Match comfy-kitchen's current dispatch order. gemm = getattr(torch, "int8_mm", None) if gemm is None: gemm = getattr(torch, "_int_mm", None) if gemm is None: raise RuntimeError("No INT8 GEMM implementation found") a = torch.randint( -8, 8, (128, 256), dtype=torch.int8 ) b = torch.randint( -8, 8, (256, 128), dtype=torch.int8 ) result = gemm(a, b) # Verify output type and numerical correctness. reference = a.to(torch.int32) @ b.to(torch.int32) if result.dtype != torch.int32: raise RuntimeError("Unexpected INT8 GEMM output dtype") if not torch.equal(result, reference): raise RuntimeError("INT8 GEMM validation failed") _cpu_int8_supported = True logging.info("[CPU-INT8] INT8 GEMM capability detected.") except Exception as e: _cpu_int8_supported = False logging.warning("[CPU-INT8] INT8 GEMM unavailable: %s", e) return _cpu_int8_supported def supports_int8_compute(device=None): try: target = mm.get_torch_device() if device is None else torch.device(device) except Exception: return _original_supports_int8_compute(device) if target.type == "cpu": return cpu_int8_probe() return _original_supports_int8_compute(device) mm.supports_int8_compute = supports_int8_compute logging.info("[CPU-INT8] ComfyUI CPU INT8 capability patch loaded.") NODE_CLASS_MAPPINGS = {} NODE_DISPLAY_NAME_MAPPINGS = {} ```