# firtoz/trellis architecture

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

- Interactive diagram: https://gitdiagram.com/firtoz/trellis
- Repository: https://github.com/firtoz/trellis

## Overview

##### Project Type and Purpose
- TRELLIS is a 3D asset generation tool that converts images (and eventually text) into high-quality 3D models in multiple representations (3D Gaussians, Radiance Fields, and textured meshes). Its core differentiator is a structured latent (SLAT) representation that unifies the generation process.
- The project supports both a web demo (using a Gradio-based interface in app.py) and local Python pipelines for generating and rendering 3D assets from input images. There are also dataset toolkits provided for data preparation and several extensions (like vox2seq) that leverage CUDA for performance acceleration.

##### Key Components
a. Frontend / Web Demo:
- “app.py” serves as the gateway for the browser-based demo. This component, depending on Gradio (or Hugging Face Spaces), allows users to upload images and view generated 3D assets.
b. Inference Pipeline & API Layer:
- The primary logic resides in the pipelines inside the “trellis/pipelines” directory (for example, TrellisImageTo3DPipeline). This module orchestrates the overall 3D generation process.
- “predict.py” and “example.py” exemplify how to trigger the pipeline from different contexts.
c. Model Logic:
- Located in “trellis/models” (including submodules like sparse_structure_flow, structured_latent_vae, etc.), this part encapsulates the deep learning models and the unified SLAT representation.
d. Core Modules & Utilities:
- The “trellis/modules” directory contains various building blocks (e.g., attention, sparse convolutions, transformers) that facilitate the computations behind the scenes.
- The “trellis/utils” provide helper functions for rendering, postprocessing, and general utilities for random and render operations.
e. Data Preparation and Toolkit Components:
- The “dataset_toolkits” folder contains scripts (e.g., build_metadata.py, download.py, encode_latent.py) for handling datasets, performing rendering tasks, voxelization, and dataset building.
f. Extensions:
- The “extensions” folder (specifically, vox2seq) includes CUDA implementations and benchmarks that extend the core functionality—especially for high-performance or experimental modules.
g. Representations & Renderers:
- Under “trellis/representations” and “trellis/renderers”, different output formats of the generated 3D models are managed, ensuring compatibility with formats like GLB, PLY, or supporting video renderings (mp4).

##### Relationships and Data Flow
- User提交s an image through the Web Demo (app.py) or a script (example.py).
- The input is fed into the pipeline (for example, TrellisImageTo3DPipeline) that locates and invokes the appropriate model modules in “trellis/models”.
- The model’s output, which leverages the defined SLAT representations, passes through various renderers and utilities:
- Renderers convert the model’s internal representations to videos (mp4) or 3D file formats (GLB, PLY).
- Postprocessing utilities in “trellis/utils” refine the outputs.
- Separately, dataset toolkits (found in “dataset_toolkits”) prepare or process training data, impacting model training and evaluation (although training code is noted as a TODO item).
- The “extensions/vox2seq” acts as an additional layer that may expedite or offer alternative computations (via GPU/CUDA kernels).

##### Architectural Patterns
- The design follows a modular, layered architecture where the interface (web demo or scripts) sits atop clearly partitioned pipelines.
- There is a clear separation between model computation, utility/rendering functions, and dataset processing.
- Components communicate via well-defined input-output contracts (e.g., pipelines expect images and output multi-format 3D assets).

## Diagram (Mermaid)

```mermaid
graph TD
    U["User Input"]:::user

    subgraph "Frontend"
        A1["Web Demo (app)"]:::frontend
    end

    subgraph "Pipeline/API Layer"
        B1["Pipeline (trellis/pipelines)"]:::pipeline
        B2["Predict (predict.py)"]:::pipeline
        B3["Example (example.py)"]:::pipeline
    end

    subgraph "Model Core"
        C1["Model Logic (trellis/models)"]:::model
    end

    subgraph "Core Modules & Utilities"
        D1["Core Modules (trellis/modules)"]:::utility
        D2["Utilities (trellis/utils)"]:::utility
    end

    subgraph "Representations"
        E1["Representations (trellis/representations)"]:::representation
    end

    subgraph "Renderers"
        F1["Renderers (trellis/renderers)"]:::renderer
    end

    subgraph "Dataset Toolkits"
        G1["Dataset Toolkits (dataset_toolkits)"]:::dataset
    end

    subgraph "Extensions"
        H1["Extensions (vox2seq)"]:::extension
        H2["CUDA Modules (extensions/vox2seq/src)"]:::extension
        H3["PyTorch Modules (extensions/vox2seq/vox2seq/pytorch)"]:::extension
    end

    %% Data Flow Connections
    U -->|"image_input"| A1
    A1 -->|"pipeline_trigger"| B1
    B2 -->|"trigger_pipeline"| B1
    B3 -->|"trigger_pipeline"| B1
    B1 -->|"latent_generation"| C1
    C1 -->|"core_processing"| D1
    C1 -->|"post_process"| D2
    D2 -->|"refine_output"| F1
    C1 -->|"3D_latent"| E1
    E1 -->|"render_output"| F1
    G1 -->|"dataset_prep"| C1
    B1 -->|"accelerated_compute"| H1
    H1 -->|"enhance_model"| C1
    H1 -->|"CUDA_kernels"| H2
    H1 -->|"PyTorch_module"| H3

    %% Click Events
    click A1 "https://github.com/firtoz/trellis/blob/main/app.py"
    click B1 "https://github.com/firtoz/trellis/blob/main/trellis/pipelines/trellis_image_to_3d.py"
    click B2 "https://github.com/firtoz/trellis/blob/main/predict.py"
    click B3 "https://github.com/firtoz/trellis/blob/main/example.py"
    click C1 "https://github.com/firtoz/trellis/tree/main/trellis/models"
    click D1 "https://github.com/firtoz/trellis/tree/main/trellis/modules"
    click D2 "https://github.com/firtoz/trellis/tree/main/trellis/utils"
    click G1 "https://github.com/firtoz/trellis/tree/main/dataset_toolkits"
    click H1 "https://github.com/firtoz/trellis/tree/main/extensions/vox2seq"
    click H2 "https://github.com/firtoz/trellis/tree/main/extensions/vox2seq/src"
    click H3 "https://github.com/firtoz/trellis/tree/main/extensions/vox2seq/vox2seq/pytorch"
    click E1 "https://github.com/firtoz/trellis/tree/main/trellis/representations"
    click F1 "https://github.com/firtoz/trellis/tree/main/trellis/renderers"

    %% Class Definitions for Colors
    classDef user fill:#D5F5E3,stroke:#333,stroke-width:2px;
    classDef frontend fill:#FCEBB6,stroke:#333,stroke-width:2px;
    classDef pipeline fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef model fill:#A9DFBF,stroke:#333,stroke-width:2px;
    classDef utility fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef dataset fill:#D2B4DE,stroke:#333,stroke-width:2px;
    classDef extension fill:#F5B7B1,stroke:#333,stroke-width:2px;
    classDef representation fill:#D1F2EB,stroke:#333,stroke-width:2px;
    classDef renderer fill:#FAD7A0,stroke:#333,stroke-width:2px;
```
