# nunchaku-tech/comfyui-nunchaku architecture

> Architecture diagram of the GitHub repository nunchaku-tech/comfyui-nunchaku, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nunchaku-tech/comfyui-nunchaku
- Repository: https://github.com/nunchaku-tech/comfyui-nunchaku

## Overview

1. **What this repo is**
   - This is a **ComfyUI custom-node plugin** that bridges ComfyUI workflows to the **Nunchaku** inference engine.
   - Its job is not to train models or host an API; it provides **node definitions, model-loading logic, patchers, and workflow examples** for running quantized diffusion models inside ComfyUI.

2. **Top-level runtime boundary**
   - The runtime boundary is: **ComfyUI graph execution** on one side, and **Nunchaku quantized model execution** on the other.
   - The plugin adapts ComfyUI concepts like nodes, model objects, LoRA injection, preprocessors, and workflow assets into the loading/execution patterns required by Nunchaku.

3. **Core package layout**
   - `nodes/` contains the user-facing ComfyUI nodes.
   - `models/`, `model_base/`, `model_configs/`, and `model_patcher/` hold the model abstraction layer that knows how to load and modify specific model families.
   - `wrappers/` provides execution wrappers around model-specific runtime details, especially for FLUX-related flows.
   - `mixins/` and `utils.py` are shared support code used across those layers.

4. **Model-family abstraction**
   - The repo is organized around multiple supported families: **FLUX**, **Qwen-Image**, and **Z-Image**.
   - `models/qwenimage.py` and `models/zimage.py` appear to define family-level model objects.
   - `model_base/` and `model_configs/` separate **generic model behavior** from **family-specific configuration**, so the plugin can map many workflows onto a smaller set of loading patterns.

5. **Patching and quantized execution**
   - `model_patcher/` is a key architectural boundary: it adapts the underlying ComfyUI model representation so quantized weights and Nunchaku-specific execution behavior can be applied.
   - `model_patcher/common.py` and `model_patcher/zimage.py` indicate a shared patching path plus model-specific customization.
   - This is where the plugin likely injects or rewires attention/blocks/weights to match the 4-bit Nunchaku runtime.

6. **Node subsystems**
   - `nodes/models/` contains the main model-loading and orchestration nodes, grouped by capability:
     - `flux.py`
     - `qwenimage.py`
     - `zimage.py`
     - `ipadapter.py`
     - `pulid.py`
     - `text_encoder.py`
     - `utils.py`
   - `nodes/lora/` contains LoRA-related nodes, with `flux.py` showing FLUX-specific LoRA handling.
   - `nodes/preprocessors/depth.py` indicates preprocessing nodes that generate conditioning inputs such as depth maps.
   - `nodes/tools/` provides utility nodes like **installer** and **safetensors merge** tooling.

7. **Feature-level integration points**
   - The architecture supports several optional side systems that plug into the main model path:
     - **LoRA** for adapter-based style/control changes
     - **IPAdapter** for image-prompt conditioning
     - **PuLID** for identity-related conditioning
     - **Text encoder** handling, including quantized encoder variants
     - **Preprocessors** such as depth extraction
   - These are implemented as separate node groups rather than being hard-coded into one monolithic model runner.

8. **Workflow-driven usage model**
   - The `example_workflows/` directory contains ready-to-import ComfyUI graphs for each supported model family and feature combination.
   - This implies the primary user interaction is **graph composition**, not direct scripting.
   - The workflows serve as executable documentation of how node outputs feed into model nodes, conditioners, and image-generation outputs.

9. **Validation and regression testing**
   - `tests/workflows/` mirrors those example workflows and includes `workflow.json`, `api.json`, and in some cases `ref.json`.
   - `tests/test_workflows.py` suggests the repo validates that workflows still load and produce expected API-level behavior.
   - `test_data/` provides model lists, custom-node metadata, and dependency manifests used to make workflow tests reproducible.

10. **Supporting scripts and operational tooling**
   - `scripts/download_models.py` and `scripts/download_inputs.py` support local setup of models and test inputs.
   - `scripts/setup_custom_nodes.py` and the `test_data/custom_nodes.yaml` / dependency files point to an environment bootstrap process for ComfyUI extensions.
   - `scripts/update_versions.py`, `scripts/parse_object_invs.py`, and version-related CI workflows indicate the package is versioned and its release artifacts are generated automatically.

11. **Documentation as part of the architecture**
   - `docs/` is extensive and mirrors the runtime structure: nodes, workflows, API reference, installation, and developer guides.
   - The documentation is generated with Sphinx and likely serves as the canonical map for how the node graph, model abstractions, and workflows relate.

12. **Packaging and delivery**
   - The presence of `pyproject.toml`, `requirements.txt`, `pypi/`, and release workflows shows this is packaged as a distributable Python plugin.
   - The repo also includes CI for building docs, running lint/tests, publishing, and building a ComfyUI Desktop artifact, so the codebase is designed for both plugin installation and packaged distribution.
   - External ecosystem dependencies that materially shape the design are **ComfyUI**, the **Nunchaku wheel/runtime**, and model assets distributed via **Hugging Face** and **ModelScope**.

Main entry points: [`nodes/models/flux.py`](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/flux.py) (FLUX nodes), [`__init__.py`](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/__init__.py) (Plugin entry), [`example_workflows/nunchaku-flux.1-dev.json`](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/example_workflows/nunchaku-flux.1-dev.json) (Example workflows), [`model_patcher/common.py`](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/model_patcher/common.py) (Patchers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_runtime["Runtime bridge"]
  node_node_init["Plugin entry<br/>comfyui plugin<br/>[__init__.py]"]
  node_node_utils["Shared utils<br/>shared helpers<br/>[utils.py]"]
end

subgraph group_group_models["Model stack"]
  node_node_mixins["Mixins<br/>[model.py]"]
  node_node_model_base["Base models<br/>generic model base<br/>[qwenimage.py]"]
  node_node_model_configs["Model configs<br/>family configs<br/>[qwenimage.py]"]
  node_node_model_patcher["Patchers<br/>quantization patching<br/>[common.py]"]
  node_node_models_qwen["Qwen-Image model<br/>family model<br/>[qwenimage.py]"]
  node_node_models_z["Z-Image model<br/>family model<br/>[zimage.py]"]
  node_node_wrapper_flux["FLUX wrapper<br/>runtime wrapper<br/>[flux.py]"]
end

subgraph group_group_nodes["Node layer"]
  node_node_nodes_flux["FLUX nodes<br/>loader nodes<br/>[flux.py]"]
  node_node_nodes_qwen["Qwen nodes<br/>loader nodes<br/>[qwenimage.py]"]
  node_node_nodes_z["Z-Image nodes<br/>loader nodes<br/>[zimage.py]"]
  node_node_nodes_ip["IPAdapter<br/>conditioning nodes<br/>[ipadapter.py]"]
  node_node_nodes_pulid["PuLID<br/>conditioning nodes<br/>[pulid.py]"]
  node_node_nodes_text["Text encoder<br/>encoder nodes<br/>[text_encoder.py]"]
  node_node_nodes_lora["LoRA nodes<br/>adapter nodes<br/>[flux.py]"]
  node_node_nodes_depth["Depth preproc<br/>preprocessor nodes<br/>[depth.py]"]
  node_node_nodes_tools["Tool nodes<br/>utility nodes<br/>[installers.py]"]
end

subgraph group_group_assets["Assets and validation"]
  node_node_example_workflows["Example workflows<br/>workflow graphs"]
  node_node_test_workflows["Workflow tests<br/>workflow validation<br/>[test_workflows.py]"]
  node_node_test_data["Test data<br/>test fixtures<br/>[models.yaml]"]
end

subgraph group_group_delivery["Docs and release"]
  node_node_docs["Docs<br/>[index.rst]"]
  node_node_scripts["Scripts<br/>[download_models.py]"]
  node_node_packaging["Packaging<br/>python package<br/>[pyproject.toml]"]
end

node_node_init -->|"registers"| node_node_nodes_flux
node_node_init -->|"registers"| node_node_nodes_qwen
node_node_init -->|"registers"| node_node_nodes_z
node_node_init -->|"registers"| node_node_nodes_lora
node_node_init -->|"registers"| node_node_nodes_tools
node_node_nodes_flux -->|"uses"| node_node_wrapper_flux
node_node_nodes_flux -->|"patches"| node_node_model_patcher
node_node_nodes_qwen -->|"loads"| node_node_models_qwen
node_node_nodes_z -->|"loads"| node_node_models_z
node_node_nodes_qwen -->|"extends"| node_node_model_base
node_node_nodes_z -->|"extends"| node_node_model_base
node_node_models_qwen -->|"configured by"| node_node_model_configs
node_node_models_z -->|"configured by"| node_node_model_configs
node_node_model_patcher -->|"shares"| node_node_mixins
node_node_model_patcher -->|"depends on"| node_node_utils
node_node_nodes_lora -->|"depends on"| node_node_utils
node_node_nodes_text -->|"supports"| node_node_model_base
node_node_nodes_ip -->|"conditions"| node_node_nodes_flux
node_node_nodes_pulid -->|"conditions"| node_node_nodes_flux
node_node_nodes_depth -->|"preprocesses"| node_node_nodes_flux
node_node_nodes_lora -->|"adapts"| node_node_nodes_flux
node_node_example_workflows -->|"demonstrates"| node_node_nodes_flux
node_node_example_workflows -->|"demonstrates"| node_node_nodes_qwen
node_node_example_workflows -->|"demonstrates"| node_node_nodes_z
node_node_test_workflows -->|"mirrors"| node_node_example_workflows
node_node_test_workflows -->|"validates with"| node_node_test_data
node_node_docs -->|"documents"| node_node_nodes_flux
node_node_docs -->|"documents"| node_node_model_patcher
node_node_scripts -->|"uses"| node_node_test_data
node_node_scripts -->|"supports"| node_node_packaging
node_node_packaging -->|"packages"| node_node_init

click node_node_init "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/__init__.py"
click node_node_utils "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/utils.py"
click node_node_mixins "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/mixins/model.py"
click node_node_model_base "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/model_base/qwenimage.py"
click node_node_model_configs "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/model_configs/qwenimage.py"
click node_node_model_patcher "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/model_patcher/common.py"
click node_node_models_qwen "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/models/qwenimage.py"
click node_node_models_z "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/models/zimage.py"
click node_node_wrapper_flux "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/wrappers/flux.py"
click node_node_nodes_flux "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/flux.py"
click node_node_nodes_qwen "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/qwenimage.py"
click node_node_nodes_z "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/zimage.py"
click node_node_nodes_ip "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/ipadapter.py"
click node_node_nodes_pulid "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/pulid.py"
click node_node_nodes_text "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/text_encoder.py"
click node_node_nodes_lora "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/lora/flux.py"
click node_node_nodes_depth "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/preprocessors/depth.py"
click node_node_nodes_tools "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/tools/installers.py"
click node_node_example_workflows "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/example_workflows/nunchaku-flux.1-dev.json"
click node_node_test_workflows "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/tests/test_workflows.py"
click node_node_test_data "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/test_data/models.yaml"
click node_node_docs "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/docs/source/index.rst"
click node_node_scripts "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/scripts/download_models.py"
click node_node_packaging "https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/pyproject.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_init,node_node_utils toneBlue
class node_node_mixins,node_node_model_base,node_node_model_configs,node_node_model_patcher,node_node_models_qwen,node_node_models_z,node_node_wrapper_flux toneAmber
class node_node_nodes_flux,node_node_nodes_qwen,node_node_nodes_z,node_node_nodes_ip,node_node_nodes_pulid,node_node_nodes_text,node_node_nodes_lora,node_node_nodes_depth,node_node_nodes_tools toneMint
class node_node_example_workflows,node_node_test_workflows,node_node_test_data toneRose
class node_node_docs,node_node_scripts,node_node_packaging toneIndigo
```

## Components

### Runtime bridge

Adapts ComfyUI graph execution to Nunchaku quantized inference.

- [Plugin entry](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/__init__.py): comfyui plugin · Registers the custom node package and connects ComfyUI to the plugin surface. · `__init__.py`
- [Shared utils](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/utils.py): shared helpers · Cross-cutting helpers used by nodes, models, and wrappers. · `utils.py`

### Model stack

Shared model abstractions, configs, and patchers for supported families.

- [Mixins](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/mixins/model.py): shared model mixins · Reusable model behavior shared across family-specific implementations. · `mixins/model.py`
- [Base models](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/model_base/qwenimage.py): generic model base · Common model behavior and interfaces, especially for Qwen-Image. · `model_base/qwenimage.py`
- [Model configs](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/model_configs/qwenimage.py): family configs · Family-specific configuration mapping for supported model variants. · `model_configs/qwenimage.py`
- [Patchers](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/model_patcher/common.py): quantization patching · Adapts ComfyUI model representations for Nunchaku-specific execution and weight handling. · `model_patcher/common.py`
- [Qwen-Image model](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/models/qwenimage.py): family model · Qwen-Image family model implementation and loading logic. · `models/qwenimage.py`
- [Z-Image model](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/models/zimage.py): family model · Z-Image family model implementation and loading logic. · `models/zimage.py`
- [FLUX wrapper](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/wrappers/flux.py): runtime wrapper · Execution wrapper for FLUX-specific runtime details. · `wrappers/flux.py`

### Node layer

User-facing ComfyUI nodes for models, adapters, preprocessors, and tools.

- [FLUX nodes](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/flux.py): loader nodes · Primary FLUX model-loading and orchestration nodes. · `nodes/models/flux.py`
- [Qwen nodes](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/qwenimage.py): loader nodes · Qwen-Image model nodes and orchestration logic. · `nodes/models/qwenimage.py`
- [Z-Image nodes](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/zimage.py): loader nodes · Z-Image model nodes and orchestration logic. · `nodes/models/zimage.py`
- [IPAdapter](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/ipadapter.py): conditioning nodes · Image-prompt conditioning integration. · `nodes/models/ipadapter.py`
- [PuLID](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/pulid.py): conditioning nodes · Identity-conditioning integration nodes. · `nodes/models/pulid.py`
- [Text encoder](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/models/text_encoder.py): encoder nodes · Text encoder handling, including quantized variants. · `nodes/models/text_encoder.py`
- [LoRA nodes](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/lora/flux.py): adapter nodes · LoRA application nodes, including FLUX-specific handling. · `nodes/lora/flux.py`
- [Depth preproc](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/preprocessors/depth.py): preprocessor nodes · Depth extraction and conditioning preprocessor nodes. · `nodes/preprocessors/depth.py`
- [Tool nodes](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/nodes/tools/installers.py): utility nodes · Installer and safetensors merge utilities exposed as nodes. · `nodes/tools/installers.py`

### Assets and validation

Example workflows, test workflows, and support data used to verify behavior.

- [Example workflows](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/example_workflows/nunchaku-flux.1-dev.json): workflow graphs · Importable ComfyUI graphs demonstrating model families and feature combinations. · `example_workflows/nunchaku-flux.1-dev.json`
- [Workflow tests](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/tests/test_workflows.py): workflow validation · Mirrors example workflows to validate loading and API-level behavior. · `tests/test_workflows.py`
- [Test data](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/test_data/models.yaml): test fixtures · Model lists, custom-node metadata, and dependency manifests for reproducible tests. · `test_data/models.yaml`

### Docs and release

Documentation, scripts, and packaging/release infrastructure.

- [Docs](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/docs/source/index.rst): sphinx docs · Sphinx documentation covering installation, nodes, workflows, and developer guidance. · `docs/source/index.rst`
- [Scripts](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/scripts/download_models.py): ops scripts · Model download, bootstrap, versioning, and release-support scripts. · `scripts/download_models.py`
- [Packaging](https://github.com/nunchaku-tech/comfyui-nunchaku/blob/main/pyproject.toml): python package · Distribution metadata and dependency definition for the ComfyUI plugin. · `pyproject.toml`

## Connections

- Plugin entry → FLUX nodes: registers · Plugin entry exposes the node families to ComfyUI.
- Plugin entry → Qwen nodes: registers · Plugin entry exposes the node families to ComfyUI.
- Plugin entry → Z-Image nodes: registers · Plugin entry exposes the node families to ComfyUI.
- Plugin entry → LoRA nodes: registers · Plugin entry exposes the node families to ComfyUI.
- Plugin entry → Tool nodes: registers · Plugin entry exposes the node families to ComfyUI.
- FLUX nodes → FLUX wrapper: uses · FLUX nodes delegate runtime specifics to the wrapper layer.
- FLUX nodes → Patchers: patches · Loader nodes prepare quantized model execution through patchers.
- Qwen nodes → Qwen-Image model: loads · Qwen nodes instantiate the family model abstraction.
- Z-Image nodes → Z-Image model: loads · Z-Image nodes instantiate the family model abstraction.
- Qwen nodes → Base models: extends · Family nodes rely on generic model behavior.
- Z-Image nodes → Base models: extends · Family nodes rely on generic model behavior.
- Qwen-Image model → Model configs: configured by · Qwen model behavior is parameterized by family configs.
- Z-Image model → Model configs: configured by · Z-Image model behavior is parameterized by family configs.
- Patchers → Mixins: shares · Patchers reuse shared model behaviors and helpers.
- Patchers → Shared utils: depends on · Patchers use common utilities.
- LoRA nodes → Shared utils: depends on · Adapter nodes share common utility logic.
- Text encoder → Base models: supports · Text encoder nodes participate in the model loading pipeline.
- IPAdapter → FLUX nodes: conditions · IPAdapter conditioning feeds into the main FLUX path.
- PuLID → FLUX nodes: conditions · PuLID conditioning feeds into the main FLUX path.
- Depth preproc → FLUX nodes: preprocesses · Depth maps are produced for diffusion conditioning flows.
- LoRA nodes → FLUX nodes: adapts · LoRA nodes modify the FLUX execution path.
- Example workflows → FLUX nodes: demonstrates · Example graphs show how nodes compose around FLUX.
- Example workflows → Qwen nodes: demonstrates · Example graphs show how nodes compose around Qwen-Image.
- Example workflows → Z-Image nodes: demonstrates · Example graphs show how nodes compose around Z-Image.
- Workflow tests → Example workflows: mirrors · Tests mirror the shipped example workflows.
- Workflow tests → Test data: validates with · Workflow tests rely on fixture manifests and expected outputs.
- Docs → FLUX nodes: documents · Docs describe the node families and runtime behavior.
- Docs → Patchers: documents · Docs cover patching and model adaptation concepts.
- Scripts → Test data: uses · Setup and download scripts consume fixture metadata.
- Scripts → Packaging: supports · Release and bootstrap scripts complement package delivery.
- Packaging → Plugin entry: packages · Packaging metadata defines the distributable plugin.
