# tencent/hunyuancustom architecture

> Architecture diagram of the GitHub repository tencent/hunyuancustom, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tencent/hunyuancustom
- Repository: https://github.com/tencent/hunyuancustom

## Overview

##### Project Type and Purpose
- HunyuanCustom is a full-stack, research-grade video generation framework.
- It provides both a programmatic inference API (Python scripts / library) and a web interface (Gradio/Flask), driven by a diffusion-based, multimodal model that accepts text, image, audio, and video conditions to produce customized videos.

##### File Structure Analysis
- Top-level:
- assets/      → static resources (images, videos, meta files) used as examples or UI materials
- hymm_sp/     → core Python library implementing model components and inference pipelines
- hymm_gradio/ → lightweight web service wrappers (Flask & Gradio endpoints)
- models/      → placeholder for downloaded pre-trained checkpoints
- scripts/     → shell scripts to launch inference or server
- requirements.txt, Docker instructions, and conda setup files for environment configuration
- hymm_sp subpackages reveal architectural layers:
- config, constants → global settings
- data_kits → data loading and preprocessing (VideoDataset)
- diffusion/pipelines → custom diffusion pipeline
- diffusion/schedulers → scheduler implementation for the denoising process
- modules → neural network building blocks (attention, activation, normalization, MLPs, FP8 optimizations, text-image fusion, AudioNet, patchify-based video conditioning)
- vae → encoder/decoder for latent video representation
- text_encoder → multimodal text/image embedding fusion (LLaVA-based)
- inference.py, sample_*.py → entry points for various inference modes (single-GPU, multi-GPU, low-VRAM)

##### README Insights
- Explicit mention of four conditioning modalities: text, image, audio, video.
- Two interfaces: CLI scripts (sample_batch.py, sample_gpu_poor.py) and Gradio server (gradio_ref2v.py, gradio_id.py).

##### Detailed Capturing of Architectural Elements
- Don’t omit the data_kits layer: show how raw assets can feed directly into preprocessing.
- Two distinct inference entry points (sample_gpu_poor.py vs. sample_batch.py) to illustrate resource-adaptive modes.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Interface Layer
    subgraph "User Interface Layer"
        subgraph "CLI Scripts" 
            run_single["run_single.sh"]:::ui
            run_gpu_poor["run_gpu_poor.sh"]:::ui
            run_batch["run_sample_batch_sp.sh"]:::ui
            samp4090["sample_4090.py"]:::ui
            sampBatchPy["sample_batch.py"]:::ui
            sampGpuPoorPy["sample_gpu_poor.py"]:::ui
            sampInferencePy["sample_inference.py"]:::ui
        end
        subgraph "Web Service"
            flaskRef["flask_ref2v.py"]:::ui
            gradioRef["gradio_ref2v.py"]:::ui
            gradioID["gradio_id.py"]:::ui
            toolEnd2End["tool_for_end2end.py"]:::ui
            runGradio["run_gradio.sh"]:::ui
        end
    end

    %% Inference API Layer
    inference["Inference API\n(inference.py)"]:::core

    %% Conditioning Modules
    subgraph "Conditioning Modules" 
        textImg["Text-Image Fusion Module"]:::cond
        audioNet["AudioNet Module"]:::cond
        videoInject["Video-Driven Injection Module"]:::cond
        imageID["Image ID Enhancement Module"]:::cond
    end

    %% Diffusion Core
    subgraph "Diffusion Core" 
        pipeline["HunyuanVideo Custom Pipeline"]:::core
        scheduler["Scheduling Flow\nMatch Discrete"]:::core
    end

    %% Latent Representation
    subgraph "Latent Video Representation"
        vaeEncDec["VAE Encoder/Decoder"]:::latent
    end

    %% Data Handling
    subgraph "Data Handling"
        assets["assets/"]:::data
        dataKits["VideoDataset\n& Data Tools"]:::data
    end

    %% Model Weights Store
    models["models/"]:::storage

    %% Configuration & Constants
    config["config.py\nconstants.py"]:::storage

    %% External Dependencies
    subgraph "External Dependencies"
        extTorch["PyTorch"]:::ext
        extDiff["diffusers"]:::ext
        extFlash["flash-attention"]:::ext
        extCUDA["CUDA"]:::ext
        extLLaVA["LLaVA"]:::ext
    end

    %% Data Flow
    run_single -->|launch| inference
    run_gpu_poor -->|launch| inference
    run_batch -->|launch| inference
    samp4090 -->|invoke| inference
    sampBatchPy -->|invoke| inference
    sampGpuPoorPy -->|invoke| inference
    sampInferencePy -->|invoke| inference
    flaskRef -->|HTTP Request| inference
    gradioRef -->|HTTP Request| inference
    gradioID -->|HTTP Request| inference
    toolEnd2End -->|invoke| inference
    runGradio -->|launch| gradioRef

    inference -->|text embeddings| textImg
    inference -->|audio features| audioNet
    inference -->|video tokens| videoInject
    inference -->|image features| imageID

    textImg -->|fused conditions| pipeline
    audioNet -->|audio conditions| pipeline
    videoInject -->|video conditions| pipeline
    imageID -->|enhanced image| pipeline

    pipeline -->|latent frames| scheduler
    scheduler -->|refined latents| pipeline
    pipeline -->|final latents| vaeEncDec
    vaeEncDec -->|raw frames| inference

    assets -->|inputs| dataKits
    dataKits -->|dataset items| inference

    inference -->|load weights| models

    inference -->|use| config

    %% External dependencies connections
    extTorch -.->|NN modules| audioNet
    extTorch -.->|NN modules| videoInject
    extTorch -.->|NN modules| imageID
    extDiff -.->|pipeline/scheduler| pipeline
    extFlash -.->|optimized attention| audioNet
    extCUDA -.->|GPU ops| pipeline
    extLLaVA -.->|embedding fusion| textImg

    %% Click Events
    click run_single "https://github.com/tencent/hunyuancustom/blob/main/scripts/run_single.sh"
    click run_gpu_poor "https://github.com/tencent/hunyuancustom/blob/main/scripts/run_gpu_poor.sh"
    click run_batch "https://github.com/tencent/hunyuancustom/blob/main/scripts/run_sample_batch_sp.sh"
    click samp4090 "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/sample_4090.py"
    click sampBatchPy "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/sample_batch.py"
    click sampGpuPoorPy "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/sample_gpu_poor.py"
    click sampInferencePy "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/sample_inference.py"
    click flaskRef "https://github.com/tencent/hunyuancustom/blob/main/hymm_gradio/flask_ref2v.py"
    click gradioRef "https://github.com/tencent/hunyuancustom/blob/main/hymm_gradio/gradio_ref2v.py"
    click gradioID "https://github.com/tencent/hunyuancustom/blob/main/hymm_gradio/gradio_id.py"
    click toolEnd2End "https://github.com/tencent/hunyuancustom/blob/main/hymm_gradio/tool_for_end2end.py"
    click runGradio "https://github.com/tencent/hunyuancustom/blob/main/scripts/run_gradio.sh"
    click inference "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/inference.py"
    click textImg "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/text_encoder/__init__.py"
    click audioNet "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/modules/models.py"
    click videoInject "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/modules/posemb_layers.py"
    click imageID "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/modules/token_refiner.py"
    click pipeline "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/diffusion/pipelines/pipeline_hunyuan_video_custom.py"
    click scheduler "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/diffusion/schedulers/scheduling_flow_match_discrete.py"
    click vaeEncDec "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/vae/vae.py"
    click assets "https://github.com/tencent/hunyuancustom/tree/main/assets/"
    click dataKits "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/data_kits/video_dataset.py"
    click models "https://github.com/tencent/hunyuancustom/tree/main/models/"
    click config "https://github.com/tencent/hunyuancustom/blob/main/hymm_sp/config.py"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#3399ff,color:#000
    classDef core fill:#ccffcc,stroke:#33cc33,color:#000
    classDef cond fill:#ffe5b4,stroke:#ff9933,color:#000
    classDef latent fill:#e5ccff,stroke:#9933ff,color:#000
    classDef data fill:#ccffff,stroke:#33cccc,color:#000
    classDef storage fill:#f2f2f2,stroke:#999999,color:#000
    classDef ext fill:#ffffff,stroke-dasharray: 3 3,stroke:#666666,color:#000
```
