How kijai/comfyui-hunyuanvideowrapper works
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: pipeline_hunyuan_video.py Pipeline · models.py Transformer core · nodes.py Nodes · text_encoder Text encoder
Components 19
ComfyUI integration
Repository-facing node entrypoints and shared session state.
- Nodes · Main ComfyUI-facing loaders, encoders, samplers, and generation nodes.
nodes.py - Inversion nodes · Specialized ComfyUI nodes for inversion and video editing workflows.
nodes_rf_inversion.py - Session state · Shared per-session context and cross-node coordination state.
context.py - Global config · Repository-wide runtime globals and caches used by multiple nodes.
HunyuanVideo runtime
Internal model, pipeline, text, and VAE implementation.
- Pipeline · Coordinates conditioning, latent setup, denoising, and decode/return flow.
hyvideo/diffusion/pipelines/pipeline_hunyuan_video.py - Schedulers · Alternate denoising step schedules such as DPM-Solver, FlowMatch, SA-Solver, and UniPC.
hyvideo/diffusion/schedulers - Transformer core · Main video denoising model composition and compute graph.
hyvideo/modules/models.py - Attention · Attention layers used in the denoising network.
hyvideo/modules/attention.py - MLP layers · Feed-forward layers for the core model.
hyvideo/modules/mlp_layers.py - Norm and modulate · Normalization and modulation layers for network conditioning.
hyvideo/modules/norm_layers.py - Embeddings · Input embedding and positional embedding layers.
hyvideo/modules/embed_layers.py - Token refiner · Refines conditioning tokens before denoising.
hyvideo/modules/token_refiner.py - FP8 optimize · FP8 weight handling and performance/memory optimization helpers.
hyvideo/modules/fp8_optimization.py - Text encoder · Bundled LLaVA-style prompt and image-conditioned encoding stack.
hyvideo/text_encoder - Prompt rewrite · Prompt rewriting utilities used before conditioning is encoded.
hyvideo/prompt_rewrite.py - VAE · Encodes and decodes between video frames and latent space.
hyvideo/vae
Post-processing
Optional output enhancement stage.
- Enhance video · Optional downstream enhancement applied after decoding.
enhance_a_video/enhance.py
Assets and examples
Externalized configs, weights, and example workflows.
- VAE config · Architecture configuration for the causal 3D autoencoder.
configs/hy_vae_config.json - Workflows · Reference ComfyUI workflows for generation, inversion, low-VRAM, IP2V, and enhancement modes.
example_workflows
Connections 23
- 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.