# mit-han-lab/smoothquant architecture

> Architecture diagram of the GitHub repository mit-han-lab/smoothquant, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mit-han-lab/smoothquant
- Repository: https://github.com/mit-han-lab/smoothquant

## Overview

##### Project Overview and Purpose
- The project is focused on post‐training quantization of LLMs. It provides a training‐free method—“SmoothQuant”—that migrates activation outliers to weights, enabling an INT8 quantization of both activations and weights. This offers significant speedups and memory reductions.
- Key use cases include integrating with PyTorch (with support for CUTLASS INT8 GEMM kernels via torch‐int), and deployment into production frameworks such as FasterTransformer, NVIDIA TensorRT‐LLM, ONNX Runtime, Amazon SageMaker, and Intel Neural Compressor.

##### Analyzing the File Structure
- Top-Level Components:
– The root includes project meta files (LICENSE, README.md, setup.py) and auxiliary directories.
– The smoothquant/ directory contains the core library code: modules for calibration, quantization (“smooth.py”), fake quantization simulation (“fake_quant.py”), the OPT-specific quantization wrapper (“opt.py”), and perplexity evaluation (“ppl_eval.py”).
– The act_scales/ directory holds precomputed activation scales for various models.
– The examples/ directory features demonstration scripts and notebooks that illustrate key usage scenarios (e.g., generating activation scales, exporting INT8 models, and evaluating performance on OPT and Llama models).
– The assets/ and figures/ directories contain presentation material (PDF slides and architecture/accuracy figures), which are useful for understanding the methodology.

##### Main Components
- Pre-trained LLM Input:
– Represent the starting LLM models (e.g., OPT, Llama, Falcon) that will be quantized.
- Calibration / Activation Scale Generation:
– Include a component that represents scripts like generate_act_scales.py and the act_scales data. This stage collects calibration data (using datasets like Pile validation data) to compute activation channel scales.
- SmoothQuant Core Transformation:
– The heart of the system lives in the smoothquant/ directory.
▪ The “smooth” module: Applies the mathematically equivalent transformation shifting quantization difficulty from activations to weights.
▪ The “calibration” module: Handles calibration related processes where activation statistics are used.
▪ The “fake_quant” module: Simulates INT8 inference using FP16 so that performance and accuracy can be compared.
▪ The “opt” module: Implements INT8 optimized inference for the OPT model via custom PyTorch modules (that depend on torch-int and CUTLASS kernels).
▪ The “ppl_eval” module: Provides evaluation of language modeling perplexity applying quantization.
- External Dependencies and Integrations:
– Include integration points with external tools such as PyTorch, torch-int (for INT8 GEMM kernels via CUTLASS), FasterTransformer, TensorRT-LLM, ONNX Runtime, and others as noted in the README.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    LLM["Pre-trained LLM Models"]:::data
    Ext["External Integrations<br/>(torch-int, CUTLASS,<br/>FasterTransformer, TensorRT-LLM, ONNX Runtime)"]:::external
    Inference["INT8 Inference Engine Integration"]:::inference

    %% Calibration / Activation Scale Generation Subgraph
    subgraph "Calibration and Activation Scale Generation"
        direction TB
        GenAct["Generate Act Scales"]:::calib
        ActScales["Activation Scales"]:::calib
    end

    %% SmoothQuant Core Subgraph
    subgraph "SmoothQuant Core"
        direction TB
        smooth["SmoothQuant Transformation"]:::core
        CalibCore["Calibration in Core"]:::core
        FakeQ["Fake Quantization Simulation"]:::core
        OPTMod["INT8 OPT Inference Module"]:::core
        PPLEval["Perplexity Evaluation"]:::core
    end

    %% Evaluation and Export Subgraph
    subgraph "Evaluation and Export"
        direction TB
        ExportModel["Export INT8 Model"]:::eval
        PerfEval["Performance Evaluation Script"]:::eval
        LlamaDemo["Llama Demo Notebook"]:::eval
        OPTDemo["OPT Demo Notebook"]:::eval
        OPTRealDemo["OPT Real INT8 Demo Notebook"]:::eval
    end

    %% Data Flow Connections
    LLM --> GenAct
    GenAct --> ActScales
    ActScales --> smooth

    smooth --> CalibCore
    CalibCore --> FakeQ
    FakeQ --> OPTMod
    OPTMod --> PPLEval
    OPTMod --> Inference

    PPLEval --> PerfEval
    Inference --> ExportModel
    ExportModel --> LlamaDemo
    LlamaDemo --> OPTDemo
    OPTDemo --> OPTRealDemo

    %% External Integration Connection
    Ext --> Inference

    %% Click Events
    click GenAct "https://github.com/mit-han-lab/smoothquant/blob/main/examples/generate_act_scales.py"
    click ActScales "https://github.com/mit-han-lab/smoothquant/tree/main/act_scales/"
    click smooth "https://github.com/mit-han-lab/smoothquant/blob/main/smoothquant/smooth.py"
    click CalibCore "https://github.com/mit-han-lab/smoothquant/blob/main/smoothquant/calibration.py"
    click FakeQ "https://github.com/mit-han-lab/smoothquant/blob/main/smoothquant/fake_quant.py"
    click OPTMod "https://github.com/mit-han-lab/smoothquant/blob/main/smoothquant/opt.py"
    click PPLEval "https://github.com/mit-han-lab/smoothquant/blob/main/smoothquant/ppl_eval.py"
    click ExportModel "https://github.com/mit-han-lab/smoothquant/blob/main/examples/export_int8_model.py"
    click PerfEval "https://github.com/mit-han-lab/smoothquant/blob/main/examples/ppl_eval.sh"
    click LlamaDemo "https://github.com/mit-han-lab/smoothquant/blob/main/examples/smoothquant_llama_demo.ipynb"
    click OPTDemo "https://github.com/mit-han-lab/smoothquant/blob/main/examples/smoothquant_opt_demo.ipynb"
    click OPTRealDemo "https://github.com/mit-han-lab/smoothquant/blob/main/examples/smoothquant_opt_real_int8_demo.ipynb"

    %% Styles
    classDef data fill:#F1C40F,stroke:#000,stroke-width:2px;
    classDef calib fill:#F9E79F,stroke:#000,stroke-width:2px;
    classDef core fill:#AED6F1,stroke:#000,stroke-width:2px;
    classDef inference fill:#A9DFBF,stroke:#000,stroke-width:2px;
    classDef eval fill:#F5B7B1,stroke:#000,stroke-width:2px;
    classDef external fill:#D2B4DE,stroke:#000,stroke-width:2px;
```
