# facebookresearch/coconut architecture

> Architecture diagram of the GitHub repository facebookresearch/coconut, 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/facebookresearch/coconut
- Repository: https://github.com/facebookresearch/coconut

## Overview

Coconut is a research training and evaluation application for language models that reason using continuous latent representations. Users prepare question/answer/steps data, select a YAML run configuration, and launch distributed training or evaluation; training can progress through latent-reasoning stages, and evaluation measures generated answers. The graph also shows the documented dataset-preparation branches and the latent-model layer. The README and excerpts support the main workflow; unsampled runner sections limit detail about checkpointing and dataset setup, so those connections are not expanded.

Main entry points: [`run.py`](https://github.com/facebookresearch/coconut/blob/main/run.py) (Experiment runner), [`coconut.py`](https://github.com/facebookresearch/coconut/blob/main/coconut.py) (Latent reasoning model), [`data`](https://github.com/facebookresearch/coconut/tree/main/data) (Prepared datasets), [`preprocessing/gsm_icot.py`](https://github.com/facebookresearch/coconut/blob/main/preprocessing/gsm_icot.py) (GSM conversion)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data preparation"]
  node_rawdata["Raw datasets"]
  node_gsm_prep["GSM conversion<br/>[gsm_icot.py]"]
  node_pronto_prep["ProntoQA conversion<br/>[prontoqa.py]"]
  node_prepared_data["Prepared datasets"]
  node_dataset["Dataset processing<br/>[dataset.py]"]
end

subgraph group_runtime["Training runtime"]
  node_configs["Run configurations"]
  node_runner["Experiment runner<br/>[run.py]"]
  node_utilities["Run utilities<br/>[utils.py]"]
  node_trainer["Distributed training<br/>[run.py]"]
  node_evaluator["Generation evaluation<br/>[run.py]"]
  node_checkpoints["Model checkpoints"]
  node_logger["Experiment logging"]
  node_metrics["Accuracy and loss"]
end

subgraph group_model["Model and evaluation"]
  node_tokenizer["Tokenizer"]
  node_base_model["Causal language model"]
  node_coconut["Latent reasoning model<br/>[coconut.py]"]
end

node_user(("Research user"))
node_wandb["Weights &amp; Biases"]

node_user -->|"selects"| node_configs
node_user -->|"provides"| node_rawdata
node_rawdata -->|"converts"| node_gsm_prep
node_rawdata -->|"converts"| node_pronto_prep
node_gsm_prep -->|"writes JSON"| node_prepared_data
node_pronto_prep -->|"writes splits"| node_prepared_data
node_user -->|"launches"| node_runner
node_configs -->|"configures"| node_runner
node_prepared_data -.->|"loads data"| node_dataset
node_runner -->|"uses"| node_dataset
node_runner -->|"uses"| node_utilities
node_runner -->|"loads"| node_tokenizer
node_runner -->|"loads"| node_base_model
node_runner -->|"wraps model"| node_coconut
node_coconut -->|"forwards to"| node_base_model
node_runner -->|"runs stages"| node_trainer
node_trainer -->|"trains"| node_coconut
node_runner -->|"runs evaluation"| node_evaluator
node_evaluator -->|"generates with"| node_coconut
node_trainer -.->|"saves"| node_checkpoints
node_evaluator -.->|"loads"| node_checkpoints
node_trainer -->|"logs metrics"| node_logger
node_evaluator -->|"computes"| node_metrics
node_logger -->|"reports to"| node_wandb

click node_gsm_prep "https://github.com/facebookresearch/coconut/blob/main/preprocessing/gsm_icot.py"
click node_pronto_prep "https://github.com/facebookresearch/coconut/blob/main/preprocessing/prontoqa.py"
click node_prepared_data "https://github.com/facebookresearch/coconut/tree/main/data"
click node_configs "https://github.com/facebookresearch/coconut/tree/main/args"
click node_runner "https://github.com/facebookresearch/coconut/blob/main/run.py"
click node_dataset "https://github.com/facebookresearch/coconut/blob/main/dataset.py"
click node_utilities "https://github.com/facebookresearch/coconut/blob/main/utils.py"
click node_trainer "https://github.com/facebookresearch/coconut/blob/main/run.py"
click node_evaluator "https://github.com/facebookresearch/coconut/blob/main/run.py"
click node_coconut "https://github.com/facebookresearch/coconut/blob/main/coconut.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_rawdata,node_gsm_prep,node_pronto_prep,node_prepared_data,node_dataset,node_user toneBlue
class node_configs,node_runner,node_utilities,node_trainer,node_evaluator,node_checkpoints,node_logger,node_metrics toneAmber
class node_tokenizer,node_base_model,node_coconut toneMint
class node_wandb toneIndigo
```

## Components

### Data preparation

- **Raw datasets**
- [GSM conversion](https://github.com/facebookresearch/coconut/blob/main/preprocessing/gsm_icot.py): `preprocessing/gsm_icot.py`
- [ProntoQA conversion](https://github.com/facebookresearch/coconut/blob/main/preprocessing/prontoqa.py): `preprocessing/prontoqa.py`
- [Prepared datasets](https://github.com/facebookresearch/coconut/tree/main/data): `data`
- [Dataset processing](https://github.com/facebookresearch/coconut/blob/main/dataset.py): `dataset.py`

### Training runtime

- [Run configurations](https://github.com/facebookresearch/coconut/tree/main/args): `args`
- [Experiment runner](https://github.com/facebookresearch/coconut/blob/main/run.py): `run.py`
- [Run utilities](https://github.com/facebookresearch/coconut/blob/main/utils.py): `utils.py`
- [Distributed training](https://github.com/facebookresearch/coconut/blob/main/run.py): `run.py`
- [Generation evaluation](https://github.com/facebookresearch/coconut/blob/main/run.py): `run.py`
- **Model checkpoints**
- **Experiment logging**
- **Accuracy and loss**

### Model and evaluation

- **Tokenizer**
- **Causal language model**
- [Latent reasoning model](https://github.com/facebookresearch/coconut/blob/main/coconut.py): `coconut.py`

### Other components

- **Research user**
- **Weights & Biases**

## Connections

- Research user → Run configurations: selects (evidence: `README.md`)
- Research user → Raw datasets: provides (evidence: `README.md`)
- Raw datasets → GSM conversion: converts (evidence: `README.md`)
- Raw datasets → ProntoQA conversion: converts (evidence: `README.md`)
- GSM conversion → Prepared datasets: writes JSON (evidence: `preprocessing/gsm_icot.py`)
- ProntoQA conversion → Prepared datasets: writes splits (evidence: `preprocessing/prontoqa.py`)
- Research user → Experiment runner: launches (evidence: `README.md`)
- Run configurations → Experiment runner: configures (evidence: `README.md`)
- Prepared datasets → Dataset processing: loads data
- Experiment runner → Dataset processing: uses (evidence: `run.py`)
- Experiment runner → Run utilities: uses (evidence: `run.py`)
- Experiment runner → Tokenizer: loads (evidence: `run.py`)
- Experiment runner → Causal language model: loads (evidence: `run.py`)
- Experiment runner → Latent reasoning model: wraps model (evidence: `run.py`)
- Latent reasoning model → Causal language model: forwards to (evidence: `coconut.py`)
- Experiment runner → Distributed training: runs stages (evidence: `README.md`)
- Distributed training → Latent reasoning model: trains (evidence: `run.py`)
- Experiment runner → Generation evaluation: runs evaluation (evidence: `README.md`)
- Generation evaluation → Latent reasoning model: generates with (evidence: `run.py`)
- Distributed training → Model checkpoints: saves
- Generation evaluation → Model checkpoints: loads
- Distributed training → Experiment logging: logs metrics (evidence: `run.py`)
- Generation evaluation → Accuracy and loss: computes (evidence: `run.py`)
- Experiment logging → Weights & Biases: reports to
