# bytedance-seed/in-place-ttt architecture

> Architecture diagram of the GitHub repository bytedance-seed/in-place-ttt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bytedance-seed/in-place-ttt
- Repository: https://github.com/bytedance-seed/in-place-ttt

## Overview

1. **What this repository is**
   - This is a research/engineering stack for **In-Place Test-Time Training (TTT)** on Transformer LLMs.
   - The end-to-end flow is: **processed dataset + base HF model assets → VeOmni continual pretraining → distributed checkpoints (DCP) → HuggingFace export → TTT-aware inference → RULER evaluation**.

2. **Top-level architecture boundary**
   - The repo is split into two major runtime paths:
     - **Training path**: `train.sh` → `tasks/train_torch.py` → VeOmni/FSDP2 training.
     - **Inference/evaluation path**: `inference_model/` + `eval.sh` + `eval_config/` → OpenCompass-based benchmarking.
   - `scripts/merge_dcp_to_hf.py` is the bridge between these two paths, converting training checkpoints into a format usable by inference and evaluation.

3. **Configuration layer**
   - `configs/pretrain/*.yaml` contains the recommended **model/data/train** settings for Qwen3 and LLaMA 3.1 long-context continual pretraining.
   - `eval_config/ruler_*.py` defines benchmark runs for multiple context lengths, while `eval_config/models.py` registers which HuggingFace checkpoint to evaluate.
   - This repo is config-driven: most behavior is selected by YAML/Python config rather than hard-coded entrypoints.

4. **Training subsystem**
   - `tasks/train_torch.py` is the main training task definition, launched through `train.sh`.
   - It is built around **VeOmni** and uses distributed training features called out in the README: **FSDP2**, mixed precision, gradient checkpointing, full-shard training, and DCP checkpoint management.
   - Training output is written to a user-specified `train.output_dir`, with optional Weights & Biases logging via `byted-wandb`.

5. **Data subsystem**
   - The repository does **not** contain dataset preprocessing code; it expects a **preprocessed external dataset** supplied at `data.train_path`.
   - The recommended data format is **plaintext iterable data** with `text_keys: content_split`, which implies tokenization/packing happens in the training pipeline rather than in this repo.
   - This is an important architectural boundary: the repo assumes upstream data preparation is already done.

6. **Model implementation layer**
   - `hf_models/` contains custom HuggingFace-style model code for **LLaMA** and **Qwen3** used in the training-oriented stack.
   - `inference_model/` contains a parallel set of model definitions for inference/evaluation, also split by LLaMA and Qwen3.
   - The duplication is intentional: it separates the **training model path** from the **inference loading path**, while keeping both compatible with HF-style model/config registration.

7. **In-Place TTT mechanism in the model stack**
   - The method updates a subset of parameters—the **MLP down-projection fast weights**—during inference.
   - The README indicates the implementation is meant to stay **inside the standard Transformer block**, avoiding extra memory modules or side networks.
   - Config fields like `ttt_layers`, `ttt_mode`, `ttt_proj`, `ttt_lr`, and `ttt_chunk` control where fast weights are enabled and how chunk-wise updates are computed.

8. **Checkpoint conversion subsystem**
   - `scripts/merge_dcp_to_hf.py` is the export/packaging step after training.
   - It merges VeOmni **DCP shards** into a HuggingFace checkpoint layout and can optionally reshard the output with a target shard size.
   - This conversion boundary matters because evaluation and inference consume the HF-formatted weights, not the raw distributed training artifacts.

9. **Evaluation subsystem**
   - `eval.sh` is the top-level evaluation launcher.
   - Evaluation is built around **OpenCompass** and the **RULER** benchmark suite, with configs spanning **4K, 8K, 16K, 32K, 64K, 128K, and 256K** context lengths.
   - The evaluation path loads a checkpoint through `inference_model/`, then runs benchmark-specific configs to measure long-context behavior.

10. **External dependencies that shape the architecture**
   - The stack is pinned to **PyTorch 2.8**, **FlashAttention**, **Transformers 4.57.3**, and a specific **VeOmni commit**, which strongly constrains runtime compatibility.
   - The README also calls out **liger-kernel**, `torchdata`, `datasets`, `diffusers`, `tiktoken`, `timm`, and `opt_einsum/einops`, indicating this is a high-performance training/inference environment rather than a lightweight library.
   - W&B integration is present for experiment tracking, but the core system remains local/distributed model training and evaluation.

11. **Primary data flow to diagram**
   - External processed text dataset enters the **training task**.
   - The training task drives **continual pretraining** of a Qwen3 or LLaMA model with in-place TTT enabled.
   - Training emits **DCP checkpoints**, which are converted to **HuggingFace checkpoints**.
   - The HF checkpoint is then consumed by the **inference model package** and evaluated through **OpenCompass/RULER**.

Main entry points: [`tasks/train_torch.py`](https://github.com/bytedance-seed/in-place-ttt/blob/main/tasks/train_torch.py) (Train task), [`tasks/infer.py`](https://github.com/bytedance-seed/in-place-ttt/blob/main/tasks/infer.py) (Infer task), [`scripts/merge_dcp_to_hf.py`](https://github.com/bytedance-seed/in-place-ttt/blob/main/scripts/merge_dcp_to_hf.py) (Merge DCP), [`inference_model/hf_llama3/modeling_llama.py`](https://github.com/bytedance-seed/in-place-ttt/blob/main/inference_model/hf_llama3/modeling_llama.py) (LLaMA infer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_training["Training"]
  node_n_train_sh["train.sh<br/>launcher<br/>[train.sh]"]
  node_n_train_task["Train task<br/>torch task<br/>[train_torch.py]"]
  node_n_data_ext[("External data<br/>dataset boundary")]
  node_n_veomni{{"VeOmni/FSDP2<br/>distributed runtime"}}
end

subgraph group_g_export["Export"]
  node_n_dcp_ckpt[("DCP shards<br/>checkpoint")]
  node_n_merge_tool["Merge DCP<br/>export script<br/>[merge_dcp_to_hf.py]"]
  node_n_hf_ckpt[("HF checkpoint<br/>artifact")]
end

subgraph group_g_inference["Inference"]
  node_n_eval_sh["eval.sh<br/>launcher<br/>[eval.sh]"]
  node_n_eval_task["Infer task<br/>eval task<br/>[infer.py]"]
  node_n_opencompass{{"OpenCompass<br/>benchmark runtime"}}
end

subgraph group_g_config["Config"]
  node_n_pretrain_cfg_qwen["Qwen3 pretrain<br/>yaml config<br/>[qwen3_longct.yaml]"]
  node_n_pretrain_cfg_llama["LLaMA pretrain<br/>yaml config<br/>[llama3_longct.yaml]"]
  node_n_ruler_cfgs["RULER configs<br/>benchmark config"]
  node_n_model_registry["Model registry<br/>python config<br/>[models.py]"]
end

subgraph group_g_models["Model code"]
  node_n_infer_llama["LLaMA infer<br/>hf model<br/>[modeling_llama.py]"]
  node_n_infer_qwen["Qwen3 infer<br/>hf model<br/>[modeling_qwen3.py]"]
  node_n_train_llama["LLaMA train<br/>hf model<br/>[modeling_llama.py]"]
  node_n_train_qwen["Qwen3 train<br/>hf model<br/>[modeling_qwen3.py]"]
end

node_n_train_sh -->|"launches"| node_n_train_task
node_n_train_task -->|"loads"| node_n_pretrain_cfg_qwen
node_n_train_task -->|"loads"| node_n_pretrain_cfg_llama
node_n_train_task -->|"reads"| node_n_data_ext
node_n_train_task -->|"uses"| node_n_veomni
node_n_veomni -->|"writes"| node_n_dcp_ckpt
node_n_dcp_ckpt -->|"converted by"| node_n_merge_tool
node_n_merge_tool -->|"produces"| node_n_hf_ckpt
node_n_eval_sh -->|"launches"| node_n_eval_task
node_n_eval_task -->|"selects"| node_n_model_registry
node_n_eval_task -->|"loads"| node_n_ruler_cfgs
node_n_eval_task -->|"runs on"| node_n_opencompass
node_n_opencompass -->|"consumes"| node_n_hf_ckpt
node_n_hf_ckpt -->|"loads into"| node_n_infer_llama
node_n_hf_ckpt -->|"loads into"| node_n_infer_qwen
node_n_train_task -->|"instantiates"| node_n_train_llama
node_n_train_task -->|"instantiates"| node_n_train_qwen
node_n_eval_task -->|"instantiates"| node_n_infer_llama
node_n_eval_task -->|"instantiates"| node_n_infer_qwen

click node_n_train_sh "https://github.com/bytedance-seed/in-place-ttt/blob/main/train.sh"
click node_n_train_task "https://github.com/bytedance-seed/in-place-ttt/blob/main/tasks/train_torch.py"
click node_n_pretrain_cfg_qwen "https://github.com/bytedance-seed/in-place-ttt/blob/main/configs/pretrain/qwen3_longct.yaml"
click node_n_pretrain_cfg_llama "https://github.com/bytedance-seed/in-place-ttt/blob/main/configs/pretrain/llama3_longct.yaml"
click node_n_merge_tool "https://github.com/bytedance-seed/in-place-ttt/blob/main/scripts/merge_dcp_to_hf.py"
click node_n_eval_sh "https://github.com/bytedance-seed/in-place-ttt/blob/main/eval.sh"
click node_n_eval_task "https://github.com/bytedance-seed/in-place-ttt/blob/main/tasks/infer.py"
click node_n_model_registry "https://github.com/bytedance-seed/in-place-ttt/blob/main/eval_config/models.py"
click node_n_infer_llama "https://github.com/bytedance-seed/in-place-ttt/blob/main/inference_model/hf_llama3/modeling_llama.py"
click node_n_infer_qwen "https://github.com/bytedance-seed/in-place-ttt/blob/main/inference_model/hf_qwen3/modeling_qwen3.py"
click node_n_train_llama "https://github.com/bytedance-seed/in-place-ttt/blob/main/hf_models/hf_llama/modeling_llama.py"
click node_n_train_qwen "https://github.com/bytedance-seed/in-place-ttt/blob/main/hf_models/hf_qwen3/modeling_qwen3.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_n_train_sh,node_n_train_task,node_n_data_ext,node_n_veomni toneBlue
class node_n_dcp_ckpt,node_n_merge_tool,node_n_hf_ckpt toneAmber
class node_n_eval_sh,node_n_eval_task,node_n_opencompass toneMint
class node_n_pretrain_cfg_qwen,node_n_pretrain_cfg_llama,node_n_ruler_cfgs,node_n_model_registry toneRose
class node_n_infer_llama,node_n_infer_qwen,node_n_train_llama,node_n_train_qwen toneIndigo
```

## Components

### Training

Continual pretraining and checkpoint creation.

- [train.sh](https://github.com/bytedance-seed/in-place-ttt/blob/main/train.sh): launcher · Shell entrypoint for the training pipeline. · `train.sh`
- [Train task](https://github.com/bytedance-seed/in-place-ttt/blob/main/tasks/train_torch.py): torch task · Main training program launched by the training script. · `tasks/train_torch.py`
- **External data**: dataset boundary · Preprocessed iterable text dataset supplied outside the repository.
- **VeOmni/FSDP2**: distributed runtime · Training runtime used for full-shard continual pretraining.

### Export

Checkpoint conversion between runtime formats.

- **DCP shards**: checkpoint · Distributed training checkpoints produced by VeOmni.
- [Merge DCP](https://github.com/bytedance-seed/in-place-ttt/blob/main/scripts/merge_dcp_to_hf.py): export script · Converts DCP shards into HuggingFace checkpoint layout. · `scripts/merge_dcp_to_hf.py`
- **HF checkpoint**: artifact · Merged HuggingFace-formatted weights consumed by inference and evaluation.

### Inference

TTT-aware model loading and evaluation path.

- [eval.sh](https://github.com/bytedance-seed/in-place-ttt/blob/main/eval.sh): launcher · Shell entrypoint for benchmark evaluation. · `eval.sh`
- [Infer task](https://github.com/bytedance-seed/in-place-ttt/blob/main/tasks/infer.py): eval task · Inference/evaluation task wrapper used by the runtime. · `tasks/infer.py`
- **OpenCompass**: benchmark runtime · Benchmarking framework used to run RULER evaluations.

### Config

Training and benchmark configuration layer.

- [Qwen3 pretrain](https://github.com/bytedance-seed/in-place-ttt/blob/main/configs/pretrain/qwen3_longct.yaml): yaml config · Recommended continual pretraining settings for Qwen3 long context. · `configs/pretrain/qwen3_longct.yaml`
- [LLaMA pretrain](https://github.com/bytedance-seed/in-place-ttt/blob/main/configs/pretrain/llama3_longct.yaml): yaml config · Recommended continual pretraining settings for LLaMA 3.1 long context. · `configs/pretrain/llama3_longct.yaml`
- **RULER configs**: benchmark config · Context-length benchmark definitions from 4K to 256K.
- [Model registry](https://github.com/bytedance-seed/in-place-ttt/blob/main/eval_config/models.py): python config · Selects which HuggingFace checkpoint to evaluate. · `eval_config/models.py`

### Model code

HF-style model implementations for training and inference.

- [LLaMA infer](https://github.com/bytedance-seed/in-place-ttt/blob/main/inference_model/hf_llama3/modeling_llama.py): hf model · Inference-side LLaMA model implementation with TTT-aware loading. · `inference_model/hf_llama3/modeling_llama.py`
- [Qwen3 infer](https://github.com/bytedance-seed/in-place-ttt/blob/main/inference_model/hf_qwen3/modeling_qwen3.py): hf model · Inference-side Qwen3 model implementation with TTT-aware loading. · `inference_model/hf_qwen3/modeling_qwen3.py`
- [LLaMA train](https://github.com/bytedance-seed/in-place-ttt/blob/main/hf_models/hf_llama/modeling_llama.py): hf model · Training-side LLaMA model implementation. · `hf_models/hf_llama/modeling_llama.py`
- [Qwen3 train](https://github.com/bytedance-seed/in-place-ttt/blob/main/hf_models/hf_qwen3/modeling_qwen3.py): hf model · Training-side Qwen3 model implementation. · `hf_models/hf_qwen3/modeling_qwen3.py`

## Connections

- train.sh → Train task: launches · Training script starts the torch task.
- Train task → Qwen3 pretrain: loads · Training behavior is selected by YAML config.
- Train task → LLaMA pretrain: loads · Training behavior is selected by YAML config.
- Train task → External data: reads · Consumes preprocessed external training data.
- Train task → VeOmni/FSDP2: uses · Runs distributed continual pretraining through VeOmni/FSDP2.
- VeOmni/FSDP2 → DCP shards: writes · Training emits distributed checkpoints.
- DCP shards → Merge DCP: converted by · Export step merges DCP shards into HF format.
- Merge DCP → HF checkpoint: produces · Creates HuggingFace checkpoint artifacts for downstream use.
- eval.sh → Infer task: launches · Evaluation script starts the inference task.
- Infer task → Model registry: selects · Chooses which checkpoint/config pair to evaluate.
- Infer task → RULER configs: loads · Benchmark runs are defined by RULER configs.
- Infer task → OpenCompass: runs on · OpenCompass orchestrates benchmark execution.
- OpenCompass → HF checkpoint: consumes · Evaluation loads the exported HuggingFace checkpoint.
- HF checkpoint → LLaMA infer: loads into · Inference path instantiates the LLaMA model code.
- HF checkpoint → Qwen3 infer: loads into · Inference path instantiates the Qwen3 model code.
- Train task → LLaMA train: instantiates · Training uses the training-side LLaMA implementation.
- Train task → Qwen3 train: instantiates · Training uses the training-side Qwen3 implementation.
- Infer task → LLaMA infer: instantiates · Evaluation path uses the inference-side LLaMA implementation.
- Infer task → Qwen3 infer: instantiates · Evaluation path uses the inference-side Qwen3 implementation.
