# baai-dcai/m3d architecture

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

- Interactive diagram: https://gitdiagram.com/baai-dcai/m3d
- Repository: https://github.com/baai-dcai/m3d

## Overview

#### Purpose and Entry Points
M3D is a Python/PyTorch system for 3D medical-image multimodal learning: image–text retrieval, report/caption generation, VQA, localization, and volumetric segmentation. Main operational entry points are Hugging Face `AutoModelForCausalLM` loading of the custom model, local demos, training programs, shell launchers, and per-task benchmark scripts. Paths: `LaMed/src/demo/demo.py`, `LaMed/src/train/train.py`, `Bench/eval/eval_vqa.py`.

#### Data Lifecycle
Raw M3D-Cap, VQA, Seg, and RefSeg datasets are externally downloaded, then transformed into local training/benchmark artifacts; example `.npy` volumes live under `Data/data/examples/`. Dataset adapters combine task sources, prompt formats, and medical-term dictionaries into model-ready multimodal samples. Paths: `Data/process/m3d_cap_data_prepare.py`, `Data/process/m3d_seg_data_preprocess.py`, `LaMed/src/dataset/multi_dataset.py`.

#### Multimodal Model Assembly
The central LaMed architecture combines a pretrained 3D visual encoder, a projector that converts visual features into language-model token space, and a selectable causal LLM implementation. Image patch placeholders in text are replaced/aligned with projected volume features before language generation. Paths: `LaMed/src/model/lamed_arch.py`, `LaMed/src/model/multimodal_encoder/builder.py`, `LaMed/src/model/multimodal_projector/spatial_pooling_projector.py`.

#### Language and Vision Backbones
Language backbones are custom Hugging Face remote-code-compatible variants for Llama and Phi-3. The visual side includes a 3D ViT and a separate M3D-CLIP training path used to obtain recommended vision weights. This establishes a boundary between external pretrained checkpoints and task-specific fusion/training. Paths: `LaMed/src/model/language_model/lamed_llama.py`, `LaMed/src/model/language_model/lamed_phi3.py`, `LaMed/src/model/CLIP.py`.

#### Segmentation Path
For mask-producing prompts, LaMed routes language-conditioned/task signals to SegVol, a volumetric Segment Anything-derived module. It contains image and prompt encoders, mask decoder, predictor, and automatic mask generator; inference returns generated text plus segmentation logits, later thresholded into masks. Paths: `LaMed/src/model/segmentation_module/segvol.py`, `LaMed/src/model/segmentation_module/builder.py`, `LaMed/src/model/segmentation_module/segment_anything_volumetric/modeling/sam.py`.

#### Training and Model Packaging
Training is explicitly staged: pretraining freezes vision and LLM while fitting the multimodal projector on image–text pairs; instruction tuning unfreezes non-LLM components and applies LoRA to the LLM across multitask data. A custom trainer coordinates this, while scripts select Llama/Phi variants and training phase. LoRA adapters can be merged into a Hugging Face-format model. Paths: `LaMed/src/train/lamed_trainer.py`, `LaMed/script/pretrain_phi3.sh`, `LaMed/src/utils/merge_lora_weights_and_save_hf_model.py`.

#### Evaluation, Runtime, and External Dependencies
Benchmark evaluation is a separate package with task-specific scoring for captioning, retrieval, positioning, segmentation, and VQA, plus optional LLM-based report evaluation. Runtime is Python with PyTorch, Transformers, NumPy, and GPU-oriented mixed/distributed training via Hugging Face Accelerate and DeepSpeed. External services/artifacts include Hugging Face model and dataset repositories, upstream Phi-3/Llama weights, M3D-CLIP weights, SegVol weights, and GPT-4V-oriented evaluation code. Paths: `Bench/eval/eval_caption.py`, `Bench/eval/eval_seg.py`, `Bench/eval/eval_with_llm.py`.

Main entry points: [`LaMed/src/train/lamed_trainer.py`](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/train/lamed_trainer.py) (LaMed trainer), [`LaMed/src/model/lamed_arch.py`](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/lamed_arch.py) (LaMed multimodal assembly), [`LaMed/src/utils/merge_lora_weights_and_save_hf_model.py`](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/utils/merge_lora_weights_and_save_hf_model.py) (HF model packaging), [`LaMed/src/dataset/multi_dataset.py`](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/dataset/multi_dataset.py) (Multitask dataset adapter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data pipeline"]
  node_external_data[("M3D task datasets<br/>external datasets")]
  node_data_preparation["Dataset preparation<br/>preprocessing scripts"]
  node_seg_preprocessing["Seg preprocessing<br/>preprocessing script"]
  node_multitask_dataset["Multitask dataset adapter<br/>PyTorch dataset<br/>[multi_dataset.py]"]
  node_examples[("Example volumes<br/>local .npy data")]
end

subgraph group_model["LaMed model"]
  node_visual_encoder["3D visual encoder<br/>vision backbone<br/>[builder.py]"]
  node_projector["Spatial pooling projector<br/>multimodal projector"]
  node_lamed_arch{{"LaMed multimodal assembly<br/>causal LM architecture<br/>[lamed_arch.py]"}}
  node_llama["LaMed Llama<br/>language backbone<br/>[lamed_llama.py]"]
  node_phi3["LaMed Phi-3<br/>language backbone<br/>[lamed_phi3.py]"]
  node_segvol["SegVol<br/>volumetric segmentation module<br/>[segvol.py]"]
  node_sam_model["Volumetric SAM model<br/>SegVol core<br/>[sam.py]"]
end

subgraph group_training["Training &amp; packaging"]
  node_clip_training["M3D-CLIP training<br/>vision pretraining<br/>[CLIP.py]"]
  node_train_entry["Training entry point<br/>training program<br/>[train.py]"]
  node_trainer["LaMed trainer<br/>custom trainer<br/>[lamed_trainer.py]"]
  node_pretrain_launcher["Phi-3 pretraining launcher<br/>shell launcher<br/>[pretrain_phi3.sh]"]
  node_model_packaging["HF model packaging<br/>LoRA merge utility"]
end

subgraph group_runtime["Runtime &amp; evaluation"]
  node_hf_artifacts[("External checkpoints<br/>HF and upstream artifacts")]
  node_demo["Local demo<br/>runtime entry point<br/>[demo.py]"]
  node_benchmark["Task benchmarks<br/>evaluation suite<br/>[eval_vqa.py]"]
  node_llm_evaluation["LLM report evaluation<br/>optional evaluator<br/>[eval_with_llm.py]"]
end

node_external_data -->|"caption data"| node_data_preparation
node_external_data -->|"segmentation data"| node_seg_preprocessing
node_data_preparation -->|"training artifacts"| node_multitask_dataset
node_seg_preprocessing -->|"mask artifacts"| node_multitask_dataset
node_examples -->|"demo volumes"| node_demo
node_multitask_dataset -->|"multitask samples"| node_trainer
node_pretrain_launcher -->|"selects phase"| node_train_entry
node_train_entry -->|"configures"| node_trainer
node_trainer -->|"trains"| node_lamed_arch
node_trainer -->|"pretrains then tunes"| node_projector
node_trainer -->|"LoRA adapters"| node_model_packaging
node_clip_training -->|"vision weights"| node_visual_encoder
node_hf_artifacts -->|"pretrained weights"| node_visual_encoder
node_hf_artifacts -->|"base weights"| node_llama
node_hf_artifacts -->|"base weights"| node_phi3
node_hf_artifacts -->|"SegVol weights"| node_segvol
node_visual_encoder -->|"volume features"| node_projector
node_projector -->|"visual tokens"| node_lamed_arch
node_llama -->|"selectable LLM"| node_lamed_arch
node_phi3 -->|"selectable LLM"| node_lamed_arch
node_sam_model -->|"SAM components"| node_segvol
node_lamed_arch -->|"mask prompts"| node_segvol
node_model_packaging -->|"loadable HF model"| node_demo
node_model_packaging -->|"evaluated model"| node_benchmark
node_benchmark -.->|"optional report scoring"| node_llm_evaluation

click node_data_preparation "https://github.com/baai-dcai/m3d/blob/main/Data/process/m3d_cap_data_prepare.py"
click node_seg_preprocessing "https://github.com/baai-dcai/m3d/blob/main/Data/process/m3d_seg_data_preprocess.py"
click node_multitask_dataset "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/dataset/multi_dataset.py"
click node_visual_encoder "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/multimodal_encoder/builder.py"
click node_projector "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/multimodal_projector/spatial_pooling_projector.py"
click node_lamed_arch "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/lamed_arch.py"
click node_llama "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/language_model/lamed_llama.py"
click node_phi3 "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/language_model/lamed_phi3.py"
click node_segvol "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/segmentation_module/segvol.py"
click node_sam_model "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/segmentation_module/segment_anything_volumetric/modeling/sam.py"
click node_clip_training "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/CLIP.py"
click node_train_entry "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/train/train.py"
click node_trainer "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/train/lamed_trainer.py"
click node_pretrain_launcher "https://github.com/baai-dcai/m3d/blob/main/LaMed/script/pretrain_phi3.sh"
click node_model_packaging "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/utils/merge_lora_weights_and_save_hf_model.py"
click node_demo "https://github.com/baai-dcai/m3d/blob/main/LaMed/src/demo/demo.py"
click node_benchmark "https://github.com/baai-dcai/m3d/blob/main/Bench/eval/eval_vqa.py"
click node_llm_evaluation "https://github.com/baai-dcai/m3d/blob/main/Bench/eval/eval_with_llm.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_external_data,node_data_preparation,node_seg_preprocessing,node_multitask_dataset,node_examples toneBlue
class node_visual_encoder,node_projector,node_lamed_arch,node_llama,node_phi3,node_segvol,node_sam_model toneAmber
class node_clip_training,node_train_entry,node_trainer,node_pretrain_launcher,node_model_packaging toneMint
class node_hf_artifacts,node_demo,node_benchmark,node_llm_evaluation toneRose
```

## Components

### Data pipeline

- **M3D task datasets**: external datasets · Downloaded Cap, VQA, Seg, and RefSeg sources.
- [Dataset preparation](https://github.com/baai-dcai/m3d/blob/main/Data/process/m3d_cap_data_prepare.py): preprocessing scripts · `Data/process/m3d_cap_data_prepare.py`
- [Seg preprocessing](https://github.com/baai-dcai/m3d/blob/main/Data/process/m3d_seg_data_preprocess.py): preprocessing script · `Data/process/m3d_seg_data_preprocess.py`
- [Multitask dataset adapter](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/dataset/multi_dataset.py): PyTorch dataset · Combines task data, prompts, and medical terminology. · `LaMed/src/dataset/multi_dataset.py`
- **Example volumes**: local .npy data

### LaMed model

- [3D visual encoder](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/multimodal_encoder/builder.py): vision backbone · `LaMed/src/model/multimodal_encoder/builder.py`
- [Spatial pooling projector](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/multimodal_projector/spatial_pooling_projector.py): multimodal projector · Maps volume features into LLM token space. · `LaMed/src/model/multimodal_projector/spatial_pooling_projector.py`
- [LaMed multimodal assembly](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/lamed_arch.py): causal LM architecture · Replaces image patch placeholders with projected visual features. · `LaMed/src/model/lamed_arch.py`
- [LaMed Llama](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/language_model/lamed_llama.py): language backbone · `LaMed/src/model/language_model/lamed_llama.py`
- [LaMed Phi-3](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/language_model/lamed_phi3.py): language backbone · `LaMed/src/model/language_model/lamed_phi3.py`
- [SegVol](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/segmentation_module/segvol.py): volumetric segmentation module · Provides image/prompt encoding and mask decoding. · `LaMed/src/model/segmentation_module/segvol.py`
- [Volumetric SAM model](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/segmentation_module/segment_anything_volumetric/modeling/sam.py): SegVol core · `LaMed/src/model/segmentation_module/segment_anything_volumetric/modeling/sam.py`

### Training & packaging

- [M3D-CLIP training](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/model/CLIP.py): vision pretraining · Produces recommended vision weights. · `LaMed/src/model/CLIP.py`
- [Training entry point](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/train/train.py): training program · `LaMed/src/train/train.py`
- [LaMed trainer](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/train/lamed_trainer.py): custom trainer · Runs projector pretraining and multitask LoRA instruction tuning. · `LaMed/src/train/lamed_trainer.py`
- [Phi-3 pretraining launcher](https://github.com/baai-dcai/m3d/blob/main/LaMed/script/pretrain_phi3.sh): shell launcher · `LaMed/script/pretrain_phi3.sh`
- [HF model packaging](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/utils/merge_lora_weights_and_save_hf_model.py): LoRA merge utility · Merges adapters and saves Hugging Face model format. · `LaMed/src/utils/merge_lora_weights_and_save_hf_model.py`

### Runtime & evaluation

- **External checkpoints**: HF and upstream artifacts · Includes Llama/Phi-3, M3D-CLIP, and SegVol weights.
- [Local demo](https://github.com/baai-dcai/m3d/blob/main/LaMed/src/demo/demo.py): runtime entry point · `LaMed/src/demo/demo.py`
- [Task benchmarks](https://github.com/baai-dcai/m3d/blob/main/Bench/eval/eval_vqa.py): evaluation suite · Scores captioning, retrieval, localization, segmentation, and VQA. · `Bench/eval/eval_vqa.py`
- [LLM report evaluation](https://github.com/baai-dcai/m3d/blob/main/Bench/eval/eval_with_llm.py): optional evaluator · `Bench/eval/eval_with_llm.py`

## Connections

- M3D task datasets → Dataset preparation: caption data
- M3D task datasets → Seg preprocessing: segmentation data
- Dataset preparation → Multitask dataset adapter: training artifacts
- Seg preprocessing → Multitask dataset adapter: mask artifacts
- Example volumes → Local demo: demo volumes
- Multitask dataset adapter → LaMed trainer: multitask samples
- Phi-3 pretraining launcher → Training entry point: selects phase
- Training entry point → LaMed trainer: configures
- LaMed trainer → LaMed multimodal assembly: trains
- LaMed trainer → Spatial pooling projector: pretrains then tunes
- LaMed trainer → HF model packaging: LoRA adapters
- M3D-CLIP training → 3D visual encoder: vision weights
- External checkpoints → 3D visual encoder: pretrained weights
- External checkpoints → LaMed Llama: base weights
- External checkpoints → LaMed Phi-3: base weights
- External checkpoints → SegVol: SegVol weights
- 3D visual encoder → Spatial pooling projector: volume features
- Spatial pooling projector → LaMed multimodal assembly: visual tokens
- LaMed Llama → LaMed multimodal assembly: selectable LLM
- LaMed Phi-3 → LaMed multimodal assembly: selectable LLM
- Volumetric SAM model → SegVol: SAM components
- LaMed multimodal assembly → SegVol: mask prompts
- HF model packaging → Local demo: loadable HF model
- HF model packaging → Task benchmarks: evaluated model
- Task benchmarks → LLM report evaluation: optional report scoring
