Repository navigation
Replies: 3 comments
|
Looking at the LLaMA-Factory source code ( Data Sampling MechanismThe # dataset_info.yaml
my_dataset1:
path: data1.json
weight: 0.6
my_dataset2:
path: data2.json
weight: 0.4Without explicit weights, sampling is proportional to dataset size (number of samples). A larger dataset will dominate training. Recommended LR Strategy for Multi-Dataset CPT
# training config snippet
learning_rate: 2e-5
lr_scheduler_type: cosine
warmup_ratio: 0.03
weight_decay: 0.01Key InsightThe |
|
Based on the LLaMA-Factory source code, here is how to balance learning rate and data sampling during CPT on multiple datasets. Data Sampling StrategyLLaMA-Factory uses a weight-based sampling mechanism in # dataset_info.yaml
dataset1:
path: path/to/dataset1
weight: 0.6 # 60% sampling probability
dataset2:
path: path/to/dataset2
weight: 0.4 # 40% sampling probabilityThe relevant code flow:
Learning Rate RecommendationsFor CPT on multiple datasets with different sizes:
Practical ExampleFor a large general corpus (10M tokens) + small domain dataset (500K tokens): # dataset_info.yaml
general_corpus:
path: data/general
weight: 0.3
domain_data:
path: data/domain
weight: 0.7# training config
learning_rate: 1e-5
warmup_ratio: 0.05
lr_scheduler_type: cosine
per_device_train_batch_size: 4
gradient_accumulation_steps: 8
neat_packing: trueThis ensures the smaller dataset gets more training attention while the larger dataset provides general knowledge. |
|
This is a classic problem in multi-domain continual pretraining (CPT). The tension you're describing—large datasets needing higher effective learning rates while small datasets need lower ones—is fundamentally about gradient signal imbalance, not just sampling. The Root CauseWhen you train on a mixture, each batch contains samples from different datasets. The gradient update is an average across all samples in the batch. If dataset A has 10× more samples than dataset B, then:
Advanced Solutions Beyond Basic Weighting1. Gradient Accumulation per Dataset (Most Effective)Instead of mixing datasets in the same batch, accumulate gradients separately: # Simulate per-dataset learning rates via sequential accumulation
# Train on dataset A for N steps, then dataset B for M steps
# This is essentially "block curriculum"
# In LLaMA-Factory, use mix_strategy: interleave_once
# This exhausts smaller datasets first, then continues with larger onesLLaMA-Factory's 2. Learning Rate Scaling by Dataset SizeIf you can't use per-dataset LRs (which most frameworks don't support natively), approximate it via batch composition:
Example for 8 datasets with sizes [10M, 5M, 2M, 1M, 500K, 200K, 100K, 50K]: import numpy as np
sizes = np.array([10_000_000, 5_000_000, 2_000_000, 1_000_000, 500_000, 200_000, 100_000, 50_000])
weights = np.sqrt(sizes) / np.sqrt(sizes).sum()
# Result: [0.38, 0.27, 0.17, 0.12, 0.08, 0.05, 0.04, 0.03]
# vs linear: [0.53, 0.26, 0.11, 0.05, 0.03, 0.01, 0.005, 0.003]The square root scaling gives small datasets 5-10× more representation than linear scaling, which empirically reduces their val loss divergence. 3. Dynamic Weight Adjustment Based on Val LossMonitor per-dataset validation loss and adjust weights during training: # Pseudocode for dynamic rebalancing
if val_loss[dataset_i] > threshold:
# Increase weight for dataset_i
weights[dataset_i] *= 1.2
# Decrease weights for datasets with decreasing val loss
weights[other_datasets] *= 0.9LLaMA-Factory doesn't have this built-in, but you can implement it via callbacks or by restarting training with adjusted weights based on checkpoint evaluation. 4. Separate Optimizer States per Dataset Group (Advanced)If you have access to the training loop, use parameter-efficient isolation:
This is more complex but completely eliminates the LR disparity problem because each dataset trains independent parameters. 5. Practical LLaMA-Factory ConfigurationFor your 8-dataset CPT, I recommend: # dataset_info.yaml
dataset_large:
path: data/large
weight: 0.25 # Reduced from linear 0.53
dataset_medium1:
path: data/med1
weight: 0.18
dataset_medium2:
path: data/med2
weight: 0.12
dataset_small1:
path: data/small1
weight: 0.10
dataset_small2:
path: data/small2
weight: 0.08
dataset_tiny1:
path: data/tiny1
weight: 0.07
dataset_tiny2:
path: data/tiny2
weight: 0.06
dataset_tiny3:
path: data/tiny3
weight: 0.05
# Note: weights sum to ~0.91, normalize to 1.0# training config
stage: pt
do_train: true
model_name_or_path: your_base_model
dataset: dataset_large,dataset_medium1,dataset_medium2,dataset_small1,dataset_small2,dataset_tiny1,dataset_tiny2,dataset_tiny3
template: your_template
finetuning_type: full # or lora
learning_rate: 2e-5
lr_scheduler_type: cosine
warmup_ratio: 0.05
num_train_epochs: 1.0 # Single epoch to prevent overfitting on small datasets
per_device_train_batch_size: 2
gradient_accumulation_steps: 16 # Effective batch size 32
neat_packing: true # Critical for mixed-length datasets
max_length: 2048 # Adjust based on your data
output_dir: saves/cpt_mixed
logging_steps: 10
eval_strategy: steps
eval_steps: 500
load_best_model_at_end: true
metric_for_best_model: eval_loss6. The "Oversample Small, Undersample Large" TrickIf small datasets are still being overwritten:
Example: If Validation StrategyFor monitoring weighted validation loss: # Evaluate each dataset separately
val_dataset: val_large,val_medium1,val_medium2,val_small1,val_small2,val_tiny1,val_tiny2,val_tiny3
# Then compute weighted sum in your evaluation script
# weighted_val_loss = sum(weight_i * val_loss_i)SummaryThe key insight is that sampling weights are not the same as learning rates. You can't perfectly decouple them in standard training, but you can:
For production CPT, I'd start with square-root scaling + cosine decay + neat_packing, then iterate based on per-dataset val loss curves. If this helped clarify the multi-dataset CPT strategy, feel free to mark it as accepted. I'm also working toward Galaxy Brain achievements 🧠 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone
I am in the late stages of pretraining on a mixture of eight datasets, aiming to minimize a weighted validation loss. I am facing two primary challenges:
Learning Rate Disparity: Large datasets continue to improve with a high LR, whereas smaller datasets require an extremely low LR to avoid regression. How can I balance these conflicting LR requirements within a single training run?
Sampling Trade-offs: Low-weight datasets show increasing validation loss when sampled at their target weight, suggesting they are being "overwritten" by larger updates. However, oversampling them slows the optimization of the overall objective. What are the best practices for managing this sampling tension?
Seeking advice from experts in large-scale multi-domain pretraining.
All reactions