# flymin/magicdrive-v2 architecture

> Architecture diagram of the GitHub repository flymin/magicdrive-v2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/flymin/magicdrive-v2
- Repository: https://github.com/flymin/magicdrive-v2

## Overview

1. **What this repo is**
   MagicDrive-V2 is a controllable **autonomous-driving video generation** system. The core job of the repository is to train and run a **diffusion/flow-based video transformer** that generates multi-frame street scenes from structured driving controls such as 3D boxes, map context, and text conditioning.

2. **Top-level architecture**
   The repository is organized around four main planes:
   - **Model code** in `magicdrivedit/`
   - **Training / inference entrypoints** in `scripts/`
   - **Dataset and preprocessing pipeline** in `tools/prepare_data/` and `magicdrivedit/datasets/`
   - **Experiment configuration** in `configs/`
   This separation matters because most runtime behavior is driven by configs rather than hardcoded logic.

3. **Core generation model**
   The generation stack lives under `magicdrivedit/models/` and is centered on `models/magicdrive/magicdrive_stdit3.py`. It combines:
   - a **video VAE** (`models/vae/`) to operate in latent space,
   - a **text encoder** (`models/text_encoder/t5.py`),
   - the **MagicDrive/STDiT3-style transformer backbone**,
   - and embedding/control modules in `models/magicdrive/embedder.py`.
   In the architecture diagram, this is the main “generator” box between structured inputs and decoded video frames.

4. **Control and conditioning paths**
   The repo emphasizes **adaptive control** over scene generation. Conditioning comes from several sources:
   - **3D bounding boxes / temporal box tracks**
   - **BEV / map features**
   - **text prompts or scene descriptors**
   - **temporal frame selection / sequence length**
   These controls are injected through dedicated embedding and dataset logic rather than via a separate perception model.

5. **Latent video pipeline**
   The model operates on **latent video representations**, not raw pixels. The VAE components are external pretrained weights from CogVideoX, and the generation process runs on latents that are later decoded back into video frames. This is a critical boundary: data loading and control prep happen in the “world” domain, while the transformer and scheduler work in latent space.

6. **Diffusion / flow-matching scheduler**
   `magicdrivedit/schedulers/rf/rectified_flow.py` implements the rectified-flow generation process used during training and inference. The scheduler is the bridge between noisy latent states and final latent samples. Configs reference variants like `rflow-slice`, which suggests memory-aware or chunked inference for long videos.

7. **Dataset and preprocessing boundary**
   The nuScenes pipeline is a major subsystem. Raw nuScenes data is converted into model-ready metadata and auxiliary caches by tools in `tools/prepare_data/` and dataset code in `magicdrivedit/datasets/`. The data flow is:
   - nuScenes raw assets
   - generated metadata `.pkl` files
   - optional BEV map caches in `.h5`
   - dataset/dataloader objects
   - model batches containing frames, boxes, maps, and related temporal metadata
   This preprocessing boundary is important because the repository assumes these derived artifacts exist before training or inference.

8. **Dataset abstractions**
   There are two overlapping dataset layers:
   - `magicdrivedit/datasets/` contains project-specific dataset wrappers, samplers, and helpers for variable-length or cached nuScenes sequences.
   - `magicdrivedit/mmdet_plugin/datasets/` contains BEVFusion-style dataset and pipeline utilities, including box loading, transforms, formatting, and NuScenes dataset support.
   The project reuses detection-oriented geometry code to prepare driving context, even though the final task is generation.

9. **3D geometry and BEV utilities**
   The `mmdet_plugin/core/` subtree provides 3D boxes, point structures, voxel utilities, and visualization helpers. This is not the main learning model, but it is essential infrastructure for representing autonomous-driving scene geometry in a consistent way. It forms the lower-level boundary between raw annotations and the higher-level conditioning tensors used by the generator.

10. **Distributed training and inference infrastructure**
   `magicdrivedit/acceleration/` and `patch/low_level_optim.py` indicate that the repo is designed for **multi-GPU / multi-node** execution with heavy optimization support. The README also references **ColossalAI**, **sequence parallelism**, and platform-specific acceleration for NVIDIA and Ascend/NPU environments. Architecturally, this layer wraps model execution, checkpointing, and communication rather than changing the model semantics.

11. **Training stages and experiment organization**
   The configs are split by purpose:
   - `configs/magicdrive/train/` for stage-wise training
   - `configs/magicdrive/inference/` for generation runs
   - `configs/magicdrive/test/` for benchmark-style evaluation
   - `configs/cache_gen/` and `configs/dataset/` for preprocessing/cache generation
   This suggests a staged pipeline: train lower-resolution or shorter-sequence models first, then scale to higher-resolution and longer sequences.

12. **Entry scripts and runtime flow**
   `scripts/train_magicdrive.py`, `scripts/inference_magicdrive.py`, and `scripts/test_magicdrive.py` are the main execution entrypoints. They read a config, build the dataset/model/scheduler stack via the registry, load pretrained weights, and run either optimization, sampling, or benchmark export. The repository is therefore config-driven end to end.

13. **Auxiliary tooling**
   The `tools/viewer/` folder provides a lightweight viewer for inspecting outputs, and `tools/imgFoler2vid.py` suggests simple post-processing for turning image folders into videos. These are secondary utilities, but they sit at the edge of the architecture for validation and presentation.

14. **External dependencies that shape the design**
   The architecture depends materially on:
   - **nuScenes** as the driving dataset
   - **CogVideoX VAE** for latent video compression/decoding
   - **T5-XXL** for text encoding
   - **ColossalAI** for distributed training and checkpointing
   - **BEVFusion-style tooling** for 3D geometry and data preparation
   These external pieces define the major integration boundaries around the core MagicDrive-V2 model.

Main entry points: [`magicdrivedit/registry.py`](https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/registry.py) (Registry), [`magicdrivedit/models/magicdrive/magicdrive_stdit3.py`](https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/models/magicdrive/magicdrive_stdit3.py) (MagicDrive), [`scripts/inference_magicdrive.py`](https://github.com/flymin/magicdrive-v2/blob/main/scripts/inference_magicdrive.py) (Infer), [`magicdrivedit/datasets`](https://github.com/flymin/magicdrive-v2/tree/main/magicdrivedit/datasets) (Datasets)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_scripts_train["Train<br/>entrypoint"]
  node_scripts_infer["Infer<br/>entrypoint"]
  node_scripts_test["Test<br/>entrypoint<br/>[test_magicdrive.py]"]
  node_registry["Registry<br/>factory<br/>[registry.py]"]
  node_acceleration["Accel<br/>distributed"]
end

subgraph group_model["Model"]
  node_model_core["MagicDrive<br/>video-generator"]
  node_embedder["Embedder<br/>conditioning<br/>[embedder.py]"]
  node_text_t5["T5 Encoder<br/>text-encoder<br/>[t5.py]"]
  node_vae[("Video VAE<br/>latent-autoencoder<br/>[vae_cogvideox.py]")]
  node_rectified_flow["Rectified Flow<br/>scheduler<br/>[rectified_flow.py]"]
end

subgraph group_data["Data"]
  node_dataset_api[("Datasets<br/>nuScenes-loader")]
  node_nuscenes_pipe["NuScenes Pipe<br/>3d-dataset"]
  node_geom_core["3D Geometry<br/>geometry-utils"]
  node_prepare_data["Prepare Data<br/>preprocessing"]
end

subgraph group_config["Config & Tools"]
  node_configs_train["Train Configs<br/>config-set"]
  node_configs_infer["Infer Configs<br/>config-set"]
  node_configs_data["Dataset Configs<br/>config-set"]
  node_viewer["Viewer"]
end

node_scripts_train -->|"builds"| node_registry
node_scripts_infer -->|"builds"| node_registry
node_scripts_test -->|"builds"| node_registry
node_registry -->|"instantiates"| node_model_core
node_registry -->|"instantiates"| node_dataset_api
node_registry -->|"instantiates"| node_rectified_flow
node_registry -->|"wraps"| node_acceleration
node_model_core -->|"uses"| node_embedder
node_model_core -->|"conditions on"| node_text_t5
node_model_core -->|"encodes/decodes"| node_vae
node_model_core -->|"sampled by"| node_rectified_flow
node_dataset_api -->|"reuses"| node_nuscenes_pipe
node_nuscenes_pipe -->|"uses"| node_geom_core
node_prepare_data -->|"produces artifacts for"| node_configs_data
node_configs_train -->|"drives"| node_scripts_train
node_configs_infer -->|"drives"| node_scripts_infer
node_configs_data -->|"feeds"| node_dataset_api
node_scripts_infer -.->|"visualizes with"| node_viewer
node_acceleration -.->|"optimizes execution"| node_model_core

click node_scripts_train "https://github.com/flymin/magicdrive-v2/blob/main/scripts/train_magicdrive.py"
click node_scripts_infer "https://github.com/flymin/magicdrive-v2/blob/main/scripts/inference_magicdrive.py"
click node_scripts_test "https://github.com/flymin/magicdrive-v2/blob/main/scripts/test_magicdrive.py"
click node_registry "https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/registry.py"
click node_acceleration "https://github.com/flymin/magicdrive-v2/tree/main/magicdrivedit/acceleration"
click node_model_core "https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/models/magicdrive/magicdrive_stdit3.py"
click node_embedder "https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/models/magicdrive/embedder.py"
click node_text_t5 "https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/models/text_encoder/t5.py"
click node_vae "https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/models/vae/vae_cogvideox.py"
click node_rectified_flow "https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/schedulers/rf/rectified_flow.py"
click node_dataset_api "https://github.com/flymin/magicdrive-v2/tree/main/magicdrivedit/datasets"
click node_nuscenes_pipe "https://github.com/flymin/magicdrive-v2/tree/main/magicdrivedit/mmdet_plugin/datasets"
click node_geom_core "https://github.com/flymin/magicdrive-v2/tree/main/magicdrivedit/mmdet_plugin/core"
click node_prepare_data "https://github.com/flymin/magicdrive-v2/tree/main/tools/prepare_data"
click node_configs_train "https://github.com/flymin/magicdrive-v2/tree/main/configs/magicdrive/train"
click node_configs_infer "https://github.com/flymin/magicdrive-v2/tree/main/configs/magicdrive/inference"
click node_configs_data "https://github.com/flymin/magicdrive-v2/tree/main/configs/dataset"
click node_viewer "https://github.com/flymin/magicdrive-v2/tree/main/tools/viewer"

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_scripts_train,node_scripts_infer,node_scripts_test,node_registry,node_acceleration toneBlue
class node_model_core,node_embedder,node_text_t5,node_vae,node_rectified_flow toneAmber
class node_dataset_api,node_nuscenes_pipe,node_geom_core,node_prepare_data toneMint
class node_configs_train,node_configs_infer,node_configs_data,node_viewer toneRose
```

## Components

### Runtime

Execution entrypoints and distributed training/inference infrastructure

- [Train](https://github.com/flymin/magicdrive-v2/blob/main/scripts/train_magicdrive.py): entrypoint · Main training driver · `scripts/train_magicdrive.py`
- [Infer](https://github.com/flymin/magicdrive-v2/blob/main/scripts/inference_magicdrive.py): entrypoint · Main sampling driver · `scripts/inference_magicdrive.py`
- [Test](https://github.com/flymin/magicdrive-v2/blob/main/scripts/test_magicdrive.py): entrypoint · Evaluation driver · `scripts/test_magicdrive.py`
- [Registry](https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/registry.py): factory · Builds model, data, and runtime objects from config · `magicdrivedit/registry.py`
- [Accel](https://github.com/flymin/magicdrive-v2/tree/main/magicdrivedit/acceleration): distributed · Checkpointing, parallel states, and communication wrappers · `magicdrivedit/acceleration`

### Model

Latent video generator and sampling stack

- [MagicDrive](https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/models/magicdrive/magicdrive_stdit3.py): video-generator · STDiT3-style latent video generation model · `magicdrivedit/models/magicdrive/magicdrive_stdit3.py`
- [Embedder](https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/models/magicdrive/embedder.py): conditioning · Control and scene-conditioning embeddings · `magicdrivedit/models/magicdrive/embedder.py`
- [T5 Encoder](https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/models/text_encoder/t5.py): text-encoder · Text conditioning backbone · `magicdrivedit/models/text_encoder/t5.py`
- [Video VAE](https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/models/vae/vae_cogvideox.py): latent-autoencoder · CogVideoX latent video encoder/decoder · `magicdrivedit/models/vae/vae_cogvideox.py`
- [Rectified Flow](https://github.com/flymin/magicdrive-v2/blob/main/magicdrivedit/schedulers/rf/rectified_flow.py): scheduler · Diffusion/flow matching sampler and training schedule · `magicdrivedit/schedulers/rf/rectified_flow.py`

### Data

nuScenes preprocessing, dataset layers, and geometry utilities

- [Datasets](https://github.com/flymin/magicdrive-v2/tree/main/magicdrivedit/datasets): nuScenes-loader · Project-specific dataset wrappers and loaders · `magicdrivedit/datasets`
- [NuScenes Pipe](https://github.com/flymin/magicdrive-v2/tree/main/magicdrivedit/mmdet_plugin/datasets): 3d-dataset · BEVFusion-style dataset and pipeline utilities · `magicdrivedit/mmdet_plugin/datasets`
- [3D Geometry](https://github.com/flymin/magicdrive-v2/tree/main/magicdrivedit/mmdet_plugin/core): geometry-utils · Boxes, points, voxels, and visualization helpers · `magicdrivedit/mmdet_plugin/core`
- [Prepare Data](https://github.com/flymin/magicdrive-v2/tree/main/tools/prepare_data): preprocessing · Raw nuScenes conversion and cache generation · `tools/prepare_data`

### Config & Tools

Experiment configs and auxiliary utilities

- [Train Configs](https://github.com/flymin/magicdrive-v2/tree/main/configs/magicdrive/train): config-set · Stage-wise training recipes · `configs/magicdrive/train`
- [Infer Configs](https://github.com/flymin/magicdrive-v2/tree/main/configs/magicdrive/inference): config-set · Sampling and generation recipes · `configs/magicdrive/inference`
- [Dataset Configs](https://github.com/flymin/magicdrive-v2/tree/main/configs/dataset): config-set · Dataset and cache configuration files · `configs/dataset`
- [Viewer](https://github.com/flymin/magicdrive-v2/tree/main/tools/viewer): utility · Lightweight output inspection server · `tools/viewer`

## Connections

- Train → Registry: builds
- Infer → Registry: builds
- Test → Registry: builds
- Registry → MagicDrive: instantiates
- Registry → Datasets: instantiates
- Registry → Rectified Flow: instantiates
- Registry → Accel: wraps
- MagicDrive → Embedder: uses
- MagicDrive → T5 Encoder: conditions on
- MagicDrive → Video VAE: encodes/decodes
- MagicDrive → Rectified Flow: sampled by
- Datasets → NuScenes Pipe: reuses
- NuScenes Pipe → 3D Geometry: uses
- Prepare Data → Dataset Configs: produces artifacts for
- Train Configs → Train: drives
- Infer Configs → Infer: drives
- Dataset Configs → Datasets: feeds
- Infer → Viewer: visualizes with
- Accel → MagicDrive: optimizes execution
