Navigation
💾 Data & Storage· 10 min read

Data Mixing

Balancing domains (code, math, web, books), curriculum strategies, upsampling/downsampling, and how mixing ratios affect model capabilities.

data-mixingdomain-balancecurriculumupsamplingtraining-data

Data mixing determines what the model learns to do well. The ratio of code vs math vs web text directly shapes capabilities — and getting it wrong means expensive retraining.


1. Why Mixing Matters

The training data distribution directly determines model capabilities:

If you over-weight…Model gets better at…But worse at…
Web textGeneral knowledge, fluencyCode, math, reasoning
CodeProgramming, structured reasoningCreative writing, general knowledge
MathMathematical reasoningGeneral fluency
BooksLong-form coherence, depthShort-form, conversational
Instruction dataFollowing instructionsRaw knowledge generation

There is no universally optimal mix — it depends on the target use case. Llama-3’s mix differs from DeepSeek’s which differs from CodeLlama’s.


2. Mixing Strategies

Static Mixing

Data Mix Proportions

Fix the ratios before training and don’t change them:

# Example: Llama-style static mix
mix = {
    "common_crawl_filtered": 0.67,  # 67% web
    "github_code": 0.08,           # 8% code  
    "wikipedia": 0.05,             # 5% encyclopedia
    "books": 0.05,                 # 5% books
    "arxiv": 0.03,                 # 3% academic
    "stackexchange": 0.02,         # 2% Q&A
    "c4_realnews": 0.10,           # 10% news
}

Upsampling and Downsampling

High-quality domains are often small. Repeat them to increase their effective weight:

DomainRaw sizeTarget weightUpsampling factor
Web (Common Crawl)5 TB67%1.0× (downsample!)
Code (GitHub)500 GB8%1.2×
Math (proofs, textbooks)20 GB5%18× (heavily upsampled)
Books100 GB5%3.5×

Upsampling risk: Too much repetition → model memorizes specific examples. Rule of thumb: don’t upsample more than 5-10× for any domain.

Curriculum Learning (Dynamic Mixing)

Change the mix during training:

Phase 1 (0-50% tokens): Heavy web text (learn language)
Phase 2 (50-80%): Increase code + math (learn reasoning)
Phase 3 (80-100%): High-quality curated data (refine quality)

Annealing: Increase the proportion of high-quality data toward the end of training. The model learns broad patterns first, then refines on curated content.


3. How Mixing Ratios Are Determined

MethodHow it worksCost
Scaling law experimentsTrain small models at different mixes, extrapolateMedium (many small runs)
Ablation studiesTrain same-size models varying one domain at a timeHigh (multiple full runs)
Domain weighting by lossWeight domains inversely to their loss (DoReMi)Medium
Copy from prior artUse published mixes (Llama, Falcon, etc.)Free

DoReMi: Data Reweighting by Model Inspection

DoReMi trains a small proxy model to determine optimal domain weights:

  1. Train a small model on uniform mix → observe per-domain losses
  2. Upweight domains where the model struggles → train again
  3. Use the resulting weights for the large model training

4. Practical Considerations

IssueSolution
Domain proportions don’t sum to 1.0Normalize: weight_i / sum(weights)
Small domain exhausted before largeRepeat small domain (with shuffle), or stop sampling it
Data quality varies within domainsQuality-filter each domain separately, then mix
Can’t re-run full training to tune mixUse scaling law proxy experiments (1B model, 10B tokens)

For how to tokenize the mixed data, see Tokenization at Scale.


Self-Check

  • Explain how data mixing ratios affect model capabilities (with examples)
  • Compare static mixing vs curriculum learning
  • Calculate upsampling factors for a given domain size and target weight
  • Explain the upsampling risk (memorization) and the rule of thumb
  • Describe DoReMi’s approach to determining optimal domain weights

References