# facebookincubator/aitemplate architecture

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

- Interactive diagram: https://gitdiagram.com/facebookincubator/aitemplate
- Repository: https://github.com/facebookincubator/aitemplate

## Overview

##### Overall layers/components
- API/Frontend Layer: Located largely in the “python/aitemplate/frontend” and parts of “python/aitemplate/nn”. This layer defines user-facing interfaces for constructing models and enables integration with PyTorch (and standalone operations for environments without PyTorch).
- Runtime Environment: The generated binaries come with a self-contained Python/C++ runtime (see “python/aitemplate/_libinfo.py” and various backend utilities) that directly accepts PyTorch tensors and serves inference.
- FX2AIT Converter: Located in the “fx2ait” directory, this specialized tool converts PyTorch models into the AITemplate engine representation. It leverages the AITLowerer to provide partial conversion for unsupported operators.
- Supporting Infrastructure: Additional components include extensive testing (in “tests”), documentation and tutorials (in “docs” and “examples”), Docker deployment scripts (in “docker”), CI/CD configurations (.github, .circleci), and third-party kernels/libraries (in “3rdparty” and “static”).

##### Out interactions and flow
- The optimized representation is then sent to the Backend Code Generator. Here, hardware-specific code (CUDA for NVIDIA and HIP for AMD) is generated, often through modular and extensible plugins.
- The generated code is compiled (using CMake, Docker builds, or directly via provided build scripts) into a self-contained binary.
- The Runtime then loads this binary, allowing direct inference with either PyTorch tensors or other compatible data.
- How the CI/CD, Docker, and test frameworks are integrated – they support the development, validation, and performance benchmarking of all above layers (as seen in directories like “.circleci”, “docs”, “tests”, and “examples”).

##### Architectural details & design patterns
- Modular and extensible design: Each major component (frontend, compiler, codegen, runtime) is clearly isolated in its own directory so that new operators or fusion strategies can be integrated by adding new files/modules.
- Plugin style for operator extensions: Adding a new operator typically involves creating a graph node (in Python) and a corresponding codegen implementation (in C++ or CUDA/HIP headers).
- Layered architecture: The system is designed with clear separation of concerns such that the high-level API, optimization passes, hardware-specific code generation, and runtime execution are all decoupled.

##### Relevant technologies and frameworks
- Programming Languages: Python (for the high-level API and compiler) and C++ (for the backend and generated code).
- GPU Programming: CUDA for NVIDIA and HIP for AMD – these are central to the Backend.
- Build Tools: CMake, Docker, and Nix build scripts (default.nix).
- Testing/CI: Extensive use of GitHub Actions, Dockerfiles in docker/, and CircleCI configurations.
- External libraries: Integration of NVIDIA CUTLASS, AMD Composable Kernel, and utility libraries (e.g., cub, picojson).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Pipeline
    Input["PyTorch Model Input"]:::default
    API_Frontend["API/Frontend Layer"]:::frontend
    Activation["Activation Module (e.g., nn/activation.py)"]:::frontend
    FX2AIT["FX2AIT Converter"]:::conversion
    Compiler["Compiler & Graph Transformation Engine"]:::compiler
    Transform_Passes["Transformation Passes (Fusion)"]:::compiler
    Backend["Code Generation Backend"]:::backend
    Backend_CUDA["CUDA Codegen"]:::backend
    Backend_ROCm["ROCm Codegen"]:::backend
    Runtime["Runtime Environment"]:::runtime
    Main_Templates["Main Templates"]:::runtime
    Profiler_Runner["Profiler Runner"]:::runtime

    %% Supporting Infrastructure Subgraph
    subgraph "Supporting Infrastructure"
        Testing["Testing (tests)"]:::infrastructure
        Docs["Documentation (docs)"]:::infrastructure
        Examples["Examples"]:::infrastructure
        Docker["Docker Deployment"]:::infrastructure
        CircleCI["CI/CD - CircleCI (.circleci)"]:::infrastructure
        GitHub["CI/CD - GitHub (.github)"]:::infrastructure
        ThirdParty["Third-party Libraries (3rdparty)"]:::infrastructure
    end

    %% Pipeline Connections
    Input -->|"feeds"| API_Frontend
    Input -->|"or through"| FX2AIT
    API_Frontend -->|"builds model graph"| Compiler
    FX2AIT -->|"converts model"| Compiler
    Compiler -->|"optimizes"| Transform_Passes
    Transform_Passes -->|"prepares optimized graph"| Backend
    Backend -->|"generates hardware-specific code"| Runtime
    Runtime -->|"integrates"| Main_Templates
    Runtime -->|"monitors"| Profiler_Runner

    %% Supporting Infra dotted connections
    Testing ---|"validates"| API_Frontend
    Docs ---|"guides"| API_Frontend
    Examples ---|"illustrate usage"| API_Frontend
    Docker ---|"deploys"| Backend
    CircleCI ---|"automates"| Compiler
    GitHub ---|"manages"| Compiler
    ThirdParty ---|"integrates with"| Backend

    %% Backend internal splits
    Backend -->|"CUDA path"| Backend_CUDA
    Backend -->|"ROCm path"| Backend_ROCm

    %% Class Definitions with Colors
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef compiler fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef backend fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef runtime fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef conversion fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef infrastructure fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef default fill:#ffffff,stroke:#000000,stroke-width:1px;

    %% Click Events
    click API_Frontend "https://github.com/facebookincubator/aitemplate/tree/main/python/aitemplate/frontend"
    click Activation "https://github.com/facebookincubator/aitemplate/blob/main/python/aitemplate/frontend/nn/activation.py"
    click FX2AIT "https://github.com/facebookincubator/aitemplate/blob/main/fx2ait/fx2ait.py"
    click Compiler "https://github.com/facebookincubator/aitemplate/tree/main/python/aitemplate/compiler"
    click Transform_Passes "https://github.com/facebookincubator/aitemplate/tree/main/python/aitemplate/compiler/transform"
    click Backend "https://github.com/facebookincubator/aitemplate/tree/main/python/aitemplate/backend"
    click Backend_CUDA "https://github.com/facebookincubator/aitemplate/tree/main/python/aitemplate/backend/cuda"
    click Backend_ROCm "https://github.com/facebookincubator/aitemplate/tree/main/python/aitemplate/backend/rocm"
    click Runtime "https://github.com/facebookincubator/aitemplate/blob/main/python/aitemplate/_libinfo.py"
    click Main_Templates "https://github.com/facebookincubator/aitemplate/blob/main/python/aitemplate/backend/main_templates.py"
    click Profiler_Runner "https://github.com/facebookincubator/aitemplate/blob/main/python/aitemplate/backend/profiler_runner.py"
    click Testing "https://github.com/facebookincubator/aitemplate/tree/main/tests"
    click Docs "https://github.com/facebookincubator/aitemplate/tree/main/docs"
    click Examples "https://github.com/facebookincubator/aitemplate/tree/main/examples"
    click Docker "https://github.com/facebookincubator/aitemplate/tree/main/docker"
    click CircleCI "https://github.com/facebookincubator/aitemplate/blob/main/.circleci"
    click GitHub "https://github.com/facebookincubator/aitemplate/blob/main/.github"
    click ThirdParty "https://github.com/facebookincubator/aitemplate/tree/main/3rdparty"
```
