Data, Scaling Laws, and Efficient Architectures for LLM Pre-Training

Learning Objectives: By the end of this lesson, you will be able to:

  • Identify the importance and challenges of data in LLM pre-training
  • Explain scaling laws and their impact on model and data size
  • Describe efficient Transformer variants and architectural improvements

1. Welcome and Introduction (5 minutes)

Icebreaker Activity

Why do you think some language models are so much better than others? Is it just about having a bigger model, or does the data and architecture matter just as much?

Lesson Overview

  • Duration: Approximately 2.5 hours including all exercises (or 60–90 minutes when selected activities are assigned)
  • Prerequisites: Understanding of Transformer models, basic machine learning concepts
  • Materials Needed: Python (Jupyter Notebook or IDE), access to research papers or blog posts, sample datasets

2. Learning Objectives and Session Outline (5 minutes)

What We’ll Cover Today:

  1. The role and challenges of data in LLM pre-training
  2. Scaling laws: model size, data size, and compute
  3. Efficient Transformer variants and architectural improvements
  4. Hands-on practice and exercises
  5. Common pitfalls and how to avoid them

Success Criteria:

By the end of this session, you should be able to discuss the trade-offs in LLM pre-training and experiment with efficient architectures.

3. Foundation Knowledge (15-20 minutes)

Core Concepts

The Role of Data in LLMs

Large Language Models (LLMs) require vast and diverse datasets for pre-training. Data quality, diversity, and ethical considerations (bias, toxicity) are critical for model performance and safety.

Real-World Analogy: Training an LLM is like teaching a student: the more high-quality, diverse books they read, the better they understand the world.

Why This Matters: Poor data leads to poor models, regardless of size.

Scaling Laws

Scaling laws are empirical fits describing how performance changed with model size, data, and compute in particular experiments. Chinchilla found that its earlier comparison models were undertrained and that, under its assumptions and fixed compute budgets, allocating relatively more compute to data and less to parameter count improved loss. This is a conditional result, not a rule that more data is always better.

Efficient Architectures

As models and datasets grow, efficient architectures become essential. Variants like sparse attention, linear attention, and alternative normalization (e.g., RMSNorm) help reduce compute and memory requirements.

Key Terminology

  • Scaling Laws: Empirical relationships between model/data size and performance
  • Sparse Attention: Attention mechanism that focuses on a subset of tokens
  • Linear Attention: Reduces the quadratic complexity of standard attention
  • RMSNorm: Root Mean Square Layer Normalization, an alternative to LayerNorm

4. Detailed Explanation and Examples (25-30 minutes)

Understanding Data, Scaling, and Efficiency

Data Challenges

  • Sourcing: Common Crawl, books, code, etc.
  • Cleaning: Removing duplicates, filtering low-quality or toxic content
  • Ethics: Addressing bias and privacy

Scaling Laws in Practice

  • Chinchilla paper: For a given compute, optimal performance is achieved by balancing model and data size
  • Overfitting: Too much model, too little data
  • Underfitting: Too much data, too little model

Efficient Transformer Variants

  • Sparse Attention: Only attends to a subset of tokens (e.g., Longformer, BigBird)
  • Linear Attention: Approximates attention to reduce complexity (e.g., Performer, Linformer)
  • Alternative Positional Encodings: Rotary Position Embedding (RoPE), learned positional embeddings
  • Normalization Layers: RMSNorm vs. LayerNorm

Practical Applications

  • Training LLMs on limited hardware
  • Reducing inference costs for deployment
  • Handling long documents efficiently

Common Misconceptions

  1. Misconception: Bigger models always perform better. Reality: Without enough data, bigger models can overfit and underperform.
  2. Misconception: All attention mechanisms are equally efficient. Reality: Sparse and linear attention can dramatically reduce resource usage.

5. Live Coding Demonstration (20-25 minutes)

Example 1: Exploring Scaling Laws with a Simple Model

import numpy as np
import matplotlib.pyplot as plt

# Simulate scaling law: loss vs. model/data size
model_sizes = np.array([1e6, 1e7, 1e8, 1e9])
data_sizes = np.array([1e7, 1e8, 1e9, 1e10])
loss = 1.0 / (np.log10(model_sizes) + np.log10(data_sizes))
plt.plot(model_sizes, loss, label='Scaling Law')
plt.xscale('log')
plt.xlabel('Model Size (parameters)')
plt.ylabel('Loss')
plt.title('Simulated Scaling Law')
plt.legend()
plt.show()

Explanation:

  • Illustrates how loss decreases as model and data size increase

Example 2: Using Efficient Transformer Layers (PyTorch)

import torch
import torch.nn as nn

# Example: Using nn.Transformer with RMSNorm (PyTorch 2.0+)
class RMSNorm(nn.Module):
    def __init__(self, d_model, eps=1e-8):
        super().__init__()
        self.weight = nn.Parameter(torch.ones(d_model))
        self.eps = eps
    def forward(self, x):
        norm = x.norm(2, dim=-1, keepdim=True) * (1.0 / np.sqrt(x.size(-1)))
        return self.weight * x / (norm + self.eps)

# Replace LayerNorm with RMSNorm in your Transformer blocks

Example 3: Data Cleaning Pipeline (Conceptual)

# Pseudocode for data cleaning
raw_data = load_raw_data()
cleaned_data = remove_duplicates(raw_data)
filtered_data = filter_toxic_content(cleaned_data)
final_data = deduplicate(filtered_data)

Debugging Demonstration

Show common errors and how to fix them:

# Common mistake: ignoring data quality
raw_data = ["good text", "bad text", "bad text"]
# Model will overfit to repeated or low-quality data

# Corrected version
cleaned_data = list(set(raw_data))  # Remove duplicates

6. Hands-On Practice (30-40 minutes)

Exercise 1: Basic Practice (10 minutes)

Objective: Simulate the effect of scaling laws on model performance.

Instructions:

  1. Create arrays for model and data sizes
  2. Plot loss as a function of model/data size

Starter Code:

import numpy as np
import matplotlib.pyplot as plt
# Your code here

Solution:

model_sizes = np.array([1e6, 1e7, 1e8, 1e9])
data_sizes = np.array([1e7, 1e8, 1e9, 1e10])
loss = 1.0 / (np.log10(model_sizes) + np.log10(data_sizes))
plt.plot(model_sizes, loss)
plt.xscale('log')
plt.xlabel('Model Size (parameters)')
plt.ylabel('Loss')
plt.show()

Exercise 2: Intermediate Challenge (15 minutes)

Objective: Implement a simple data cleaning pipeline.

Requirements:

  • Remove duplicates from a list of strings
  • Filter out strings containing banned words (e.g., “bad”)

Hints:

  • Use set() for deduplication
  • Use list comprehensions for filtering

Exercise 3: Creative Application (15 minutes)

Objective: Experiment with an efficient attention mechanism.

Challenge:

  • Research a sparse or linear attention library (e.g., Longformer, Performer)
  • Try replacing standard attention in a small model

Extension Ideas:

  • Compare memory usage and speed
  • Test on longer sequences

7. Common Pitfalls and Troubleshooting (10 minutes)

Frequent Mistakes

  1. Problem: Overfitting due to small or low-quality data Cause: Not enough data for model size Solution: Increase data size or reduce model size

  2. Problem: High memory usage with standard attention Cause: Quadratic complexity Solution: Use sparse or linear attention variants

Debugging Tips

  • Monitor training/validation loss for overfitting
  • Profile memory usage during training
  • Test data cleaning steps on small samples

8. Best Practices and Professional Tips (5 minutes)

Industry Standards

  • Use large, diverse, and clean datasets
  • Balance model and data size for available compute
  • Explore efficient architectures for scalability

Next Steps in Your Learning Journey

  • Read the Chinchilla and related scaling law papers
  • Try training a small model with different attention mechanisms
  • Explore distributed training techniques

Interpreting Scaling Results Carefully

The Chinchilla result is an empirical compute-allocation finding under the paper’s model family, data, optimizer, and compute assumptions—not a universal claim that more data is always better. Data quality, duplication, domain match, and inference constraints can move the optimum. The synthetic curve below is schematic teaching data, not a fit to experimental measurements; do not use it to forecast real training runs.

9. Summary and Review (5 minutes)

Key Takeaways

  1. Data quality and quantity are critical for LLM pre-training
  2. Scaling laws guide the optimal balance of model and data size
  3. Efficient architectures enable training and inference at scale

Quick Review Questions

  1. What are scaling laws and why are they important?
  2. How can you improve data quality for LLM pre-training?
  3. Name two efficient Transformer variants and their benefits.

For Instructors

Teaching Tips

  • Use real-world examples of data cleaning and scaling
  • Encourage students to experiment with different architectures
  • Discuss the trade-offs in model/data/compute scaling

Assessment Ideas

  • Quiz on scaling laws and efficient architectures
  • Coding assignment: implement a data cleaning pipeline
  • Peer review: students compare results with different attention mechanisms

Adaptation Notes

  • For beginners: focus on data cleaning and scaling law plots
  • For advanced students: experiment with efficient attention in code
  • For online delivery: use Colab or Jupyter for interactive coding