# kijai/comfyui-hunyuanvideowrapper architecture

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

- Interactive diagram: https://gitdiagram.com/kijai/comfyui-hunyuanvideowrapper
- Repository: https://github.com/kijai/comfyui-hunyuanvideowrapper

## Overview

1. This repository is a ComfyUI custom-node package for HunyuanVideo, not a standalone application. Its job is to bridge ComfyUI graphs with a full video-generation stack: model loading, text/image conditioning, diffusion sampling, VAE encode/decode, optional inversion/editing, and post-processing.

2. The top-level node entrypoints are `nodes.py` and `nodes_rf_inversion.py`. These expose the graph-facing API: loaders, encoders, samplers, and editing/inversion nodes. `context.py` and `hyvideo/globals.py` appear to hold shared runtime state and cross-node configuration/caches so multiple nodes can cooperate inside one ComfyUI session.

3. The main model implementation lives under `hyvideo/`, which is the repository’s internal HunyuanVideo runtime. It contains the core diffusion pipeline, model blocks, text encoder, VAE, schedulers, and utility code needed to run the model without relying on a separate external package.

4. `hyvideo/diffusion/pipelines/pipeline_hunyuan_video.py` is the orchestration layer. It is where conditioning, latent initialization, denoising steps, and decode/return behavior are coordinated. The schedulers in `hyvideo/diffusion/schedulers/` provide alternate sampling algorithms, including DPM-Solver, FlowMatch, SA-Solver, and UniPC variants.

5. The transformer-side network is split across `hyvideo/modules/` into reusable building blocks: attention, MLP, normalization, modulation, embedding, positional embedding, activation layers, and token refinement. This is the main compute path for denoising video latents. `fp8_optimization.py` and `hyvideo/modules/fp8_optimization.py` support FP8 weight handling and speed/memory optimizations.

6. Conditioning is handled by `hyvideo/text_encoder/`, which implements an LLaVA-style multimodal text encoder stack (`configuration_llava.py`, `modeling_llava.py`, `processing_llava.py`). This supports prompt rewriting and image-conditioned prompting, matching the README’s “IP2V” workflow where images can be injected as part of the prompt rather than as a first video frame.

7. There is also a boundary that allows the native ComfyUI text encoders to be used instead of the bundled multimodal encoder. In architecture terms, that means the prompt-conditioning path can be either “internal HunyuanVideo text/VLM encoder” or “external ComfyUI CLIP/text embeddings,” depending on the workflow.

8. Video compression and reconstruction are isolated in `hyvideo/vae/`. The causal 3D autoencoder (`autoencoder_kl_causal_3d.py`, `unet_causal_3d_blocks.py`, `vae.py`) converts between frame/video space and latent space. `configs/hy_vae_config.json` supplies the architecture/configuration details for that component.

9. Post-processing is optionally handled by the separate `enhance_a_video/` package. This is an enhancement stage that can be inserted after generation to improve output quality with little or no memory overhead, so in a diagram it should sit downstream of the decoder rather than inside the main diffusion loop.

10. The repo also includes dedicated support for video inversion/editing via `nodes_rf_inversion.py`, plus example workflows under `example_workflows/` that exercise the main modes: text-to-video, image-to-video, video-to-video, IP2V, low-VRAM, keyframe control, prompt mixing, and enhancement. These examples are useful for understanding the intended graph topology and boundaries.

11. Runtime/model assets are externalized rather than embedded: Hugging Face downloads and ComfyUI model directories are used for transformer weights, VAE weights, CLIP models, and LLM/text-encoder assets. The repository therefore sits between ComfyUI and model storage, with model artifacts being a significant part of the deployment architecture.

12. Overall data flow is: ComfyUI inputs and model selections → node layer assembles prompt/image/video conditioning → text/VLM encoder and/or native ComfyUI embeddings produce conditioning tensors → diffusion pipeline iterates latent denoising with a selected scheduler and transformer blocks → VAE decodes video frames → optional enhancement/post-processing → final video output.

Main entry points: [`hyvideo/diffusion/pipelines/pipeline_hunyuan_video.py`](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/diffusion/pipelines/pipeline_hunyuan_video.py) (Pipeline), [`hyvideo/modules/models.py`](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/models.py) (Transformer core), [`nodes.py`](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/nodes.py) (Nodes), [`hyvideo/text_encoder`](https://github.com/kijai/comfyui-hunyuanvideowrapper/tree/main/hyvideo/text_encoder) (Text encoder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_comfyui["ComfyUI integration"]
  node_node_nodes["Nodes<br/>comfyui node api<br/>[nodes.py]"]
  node_node_rf_inversion["Inversion nodes<br/>comfyui editing api"]
  node_node_context(("Session state<br/>runtime state<br/>[context.py]"))
  node_node_globals(("Global config<br/>shared caches"))
end

subgraph group_group_runtime["HunyuanVideo runtime"]
  node_node_pipeline["Pipeline<br/>diffusion orchestration"]
  node_node_schedulers["Schedulers<br/>sampling algorithms"]
  node_node_models["Transformer core<br/>denoiser network<br/>[models.py]"]
  node_node_attention["Attention<br/>transformer blocks<br/>[attention.py]"]
  node_node_mlp["MLP layers<br/>transformer blocks<br/>[mlp_layers.py]"]
  node_node_norm["Norm and modulate<br/>transformer blocks<br/>[norm_layers.py]"]
  node_node_embeddings["Embeddings<br/>token and pos embeds<br/>[embed_layers.py]"]
  node_node_token_refiner["Token refiner<br/>conditioning refine<br/>[token_refiner.py]"]
  node_node_fp8["FP8 optimize<br/>precision optimization"]
  node_node_text_encoder["Text encoder<br/>llava multimodal encoder"]
  node_node_prompt_rewrite["Prompt rewrite<br/>conditioning prep<br/>[prompt_rewrite.py]"]
  node_node_vae["VAE<br/>causal video autoencoder"]
end

subgraph group_group_post["Post-processing"]
  node_node_enhance["Enhance video<br/>postprocess stage<br/>[enhance.py]"]
end

subgraph group_group_assets["Assets and examples"]
  node_node_vae_config["VAE config<br/>model config<br/>[hy_vae_config.json]"]
  node_node_examples["Workflows<br/>example graphs"]
end

node_node_nodes -->|"uses"| node_node_context
node_node_nodes -->|"uses"| node_node_globals
node_node_nodes -->|"builds"| node_node_pipeline
node_node_nodes -->|"selects"| node_node_text_encoder
node_node_nodes -->|"loads"| node_node_vae
node_node_rf_inversion -->|"drives"| node_node_pipeline
node_node_rf_inversion -->|"conditions"| node_node_text_encoder
node_node_pipeline -->|"samples with"| node_node_schedulers
node_node_pipeline -->|"runs"| node_node_models
node_node_pipeline -->|"decodes via"| node_node_vae
node_node_pipeline -->|"prepares"| node_node_prompt_rewrite
node_node_text_encoder -->|"refines"| node_node_token_refiner
node_node_text_encoder -->|"emits"| node_node_embeddings
node_node_models -->|"contains"| node_node_attention
node_node_models -->|"contains"| node_node_mlp
node_node_models -->|"contains"| node_node_norm
node_node_models -->|"uses"| node_node_embeddings
node_node_models -->|"optimizes with"| node_node_fp8
node_node_vae -->|"configured by"| node_node_vae_config
node_node_pipeline -->|"optionally postprocesses"| node_node_enhance
node_node_examples -->|"demonstrates"| node_node_nodes
node_node_examples -->|"demonstrates"| node_node_rf_inversion
node_node_examples -->|"demonstrates"| node_node_enhance

click node_node_nodes "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/nodes.py"
click node_node_rf_inversion "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/nodes_rf_inversion.py"
click node_node_context "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/context.py"
click node_node_pipeline "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/diffusion/pipelines/pipeline_hunyuan_video.py"
click node_node_schedulers "https://github.com/kijai/comfyui-hunyuanvideowrapper/tree/main/hyvideo/diffusion/schedulers"
click node_node_models "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/models.py"
click node_node_attention "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/attention.py"
click node_node_mlp "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/mlp_layers.py"
click node_node_norm "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/norm_layers.py"
click node_node_embeddings "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/embed_layers.py"
click node_node_token_refiner "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/token_refiner.py"
click node_node_fp8 "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/fp8_optimization.py"
click node_node_text_encoder "https://github.com/kijai/comfyui-hunyuanvideowrapper/tree/main/hyvideo/text_encoder"
click node_node_prompt_rewrite "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/prompt_rewrite.py"
click node_node_vae "https://github.com/kijai/comfyui-hunyuanvideowrapper/tree/main/hyvideo/vae"
click node_node_enhance "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/enhance_a_video/enhance.py"
click node_node_vae_config "https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/configs/hy_vae_config.json"
click node_node_examples "https://github.com/kijai/comfyui-hunyuanvideowrapper/tree/main/example_workflows"

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_nodes,node_node_rf_inversion,node_node_context,node_node_globals toneBlue
class node_node_pipeline,node_node_schedulers,node_node_models,node_node_attention,node_node_mlp,node_node_norm,node_node_embeddings,node_node_token_refiner,node_node_fp8,node_node_text_encoder,node_node_prompt_rewrite,node_node_vae toneAmber
class node_node_enhance toneMint
class node_node_vae_config,node_node_examples toneRose
```

## Components

### ComfyUI integration

Repository-facing node entrypoints and shared session state.

- [Nodes](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/nodes.py): comfyui node api · Main ComfyUI-facing loaders, encoders, samplers, and generation nodes. · `nodes.py`
- [Inversion nodes](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/nodes_rf_inversion.py): comfyui editing api · Specialized ComfyUI nodes for inversion and video editing workflows. · `nodes_rf_inversion.py`
- [Session state](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/context.py): runtime state · Shared per-session context and cross-node coordination state. · `context.py`
- **Global config**: shared caches · Repository-wide runtime globals and caches used by multiple nodes.

### HunyuanVideo runtime

Internal model, pipeline, text, and VAE implementation.

- [Pipeline](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/diffusion/pipelines/pipeline_hunyuan_video.py): diffusion orchestration · Coordinates conditioning, latent setup, denoising, and decode/return flow. · `hyvideo/diffusion/pipelines/pipeline_hunyuan_video.py`
- [Schedulers](https://github.com/kijai/comfyui-hunyuanvideowrapper/tree/main/hyvideo/diffusion/schedulers): sampling algorithms · Alternate denoising step schedules such as DPM-Solver, FlowMatch, SA-Solver, and UniPC. · `hyvideo/diffusion/schedulers`
- [Transformer core](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/models.py): denoiser network · Main video denoising model composition and compute graph. · `hyvideo/modules/models.py`
- [Attention](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/attention.py): transformer blocks · Attention layers used in the denoising network. · `hyvideo/modules/attention.py`
- [MLP layers](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/mlp_layers.py): transformer blocks · Feed-forward layers for the core model. · `hyvideo/modules/mlp_layers.py`
- [Norm and modulate](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/norm_layers.py): transformer blocks · Normalization and modulation layers for network conditioning. · `hyvideo/modules/norm_layers.py`
- [Embeddings](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/embed_layers.py): token and pos embeds · Input embedding and positional embedding layers. · `hyvideo/modules/embed_layers.py`
- [Token refiner](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/token_refiner.py): conditioning refine · Refines conditioning tokens before denoising. · `hyvideo/modules/token_refiner.py`
- [FP8 optimize](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/modules/fp8_optimization.py): precision optimization · FP8 weight handling and performance/memory optimization helpers. · `hyvideo/modules/fp8_optimization.py`
- [Text encoder](https://github.com/kijai/comfyui-hunyuanvideowrapper/tree/main/hyvideo/text_encoder): llava multimodal encoder · Bundled LLaVA-style prompt and image-conditioned encoding stack. · `hyvideo/text_encoder`
- [Prompt rewrite](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/hyvideo/prompt_rewrite.py): conditioning prep · Prompt rewriting utilities used before conditioning is encoded. · `hyvideo/prompt_rewrite.py`
- [VAE](https://github.com/kijai/comfyui-hunyuanvideowrapper/tree/main/hyvideo/vae): causal video autoencoder · Encodes and decodes between video frames and latent space. · `hyvideo/vae`

### Post-processing

Optional output enhancement stage.

- [Enhance video](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/enhance_a_video/enhance.py): postprocess stage · Optional downstream enhancement applied after decoding. · `enhance_a_video/enhance.py`

### Assets and examples

Externalized configs, weights, and example workflows.

- [VAE config](https://github.com/kijai/comfyui-hunyuanvideowrapper/blob/main/configs/hy_vae_config.json): model config · Architecture configuration for the causal 3D autoencoder. · `configs/hy_vae_config.json`
- [Workflows](https://github.com/kijai/comfyui-hunyuanvideowrapper/tree/main/example_workflows): example graphs · Reference ComfyUI workflows for generation, inversion, low-VRAM, IP2V, and enhancement modes. · `example_workflows`

## Connections

- Nodes → Session state: uses · Node execution reads and updates shared session state.
- Nodes → Global config: uses · Node layer consults global caches and runtime configuration.
- Nodes → Pipeline: builds · ComfyUI nodes assemble inputs for the diffusion pipeline.
- Nodes → Text encoder: selects · Node layer can route conditioning through the bundled encoder.
- Nodes → VAE: loads · Nodes expose model loading for the video autoencoder.
- Inversion nodes → Pipeline: drives · Inversion/editing workflows reuse the core diffusion pipeline.
- Inversion nodes → Text encoder: conditions · Editing and inversion may still depend on prompt/image conditioning.
- Pipeline → Schedulers: samples with · Pipeline chooses a scheduler to control denoising updates.
- Pipeline → Transformer core: runs · Pipeline executes the transformer denoiser over latent states.
- Pipeline → VAE: decodes via · Pipeline hands final latents to the VAE for video reconstruction.
- Pipeline → Prompt rewrite: prepares · Prompt rewriting can occur before conditioning is encoded.
- Text encoder → Token refiner: refines · Multimodal encoding feeds refined conditioning tokens.
- Text encoder → Embeddings: emits · Encoder stack produces token and position embeddings for conditioning.
- Transformer core → Attention: contains · Core denoiser is built from attention blocks.
- Transformer core → MLP layers: contains · Core denoiser includes MLP/feed-forward blocks.
- Transformer core → Norm and modulate: contains · Model uses normalization and modulation layers.
- Transformer core → Embeddings: uses · Network consumes embedded conditioning and latent tokens.
- Transformer core → FP8 optimize: optimizes with · Model execution can be accelerated or reduced in memory with FP8 helpers.
- VAE → VAE config: configured by · Autoencoder architecture is parameterized by the VAE config file.
- Pipeline → Enhance video: optionally postprocesses · Decoded output may pass to an enhancement stage.
- Workflows → Nodes: demonstrates · Example workflows show how the ComfyUI node layer is intended to be used.
- Workflows → Inversion nodes: demonstrates · Example workflows cover inversion and editing modes.
- Workflows → Enhance video: demonstrates · Some examples include the optional enhancement stage.
