# tsinghua-mars-lab/drivema architecture

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

- Interactive diagram: https://gitdiagram.com/tsinghua-mars-lab/drivema
- Repository: https://github.com/tsinghua-mars-lab/drivema

## Overview

DriveMA extends ms-swift into a vision-language-action driving planner. Its principal workflow predicts an interpretable meta-action from driving observations, generates waypoints conditioned on that action, and trains these behaviors through action-centric pretraining, trajectory SFT, then turn-level RL. The README also describes driving/VQA annotations, trajectory-aware rewards, inference, and Waymo planning evaluation. The tree shows likely runtime areas, but the sampled code is mostly inherited ms-swift infrastructure; connections to DriveMA-specific stages are therefore left unverified rather than inferred as direct calls.

Main entry points: [`swift/infer_engine/infer_engine.py`](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/infer_engine/infer_engine.py) (Inference engine), [`swift/pipelines/train/rlhf.py`](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/pipelines/train/rlhf.py) (Turn-level RL), [`swift/dataset`](https://github.com/tsinghua-mars-lab/drivema/tree/main/swift/dataset) (Dataset loading), [`swift/pipelines/train/sft.py`](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/pipelines/train/sft.py) (Trajectory SFT)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_drive["DriveMA workflow"]
  node_acp["Action pretraining<br/>[sft.py]"]
  node_traj_sft["Trajectory SFT<br/>[sft.py]"]
  node_rl["Turn-level RL<br/>[rlhf.py]"]
  node_reward["Trajectory rewards"]
end

subgraph group_data["Data and evaluation"]
  node_observations["Driving observations"]
  node_annotations["Driving annotations"]
  node_dataset["Dataset loading"]
  node_preprocess["Trajectory preparation"]
  node_evaluation["Planning evaluation<br/>[cal_ade.py]"]
end

subgraph group_train["Training runtime"]
  node_train_runtime["Trainer runtime<br/>[trainer.py]"]
  node_megatron_trainer["Megatron trainers<br/>[base.py]"]
end

subgraph group_model["Model and inference"]
  node_model_registry["Model registry<br/>[register.py]"]
  node_model_arch["Model architecture<br/>[model_arch.py]"]
  node_templates["Prompt templates<br/>[base.py]"]
  node_inference["Inference engine<br/>[infer_engine.py]"]
  node_vllm["vLLM backend<br/>[vllm_engine.py]"]
  node_transformers["Transformers backend"]
  node_request_protocol["Inference protocol<br/>[protocol.py]"]
end

subgraph group_base["Shared framework"]
  node_metrics["Metrics"]
  node_args["Training arguments<br/>[rlhf_args.py]"]
  node_utils["Shared utilities<br/>[torch_utils.py]"]
end

node_driver(("Driving user"))

node_driver -->|"provides"| node_observations
node_observations -.->|"submits for planning"| node_inference
node_annotations -.->|"loaded as data"| node_dataset
node_annotations -.->|"used to prepare"| node_preprocess
node_dataset -.->|"supplies training data"| node_acp
node_dataset -.->|"supplies training data"| node_traj_sft
node_dataset -.->|"supplies training data"| node_rl
node_acp -->|"precedes"| node_traj_sft
node_traj_sft -->|"precedes"| node_rl
node_rl -->|"uses consistency rewards"| node_reward
node_acp -.->|"trains with"| node_train_runtime
node_traj_sft -.->|"trains with"| node_train_runtime
node_rl -.->|"trains with"| node_train_runtime
node_rl -.->|"may use"| node_megatron_trainer
node_inference -->|"handles requests"| node_request_protocol
node_inference -->|"uses"| node_templates
node_inference -->|"backend implementation"| node_vllm
node_inference -->|"backend implementation"| node_transformers
node_inference -->|"loads model through"| node_model_registry
node_model_registry -->|"uses architecture metadata"| node_model_arch
node_inference -.->|"produces plans for"| node_evaluation
node_evaluation -.->|"reports planning scores"| node_metrics
node_args -.->|"configures arguments"| node_rl
node_train_runtime -.->|"uses shared utilities"| node_utils

click node_dataset "https://github.com/tsinghua-mars-lab/drivema/tree/main/swift/dataset"
click node_preprocess "https://github.com/tsinghua-mars-lab/drivema/blob/main/tools/build_data/convert_traj_jsonl_to_mllm_vision_mutil_turn_random_mask.py"
click node_acp "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/pipelines/train/sft.py"
click node_traj_sft "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/pipelines/train/sft.py"
click node_rl "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/pipelines/train/rlhf.py"
click node_reward "https://github.com/tsinghua-mars-lab/drivema/tree/main/swift/rewards"
click node_train_runtime "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/trainers/trainer.py"
click node_megatron_trainer "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/megatron/trainers/base.py"
click node_model_registry "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/model/register.py"
click node_model_arch "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/model/model_arch.py"
click node_templates "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/template/base.py"
click node_inference "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/infer_engine/infer_engine.py"
click node_vllm "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/infer_engine/vllm_engine.py"
click node_transformers "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/infer_engine/transformers_engine.py"
click node_request_protocol "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/infer_engine/protocol.py"
click node_evaluation "https://github.com/tsinghua-mars-lab/drivema/blob/main/tools/eval/cal_ade.py"
click node_metrics "https://github.com/tsinghua-mars-lab/drivema/tree/main/swift/metrics"
click node_args "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/arguments/rlhf_args.py"
click node_utils "https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/utils/torch_utils.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_acp,node_traj_sft,node_rl,node_reward,node_driver toneBlue
class node_observations,node_annotations,node_dataset,node_preprocess,node_evaluation toneAmber
class node_train_runtime,node_megatron_trainer toneMint
class node_model_registry,node_model_arch,node_templates,node_inference,node_vllm,node_transformers,node_request_protocol toneRose
class node_metrics,node_args,node_utils toneIndigo
```

## Components

### DriveMA workflow

- [Action pretraining](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/pipelines/train/sft.py): `swift/pipelines/train/sft.py`
- [Trajectory SFT](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/pipelines/train/sft.py): `swift/pipelines/train/sft.py`
- [Turn-level RL](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/pipelines/train/rlhf.py): `swift/pipelines/train/rlhf.py`
- [Trajectory rewards](https://github.com/tsinghua-mars-lab/drivema/tree/main/swift/rewards): `swift/rewards`

### Data and evaluation

- **Driving observations**
- **Driving annotations**
- [Dataset loading](https://github.com/tsinghua-mars-lab/drivema/tree/main/swift/dataset): `swift/dataset`
- [Trajectory preparation](https://github.com/tsinghua-mars-lab/drivema/blob/main/tools/build_data/convert_traj_jsonl_to_mllm_vision_mutil_turn_random_mask.py): `tools/build_data/convert_traj_jsonl_to_mllm_vision_mutil_turn_random_mask.py`
- [Planning evaluation](https://github.com/tsinghua-mars-lab/drivema/blob/main/tools/eval/cal_ade.py): `tools/eval/cal_ade.py`

### Training runtime

- [Trainer runtime](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/trainers/trainer.py): `swift/trainers/trainer.py`
- [Megatron trainers](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/megatron/trainers/base.py): `swift/megatron/trainers/base.py`

### Model and inference

- [Model registry](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/model/register.py): `swift/model/register.py`
- [Model architecture](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/model/model_arch.py): `swift/model/model_arch.py`
- [Prompt templates](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/template/base.py): `swift/template/base.py`
- [Inference engine](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/infer_engine/infer_engine.py): `swift/infer_engine/infer_engine.py`
- [vLLM backend](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/infer_engine/vllm_engine.py): `swift/infer_engine/vllm_engine.py`
- [Transformers backend](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/infer_engine/transformers_engine.py): `swift/infer_engine/transformers_engine.py`
- [Inference protocol](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/infer_engine/protocol.py): `swift/infer_engine/protocol.py`

### Shared framework

- [Metrics](https://github.com/tsinghua-mars-lab/drivema/tree/main/swift/metrics): `swift/metrics`
- [Training arguments](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/arguments/rlhf_args.py): `swift/arguments/rlhf_args.py`
- [Shared utilities](https://github.com/tsinghua-mars-lab/drivema/blob/main/swift/utils/torch_utils.py): `swift/utils/torch_utils.py`

### Other components

- **Driving user**

## Connections

- Driving user → Driving observations: provides (evidence: `README.md`)
- Driving observations → Inference engine: submits for planning
- Driving annotations → Dataset loading: loaded as data
- Driving annotations → Trajectory preparation: used to prepare
- Dataset loading → Action pretraining: supplies training data
- Dataset loading → Trajectory SFT: supplies training data
- Dataset loading → Turn-level RL: supplies training data
- Action pretraining → Trajectory SFT: precedes (evidence: `README.md`)
- Trajectory SFT → Turn-level RL: precedes (evidence: `README.md`)
- Turn-level RL → Trajectory rewards: uses consistency rewards (evidence: `README.md`)
- Action pretraining → Trainer runtime: trains with
- Trajectory SFT → Trainer runtime: trains with
- Turn-level RL → Trainer runtime: trains with
- Turn-level RL → Megatron trainers: may use
- Inference engine → Inference protocol: handles requests (evidence: `swift/infer_engine/infer_engine.py`)
- Inference engine → Prompt templates: uses (evidence: `swift/infer_engine/infer_engine.py`)
- Inference engine → vLLM backend: backend implementation (evidence: `swift/infer_engine/vllm_engine.py`)
- Inference engine → Transformers backend: backend implementation (evidence: `swift/infer_engine/transformers_engine.py`)
- Inference engine → Model registry: loads model through (evidence: `swift/infer_engine/infer_engine.py`)
- Model registry → Model architecture: uses architecture metadata (evidence: `swift/model/__init__.py`)
- Inference engine → Planning evaluation: produces plans for
- Planning evaluation → Metrics: reports planning scores
- Training arguments → Turn-level RL: configures arguments
- Trainer runtime → Shared utilities: uses shared utilities
