# pulp-platform/deepquant architecture

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

- Interactive diagram: https://gitdiagram.com/pulp-platform/deepquant
- Repository: https://github.com/pulp-platform/deepquant

## Overview

##### Project type and purpose
- This is a Python library (“DeepQuant”) for performing true quantization and optimization on neural networks. It hooks into PyTorch via FX tracing, manipulates the computation graph to insert quant/dequant nodes, and can export quantized networks (e.g., to Brevitas).

##### File structure
- Top‐level:
- .github/workflows – CI pipelines for building, testing, linting.
- Container/Dockerfile – Docker image for environment consistency.
- DeepQuant/ – the library source divided into:
– CustomTracer & CustomForwards (wrap/override PyTorch modules and activations),
– Injects (graph‐injection logic: base classes, transformation executor),
– QuantManipulation (parameter extraction, node division, dequant modification),
– ExportBrevitas (optional exporter to Brevitas),
– Utils (FX interpreter, graph printer).
- Tests/ – pytest unit tests for single layers and full models.
- pyproject.toml, setup.py – build/install configuration.

##### README for additional insights
- Confirms Python ≥3.11, editable install, pytest markers, library in beta.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Swimlanes as subgraphs
    subgraph "Tracing Stage"
        direction TB
        ExternalClient["External Client\n(User’s PyTorch model + DeepQuant API)"]:::external
        CustomTracer["CustomTracer"]:::library
    end

    subgraph "Manipulation Stage"
        direction TB
        FXGraph["FX Graph"]:::data
        ParameterExtractor["ParameterExtractor"]:::library
        ParamMetadata["Parameter Metadata"]:::data
        QuantNodesDivider["QuantNodesDivider"]:::library
        DequantModifier["DequantModifier"]:::library
        InterimGraph1["Intermediate Graph"]:::data
        InterimGraph2["Modified Graph"]:::data
    end

    subgraph "Injection Stage"
        direction TB
        Base["Base"]:::library
        Executor["Executor"]:::library
        Transformations["Transformations"]:::library
        InjectsEngine["Injects Engine"]:::library
        TransformedGraph["Transformed Graph"]:::data
        subgraph "CustomForwards Modules"
            direction TB
            Activations["Activations"]:::library
            Linear["Linear"]:::library
            MultiHeadAttention["MultiHeadAttention"]:::library
        end
    end

    subgraph "Export Stage"
        direction TB
        ExportBrevitas["ExportBrevitas"]:::library
        QuantizedModel["Quantized Model"]:::data
    end

    subgraph "Utils"
        direction TB
        FxInterpreter["FXInterpreter"]:::util
        GraphPrinter["GraphPrinter"]:::util
    end

    subgraph "Tests Harness"
        direction TB
        TestConv["TestConv.py"]:::test
        TestLinear["TestLinear.py"]:::test
        TestMHSA["TestMHSA.py"]:::test
        TestMobileNet["TestMobileNetV3Small.py"]:::test
        TestResNet["TestResNet18.py"]:::test
        TestSimpleCNN["TestSimpleCNN.py"]:::test
        TestSimpleFCNN["TestSimpleFCNN.py"]:::test
        Conftest["conftest.py"]:::test
    end

    subgraph "Infrastructure"
        direction TB
        CICD["CI/CD Workflows"]:::infra
        DockerContainer["Docker Container"]:::infra
    end

    %% Connections
    ExternalClient --> CustomTracer
    CustomTracer --> FXGraph
    FXGraph --> ParameterExtractor --> ParamMetadata
    ParamMetadata --> QuantNodesDivider --> InterimGraph1
    InterimGraph1 --> DequantModifier --> InterimGraph2
    InterimGraph2 --> Executor --> TransformedGraph
    Executor --> Base
    Executor --> Transformations
    TransformedGraph --> Activations
    TransformedGraph --> Linear
    TransformedGraph --> MultiHeadAttention
    TransformedGraph --> ExportBrevitas --> QuantizedModel

    TransformedGraph --> FxInterpreter
    TransformedGraph --> GraphPrinter

    TransformedGraph --> TestConv
    TransformedGraph --> TestLinear
    TransformedGraph --> TestMHSA
    TransformedGraph --> TestMobileNet
    TransformedGraph --> TestResNet
    TransformedGraph --> TestSimpleCNN
    TransformedGraph --> TestSimpleFCNN
    TransformedGraph --> Conftest

    CICD --> TestConv
    CICD --> TestLinear
    CICD --> DockerContainer

    %% Click Events
    click CustomTracer "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/CustomTracer.py"
    click Activations "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/CustomForwards/Activations.py"
    click Linear "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/CustomForwards/Linear.py"
    click MultiHeadAttention "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/CustomForwards/MultiHeadAttention.py"
    click ParameterExtractor "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/QuantManipulation/ParameterExtractor.py"
    click QuantNodesDivider "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/QuantManipulation/QuantNodesDivider.py"
    click DequantModifier "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/QuantManipulation/DequantModifier.py"
    click QuantDequantNodes "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/QuantManipulation/QuantDequantNodes.py"
    click Base "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/Injects/Base.py"
    click Executor "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/Injects/Executor.py"
    click Transformations "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/Injects/Transformations.py"
    click ExportBrevitas "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/ExportBrevitas.py"
    click FxInterpreter "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/Utils/FxInterpreter.py"
    click GraphPrinter "https://github.com/pulp-platform/deepquant/blob/devel/DeepQuant/Utils/GraphPrinter.py"
    click TestConv "https://github.com/pulp-platform/deepquant/blob/devel/Tests/TestConv.py"
    click TestLinear "https://github.com/pulp-platform/deepquant/blob/devel/Tests/TestLinear.py"
    click TestMHSA "https://github.com/pulp-platform/deepquant/blob/devel/Tests/TestMHSA.py"
    click TestMobileNet "https://github.com/pulp-platform/deepquant/blob/devel/Tests/TestMobileNetV3Small.py"
    click TestResNet "https://github.com/pulp-platform/deepquant/blob/devel/Tests/TestResNet18.py"
    click TestSimpleCNN "https://github.com/pulp-platform/deepquant/blob/devel/Tests/TestSimpleCNN.py"
    click TestSimpleFCNN "https://github.com/pulp-platform/deepquant/blob/devel/Tests/TestSimpleFCNN.py"
    click Conftest "https://github.com/pulp-platform/deepquant/blob/devel/Tests/conftest.py"
    click BuildDockerYML "https://github.com/pulp-platform/deepquant/blob/devel/.github/workflows/BuildDocker.yml"
    click CIyml "https://github.com/pulp-platform/deepquant/blob/devel/.github/workflows/CI.yml"
    click LintingYML "https://github.com/pulp-platform/deepquant/blob/devel/.github/workflows/Linting.yml"
    click DockerContainer "https://github.com/pulp-platform/deepquant/tree/devel/Container/Dockerfile"

    %% Styles
    classDef library fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef external fill:#90EE90,stroke:#000,stroke-width:1px
    classDef data fill:#FFFF99,stroke:#000,stroke-width:1px
    classDef infra fill:#DDA0DD,stroke:#000,stroke-width:1px
    classDef util fill:#FFA500,stroke:#000,stroke-width:1px
    classDef test fill:#FFB6C1,stroke:#000,stroke-width:1px
```
