# xhanxu/mamba3d architecture

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

- Interactive diagram: https://gitdiagram.com/xhanxu/mamba3d
- Repository: https://github.com/xhanxu/mamba3d

## Overview

##### Project type and purpose
- This is an open-source, command-line driven machine-learning framework for 3D point-cloud classification and analysis.
- Its core contribution is “Mamba3D,” a state-space-model-based architecture that trains and evaluates point-cloud classifiers on datasets such as ModelNet40, ScanObjectNN, and ShapeNet.

##### File structure
- Top-level components:
- cfgs/        ← configuration files (YAML) for pretraining, fine-tuning, few-shot experiments, dataset specs
- datasets/    ← PyTorch Dataset classes, data transforms, I/O and few-shot data generator
- extensions/  ← custom CUDA/C++ extensions (Chamfer distance, Earth Mover’s Distance)
- models/      ← core model code: Mamba3D wrapper, the bimamba_ssm package (submodules: models, modules, ops, utils), helper modules (rope, z_order)
- tools/       ← experiment drivers (builder, runner scripts for pretrain/finetune/fewshot/SVM/TSNE)
- script/      ← shell scripts invoking Python entrypoints with specific configs
- utils/       ← generic utilities (config loader, registry, logging, checkpointing, distributed-training helpers)
- main.py      ← primary CLI entrypoint that parses a cfg, builds components, and kicks off training/evaluation

##### README for additional insights
- Describes dependencies (PyTorch, CUDA, mamba-ssm, causal-conv1d) and external installs (PointNet++, GPU kNN).
- Points to DATASET.md for data details and demonstrates how to run training, voting, fine-tuning, few-shot experiments.
- Emphasizes pretrained weights and performance targets.

a. Main components
1. Data Sources
- Raw datasets on disk (ShapeNet, ModelNet, ScanObjectNN)
2. Data Module
- `datasets/` package: Dataset classes, transforms, few-shot data generator
3. Configuration
- YAML config files in `cfgs/` + `utils/config.py`
4. Model Module
- `models/Mamba3D.py` (high-level builder)
- `models/bimamba_ssm/` sub-package (state space modules, mixers, ops)
5. Custom Extensions
- Chamfer Distance and EMD CUDA kernels under `extensions/`
6. Training Engine
- `main.py` + `tools/runner_*.py` orchestrate training loops, loss computation, optimization
7. Utilities
- Logging (`utils/logger.py`), checkpointing (`utils/checkpoint.py`), distributed training (`utils/dist_utils.py`), registry pattern etc.
8. CLI & Scripts
- Shell wrappers in `script/` invoke the Python entrypoint with specific config files
9. External Libraries
- PyTorch, mamba-ssm, causal-conv1d, PointNet++ ops, GPU kNN

b. Relationships and interactions
- Data Sources → Data Module → DataLoader
- Config Loader reads YAML → passes hyperparameters to Data Module, Model Module, Training Engine
- Training Engine builds model via Model Module → forward pass + custom extensions → loss
- Optimizer updates model → outputs checkpoints & logs via Utilities
- CLI/Scripts invoke Training Engine with specific configs (pretrain, finetune, few-shot, vote)
- Tools/runner scripts wrap common experiment patterns

c. Architectural patterns and design principles
- Configuration-driven design: all hyperparameters, dataset specs, model settings in YAML files
- Modular code organization: separation of concerns (datasets, models, training logic, utilities, extensions)
- Registry pattern: dynamic creation of datasets, models, losses based on config
- Plugin-style extensions: custom CUDA ops loaded at runtime

d. Relevant technologies, frameworks, and libraries
- PyTorch for deep learning
- CUDA/C++ for speed-critical kernels (Chamfer, EMD)
- mamba-ssm & causal-conv1d for state-space modeling
- YAML for experiment configuration
- Shell scripting + Python CLI for reproducible runs

##### Elements
- Blue for code modules (datasets, models, utils)
- Green for data components (raw datasets, DataLoader)
- Orange for configuration and CLI/scripts
- Red for external libraries and extensions
- Annotations for key file paths or classes (e.g., datasets/ModelNetDataset.py, models/Mamba3D.py, extensions/chamfer_dist)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data Sources
    RawData["Raw Datasets<br/>(ShapeNet, ModelNet, ScanObjectNN)"]:::data

    %% Configuration
    subgraph "Configuration"
        cfgs["cfgs/"]:::config
        config_py["utils/config.py"]:::config
    end

    %% Data Module
    subgraph "Data Module"
        ModelNet["ModelNetDataset.py"]:::code
        ModelNetFS["ModelNetDatasetFewShot.py"]:::code
        ScanNN["ScanObjectNNDataset.py"]:::code
        ShapeNet["ShapeNet55Dataset.py"]:::code
        Transforms["data_transforms.py"]:::code
        FewShotGen["generate_few_shot_data.py"]:::code
        IO["io.py"]:::code
        BuildData["build.py"]:::code
    end

    %% Model Module
    subgraph "Model Module"
        Mamba3D["Mamba3D.py"]:::code
        BuildModel["build_fn.py"]:::code
        Rope["rope.py"]:::code
        Zorder["z_order.py"]:::code
        subgraph "bimamba_ssm"
            Mixer["mixer_seq_simple.py"]:::code
            MambaSimple["mamba_simple.py"]:::code
            SelectiveScan["selective_scan_interface.py"]:::code
            LayerNorm["layernorm.py"]:::code
            SelectiveStateUpdate["selective_state_update.py"]:::code
            GenUtils["generation.py"]:::code
            HFUtils["hf.py"]:::code
        end
    end

    %% Custom Extensions
    subgraph "Custom Extensions"
        ChamferDir["extensions/chamfer_dist/"]:::ext
        EMDDir["extensions/emd/"]:::ext
    end

    %% Training Engine
    subgraph "Training Engine"
        Main["main.py"]:::code
        Builder["builder.py"]:::code
        Runner["runner.py"]:::code
        RunnerPre["runner_pretrain.py"]:::code
        RunnerFine["runner_finetune.py"]:::code
        RunnerFew["runner_fewshot.py"]:::code
        RunnerSVM["runner_svm.py"]:::code
        RunnerTSNE["runner_tsne.py"]:::code
    end

    %% Utilities
    subgraph "Utilities"
        Logger["logger.py"]:::util
        Checkpoint["checkpoint.py"]:::util
        Dist["dist_utils.py"]:::util
        Registry["registry.py"]:::util
        Misc["misc.py"]:::util
        AverageMeter["AverageMeter.py"]:::util
        Parser["parser.py"]:::util
        TSNEUtil["tsne_utils.py"]:::util
    end

    %% CLI & Scripts
    subgraph "CLI & Scripts"
        PreScript["run_pretrain.sh"]:::script
        FineScript["run_finetune.sh"]:::script
        FewScript["run_fewshot.sh"]:::script
        ScratchScript["run_scratch.sh"]:::script
        VoteScript["run_vote.sh"]:::script
    end

    %% External Libraries
    subgraph "External Libraries"
        PT["PyTorch"]:::external
        MambaSSM["mamba-ssm"]:::external
        Causal["causal-conv1d"]:::external
        PointNet["PointNet++"]:::external
        GPUKNN["GPU kNN"]:::external
    end

    %% Connections
    RawData -->|load| BuildData
    RawData -->|load| ModelNet
    RawData -->|load| ScanNN
    RawData -->|load| ShapeNet

    cfgs -->|reads| Main
    config_py -->|parses| Main
    cfgs -->|feeds| ModelModule[/"Model Module"/]
    config_py -->|feeds| ModelModule
    Main -->|uses| BuildData
    Main -->|uses| DataModule[/"Data Module"/]
    Main -->|builds| Mamba3D
    Main -->|loads| ChamferDir
    Main -->|loads| EMDDir
    Main -->|orchestrates| Runner
    Runner --> RunnerPre
    Runner --> RunnerFine
    Runner --> RunnerFew
    Runner --> RunnerSVM
    Runner --> RunnerTSNE
    Main -->|logs| Logger
    Main -->|checkpoint| Checkpoint
    Main -->|distribute| Dist
    Main -->|registry| Registry
    Main -->|misc| Misc

    PreScript --> Main
    FineScript --> Main
    FewScript --> Main
    ScratchScript --> Main
    VoteScript --> Main

    Main --> PT
    Main --> MambaSSM
    Main --> Causal
    Main --> PointNet
    Main --> GPUKNN

    %% Click Events
    click cfgs "https://github.com/xhanxu/mamba3d/tree/main/cfgs/"
    click config_py "https://github.com/xhanxu/mamba3d/blob/main/utils/config.py"
    click ModelNet "https://github.com/xhanxu/mamba3d/blob/main/datasets/ModelNetDataset.py"
    click ModelNetFS "https://github.com/xhanxu/mamba3d/blob/main/datasets/ModelNetDatasetFewShot.py"
    click ScanNN "https://github.com/xhanxu/mamba3d/blob/main/datasets/ScanObjectNNDataset.py"
    click ShapeNet "https://github.com/xhanxu/mamba3d/blob/main/datasets/ShapeNet55Dataset.py"
    click Transforms "https://github.com/xhanxu/mamba3d/blob/main/datasets/data_transforms.py"
    click FewShotGen "https://github.com/xhanxu/mamba3d/blob/main/datasets/generate_few_shot_data.py"
    click IO "https://github.com/xhanxu/mamba3d/blob/main/datasets/io.py"
    click BuildData "https://github.com/xhanxu/mamba3d/blob/main/datasets/build.py"
    click Mamba3D "https://github.com/xhanxu/mamba3d/blob/main/models/Mamba3D.py"
    click BuildModel "https://github.com/xhanxu/mamba3d/blob/main/models/build_fn.py"
    click Rope "https://github.com/xhanxu/mamba3d/blob/main/models/rope.py"
    click Zorder "https://github.com/xhanxu/mamba3d/blob/main/models/z_order.py"
    click Mixer "https://github.com/xhanxu/mamba3d/blob/main/models/bimamba_ssm/models/mixer_seq_simple.py"
    click MambaSimple "https://github.com/xhanxu/mamba3d/blob/main/models/bimamba_ssm/modules/mamba_simple.py"
    click SelectiveScan "https://github.com/xhanxu/mamba3d/blob/main/models/bimamba_ssm/ops/selective_scan_interface.py"
    click LayerNorm "https://github.com/xhanxu/mamba3d/blob/main/models/bimamba_ssm/ops/triton/layernorm.py"
    click SelectiveStateUpdate "https://github.com/xhanxu/mamba3d/blob/main/models/bimamba_ssm/ops/triton/selective_state_update.py"
    click GenUtils "https://github.com/xhanxu/mamba3d/blob/main/models/bimamba_ssm/utils/generation.py"
    click HFUtils "https://github.com/xhanxu/mamba3d/blob/main/models/bimamba_ssm/utils/hf.py"
    click ChamferDir "https://github.com/xhanxu/mamba3d/tree/main/extensions/chamfer_dist/"
    click EMDDir "https://github.com/xhanxu/mamba3d/tree/main/extensions/emd/"
    click Main "https://github.com/xhanxu/mamba3d/blob/main/main.py"
    click Builder "https://github.com/xhanxu/mamba3d/blob/main/tools/builder.py"
    click Runner "https://github.com/xhanxu/mamba3d/blob/main/tools/runner.py"
    click RunnerPre "https://github.com/xhanxu/mamba3d/blob/main/tools/runner_pretrain.py"
    click RunnerFine "https://github.com/xhanxu/mamba3d/blob/main/tools/runner_finetune.py"
    click RunnerFew "https://github.com/xhanxu/mamba3d/blob/main/tools/runner_fewshot.py"
    click RunnerSVM "https://github.com/xhanxu/mamba3d/blob/main/tools/runner_svm.py"
    click RunnerTSNE "https://github.com/xhanxu/mamba3d/blob/main/tools/runner_tsne.py"
    click Logger "https://github.com/xhanxu/mamba3d/blob/main/utils/logger.py"
    click Checkpoint "https://github.com/xhanxu/mamba3d/blob/main/utils/checkpoint.py"
    click Dist "https://github.com/xhanxu/mamba3d/blob/main/utils/dist_utils.py"
    click Registry "https://github.com/xhanxu/mamba3d/blob/main/utils/registry.py"
    click Misc "https://github.com/xhanxu/mamba3d/blob/main/utils/misc.py"
    click AverageMeter "https://github.com/xhanxu/mamba3d/blob/main/utils/AverageMeter.py"
    click Parser "https://github.com/xhanxu/mamba3d/blob/main/utils/parser.py"
    click TSNEUtil "https://github.com/xhanxu/mamba3d/blob/main/utils/tsne_utils.py"
    click PreScript "https://github.com/xhanxu/mamba3d/blob/main/script/run_pretrain.sh"
    click FineScript "https://github.com/xhanxu/mamba3d/blob/main/script/run_finetune.sh"
    click FewScript "https://github.com/xhanxu/mamba3d/blob/main/script/run_fewshot.sh"
    click ScratchScript "https://github.com/xhanxu/mamba3d/blob/main/script/run_scratch.sh"
    click VoteScript "https://github.com/xhanxu/mamba3d/blob/main/script/run_vote.sh"

    %% Styles
    classDef code fill:#D0E8FF,stroke:#0366D6,color:#0366D6;
    classDef data fill:#DFF2BF,stroke:#4F8A10,color:#4F8A10;
    classDef config fill:#FFE8A1,stroke:#E69500,color:#E69500;
    classDef ext fill:#FFD1D1,stroke:#D73A49,color:#D73A49;
    classDef util fill:#E8E8E8,stroke:#6A737D,color:#24292E;
    classDef script fill:#FFF5B1,stroke:#735C0F,color:#735C0F;
    classDef external fill:#F0F0F0,stroke:#888888,color:#888888;
```
