Training Large Language Model to Reason in a Continuous Latent Space
Python
1,711
4 commits
updated Jul 2, 2026
The code base is the official implementation of Training Large Language Models to Reason in a Continuous Latent Space.

Clone repo:
git clone git@github.com:facebookresearch/coconut.git
cd coconut
Setup environment:
conda create --name coconut python=3.12
conda activate coconut
pip install -r requirements.txt
The code relies on wandb for logging. Please log in your wandb account following this document before running any experiments.
The data for training and evaluation should be presented as a json file like below:
[
{
"question": "...",
"answer": "...",
"steps": ["...", "...", ...]
},
...
]
The file should contain a list of data points. Each data point is composed of a question (str), an answer (str), and a list of steps (str), where each of them is a string.
For example, you can download and process the GSM8K dataset (with augmented training and validation sets) by running:
bash preprocessing/gsm_icot.bash
The configuration of a run should be specified in a yaml file (an example can be found here).
General settings
val_path (must used along with load_model_path). Otherwise, train the model on train_path and test on val_path after every epoch.Method
Training settings
False for Coconut model training, because otherwise the checkpoints in the last stage might now get saved.openai-community/gpt2only_eval)Run the following commands (replacing N_GPUS and PATH_TO_ARGS):
torchrun --nnodes 1 --nproc_per_node N_GPUS run.py PATH_TO_ARGS
Here we provide instructions to reproduce our experiments in the paper.
All the commands below assume 4 * A100 (80GB) GPUs. You may change the corresponding arguments in the config file (batch_size_training, gradient_accumulation_steps) and nproc_per_node when launching the run, to adapt your resources.
Preprocessing data:
bash preprocessing/gsm_icot.bash
First train the model with CoT (as the stage 0 training)
torchrun --nnodes 1 --nproc_per_node 4 run.py args/gsm_cot.yaml
Select a checkpoint as the initialization of Coconut (the validation accuracy is expected to be around 40%). Replace the load_model_path in the args/gsm_coconut.yaml with your selected checkpoint, and run:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/gsm_coconut.yaml
Find the checkpoint with best validation accuracy, and put the path as load_model_path in args/gsm_coconut_eval.yaml. To evaluate:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/gsm_coconut_eval.yaml
Please clone the official github repo of ProntoQA and generate a raw dataset with:
cd prontoqa
python run_experiment.py --model-name json --model-size dummy --ordering random --num-trials 10000 --few-shot-examples 0 --ontology fictional --min-hops 5 --max-hops 5 --hops-skip 1
Then copy the generated 5hop_0shot_random.json file to data directory, and preprocess the dataset with:
python preprocessing/prontoqa.py
Then run the following to train the model:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/prontoqa_coconut.yaml
Find the checkpoint with best validation accuracy, and put the path as load_model_path in args/prosqa_coconut_eval.yaml. To evaluate:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/prosqa_coconut_eval.yaml
The ProsQA dataset is at data/prosqa_*.json.
Then run the following to train the model:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/prosqa_coconut.yaml
Find the checkpoint with best validation accuracy, and put the path as load_model_path in args/prosqa_coconut_eval.yaml. To evaluate:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/prosqa_coconut_eval.yaml
If you use this code base in your research, please cite our paper with the following BibTex entry:
@article{hao2024training,
title={Training Large Language Models to Reason in a Continuous Latent Space},
author={Hao, Shibo and Sukhbaatar, Sainbayar and Su, DiJia and Li, Xian and Hu, Zhiting and Weston, Jason and Tian, Yuandong},
journal={arXiv preprint arXiv:2412.06769},
year={2024}
}
This code is released under the MIT license (see LICENSE).
3 commits
1 commits
Python
98.1%
Shell
1.9%
Training Large Language Model to Reason in a Continuous Latent Space
Python
1,711
4 commits
updated Jul 2, 2026
The code base is the official implementation of Training Large Language Models to Reason in a Continuous Latent Space.

Clone repo:
git clone git@github.com:facebookresearch/coconut.git
cd coconut
Setup environment:
conda create --name coconut python=3.12
conda activate coconut
pip install -r requirements.txt
The code relies on wandb for logging. Please log in your wandb account following this document before running any experiments.
The data for training and evaluation should be presented as a json file like below:
[
{
"question": "...",
"answer": "...",
"steps": ["...", "...", ...]
},
...
]
The file should contain a list of data points. Each data point is composed of a question (str), an answer (str), and a list of steps (str), where each of them is a string.
For example, you can download and process the GSM8K dataset (with augmented training and validation sets) by running:
bash preprocessing/gsm_icot.bash
The configuration of a run should be specified in a yaml file (an example can be found here).
General settings
val_path (must used along with load_model_path). Otherwise, train the model on train_path and test on val_path after every epoch.Method
Training settings
False for Coconut model training, because otherwise the checkpoints in the last stage might now get saved.openai-community/gpt2only_eval)Run the following commands (replacing N_GPUS and PATH_TO_ARGS):
torchrun --nnodes 1 --nproc_per_node N_GPUS run.py PATH_TO_ARGS
Here we provide instructions to reproduce our experiments in the paper.
All the commands below assume 4 * A100 (80GB) GPUs. You may change the corresponding arguments in the config file (batch_size_training, gradient_accumulation_steps) and nproc_per_node when launching the run, to adapt your resources.
Preprocessing data:
bash preprocessing/gsm_icot.bash
First train the model with CoT (as the stage 0 training)
torchrun --nnodes 1 --nproc_per_node 4 run.py args/gsm_cot.yaml
Select a checkpoint as the initialization of Coconut (the validation accuracy is expected to be around 40%). Replace the load_model_path in the args/gsm_coconut.yaml with your selected checkpoint, and run:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/gsm_coconut.yaml
Find the checkpoint with best validation accuracy, and put the path as load_model_path in args/gsm_coconut_eval.yaml. To evaluate:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/gsm_coconut_eval.yaml
Please clone the official github repo of ProntoQA and generate a raw dataset with:
cd prontoqa
python run_experiment.py --model-name json --model-size dummy --ordering random --num-trials 10000 --few-shot-examples 0 --ontology fictional --min-hops 5 --max-hops 5 --hops-skip 1
Then copy the generated 5hop_0shot_random.json file to data directory, and preprocess the dataset with:
python preprocessing/prontoqa.py
Then run the following to train the model:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/prontoqa_coconut.yaml
Find the checkpoint with best validation accuracy, and put the path as load_model_path in args/prosqa_coconut_eval.yaml. To evaluate:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/prosqa_coconut_eval.yaml
The ProsQA dataset is at data/prosqa_*.json.
Then run the following to train the model:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/prosqa_coconut.yaml
Find the checkpoint with best validation accuracy, and put the path as load_model_path in args/prosqa_coconut_eval.yaml. To evaluate:
torchrun --nnodes 1 --nproc_per_node 4 run.py args/prosqa_coconut_eval.yaml
If you use this code base in your research, please cite our paper with the following BibTex entry:
@article{hao2024training,
title={Training Large Language Models to Reason in a Continuous Latent Space},
author={Hao, Shibo and Sukhbaatar, Sainbayar and Su, DiJia and Li, Xian and Hu, Zhiting and Weston, Jason and Tian, Yuandong},
journal={arXiv preprint arXiv:2412.06769},
year={2024}
}
This code is released under the MIT license (see LICENSE).
3 commits
1 commits
Python
98.1%
Shell
1.9%