Skip to main content

Overview

ChemLactica supports supervised fine-tuning (SFT) to adapt pretrained models for specific chemical tasks. The SFT framework uses TRL’s SFTTrainer with custom data collators for completion-only training.

Supervised Fine-Tuning (SFT)

Quick Start

1

Prepare SFT Data

Format your data with prompts and completions in JSONL format
2

Configure SFT Parameters

Set up packing, sequence length, and noise parameters
3

Run SFT Training

Launch training with the sft train type

Basic SFT Command

SFT Configuration

SFT-specific parameters are defined in config/default_train_config.py:

SFT Parameters

boolean
default:"false"
Enable packing multiple samples into a single sequence for efficiency
integer
default:"64"
Maximum sequence length for SFT samples
integer
default:"10"
NEFTune noise parameter for embedding perturbation (improves generalization)

Data Format

Response Template

The SFT trainer uses a response template to train only on completions:

Example Data Format

Training Configuration

Advanced SFT Features

NEFTune (Noisy Embeddings)

NEFTune adds noise to embeddings during training to improve generalization:
This technique is particularly effective for instruction following and has been shown to improve downstream performance.

Completion-Only Training

The DataCollatorForCompletionOnlyLM ensures loss is computed only on the completion tokens:
This focuses learning on generating the target output rather than memorizing the prompt.

SFT Numerical Evaluation

ChemLactica includes a custom callback for evaluating SFT models:

Example: Activity Prediction SFT

Data Preparation

Create training data with molecular descriptors and activity values:

Training Command

Multi-GPU SFT Training

For larger models or datasets:

Evaluation-Only Mode

Evaluate a fine-tuned model without training:

SFT vs Pretraining

Best Practices

  • Use 10-100x lower learning rate than pretraining
  • Start with 1e-4 for small models
  • Use 2e-5 or lower for models >1B parameters
  • Monitor validation loss closely for overfitting
  • Ensure consistent formatting across all samples
  • Balance your dataset across different property values
  • Remove duplicates and low-quality samples
  • Use validation set from same distribution as training
  • Keep max_seq_length as small as possible
  • Typical values: 64-512 tokens
  • Longer sequences require more memory
  • Consider packing for short sequences
  • SFT typically requires 1000-10000 steps
  • Watch for overfitting after a few epochs
  • Use early stopping based on validation loss
  • Save checkpoints frequently for model selection

Troubleshooting

Model Not Learning

  • Check that response template matches your data format
  • Verify data formatting is consistent
  • Try increasing learning rate slightly
  • Ensure sufficient training steps

Overfitting

  • Reduce learning rate
  • Increase weight decay
  • Add more training data
  • Enable dropout if needed
  • Use NEFTune noise

Memory Issues

  • Reduce max_seq_length
  • Decrease train_batch_size
  • Increase gradient_accumulation_steps
  • Enable packing for short sequences
  • Use --gradient_checkpointing

Next Steps

Rejection Sampling

Learn about rejection sampling fine-tuning strategy

Configuration

Explore detailed configuration options