# fastmachinelearning/hls4ml architecture

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

- Interactive diagram: https://gitdiagram.com/fastmachinelearning/hls4ml
- Repository: https://github.com/fastmachinelearning/hls4ml

## Overview

##### Main Components

a) Frontend Layer:
- Multiple ML framework inputs (Keras, PyTorch, QONNX)
- Model conversion interfaces
- Configuration management

b) Core Processing Layer:
- Model optimization pipeline
- IR (Intermediate Representation) system
- Flow management system
- Attribute handling system

c) Backend Layer:
- Multiple HLS backend support (Vivado, Vitis, Catapult, OneAPI, Quartus)
- Code generation system
- Template management
- Build system interface

##### Component Relationships

- Input flow: ML model → Frontend converters → Internal representation
- Optimization flow: IR → Optimization passes → Optimized IR
- Backend flow: Optimized IR → Backend-specific code generation → HLS output

##### Specific Implementation Details

**Frontend Layer:**

- Model Converters |
- Keras         |
- PyTorch       |
- QONNX         |

**Core Layer:**

- Optimizer        |
- Passes        |
- Profiling     |
- Quantization  |

**Backend Layer:**

- Code Generation  |
- Vivado        |
- Vitis         |
- Catapult      |
- OneAPI        |
- Quartus       |

##### Key Elements to Highlight

a) Model Processing Pipeline:
- Model parsing
- Graph optimization
- Code generation
- Build system integration

b) Support Systems:
- Profiling tools
- Report generation
- Testing framework
- Configuration management

##### Architectural Patterns

Following patterns:
- Plugin architecture for backends
- Pipeline pattern for model processing
- Factory pattern for converters
- Template pattern for code generation

##### Data Flow Indicators

- Model conversion flow
- Optimization pass sequence
- Backend selection paths
- Build process flow

**[ML Frameworks] → [Frontend Converters] → [IR/Graph Representation] → [Optimization Pipeline] → [Backend Selection] → [Code Generation] → [HLS Output]**

- Component types
- Special symbols

Keep the abstraction level appropriate for a principal software engineer while maintaining sufficient technical detail about the ML-to-FPGA conversion process.

This system design should emphasize:

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef frontend fill:#b3d9ff,stroke:#333
    classDef core fill:#b3ffb3,stroke:#333
    classDef backend fill:#ffd9b3,stroke:#333
    classDef support fill:#e6b3ff,stroke:#333
    classDef external fill:#f2f2f2,stroke:#333

    %% Frontend Layer
    subgraph Frontend
        ML["ML Frameworks"]:::external
        KERAS["Keras Converter"]:::frontend
        PYTORCH["PyTorch Converter"]:::frontend
        QONNX["QONNX Converter"]:::frontend
    end

    %% Core Processing Layer
    subgraph Core
        IR["IR System"]:::core
        FLOW["Flow Management"]:::core
        OPT["Optimizer"]:::core
        ATTR["Attribute System"]:::core
        PROF["Profiling"]:::support
    end

    %% Backend Layer
    subgraph Backends
        direction TB
        VIVADO["Vivado Backend"]:::backend
        VITIS["Vitis Backend"]:::backend
        CATAPULT["Catapult Backend"]:::backend
        ONEAPI["OneAPI Backend"]:::backend
        QUARTUS["Quartus Backend"]:::backend
    end

    %% Code Generation Layer
    subgraph CodeGen
        direction TB
        VIV_W["Vivado Writer"]:::backend
        VIT_W["Vitis Writer"]:::backend
        CAT_W["Catapult Writer"]:::backend
        ONE_W["OneAPI Writer"]:::backend
        QUA_W["Quartus Writer"]:::backend
    end

    %% Template System
    subgraph Templates
        direction TB
        VIV_T["Vivado Templates"]:::backend
        VIT_T["Vitis Templates"]:::backend
        CAT_T["Catapult Templates"]:::backend
        ONE_T["OneAPI Templates"]:::backend
        QUA_T["Quartus Templates"]:::backend
    end

    %% Support Systems
    CONFIG["Configuration"]:::support
    REPORT["Report Generation"]:::support

    %% Relationships
    ML --> KERAS & PYTORCH & QONNX
    KERAS & PYTORCH & QONNX --> IR
    IR --> FLOW
    FLOW --> OPT
    OPT --> ATTR
    ATTR --> VIVADO & VITIS & CATAPULT & ONEAPI & QUARTUS
    
    VIVADO --> VIV_W
    VITIS --> VIT_W
    CATAPULT --> CAT_W
    ONEAPI --> ONE_W
    QUARTUS --> QUA_W

    VIV_W --> VIV_T
    VIT_W --> VIT_T
    CAT_W --> CAT_T
    ONE_W --> ONE_T
    QUA_W --> QUA_T

    PROF --> REPORT
    CONFIG --> FLOW

    %% Click events
    click KERAS "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/converters/keras/"
    click PYTORCH "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/converters/pytorch/"
    click QONNX "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/converters/onnx/"
    click IR "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/model/graph.py"
    click FLOW "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/model/flow/flow.py"
    click OPT "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/model/optimizer/optimizer.py"
    click ATTR "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/model/attributes.py"
    click VIVADO "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/backends/vivado/"
    click VITIS "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/backends/vitis/"
    click CATAPULT "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/backends/catapult/"
    click ONEAPI "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/backends/oneapi/"
    click QUARTUS "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/backends/quartus/"
    click VIV_W "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/writer/vivado_writer.py"
    click VIT_W "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/writer/vitis_writer.py"
    click CAT_W "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/writer/catapult_writer.py"
    click ONE_W "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/writer/oneapi_writer.py"
    click QUA_W "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/writer/quartus_writer.py"
    click VIV_T "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/templates/vivado/"
    click VIT_T "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/templates/vitis/"
    click CAT_T "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/templates/catapult/"
    click ONE_T "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/templates/oneapi/"
    click QUA_T "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/templates/quartus/"
    click PROF "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/model/profiling.py"
    click REPORT "https://github.com/fastmachinelearning/hls4ml/tree/main/hls4ml/report/"
    click CONFIG "https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/utils/config.py"
```
