Skip to main content

Overview

ChemLactica supports pretraining language models on chemical data using the train.py script. The training system is built on top of Hugging Face Transformers with custom features for efficient distributed training.

Quick Start

1

Prepare Your Data

Organize your training data in JSONL format in directories. You’ll need:
  • Training data directories (can be multiple)
  • Validation data directory
  • Data type labels for each directory
2

Choose Model Configuration

Select a model configuration from the available options:
  • 125m - 125 million parameter model
  • 1.3b - 1.3 billion parameter model
  • 6.7b - 6.7 billion parameter model
  • mistral7b - Mistral 7B based model
  • llama2 - Llama 2 based model
3

Run Training

Execute the training script with your configuration.

Basic Training Command

Command-Line Arguments

Required Arguments

string
required
Type of training to perform. Options: pretrain, sft, isft, dpo
string
required
Path to pretrained model directory or Hugging Face model identifier
string
required
Model configuration name (e.g., 125m, 1.3b, llama2)
string[]
required
List of directories containing training data in JSONL format
string[]
required
Data type labels for each training directory (same order as training_data_dirs)
string
required
Directory containing validation data
integer
required
Training batch size per GPU
integer
required
Number of training steps between evaluations
integer
required
Number of steps between checkpoint saves
string
required
Root directory for saving model checkpoints

Optional Arguments

float
Learning rate (defaults to config value if not specified)
integer
Number of warmup steps for learning rate scheduler
integer
default:"-1"
Maximum number of training steps (overrides num_train_epochs)
integer
Number of training epochs
integer
Number of steps for LR scheduler (defaults to max_steps)
integer
Validation batch size (defaults to train_batch_size)
integer
default:"4"
Buffer size for dataset shuffling
string
default:"none"
Name for the experiment (used in tracking)
integer
default:"0"
Number of dataloader worker processes
integer
default:"1"
Number of gradient accumulation steps
boolean
default:"false"
Enable gradient checkpointing to save memory
boolean
default:"false"
Use Flash Attention 2 for faster training

Tracking and Debugging

boolean
default:"true"
Enable experiment tracking with Aim
string
Directory for saving tracking data
boolean
default:"false"
Enable PyTorch profiler for performance analysis
string
Directory for profiling output
boolean
default:"false"
Enable reproducibility checks (for testing only)

Advanced Usage

Multi-GPU Training

ChemLactica uses Accelerate for distributed training. Launch with:

Resume from Checkpoint

To resume training from a checkpoint, pass the checkpoint directory path to --from_pretrained:

Multiple Data Sources

You can train on multiple data directories with different types:

Dynamic Gradient Accumulation

Enable automatic gradient accumulation scheduling:

Training Features

Custom Callbacks

ChemLactica includes several custom callbacks:
  • WPSCounterCallback: Tracks words per second
  • CustomProgressCallback: Enhanced progress reporting with FLOPS tracking
  • EarlyStoppingCallback: Stops training at specified steps
  • JsonlDatasetResumeCallback: Handles dataset resumption for streaming data
  • ReproducibilityCallback: Validates training reproducibility
  • GradientAccumulationScheduler: Dynamically adjusts gradient accumulation

Memory Optimization

Checkpoint Management

Checkpoints are saved with the following structure:
The save_total_limit configuration controls how many checkpoints are kept:

Monitoring Training

Aim Tracking

When --track is enabled, training metrics are logged to Aim:
View metrics with:

Key Metrics

  • Training loss
  • Validation loss
  • Learning rate
  • Words per second (WPS)
  • Gradient norm
  • FLOPS utilization

Example Configurations

Troubleshooting

Out of Memory

If you encounter OOM errors:
  1. Reduce train_batch_size
  2. Increase gradient_accumulation_steps
  3. Enable --gradient_checkpointing
  4. Use --flash_attn if available
  5. Reduce save_total_limit to save memory

Slow Training

  • Enable --flash_attn for 2-3x speedup
  • Increase dataloader_num_workers
  • Adjust shuffle_buffer_size
  • Use mixed precision training (bf16 is enabled by default)

Checkpoint Loading Issues

Make sure the checkpoint path includes the full path to the checkpoint directory, not just the parent directory.

Next Steps

Fine-tuning

Learn how to fine-tune models for specific tasks

Configuration

Explore all training configuration options