# google/jax architecture

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

- Interactive diagram: https://gitdiagram.com/google/jax
- Repository: https://github.com/google/jax

## Overview

##### Core Components

a) Main Transformation Layer
- grad (automatic differentiation)
- jit (just-in-time compilation)
- vmap (vectorization)
- pmap (parallel computation)

b) Backend Integration Layer
- CPU
- NVIDIA GPU
- Google TPU
- AMD GPU
- Apple GPU
- Intel GPU

##### Key Architectural Components

a) NumPy API Layer
- Jax.numpy module as the primary interface
- Common operations (linalg, fft, etc.)
- To the transformation layer

b) Compiler Infrastructure
- JIT compilation pipeline
- Connection to XLA
- Optimization paths

c) Device Management
- Device abstraction layer
- Memory management
- Multi-device coordination

##### Integration Points

a) External Libraries Support
- Integration with neural network libraries:
- Flax
- Optax
- Other ecosystem tools

b) Python Integration
- Python runtime interaction
- PyTree handling
- Python function transformation

##### Data Flow

- Input data flow through transformations
- Compilation paths
- Memory transfer between devices
- Gradient computation paths

##### Specific Implementation Details

a) Core Modules (based on file structure):
- jax._src.api (main API)
- jax._src.lax (primitive operations)
- jax._src.interpreters (transformation implementations)
- jax._src.lib (low-level implementations)

b) Hardware-Specific Components:
- Specific optimizations for each platform

##### The compilation and optimization pipeline

Use a hierarchical layout with:
- User-facing APIs at the top
- Transformation layer in the middle
- Hardware interfaces at the bottom

This design should capture JAX's unique ability to transform numerical functions while maintaining its high-performance characteristics across different hardware accelerators.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph User["User Interface"]
        API["Main API"]:::api
        NumpyAPI["NumPy Interface"]:::numpy
        SciPyAPI["SciPy Interface"]:::scipy
    end

    subgraph Transform["Core Transformation Layer"]
        AD["Automatic Differentiation"]:::transform
        JIT["JIT Compilation"]:::transform
        VMAP["Vectorization (vmap)"]:::transform
        PMAP["Parallel Computing (pmap)"]:::transform
    end

    subgraph Compiler["Compiler Infrastructure"]
        CompCore["Main Compiler"]:::compiler
        XLABridge["XLA Bridge"]:::compiler
        MLIR["MLIR Support"]:::compiler
    end

    subgraph Core["Core Implementation"]
        CorePrim["Core Primitives"]:::core
        LAXPrim["LAX Primitives"]:::core
        BaseArray["Base Array"]:::core
        Effects["Effects System"]:::core
    end

    subgraph Device["Device Management"]
        DevAbs["Device Abstraction"]:::device
        HWUtils["Hardware Utils"]:::device
    end

    subgraph Backend["Backend Integration Layer"]
        CPU["CPU Backend"]:::backend
        GPU["GPU Backend"]:::backend
        CUDA["CUDA Support"]:::backend
        ROCm["ROCm Support"]:::backend
    end

    subgraph Debug["Error Handling & Debugging"]
        Errors["Error System"]:::debug
        DebugTools["Debugging Tools"]:::debug
        SourceInfo["Source Info"]:::debug
    end

    %% Connections
    API --> Transform
    NumpyAPI --> Transform
    SciPyAPI --> Transform

    Transform --> Compiler
    Transform --> Core
    
    Compiler --> Device
    Core --> Device
    
    Device --> Backend
    
    Debug --> Transform
    Debug --> Compiler
    Debug --> Device

    %% Styles
    classDef api fill:#f9f,stroke:#333
    classDef numpy fill:#ff9,stroke:#333
    classDef scipy fill:#9ff,stroke:#333
    classDef transform fill:#f96,stroke:#333
    classDef compiler fill:#69f,stroke:#333
    classDef core fill:#9c6,stroke:#333
    classDef device fill:#c9f,stroke:#333
    classDef backend fill:#fc9,stroke:#333
    classDef debug fill:#ccc,stroke:#333

    %% Click Events
    click API "https://github.com/google/jax/blob/main/jax/_src/api.py"
    click AD "https://github.com/google/jax/blob/main/jax/_src/interpreters/ad.py"
    click JIT "https://github.com/google/jax/blob/main/jax/_src/interpreters/mlir.py"
    click VMAP "https://github.com/google/jax/blob/main/jax/_src/interpreters/batching.py"
    click PMAP "https://github.com/google/jax/blob/main/jax/_src/interpreters/pxla.py"
    click CPU "https://github.com/google/jax/tree/main/jaxlib/cpu/"
    click GPU "https://github.com/google/jax/tree/main/jaxlib/gpu/"
    click CUDA "https://github.com/google/jax/tree/main/jaxlib/cuda/"
    click ROCm "https://github.com/google/jax/tree/main/jaxlib/rocm/"
    click NumpyAPI "https://github.com/google/jax/blob/main/jax/numpy/__init__.py"
    click CompCore "https://github.com/google/jax/blob/main/jax/_src/compiler.py"
    click XLABridge "https://github.com/google/jax/blob/main/jax/_src/xla_bridge.py"
    click MLIR "https://github.com/google/jax/blob/main/jax/_src/interpreters/mlir.py"
    click DevAbs "https://github.com/google/jax/blob/main/jax/_src/devices.py"
    click HWUtils "https://github.com/google/jax/blob/main/jax/_src/hardware_utils.py"
    click CorePrim "https://github.com/google/jax/blob/main/jax/_src/core.py"
    click LAXPrim "https://github.com/google/jax/blob/main/jax/_src/lax/lax.py"
    click BaseArray "https://github.com/google/jax/blob/main/jax/_src/basearray.py"
    click Effects "https://github.com/google/jax/blob/main/jax/_src/effects.py"
    click SciPyAPI "https://github.com/google/jax/tree/main/jax/scipy/"
    click Errors "https://github.com/google/jax/blob/main/jax/_src/errors.py"
    click DebugTools "https://github.com/google/jax/blob/main/jax/_src/debugging.py"
    click SourceInfo "https://github.com/google/jax/blob/main/jax/_src/source_info_util.py"
```
