Training models
Training, and why it matters
Large Language Models (LLMs) are developed in two main training stages:
-
Pretraining is the foundational phase, where the model learns general language patterns by predicting the next token in a sentence. This is done on massive, diverse datasets that include books, websites, code, and other text sources. The model doesn't learn specific tasks, it learns to model language itself.
-
Fine-tuning is an optional second phase where the model is adapted to specific tasks, domains, or behavioral expectations. This is typically done on much smaller, curated datasets, such as examples of customer support interactions, legal document summaries, or math problem solutions.
Why this matters to software developers
Understanding the training process helps you reason about:
- What the model "knows" and how it behaves by default
- Why it might struggle with your specific use case
- When it's appropriate to fine-tune, and when it's not
LLMs are not trained to know everything or do every task. They are trained to model statistical patterns in language, and later fine-tuned (if needed) to act in specific ways.
Start with a model that's already close to your goal
As an application developer, you almost certainly do not need to pretrain a model from scratch, as training state-of-the-art models requires significant computational resources (even the smallest Olmo 1B model requires 5,000 H100 hours per 1 trillion tokens). Instead:
- Choose a pretrained model that already performs well on your type of task (e.g., chat, summarization, instruction-following, domain-specific language).
- Use prompt engineering or retrieval-augmented generation (RAG) to get solid results without modifying the model at all.
- If needed, apply fine-tuning as a targeted refinement, for example, to adapt tone, correct specific mistakes, or teach new behavior.
Key principle: Pretraining is foundational, but not something most developers need to do. Start with a strong base model, and use the lightest technique that meets your needs.
Training your own models
While most software developers will not need to pretrain their own models, Ai2 makes the process fully transparent and reproducible! This has obvious benefits for research, but also gives software developers a way to understand and evaluate models on a deeper level. For more information about adapting models to your specific needs, see our Fine-Tuning guide.
The following sections provide code examples for training different Ai2 models. Remember that training large language models requires significant computational resources and expertise. Make sure you have access to appropriate hardware (typically multiple high-end GPUs or TPUs) and understand the training process before proceeding. You may also need job management software that will help your training job resume after hardware failures, which are expected when running at scale.
- Olmo 3 7B and 32B
- Olmo 2 32B
- Olmo 2 7B and 13B
- Tülu 3
- Molmo
-
Create a new Python environment: This step will create an isolated Python environment using venv, which helps manage dependencies and avoid conflicts between different projects. You can create a new Python environment using venv:
python -m venv olmo_core
source olmo_core/bin/activate # On Unix/macOS
# or
.\olmo_core\Scripts\activate # On Windows -
Install OLMo-core: This step will install the OLMo-core package, which provides the necessary components for training Olmo 3 models. We recommend installing from source:
git clone https://github.com/allenai/OLMo-core.git
cd OLMo-core
pip install -e ."[all]"Alternatively, you can install from PyPI with
pip install ai2-olmo-core. You will also need to installflash-attn. -
Train the model: This step will start the training process for the Olmo 3 7B model. The training script uses distributed training across multiple GPUs for efficient processing:
torchrun --nproc-per-node=gpu src/scripts/official/OLMo3/OLMo-3-1025-7B-pretrain-1.py \
--save-folder=models/tutorial-run-01 \
--train_module.rank_microbatch_size=8192For the 32B model, use the corresponding script:
torchrun --nproc-per-node=gpu src/scripts/official/OLMo3/OLMo-3-1025-32B-pretrain.py \
--save-folder=models/tutorial-run-01
Customize training parameters (optional):
The key components and hyperparameters are defined through the ExperimentConfig class in src/olmo_core/script_utils.py. To override any fields at runtime, pass them as command-line options:
torchrun --nproc-per-node=gpu src/scripts/official/OLMo3/OLMo-3-1025-7B-pretrain-1.py \
--save-folder=models/tutorial-run-01 \
--train_module.rank_microbatch_size=8192 \
--trainer.callbacks.lm_evaluator.enabled=false \
--trainer.callbacks.downstream_evaluator.enabled=false \
--trainer.hard_stop='{value: 100, unit: steps}'
Use --dry-run to validate your config without launching training.
Convert to HuggingFace format (optional):
To convert your trained model to HuggingFace format (requires transformers):
python src/examples/huggingface/convert_checkpoint_to_hf.py \
--checkpoint-input-path models/tutorial-run-01/step100 \
--huggingface-output-dir hf_models/tutorial-run-01/step100
-
Create a new Python environment: This step will create an isolated Python environment using venv, which helps manage dependencies and avoid conflicts between different projects. You can create a new Python environment using venv:
python -m venv olmo_core
source olmo_core/bin/activate # On Unix/macOS
# or
.\olmo_core\Scripts\activate # On Windows -
Install Olmo-core: This step will install the Olmo-core package, which provides the necessary components for training the Olmo 2 32B model. You can install it using pip:
pip install ai2-olmo-coreor build from source (recommended) for the latest features and bug fixes:
git clone https://github.com/allenai/OLMo-core.git
cd OLMo-core
pip install -e ."[all]" -
Train the model: This step will start the training process for the Olmo 2 32B model. The training script uses distributed training across multiple GPUs for efficient processing:
torchrun --nproc-per-node=8 ./src/scripts/official/OLMo-2-0325-32B-train.py run01
Customize training parameters (optional): This step allows you to override default training configurations, such as learning rate, to optimize the training process for your specific needs:
torchrun --nproc-per-node=8 ./src/scripts/train/OLMo-2-0325-32B-train.py run01 --train_module.optim.lr=6e-3
Continue training from checkpoint (optional): This step enables you to resume training from a saved checkpoint, useful for continuing interrupted training sessions or fine-tuning from a specific point:
torchrun --nproc-per-node=8 ./src/scripts/train/OLMo-2-0325-32B-anneal.py anneal_run01 https://olmo-checkpoints.org/ai2-llm/peteish32/step721901/
-
Create a new Python environment: This step will create an isolated Python environment using venv, which helps manage dependencies and avoid conflicts between different projects. You can create a new Python environment using venv:
python -m venv olmo
source olmo/bin/activate # On Unix/macOS
# or
.\olmo\Scripts\activate # On Windows -
Install Olmo: This step will install the Olmo package, which provides the necessary components for training the Olmo 2 7B and 13B models. You can install it using pip:
pip install ai2-olmoor build from source (recommended) for the latest features and bug fixes:
git clone https://github.com/allenai/OLMo.git
cd OLMo
pip install -e ."[all]"
pip install datasets wandb -
Train the model: This step will start the training process for the Olmo 2 7B or 13B model. The training script uses distributed training across multiple GPUs for efficient processing:
torchrun --nproc_per_node={processes per node} scripts/train.py {config file} --save_overwrite
Customize training parameters (optional): This step shows a specific example of how to train the model with a particular configuration:
torchrun --nproc_per_node=4 scripts/train.py configs/tiny/OLMo-20M.yaml --save_overwrite
You can find additional training configs on GitHub.
-
Create a new Python environment: This step will create an isolated Python environment using venv, which helps manage dependencies and avoid conflicts between different projects. You can create a new Python environment using venv:
python -m venv myenv
source myenv/bin/activate # On Unix/macOS
# or
.\myenv\Scripts\activate # On Windows -
Install dependencies: This step will install all necessary dependencies for training the Tülu 3 model, including PyTorch, flash-attention, and other required packages. These dependencies are essential for the model's training process:
git clone https://github.com/allenai/open-instruct.git
cd open-instruct
pip install --upgrade pip "setuptools<70.0.0" wheel
pip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 --index-url https://download.pytorch.org/whl/cu121
pip install packaging
pip install flash-attn==2.6.3 --no-build-isolation
pip install -r requirements.txt
python -m nltk.downloader punkt
pip install -e . -
Fine-tune the model: This step will perform supervised fine-tuning (SFT) on the Tülu-3 model to adapt it to specific tasks and instruction-following capabilities:
sh scripts/finetune_with_accelerate_config.sh 4 configs/train_configs/tulu3/tulu3_sft.yaml -
Preference-tune the model: This step will perform preference-tuning using Direct Preference Optimization (DPO) to align the model with human preferences:
sh scripts/dpo_train_with_accelerate_config.sh 8 configs/train_configs/tulu3/tulu3_dpo_8b.yaml -
Run RVLR (Reward Value Learning and Reinforcement): This step will execute the RVLR process, which combines reward learning with reinforcement learning to improve the model's performance. This process uses multiple GPUs and includes various parameters for fine-tuning the learning process:
python open_instruct/ppo_vllm_thread_ray_gtrl.py \
--dataset_mixer '{"ai2-adapt-dev/gsm8k_math_ifeval_ground_truth_mixed": 1.0}' \
--dataset_train_splits train \
--dataset_eval_mixer '{"ai2-adapt-dev/gsm8k_math_ground_truth": 1.0}' \
--dataset_eval_splits test \
--max_token_length 2048 \
--max_prompt_token_length 2048 \
--response_length 2048 \
--model_name_or_path allenai/Llama-3.1-Tulu-3-8B-DPO \
--reward_model_path allenai/Llama-3.1-Tulu-3-8B-RM \
--non_stop_penalty \
--stop_token eos \
--temperature 1.0 \
--ground_truths_key ground_truth \
--chat_template tulu \
--sft_messages_key messages \
--learning_rate 3e-7 \
--total_episodes 10000000 \
--penalty_reward_value -10.0 \
--deepspeed_stage 3 \
--per_device_train_batch_size 2 \
--local_rollout_forward_batch_size 2 \
--local_mini_batch_size 32 \
--local_rollout_batch_size 32 \
--actor_num_gpus_per_node 7 \
--vllm_tensor_parallel_size 1 \
--beta 0.05 \
--apply_verifiable_reward true \
--output_dir output/rlvr_8b \
--seed 3 \
--num_evals 3 \
--save_freq 100 \
--reward_model_multiplier 0.0 \
--gradient_checkpointing \
--with_tracking
-
Create a new Python environment: This step will create an isolated Python environment using venv, which helps manage dependencies and avoid conflicts between different projects. You can create a new Python environment using venv:
python -m venv myenv
source myenv/bin/activate # On Unix/macOS
# or
.\myenv\Scripts\activate # On Windows -
Install dependencies: This step will install the Molmo package and its dependencies, which are required for training the Molmo model. This includes the core package and additional tools for dataset handling and experiment tracking:
git clone https://github.com/allenai/molmo.git
cd molmo
pip install -e ."[all]"
pip install datasets wandb -
Download the Pixmo dataset: This step will download the required dataset for training the Molmo model. The dataset is stored in a specified directory for easy access during training:
export MOLMO_DATA_DIR=/data/molmo
export HF_HOME=/data/molmo/huggingface
python scripts/download.py all --n_proc 12 -
Download specific dataset subset (optional): This step allows you to download a specific subset of the dataset, such as ChartQa, if you don't need the entire dataset:
python scripts/download_data.py ChartQa --n_proc 12 -
Visualize the dataset (optional): This step enables you to visualize the dataset contents, which can be helpful for understanding the data structure and quality:
python scripts/dataset_visualize.py chart_qa /path/to/viz/dir -
Train the model: This step will start the training process for the Molmo model using the Qwen2 LLM and CLIP vision encoder. The training script uses distributed training across multiple GPUs:
WANDB_API_KEY=key torchrun --nproc-per-node=8 launch_scripts/train_captioner.py qwen2_7b --wandb.name=run_name --wandb.entity=entity --wandb.project=project --save_folder=/path/to/save/folder -
Use alternative vision encoders (optional): This step allows you to use different vision encoders, such as SigLIP, MetaCLIP, or DINOv2, by specifying the
--vision_backboneoption:torchrun --nproc_per_node=4 scripts/train.py configs/tiny/OLMo-20M.yaml --save_overwriteYou can find additional training configs on GitHub.