How bytedance-seed/in-place-ttt works
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: train_torch.py Train task · infer.py Infer task · merge_dcp_to_hf.py Merge DCP · modeling_llama.py LLaMA infer
Components 18
Training
Continual pretraining and checkpoint creation.
- train.sh · Shell entrypoint for the training pipeline.
train.sh - Train task · Main training program launched by the training script.
tasks/train_torch.py - External data · Preprocessed iterable text dataset supplied outside the repository.
- VeOmni/FSDP2 · Training runtime used for full-shard continual pretraining.
Export
Checkpoint conversion between runtime formats.
- DCP shards · Distributed training checkpoints produced by VeOmni.
- Merge DCP · Converts DCP shards into HuggingFace checkpoint layout.
scripts/merge_dcp_to_hf.py - HF checkpoint · Merged HuggingFace-formatted weights consumed by inference and evaluation.
Inference
TTT-aware model loading and evaluation path.
- eval.sh · Shell entrypoint for benchmark evaluation.
eval.sh - Infer task · Inference/evaluation task wrapper used by the runtime.
tasks/infer.py - OpenCompass · Benchmarking framework used to run RULER evaluations.
Config
Training and benchmark configuration layer.
- Qwen3 pretrain · Recommended continual pretraining settings for Qwen3 long context.
configs/pretrain/qwen3_longct.yaml - LLaMA pretrain · Recommended continual pretraining settings for LLaMA 3.1 long context.
configs/pretrain/llama3_longct.yaml - RULER configs · Context-length benchmark definitions from 4K to 256K.
- Model registry · Selects which HuggingFace checkpoint to evaluate.
eval_config/models.py
Model code
HF-style model implementations for training and inference.
- LLaMA infer · Inference-side LLaMA model implementation with TTT-aware loading.
inference_model/hf_llama3/modeling_llama.py - Qwen3 infer · Inference-side Qwen3 model implementation with TTT-aware loading.
inference_model/hf_qwen3/modeling_qwen3.py - LLaMA train · Training-side LLaMA model implementation.
hf_models/hf_llama/modeling_llama.py - Qwen3 train · Training-side Qwen3 model implementation.
hf_models/hf_qwen3/modeling_qwen3.py
Connections 19
- 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.