# lucidrains/flash-attention-jax architecture

> Architecture diagram of the GitHub repository lucidrains/flash-attention-jax, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lucidrains/flash-attention-jax
- Repository: https://github.com/lucidrains/flash-attention-jax

## Overview

##### Project type and purpose
- This is an open-source Python library (“flash-attention-jax”) that implements Flash Attention algorithms in JAX.
- Purpose: provide high-performance, memory-efficient exact attention kernels (flash_attention, causal_flash_attention, cosine_sim_flash_attention, rabe_attention) for educational use and experimentation with XLA compiler optimizations.

##### File structure
- Top level:
- .github/workflows/python-publish.yml → CI/CD pipeline for packaging and publishing to PyPI
- setup.py → Python packaging metadata
- flash_attention_jax/ → main package directory
- Inside flash_attention_jax:
- attention.py, flash_attention.py, causal_flash_attention.py, cosine_sim_flash_attention.py, rabe_attention.py → algorithm implementations
- utils.py → shared helper routines
- __init__.py → API surface exposing the key functions

##### README for additional insights
- Lists provided functions, usage examples, performance caveats, and citation references.
- No distributed or multi-service components: purely a local library layered on top of JAX and XLA.

##### Main components
a. User Application / Notebook
– Imports flash-attention-jax and JAX primitives (PRNGKey).
b. flash_attention_jax API Layer
– Exposed functions: flash_attention, plain_attention, causal_flash_attention, cosine_sim_flash_attention, rabe_attention, value_and_grad_difference
c. Internal Modules
– attention.py (shared core logic), flash_attention.py (IO-aware kernel), causal_*.py, cosine_sim_*.py, rabe_*.py, utils.py
d. JAX/XLA Compiler
– Traces Python functions into XLA HLO, performs fusion and memory planning
e. Hardware Backend
– CPU or GPU runtime executing the compiled kernels
f. Packaging & CI/CD
– setup.py for pip install; GitHub Actions workflow for publish

##### Relationships and interactions
- API Layer → internal modules (calls into flash_attention.py, causal_flash_attention.py, etc.)
- Internal modules → JAX tracer → XLA compiler
- XLA compiler → Hardware Backend (GPU/CPU) → returns results back up the chain
- Packaging pipeline separate: GitHub → PyPI

##### Architectural patterns and design principles
- Modular functional design: each attention variant in its own module
- Single-package library (no microservices)
- Separation of concerns: API vs. implementation vs. utilities vs. packaging
- Leverage external dependency (JAX) for compilation and backend abstraction

##### Diagramming recommendations
- Blue: User & Application Layer
- Green: flash_attention_jax library (API + modules)
- Orange: JAX/XLA compiler
- Purple: Hardware Backend
- Gray: Packaging/CI

##### Final advice
- Be exhaustive: list every Python module and its relationship to the API surface
- Flow of data and control from the top (user) to the bottom (hardware)
- A small inset showing the CI/CD pipeline for completeness (GitHub Actions → PyPI)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Layer
    UserApp["User Application/Notebook"]:::blue

    %% Library Layer
    subgraph "flash_attention_jax Library"
        API["flash_attention_jax API Layer"]:::green
        Core["attention.py"]:::green
        FlashKernel["flash_attention.py"]:::green
        Causal["causal_flash_attention.py"]:::green
        Cosine["cosine_sim_flash_attention.py"]:::green
        Rabe["rabe_attention.py"]:::green
        Utils["utils.py"]:::green
    end

    %% Compilation Layer
    Compiler["JAX/XLA Compiler"]:::orange

    %% Hardware Layer
    Hardware["Hardware Backend (CPU/GPU)"]:::purple

    %% Packaging & CI/CD
    subgraph "Packaging & CI/CD"
        Setup["setup.py (Packaging Metadata)"]:::gray
        CI["GitHub Actions Workflow"]:::gray
        PyPI(("PyPI Registry")):::gray
    end

    %% Connections
    UserApp -->|"import & calls (q, k, v arrays)"| API
    API -->|"delegates to core logic"| Core
    API -->|"invokes flash kernel"| FlashKernel
    API -->|"invokes causal variant"| Causal
    API -->|"invokes cosine sim variant"| Cosine
    API -->|"invokes rabe variant"| Rabe
    API -->|"uses utilities"| Utils

    Core & FlashKernel & Causal & Cosine & Rabe & Utils -->|"JAX tracer"| Compiler
    Compiler -->|"compiled HLO"| Hardware
    Hardware -->|"results (tensor)"| UserApp

    Setup -->|"defines packaging"| CI
    CI -->|"publishes package"| PyPI

    %% Click Events
    click API "https://github.com/lucidrains/flash-attention-jax/blob/main/flash_attention_jax/__init__.py"
    click Core "https://github.com/lucidrains/flash-attention-jax/blob/main/flash_attention_jax/attention.py"
    click FlashKernel "https://github.com/lucidrains/flash-attention-jax/blob/main/flash_attention_jax/flash_attention.py"
    click Causal "https://github.com/lucidrains/flash-attention-jax/blob/main/flash_attention_jax/causal_flash_attention.py"
    click Cosine "https://github.com/lucidrains/flash-attention-jax/blob/main/flash_attention_jax/cosine_sim_flash_attention.py"
    click Rabe "https://github.com/lucidrains/flash-attention-jax/blob/main/flash_attention_jax/rabe_attention.py"
    click Utils "https://github.com/lucidrains/flash-attention-jax/blob/main/flash_attention_jax/utils.py"
    click Setup "https://github.com/lucidrains/flash-attention-jax/blob/main/setup.py"
    click CI "https://github.com/lucidrains/flash-attention-jax/blob/main/.github/workflows/python-publish.yml"

    %% Styles
    classDef blue fill:#CCF,stroke:#888,stroke-width:1px
    classDef green fill:#CFC,stroke:#888,stroke-width:1px
    classDef orange fill:#FC9,stroke:#888,stroke-width:1px
    classDef purple fill:#C9C,stroke:#888,stroke-width:1px
    classDef gray fill:#DDD,stroke:#888,stroke-width:1px
```
