ComfyUI Workflows — Ngôn ngữ lập trình trực quan cho AI image generation

Hướng dẫn toàn diện về ComfyUI workflows để tạo AI images chuyên nghiệp. Xây dựng pipeline phức tạp với nodes, quản lý dependencies và tạo template workflows có thể chia sẻ.

  • Cập nhật 2026-07-16

TL;DR #

ComfyUI là một giao diện đồ thị dựa trên nodes mạnh mẽ để chạy các model AI image generation, cho phép bạn xây dựng pipeline tùy chỉnh bằng cách kết nối các nodes thay vì viết code. Nó hỗ trợ Stable Diffusion, Flux, SDXL và hàng chục model khác. Bài viết này bao gồm các mẫu thiết kế workflow, quản lý nodes, tối ưu hiệu năng và cách xây dựng các pipeline tạo ảnh chất lượng cao.


ComfyUI là gì? #

ComfyUI là một giao diện đồ thị dựa trên nodes để chạy các model AI image generation. Khác với các UI truyền thống nơi bạn chỉ điều chỉnh thanh trượt và nhấn “generate”, ComfyUI cho phép bạn xây dựng pipeline tùy chỉnh bằng cách kết nối các processing nodes với nhau — tương tự như hệ thống nodes của Blender hoặc TouchDesigner.

Triết lý cốt lõi: mang đến cho người dùng quyền kiểm soát hoàn toàn mọi bước trong quá trình tạo ảnh. Điều này có nghĩa là bạn có thể:

  • Chaining nhiều model với nhau (ví dụ: text → image → upscale → refine)
  • Sử dụng conditional logic (if A then B else C)
  • Xử lý nhiều ảnh đồng thời
  • Tạo các template workflow có thể tái sử dụng
  • Tinh chỉnh từng tham số ở mỗi giai đoạn

Tại sao AI workflows dạng nodes lại quan trọng #

Các công cụ AI image generation truyền thống cung cấp pipeline cố định: bạn nhập prompt, điều chỉnh cài đặt và nhận ảnh. Nhưng công việc sáng tạo thực tế thường yêu cầu:

  1. Xử lý đa giai đoạn — Tạo ảnh base, detect faces, upscale vùng cụ thể, apply style transfer
  2. Conditional generation — Prompt khác nhau dựa trên nội dung được detect
  3. Batch processing — Tạo biến thể một cách hiệu quả
  4. Custom post-processing — Áp dụng filter, composite hoặc correction cụ thể

Workflows dạng nodes xử lý tất cả những điều này một cách native.


Khái niệm cốt lõi #

Nodes và Connections #

Mỗi thao tác trong ComfyUI là một node — một đơn vị xử lý tự chứa với inputs và outputs:

[Load Checkpoint] → [CLIP Text Encode] → [KSampler] → [VAE Decode] → [Save Image]
     │                    │                      │                │
  model              positive/negative        seed/samples      output

Mỗi loại node xử lý một nhiệm vụ cụ thể:

  • Model Loading: Load Stable Diffusion checkpoints, LoRAs, embeddings
  • Text Encoding: Chuyển đổi prompt thành biểu diễn latent space
  • Sampling: Tạo ảnh bằng các thuật toán khác nhau (Euler, DPM++, DDIM)
  • Post-processing: Upscale, color correction, face enhancement
  • Output: Lưu ảnh, stream kết quả, trigger downstream actions

Kiến trúc Workflow #

Một ComfyUI workflow hoàn chỉnh tuân theo pattern sau:

# Flow khái niệm (ComfyUI thực tế sử dụng connections trực quan)
workflow = {
    "input": {
        "prompt_positive": "một hồ nước yên bình lúc hoàng hôn, photorealistic",
        "prompt_negative": "mờ, chất lượng thấp, méo mó",
        "seed": 42,
        "steps": 30,
        "cfg_scale": 7.5
    },
    "pipeline": [
        "load_checkpoint(sdxl_v1.0)",
        "encode_prompts(positive, negative)",
        "generate_latents(seed, steps, cfg)",
        "decode_latents(vae_model)",
        "post_process(image, upscale=2x)"
    ],
    "output": {
        "format": "png",
        "resolution": "1024x1024",
        "save_path": "./outputs/"
    }
}

Các Category Node Chính #

CategoryMục đíchVí dụ
Model LoadingLoad base models và extensionsCheckpointLoader, LoraLoader
ConditioningXử lý text promptsCLIPTextEncode, Condition
SamplingTạo ảnhKSampler, Euler, DPM++
Latent SpaceManipulate latent representationsEmptyLatentImage, LatentUpscale
VAEEncode/decode giữa pixel và latent spaceVAELoader, VAE Decode
Post-ProcessingEnhance và modify outputsUpscaleImage, FaceRestore
ControlNetGuide generation với referencesControlNetApply, Preprocessor
OutputSave và manage resultsSaveImage, PreviewImage

Xây dựng Workflow đầu tiên #

Image Generation cơ bản #

Bước 1: Load Checkpoint → Chọn model (SDXL, Flux, v.v.)
Bước 2: CLIP Text Encode → Nhập positive và negative prompts
Bước 3: KSampler → Đặt steps (20-50), CFG (7-12), seed
Bước 4: VAE Decode → Chuyển latent sang pixel space
Bước 5: Save Image → Chọn định dạng và vị trí

Nâng cao: Multi-stage Pipeline #

Để có kết quả chuyên nghiệp, chain nhiều stages:

Stage 1: Base Generation
├── Load Checkpoint (SDXL)
├── Encode Prompts
└── KSampler (low res, fast)

Stage 2: Face Enhancement
├── Load FaceRestore Model
├── Detect Faces
└── Restore Faces

Stage 3: Upscaling
├── Load Upscale Model (4x)
├── Latent Upscale (2x)
└── Pixel Upscale (2x)

Stage 4: Final Polish
├── Color Correction
├── Detail Enhancement
└── Save High-Res PNG

Các Pattern Workflow Phổ biến #

Pattern 1: Iterative Refinement #

Tạo ảnh base, đánh giá, sau đó refine các khía cạnh cụ thể:

{
  "workflow_id": "iterative-refinement",
  "stages": [
    {"name": "base", "steps": 20, "resolution": "512x512"},
    {"name": "refine", "steps": 40, "resolution": "1024x1024", "denoise": 0.6},
    {"name": "detail", "steps": 30, "resolution": "2048x2048", "denoise": 0.3}
  ]
}

Pattern 2: Batch Variation Generation #

Tạo nhiều biến thể để so sánh:

{
  "workflow_id": "batch-variations",
  "config": {
    "base_prompt": "một cảnh quan thành phố tương lai",
    "variations": [
      {"seed": 100, "style": "cyberpunk"},
      {"seed": 200, "style": "art deco"},
      {"seed": 300, "style": "brutalist"},
      {"seed": 400, "style": "biophilic"}
    ],
    "parallel_workers": 4
  }
}

Pattern 3: ControlNet-Guided Generation #

Sử dụng reference images để guide composition:

Input: Reference Image
   ↓
Canny Edge Detection → ControlNet (edge guidance)
   ↓
Depth Estimation → ControlNet (depth guidance)
   ↓
Combined Conditioning → KSampler
   ↓
Final Image với precise composition control

Pattern 4: Img2Img Pipeline #

Biến đổi existing images trong khi vẫn giữ nguyên structure:

Original Image → Encode (VAE) → Add Noise → KSampler (denoise) → Decode (VAE) → Result

Điều chỉnh denoising strength (0.1-0.9) để kiểm soát cường độ transformation.


Quản lý Models #

Các Model Được Hỗ trợ #

ComfyUI hỗ trợ rộng rãi các model:

Model TypeVí dụTốt nhất cho
Stable Diffusion 1.5sd-v1-5, dreamshaperPrototyping nhanh
SDXLsdxl_v1.0, juggernautQuality cao base
Fluxflux-dev, flux-schnellPhotorealistic
Custom CheckpointsAny Civitai modelStyles cụ thể
LoRAsStyle-specific fine-tunesStyle transfer
EmbeddingsNegative prompts, conceptsPrompt enhancement

Cài đặt Models #

# Tải model vào ComfyUI/models/checkpoints/
wget -P models/checkpoints/ https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors

# Cài đặt LoRAs
wget -P models/loras/ https://civitai.com/api/download/models/12345

# Cài đặt VAEs
wget -P models/vae/ https://huggingface.co/stabilityai/sdxl-vae/resolve/main/sdxl_vae.safetensors

Quản lý Dependencies #

{
  "dependencies": {
    "checkpoints": ["sdxl_v1.0.safetensors"],
    "loras": ["realism_lora_v2.safetensors"],
    "vae": ["sdxl_vae.safetensors"],
    "controlnet": ["control_canny.safetensors"],
    "upscale": ["4x-UltraSharp.pth"]
  }
}

Tối ưu Hiệu năng #

Quản lý GPU Memory #

# Tối ưu cho các kích thước GPU khác nhau
optimization_config = {
    "24GB_GPU": {
        "precision": "fp16",
        "attention": "flash_attention_2",
        "vram_optimize": True
    },
    "12GB_GPU": {
        "precision": "fp16",
        "attention": "xformers",
        "vram_optimize": True,
        "split_execution": True
    },
    "8GB_GPU": {
        "precision": "fp16",
        "attention": "xformers",
        "vram_optimize": True,
        "split_execution": True,
        "lowvram_mode": True
    }
}

Tốc độ Batch Processing #

ConfigurationImages/MinuteQuality
Single, SDXL, 30 steps2-3High
Batch 4, SDXL, 30 steps8-12High
Batch 8, SD 1.5, 20 steps16-24Medium
Single, Flux, 25 steps1-2Very High

Strategies Caching #

{
  "caching": {
    "checkpoint_cache": true,
    "lora_cache": true,
    "vae_cache": true,
    "embeddings_cache": true,
    "max_cache_size_gb": 8
  }
}

Kỹ thuật Nâng cao #

Technique 1: Hierarchical Generation #

Tạo ở low resolution trước, sau đó progressively upscale:

Low Res (512x512) → Mid Res (1024x1024) → High Res (2048x2048)
       ↓                   ↓                    ↓
    Coarse details     Fine details          Ultra details

Technique 2: Region-based Editing #

Chỉnh sửa các phần cụ thể của ảnh mà không ảnh hưởng đến phần khác:

Mask Selection → Inpaint Node → Local Prompt → KSampler (masked only)

Technique 3: Style Transfer Pipeline #

Áp dụng artistic styles trong khi vẫn giữ nguyên content:

Content Image → CLIP Vision → Style Reference → Cross-Attention → KSampler

Technique 4: Automated Quality Scoring #

Scoring và lọc tự động các ảnh được tạo:

Generated Images → CLIP Score Node → Filter (> threshold) → Save Best

Troubleshooting #

Issue 1: Lỗi Out of Memory #

Error: CUDA out of memory

Fixes:

  • Giảm batch size
  • Enable --lowvram flag
  • Sử dụng fp16 precision
  • Đóng các ứng dụng GPU khác
  • Chia workflow thành các stages nhỏ hơn

Issue 2: Generate chậm #

Warning: Generation taking longer than expected

Fixes:

  • Sử dụng sampler nhanh hơn (Euler a, DPM++ 2M)
  • Giảm steps (20-25 cho hầu hết trường hợp)
  • Enable Flash Attention
  • Sử dụng SD 1.5 thay vì SDXL cho tốc độ
  • Pre-load models vào VRAM

Issue 3: Output chất lượng kém #

Images trông mờ hoặc có artifacts

Fixes:

  • Tăng steps lên 30-50
  • Điều chỉnh CFG scale (7-12)
  • Sử dụng checkpoint/LoRA tốt hơn
  • Enable high-res fix
  • Kiểm tra quality của negative prompt

So sánh: ComfyUI vs Alternatives #

FeatureComfyUIAutomatic1111FooocusSD WebUI Forge
Node-based UI
Custom pipelinesLimitedLimited
PerformanceExcellentGoodGoodExcellent
Learning curveSteepModerateEasyModerate
Extension ecosystemGrowingLargeSmallGrowing
Multi-GPU support

ComfyUI thắng cho complex, custom workflows. Các tool khác dễ hơn cho simple generation.


Bắt đầu #

Installation #

# Clone ComfyUI
git clone https://github.com/comfyanonymous/ComfyUI.git
cd ComfyUI

# Install dependencies
pip install -r requirements.txt

# Download model (optional, will auto-download on first run)
# Đặt trong models/checkpoints/

# Start ComfyUI
python main.py --listen 0.0.0.0 --port 8188

Browser Interface #

Mở http://localhost:8188 trong browser. Bạn sẽ thấy:

  • Canvas trống để build workflows
  • Node library bên phải
  • Settings panel (icon bánh răng)
  • Queue và history tabs

Loading Presets #

ComfyUI bao gồm nhiều preset workflows:

  • Basic: Simple text-to-image
  • Img2Img: Image-to-image transformation
  • ControlNet: Reference-guided generation
  • Upscale: Resolution enhancement
  • AnimateDiff: Animation generation

Community Resources #

  • Juggernaut Workflow: Professional photorealistic generation
  • DreamShaper Flow: Artistic và illustration styles
  • RealVis Pipeline: Realistic portrait generation
  • Flux Dev Setup: Latest Flux model workflows
  • ControlNet Studio: Advanced pose và composition control

Nơi tìm Workflows #

  • Civitai: Community-shared workflows với models
  • ComfyUI Manager: Built-in workflow marketplace
  • GitHub: Open-source workflow collections
  • Discord: Active community sharing tips và templates

FAQ #

Q: Tôi có cần GPU mạnh cho ComfyUI không? #

ComfyUI hiệu quả hơn hầu hết alternatives. GPU 12GB (RTX 3060/4070) xử lý SDXL tốt. Ngay cả 8GB cards hoạt động với optimizations. Chế độ CPU-only khả thi nhưng rất chậm.

Q: Tôi có thể dùng ComfyUI cho video generation không? #

Có. Với AnimateDiff và các animation nodes khác, bạn có thể tạo short videos và GIFs. Workflow thêm temporal consistency nodes giữa các frames.

Q: Làm thế nào để chia sẻ workflows với người khác? #

Export dưới dạng .json hoặc .png files. Chia sẻ qua Civitai, GitHub hoặc Discord. Recipients import bằng cách drag file lên canvas ComfyUI.

Q: ComfyUI có miễn phí không? #

Có, ComfyUI hoàn toàn free và open-source. Bạn chỉ trả tiền điện và GPU time. Một số community nodes có thể yêu cầu tải model riêng.

Q: Tôi có thể dùng ComfyUI với cloud GPUs không? #

Chắc chắn. ComfyUI hoạt động trên mọi GPU cloud: RunPod, Vast.ai, Lambda Labs, AWS EC2, Google Cloud. Chỉ cần install và point đến model files của bạn.

Q: Sự khác biệt giữa ComfyUI và ComfyUI Manager là gì? #

ComfyUI là core application. ComfyUI Manager là một extension giúp cài đặt models, nodes và workflows dễ dàng hơn nhiều. Install nó đầu tiên để có trải nghiệm tốt nhất.


References #


Tham gia nhóm Telegram để thảo luận công cụ AI thời gian thực và mẹo deployment: t.me/dibi8

📦 Xuất hiện trong các bộ sưu tập

💬 Bình luận & Thảo luận