Module 1 · Chapter 3 Prompt Engineering Foundations › Large Language Model Fundamentals

Pre-Training Explained

Pre-training is the large-scale, self-supervised learning phase where a model repeatedly predicts missing or upcoming tokens across billions of examples, building the reusable foundation of grammar, knowledge, and reasoning patterns that later gets specialized through fine-tuning, alignment, and retrieval.

Quick takeaway: pre-training is transfer learning at massive scale - a single foundation model learns general language patterns once, then gets adapted to many downstream tasks through fine-tuning, instruction tuning, or prompting rather than training from scratch every time. Data quality and mixture matter as much as raw scale: scaling laws show that model size, token count, and compute budget must be balanced together, not maximized independently.

Introduction

Pre-training is the large-scale learning phase in which an artificial intelligence model learns general patterns from massive amounts of data before it is adapted for a specific task.

For a large language model, pre-training usually involves processing billions or trillions of text tokens and repeatedly attempting to predict missing or upcoming tokens. Through this process, the model gradually learns:

  • Grammar and sentence structure
  • Word relationships
  • General knowledge patterns
  • Programming syntax
  • Writing styles
  • Logical associations
  • Contextual relationships
  • Basic reasoning patterns
  • Relationships between concepts
  • Common structures found in human language

A pre-trained model is not usually built to perform only one task. Instead, it develops broad capabilities that can later be adapted through instruction tuning, supervised fine-tuning, reinforcement learning, retrieval systems, or prompt engineering.

Pre-training is responsible for most of the general language understanding found in modern large language models.

What Is Pre-Training?

Pre-training is the first major training stage in which a machine learning model learns from a large and diverse dataset without being designed for one narrowly defined application.

The word pre-training means that the model is trained before task-specific training takes place.

For example, instead of initially training a model only to answer banking questions, developers may first train it on:

  • Books
  • Articles
  • Documentation
  • Educational material
  • Public web pages
  • Research papers
  • Programming code
  • Question-and-answer content
  • Conversational text
  • Structured datasets

After learning general patterns from this material, the model can be adapted for banking support, healthcare assistance, coding, summarization, translation, or another specialized use case.

Simple Definition of Pre-Training

Pre-training is the process of teaching an AI model general knowledge and language patterns using a very large dataset before training it for a specific purpose.

Why Pre-Training Is Necessary

Training a separate language model from the beginning for every application would be extremely expensive and inefficient.

Pre-training solves this problem by creating a reusable foundation model.

A single pre-trained model can later support many tasks, including:

  • Text generation
  • Question answering
  • Sentiment analysis
  • Translation
  • Summarization
  • Code generation
  • Information extraction
  • Classification
  • Chatbot development
  • Document analysis

This approach is known as transfer learning because knowledge learned during pre-training is transferred to new tasks.

Pre-Training in Large Language Models

In large language models, pre-training normally uses self-supervised learning.

Self-supervised learning does not require humans to manually label every training example. Instead, the training data itself provides the learning signal.

Consider the following sentence:

The developer deployed the application to the cloud.

During pre-training, the model may receive:

The developer deployed the application to the

The expected next token might be:

cloud

The model predicts a token, compares its prediction with the actual token, calculates an error, and updates its internal parameters.

This process is repeated across enormous amounts of text.

Main Goal of Pre-Training

The main goal of pre-training is to estimate the probability of tokens based on their context.

For an autoregressive language model, the objective can be represented as:

P(x1, x2, x3, ..., xn) = P(x1) × P(x2 | x1) × P(x3 | x1, x2) × ... × P(xn | x1, ..., xn-1)

This means that the probability of an entire sequence is calculated from the probability of each token given the tokens that appeared before it.

The model learns to assign higher probabilities to likely token sequences and lower probabilities to unlikely sequences.

How Pre-Training Works

Pre-training can be understood through the following major stages:

  1. Data collection
  2. Data filtering
  3. Data cleaning
  4. Deduplication
  5. Tokenization
  6. Dataset preparation
  7. Model initialization
  8. Forward propagation
  9. Loss calculation
  10. Backpropagation
  11. Parameter updates
  12. Distributed training
  13. Checkpoint creation
  14. Model evaluation
  15. Final pre-trained model generation

Each stage affects the model’s quality, safety, accuracy, and computational cost.

Stage 1: Data Collection

The first stage is collecting a very large training corpus.

A training corpus is the complete collection of data used to train the model.

Possible data sources include:

  • Publicly available web content
  • Licensed datasets
  • Books
  • Technical documentation
  • Academic papers
  • Source code repositories
  • Educational material
  • Reference content
  • Multilingual text
  • Human-created datasets
  • Organization-owned documents

The dataset must contain enough diversity for the model to learn different writing styles, subjects, languages, formats, and reasoning patterns.

Example

A coding-focused model may include more:

  • Programming tutorials
  • API documentation
  • Source code
  • Technical discussions
  • Bug reports
  • Code review examples

A general-purpose model may contain a wider mixture of literature, science, technology, history, business, and conversational content.

Stage 2: Data Filtering

Raw data collected from the internet or other sources contains a large amount of low-quality material.

Filtering removes content that may reduce model quality.

Common filtering criteria include:

  • Extremely short or incomplete documents
  • Automatically generated spam
  • Repeated keyword pages
  • Corrupted text
  • Unreadable character sequences
  • Excessive advertisements
  • Malicious content
  • Low-information pages
  • Pages containing mostly navigation elements
  • Documents in unsupported formats

Machine learning classifiers, rules, language detectors, and quality scores can be used during filtering.

Stage 3: Data Cleaning

Data cleaning transforms raw text into a more consistent and usable format.

Cleaning may include:

  • Removing broken markup
  • Converting text into a standard encoding
  • Removing invalid characters
  • Fixing formatting problems
  • Normalizing whitespace
  • Separating documents correctly
  • Removing unnecessary HTML elements
  • Detecting document boundaries
  • Identifying language
  • Removing unusable metadata

Poorly cleaned data can teach the model incorrect formatting or meaningless patterns.

Stage 4: Deduplication

Large datasets often contain repeated content.

The same article may appear:

  • On multiple websites
  • In copied blog posts
  • In archived pages
  • In quoted documents
  • In duplicated repositories
  • In multiple versions of the same dataset

Deduplication removes identical or nearly identical content.

This is important because duplicated data can:

  • Bias the model toward repeated information
  • Increase memorization risk
  • Waste computational resources
  • Reduce dataset diversity
  • Distort evaluation results
  • Overrepresent certain sources

Deduplication may be performed at the document, paragraph, sentence, or sequence level.

Stage 5: Tokenization

Language models do not directly process text as complete sentences. Text is converted into smaller units called tokens.

A token may represent:

  • A complete word
  • Part of a word
  • A punctuation symbol
  • A number
  • A programming operator
  • A whitespace-related pattern
  • A special control symbol

For example:

Pre-training helps language models learn.

A tokenizer might split it approximately as:

Pre

training helps language models learn .

The exact output depends on the tokenizer.

Token IDs

After tokenization, every token is mapped to a numerical identifier.

Example:

Pre → 1452 training → 7631 helps → 912 language → 3320 models → 4410

The model processes these numeric token IDs instead of raw words.

Vocabulary

The tokenizer uses a fixed vocabulary.

A vocabulary is the complete set of tokens recognized by the model.

A larger vocabulary may represent more complete words but requires a larger output layer. A smaller vocabulary may split words into more pieces, increasing sequence length.

Vocabulary design affects:

  • Training efficiency
  • Multilingual support
  • Code understanding
  • Handling of rare words
  • Memory usage
  • Inference speed
  • Token cost

Common Tokenization Methods

Common tokenization techniques include:

  • Byte Pair Encoding
  • WordPiece
  • Unigram Language Model
  • SentencePiece
  • Byte-level tokenization

Modern models often use subword or byte-level tokenization because these methods can represent unfamiliar words without requiring every possible word to exist in the vocabulary.

Stage 6: Training Sequence Preparation

Tokenized documents are divided into sequences of a fixed maximum length.

For example, a model may be trained with sequence lengths such as:

  • 512 tokens
  • 1,024 tokens
  • 2,048 tokens
  • 4,096 tokens
  • 8,192 tokens
  • Longer context lengths

Documents may be packed together to reduce unused space in training batches.

Special tokens can indicate:

  • Beginning of a sequence
  • End of a sequence
  • Padding
  • Document separation
  • Unknown content
  • Different modalities

Stage 7: Model Initialization

Before training begins, the model’s parameters are initialized.

Parameters are numerical values stored inside the neural network.

They include values used in:

  • Embedding matrices
  • Attention projections
  • Feed-forward layers
  • Normalization layers
  • Output projections

A large model may contain millions, billions, or more parameters.

At initialization, the parameters do not contain useful language knowledge. They are usually initialized with small values based on a carefully selected statistical distribution.

Transformer Architecture in Pre-Training

Most modern large language models are based on the Transformer architecture.

A Transformer commonly contains:

  • Token embeddings
  • Positional information
  • Self-attention layers
  • Feed-forward neural networks
  • Residual connections
  • Normalization layers
  • Output projection layers

The Transformer is effective because it can process relationships between tokens across a sequence.

Token Embeddings

A token ID is converted into a dense numerical vector called an embedding.

An embedding may contain hundreds or thousands of dimensions.

During training, tokens used in similar contexts gradually develop related vector representations.

For example, the embeddings for:

  • Doctor
  • Nurse
  • Hospital
  • Patient

may become closer in the model’s representation space than unrelated terms such as:

  • Mountain
  • Keyboard
  • Satellite

The model does not store dictionary definitions in these vectors. It learns statistical relationships from usage patterns.

Positional Information

Self-attention does not naturally understand token order. Therefore, the model needs positional information.

Positional information helps distinguish between:

Dog chased cat.

and:

Cat chased dog.

The words are similar, but their order changes the meaning.

Models may use:

  • Learned positional embeddings
  • Sinusoidal positional encoding
  • Rotary positional embeddings
  • Relative position methods
  • Attention bias techniques

Self-Attention

Self-attention allows every token to examine other relevant tokens in the sequence.

For each token, the model creates three representations:

  • Query
  • Key
  • Value

Attention scores are calculated by comparing queries with keys.

A simplified attention equation is:

Attention(Q, K, V) = softmax(QKᵀ / √dk)V

Where:

  • Q represents query vectors
  • K represents key vectors
  • V represents value vectors
  • dk represents the key dimension
  • softmax converts scores into normalized attention weights

The output is a weighted combination of value vectors.

Self-Attention Example

Consider the sentence:

The programmer fixed the server because it had failed.

To understand the word it, the model must identify that it probably refers to the server.

Self-attention allows the token it to assign greater importance to the token server than to less relevant words.

Multi-Head Attention

Transformers normally use multiple attention heads.

Different attention heads can learn different relationships, such as:

  • Subject and verb relationships
  • Pronoun references
  • Long-distance dependencies
  • Code variable usage
  • Punctuation patterns
  • Document structure
  • Semantic similarity

The outputs of the attention heads are combined before being passed to the next layer.

Feed-Forward Network

After attention, each token representation passes through a feed-forward neural network.

A simplified feed-forward operation is:

FFN(x) = W2 × activation(W1 × x + b1) + b2

The feed-forward network transforms the representation and helps the model learn more complex patterns.

Modern models may use activation functions such as:

  • GELU
  • ReLU
  • SiLU
  • SwiGLU

Residual Connections

Residual connections add the original input of a layer to the layer’s output.

A simplified representation is:

output = input + layer(input)

Residual connections help:

  • Preserve information
  • Improve gradient flow
  • Stabilize deep networks
  • Support training with many layers

Normalization Layers

Normalization helps maintain stable numerical values during training.

Common methods include:

  • Layer Normalization
  • RMS Normalization

Normalization can be applied before or after attention and feed-forward operations, depending on the architecture.

Stage 8: Forward Propagation

During forward propagation, a batch of token sequences passes through the model.

The model performs the following operations:

  1. Converts token IDs into embeddings
  2. Adds or applies positional information
  3. Passes representations through Transformer layers
  4. Produces output scores for every vocabulary token
  5. Converts scores into probabilities

The resulting scores before probability normalization are called logits.

From Logits to Probabilities

The softmax function converts logits into probabilities.

Suppose the model predicts the next token after:

Machine learning is

Possible probabilities may be:

  • useful: 0.31
  • powerful: 0.25
  • a: 0.18
  • changing: 0.09
  • difficult: 0.05
  • other tokens: 0.12

During training, the model compares these probabilities with the actual next token.

Stage 9: Loss Calculation

Loss measures how incorrect the model’s prediction is.

Language models commonly use cross-entropy loss.

For one target token, the simplified loss is:

Loss = -log(P(correct token))

When the model assigns a high probability to the correct token, the loss is low.

When the model assigns a low probability to the correct token, the loss is high.

Example

Actual next token:

powerful

Model probability for powerful:

0.70

Loss:

-log(0.70)

This produces a relatively low loss.

If the probability were 0.01, the loss would be much higher.

Average Training Loss

A training batch contains many predicted tokens. The final loss is generally calculated by averaging or summing the losses for valid target tokens.

Padding tokens and selected masked positions may be excluded from the loss.

The objective of training is to reduce the average loss across the dataset.

Stage 10: Backpropagation

Backpropagation determines how each model parameter contributed to the prediction error.

It calculates gradients for the parameters.

A gradient indicates:

  • The direction in which a parameter should move
  • The approximate amount by which it should change

Backpropagation applies the chain rule of calculus through every layer of the network.

Without backpropagation, the model would not know how to improve its parameters.

Stage 11: Parameter Updates

An optimizer uses gradients to update the model’s parameters.

A simplified parameter update is:

new_parameter = old_parameter - learning_rate × gradient

The learning rate controls the size of each update.

If the learning rate is too high:

  • Training may become unstable
  • Loss may increase
  • Parameters may diverge
  • The model may fail to converge

If the learning rate is too low:

  • Training may take too long
  • The model may learn inefficiently
  • Computational resources may be wasted

Common Optimizers

Common optimizers for language model pre-training include:

  • Adam
  • AdamW
  • Adafactor
  • Distributed optimizer variants

AdamW is widely used because it combines adaptive updates with decoupled weight decay.

Learning Rate Schedule

The learning rate normally changes during training.

A typical schedule includes:

  1. Warmup phase
  2. Peak learning rate
  3. Gradual decay
  4. Final low learning rate

During warmup, the learning rate starts small and gradually increases. This helps avoid unstable updates at the beginning of training.

After warmup, the learning rate may decay using:

  • Linear decay
  • Cosine decay
  • Polynomial decay
  • Step-based decay

Simplified Pre-Training Example

The following Python-style example demonstrates the major training operations.

Prompt
# Put the model into training mode
model.train()
# Read one batch of token sequences
input_ids = batch["input_ids"]
# Use shifted tokens as prediction targets
labels = input_ids.clone()
# Clear gradients from the previous step
optimizer.zero_grad()
# Run the forward pass and calculate language modeling loss
outputs = model(input_ids=input_ids, labels=labels)
loss = outputs.loss
# Calculate gradients using backpropagation
loss.backward()
# Prevent extremely large gradients
clip_grad_norm_(model.parameters(), max_norm=1.0)
# Update model parameters
optimizer.step()
# Update the learning rate
scheduler.step()

This example simplifies many production-level details, but it represents the main learning cycle.

Next-Token Prediction

Decoder-only language models commonly use causal language modeling.

In causal language modeling, each token can attend only to earlier tokens, not future tokens.

Suppose the training sequence is:

Artificial intelligence can generate text.

The model learns from multiple prediction targets:

  • Artificial → intelligence
  • Artificial intelligence → can
  • Artificial intelligence can → generate
  • Artificial intelligence can generate → text
  • Artificial intelligence can generate text → period

A single sequence therefore provides several learning examples.

Causal Attention Mask

A causal attention mask prevents a token from seeing future tokens during next-token prediction.

Without this mask, the model could directly access the answer it is supposed to predict.

Conceptual causal mask:

Prompt
# Each row represents the token making the prediction
# Each column represents the token that can be attended to
causal_mask = [
    [1, 0, 0, 0],
    [1, 1, 0, 0],
    [1, 1, 1, 0],
    [1, 1, 1, 1]
]

The first token sees only itself.

The second token sees the first and second positions.

The final token sees all current and previous positions.

Masked Language Modeling

Some models use masked language modeling rather than next-token prediction.

In masked language modeling, selected tokens are hidden, and the model predicts them using surrounding context.

Example:

The database stores information in a masked location.

The hidden token might be:

structured

This method allows the model to use both left and right context.

Masked language modeling is commonly associated with encoder-based models.

Causal Modeling vs Masked Modeling

FeatureCausal Language ModelingMasked Language Modeling
Main predictionNext tokenHidden token
Context directionPrevious tokensBoth left and right context
Common architectureDecoder-only TransformerEncoder-only Transformer
Text generationNaturally suitableNot naturally autoregressive
Typical useGeneration and conversationClassification and understanding

Encoder-Decoder Pre-Training

Encoder-decoder models process an input sequence and generate an output sequence.

They may be pre-trained using tasks such as:

  • Denoising corrupted text
  • Reconstructing missing spans
  • Predicting removed sentences
  • Transforming one text sequence into another

Example input:

The developer masked the application to production.

Expected output:

deployed

Encoder-decoder architectures are useful for:

  • Translation
  • Summarization
  • Text transformation
  • Structured generation

Self-Supervised Learning

Pre-training is often called self-supervised because labels are automatically derived from the original data.

For next-token prediction:

Input:

The API returned a successful

Label:

response

No human needs to manually write the label because the next token already exists in the text.

This makes it possible to generate an enormous number of training examples from raw data.

Training Batches

Models are not usually trained on one sequence at a time.

Multiple sequences are grouped into a batch.

A batch improves computational efficiency because modern accelerators can process many operations in parallel.

Important batch-related concepts include:

  • Micro-batch size
  • Global batch size
  • Gradient accumulation
  • Number of devices
  • Tokens per batch

A simplified global batch calculation is:

Global batch size = Micro-batch size × Number of devices × Gradient accumulation steps

Gradient Accumulation

Large models may not fit a large batch into accelerator memory.

Gradient accumulation solves this problem by processing several smaller batches before updating the parameters.

Prompt
# Clear gradients before accumulation begins
optimizer.zero_grad()
# Accumulate gradients across multiple micro-batches
for micro_batch in micro_batches:
    # Calculate loss for the current micro-batch
    outputs = model(input_ids=micro_batch["input_ids"], labels=micro_batch["labels"])
    # Scale the loss to preserve the effective gradient magnitude
    loss = outputs.loss / accumulation_steps
    # Add gradients without updating parameters immediately
    loss.backward()
# Update parameters after all micro-batches are processed
optimizer.step()

The model behaves approximately as though it processed one larger batch.

Epochs, Steps, and Tokens

An epoch means one complete pass through the training dataset.

However, very large language model training is often measured using:

  • Optimization steps
  • Total tokens processed
  • Tokens per second
  • Compute operations
  • Training time
  • Accelerator utilization

A model may not always complete multiple traditional epochs because the dataset can be extremely large.

Context Length During Pre-Training

Context length is the maximum number of tokens processed in one sequence.

A longer context allows the model to learn relationships across larger passages.

Benefits include:

  • Better document understanding
  • Improved long-form generation
  • Stronger code comprehension
  • Better reference tracking
  • More complete conversation context

However, longer sequences increase:

  • Memory usage
  • Attention computation
  • Training cost
  • Communication overhead
  • Data preparation complexity

Standard attention has approximately quadratic complexity with sequence length because each token may interact with every other token.

Distributed Pre-Training

Large language models are too large to train efficiently on one device.

Distributed training divides the workload across many accelerators and machines.

Common distribution methods include:

  • Data parallelism
  • Tensor parallelism
  • Pipeline parallelism
  • Sequence parallelism
  • Expert parallelism
  • Fully sharded data parallelism

Large training systems often combine several of these techniques.

Data Parallelism

In data parallelism, each device holds a model replica and processes a different batch of data.

After backpropagation, gradients are synchronized across devices.

The synchronized gradient is used to update every model replica consistently.

Data parallelism is effective when the model fits into each device’s memory.

Tensor Parallelism

Tensor parallelism splits large matrix operations across multiple devices.

For example, a large weight matrix may be divided by rows or columns.

Each device computes part of the result, and the partial results are combined.

Tensor parallelism is useful when individual model layers are too large for one accelerator.

Pipeline Parallelism

Pipeline parallelism places different model layers on different devices.

For example:

  • Device 1 processes layers 1 to 10
  • Device 2 processes layers 11 to 20
  • Device 3 processes layers 21 to 30
  • Device 4 processes layers 31 to 40

Micro-batches move through the devices like items moving through a production pipeline.

Fully Sharded Training

Fully sharded training divides model states across devices.

Sharded states may include:

  • Parameters
  • Gradients
  • Optimizer states

Instead of storing every state on every device, each device stores only a portion.

This significantly reduces per-device memory requirements.

Mixed-Precision Training

Full-precision numbers require substantial memory and computation.

Mixed-precision training uses lower-precision formats for many operations.

Common formats include:

  • FP32
  • FP16
  • BF16
  • FP8 in supported systems

Benefits include:

  • Lower memory usage
  • Faster matrix operations
  • Higher training throughput
  • Larger possible batch sizes

Some values may remain in higher precision to maintain numerical stability.

Gradient Clipping

Gradients can occasionally become extremely large.

Gradient clipping limits their magnitude.

Prompt
# Calculate the training loss
loss = outputs.loss
# Calculate parameter gradients
loss.backward()
# Limit the total gradient norm to improve stability
clip_grad_norm_(model.parameters(), max_norm=1.0)
# Apply the controlled parameter update
optimizer.step()

Gradient clipping can reduce the risk of unstable training.

Weight Decay

Weight decay discourages parameters from becoming unnecessarily large.

It acts as a form of regularization and can improve generalization.

Modern optimizers such as AdamW apply weight decay separately from the adaptive gradient update.

Training Checkpoints

Pre-training can continue for days, weeks, or longer.

A checkpoint stores the current training state.

A checkpoint may contain:

  • Model parameters
  • Optimizer state
  • Learning rate scheduler state
  • Current training step
  • Random number generator state
  • Data loader progress
  • Mixed-precision scaler state
  • Training configuration

If training is interrupted, the system can resume from the latest valid checkpoint.

Checkpoint Example

Prompt
# Store the complete recoverable training state
checkpoint = {
    "model_state": model.state_dict(),
    "optimizer_state": optimizer.state_dict(),
    "scheduler_state": scheduler.state_dict(),
    "training_step": current_step
}
# Save the checkpoint to persistent storage
save_checkpoint(checkpoint, "checkpoint-step-100000.pt")

Production systems may save checkpoints across distributed storage rather than one local file.

Validation During Pre-Training

Training loss alone is not sufficient for evaluating a model.

A separate validation dataset is used to measure performance on data that was not used for parameter updates.

Validation helps detect:

  • Overfitting
  • Data leakage
  • Training instability
  • Poor generalization
  • Unexpected loss increases
  • Dataset quality problems

The validation dataset should not overlap with the training dataset.

Perplexity

Perplexity is a common evaluation metric for language models.

It can be calculated as:

Perplexity = e^(cross-entropy loss)

Lower perplexity generally means the model predicts the evaluation text more confidently.

However, perplexity should not be treated as the only measure of model quality.

A model with lower perplexity may still perform poorly in:

  • Following instructions
  • Producing safe answers
  • Solving reasoning tasks
  • Handling factual questions
  • Generating useful conversations

Scaling Laws

Scaling laws describe relationships between:

  • Model size
  • Dataset size
  • Training compute
  • Prediction loss
  • Performance

Increasing only the number of parameters does not guarantee optimal performance.

A well-designed training run balances:

  • Parameter count
  • Number of training tokens
  • Compute budget
  • Dataset quality
  • Architecture design

A model that is too large for its dataset may be undertrained.

A dataset that is extremely large for a small model may provide diminishing returns.

Data Quality vs Data Quantity

More data is not always better.

High-quality training data can be more valuable than a larger amount of noisy data.

Good pre-training data should ideally be:

  • Relevant
  • Diverse
  • Accurate
  • Well-structured
  • Deduplicated
  • Legally usable
  • Properly formatted
  • Representative of intended languages and domains

Low-quality data can teach the model:

  • Incorrect facts
  • Broken grammar
  • Spam patterns
  • Biased associations
  • Unsafe instructions
  • Poor coding practices

Data Mixture

A pre-training dataset normally contains several data categories.

Each category may receive a specific sampling weight.

Example mixture:

  • General web text: 45 percent
  • Books: 15 percent
  • Technical documentation: 10 percent
  • Academic content: 10 percent
  • Source code: 10 percent
  • Question-and-answer data: 5 percent
  • Other curated sources: 5 percent

These percentages are illustrative.

Changing the mixture can significantly affect model behavior.

A larger code proportion may improve programming capability but could reduce exposure to some natural-language domains if the total token budget remains fixed.

Data Sampling

Datasets are not always sampled directly according to their original sizes.

Smaller but high-quality datasets may be oversampled.

Very large low-quality datasets may be downsampled.

Sampling strategies help control:

  • Domain balance
  • Language representation
  • Code exposure
  • Educational content
  • Repetition
  • Quality distribution

Curriculum Learning

Curriculum learning presents training examples in a planned order.

The model may begin with:

  • Shorter sequences
  • Cleaner text
  • Simpler examples
  • Higher-quality documents

Later training may include:

  • Longer sequences
  • More complex reasoning
  • Specialized domains
  • Noisier real-world data

Not every pre-training system uses a strict curriculum, but data order can influence optimization.

Domain-Adaptive Pre-Training

A general model can undergo additional pre-training on domain-specific data.

This is called domain-adaptive pre-training or continued pre-training.

Examples include:

  • Medical publications
  • Legal documents
  • Financial reports
  • Cybersecurity material
  • Scientific literature
  • Enterprise documentation
  • Programming repositories

The model continues using a language-modeling objective, but the dataset becomes more specialized.

Continued Pre-Training

Continued pre-training starts from an existing pre-trained model rather than random parameters.

It can be used to:

  • Add domain knowledge
  • Improve support for a language
  • Update time-sensitive knowledge
  • Improve coding capability
  • Adapt to an organization’s terminology
  • Increase context-length capability

Continued pre-training must be carefully controlled because excessive specialization can reduce performance in previously learned areas.

Catastrophic Forgetting

Catastrophic forgetting occurs when a model loses previously learned capabilities while learning new information.

For example, continued training only on legal text might improve legal terminology but weaken conversational or programming abilities.

Possible mitigation strategies include:

  • Mixing general data with domain data
  • Using a smaller learning rate
  • Limiting the number of additional training steps
  • Regularly evaluating general benchmarks
  • Applying parameter-efficient adaptation
  • Controlling domain sampling weights

Pre-Training vs Fine-Tuning

AspectPre-TrainingFine-Tuning
Main purposeLearn general patternsAdapt to a specific task or behavior
Dataset sizeUsually extremely largeUsually smaller
Starting parametersOften randomUsually pre-trained
Computational costVery highLower than pre-training
Learning objectiveGeneral language modelingTask-specific or instruction-based
OutputFoundation modelSpecialized model
Data labelingOften self-supervisedOften supervised or preference-based

Pre-Training vs Instruction Tuning

Pre-training teaches the model how language and information patterns work.

Instruction tuning teaches the model how to respond to user instructions.

During pre-training, the model may learn to continue:

Explain polymorphism in Java:

During instruction tuning, the model learns that this input should be treated as a request requiring a structured explanation rather than simple text continuation.

A pre-trained model may know a large amount of information but still behave poorly as an assistant until it receives post-training.

Pre-Training vs Reinforcement Learning

Pre-training uses a prediction objective such as next-token prediction.

Reinforcement learning optimizes behavior using rewards.

A reward may represent:

  • Human preference
  • Correctness
  • Safety
  • Instruction following
  • Task completion
  • Response quality

Reinforcement learning is normally applied after the model has already developed broad capabilities through pre-training.

What the Model Learns During Pre-Training

The model may learn statistical representations of:

  • Syntax
  • Grammar
  • Semantics
  • Facts appearing in training data
  • Common reasoning structures
  • Formatting conventions
  • Code patterns
  • Translation relationships
  • Writing styles
  • Domain terminology
  • Relationships between entities
  • Common task structures

These capabilities emerge from repeated prediction rather than direct storage of explicit rules.

Does Pre-Training Create Real Understanding?

Pre-training gives the model highly effective internal representations of language and concepts.

However, whether this should be called human-like understanding is debated.

A language model:

  • Learns statistical relationships
  • Produces outputs from learned probability distributions
  • Does not experience the physical world like a human
  • Does not automatically verify every statement
  • Can produce confident but incorrect answers
  • May fail when patterns differ from its training experience

It can demonstrate sophisticated behavior without possessing human consciousness or human experience.

Emergent Capabilities

Some abilities may become noticeably stronger after models reach certain combinations of scale, data, and training quality.

Examples may include:

  • Few-shot learning
  • Code completion
  • Translation
  • Multi-step reasoning
  • Style imitation
  • Tool-use planning
  • Structured output generation

These capabilities are called emergent when they appear without being separately programmed as explicit rules.

However, measured emergence can also depend on benchmark design and evaluation thresholds.

Memorization During Pre-Training

Language models can memorize some training sequences, especially when content is:

  • Repeated many times
  • Highly unique
  • Short
  • Predictable
  • Included in many duplicate documents
  • Strongly overrepresented

Memorization is different from generalization.

Generalization means learning a reusable pattern.

Memorization means reproducing a specific sequence.

Deduplication, data governance, privacy filtering, and evaluation can reduce memorization risks.

Bias in Pre-Training Data

Training data may contain social, cultural, geographic, linguistic, and historical biases.

The model can learn these biases because it learns patterns from the dataset.

Possible sources include:

  • Unequal representation
  • Stereotypical text
  • Historical discrimination
  • Regional imbalance
  • Language imbalance
  • Toxic content
  • Dominant cultural perspectives

Mitigation can include:

  • Dataset analysis
  • Balanced sampling
  • Safety filtering
  • Post-training
  • Red-team testing
  • Bias benchmarks
  • Human evaluation

Completely eliminating all bias is extremely difficult.

Privacy Risks

Training datasets may accidentally contain personal or sensitive information.

Privacy protection may involve:

  • Removing known personal identifiers
  • Filtering private records
  • Excluding unauthorized data sources
  • Deduplicating repeated personal content
  • Applying privacy-preserving methods
  • Testing for memorization
  • Restricting access to raw datasets

Data governance is therefore a critical part of pre-training.

Pre-training data must be evaluated for legal and licensing considerations.

Important questions include:

  • Was the data publicly accessible?
  • Was it licensed for machine learning use?
  • Does the organization have permission to process it?
  • Are there restrictions on redistribution?
  • Does the dataset contain protected material?
  • Can generated output reproduce protected sequences?

Technical capability does not remove legal responsibility.

Security Risks in Pre-Training Data

Training data may contain:

  • Malicious code
  • Insecure programming examples
  • Prompt injection text
  • Exploit descriptions
  • Fraudulent instructions
  • Harmful procedural content
  • Manipulated documents

Filtering and post-training reduce risk, but models may still learn unsafe patterns from large datasets.

Data Contamination

Data contamination occurs when evaluation questions or benchmark answers appear in the training data.

This can make a model appear more capable than it actually is.

A model may score highly because it memorized benchmark content rather than generalizing to unseen problems.

Contamination detection can include:

  • Exact matching
  • Near-duplicate detection
  • N-gram analysis
  • Semantic similarity search
  • Benchmark canary strings
  • Training-data audits

Training Stability

Large-scale pre-training can fail because of numerical or system problems.

Common issues include:

  • Exploding gradients
  • Invalid numerical values
  • Hardware failure
  • Network interruption
  • Corrupted checkpoints
  • Data loader errors
  • Loss spikes
  • Learning rate instability
  • Synchronization errors

Training systems therefore monitor many metrics continuously.

Important Training Metrics

Common metrics include:

  • Training loss
  • Validation loss
  • Learning rate
  • Gradient norm
  • Tokens processed
  • Tokens per second
  • Accelerator utilization
  • Memory consumption
  • Communication time
  • Checkpoint duration
  • Invalid value count
  • Data loading speed

Monitoring helps detect problems before they waste a large amount of computation.

Loss Spikes

A loss spike is a sudden increase in training loss.

Possible causes include:

  • Corrupted training data
  • An unusually difficult data batch
  • Numerical instability
  • Excessive learning rate
  • Hardware error
  • Optimizer-state corruption
  • Incorrect data formatting

Training systems may skip problematic batches, restore an earlier checkpoint, reduce the learning rate, or inspect the data source.

Training Compute

Pre-training requires large numbers of matrix multiplications.

Compute requirements depend on:

  • Parameter count
  • Number of training tokens
  • Sequence length
  • Batch size
  • Architecture
  • Numerical precision
  • Hardware efficiency

A commonly used rough relationship is that training compute grows proportionally with model parameters multiplied by the number of training tokens.

Actual cost also depends on implementation efficiency and hardware utilization.

Hardware Used for Pre-Training

Pre-training commonly uses specialized accelerators such as:

  • Graphics Processing Units
  • Tensor Processing Units
  • Other AI accelerators

A large training cluster may also require:

  • High-speed networking
  • Distributed storage
  • Checkpoint storage
  • Data preprocessing systems
  • Monitoring services
  • Job scheduling infrastructure
  • Cooling systems
  • Reliable power infrastructure

The surrounding system is often as important as the accelerators themselves.

Energy Consumption

Large-scale pre-training consumes substantial electricity.

Energy usage depends on:

  • Number of accelerators
  • Training duration
  • Accelerator efficiency
  • Data-center efficiency
  • Cooling requirements
  • Hardware utilization
  • Power source

Efficiency improvements can reduce energy consumption through:

  • Better model architectures
  • Lower-precision computation
  • Efficient data loading
  • Improved hardware
  • Better parallelism
  • Early stopping
  • Reusing pre-trained models

Mixture-of-Experts Pre-Training

A Mixture-of-Experts model contains multiple specialized feed-forward components called experts.

A routing mechanism selects a small number of experts for each token.

This allows the model to contain many total parameters while activating only part of the network for each token.

Potential benefits include:

  • Higher parameter capacity
  • Lower active computation than a dense model of equal size
  • Specialized internal representations

Challenges include:

  • Load balancing
  • Routing stability
  • Communication overhead
  • Expert underutilization
  • Distributed system complexity

Dense Models vs Mixture-of-Experts Models

FeatureDense ModelMixture-of-Experts Model
Active parameters per tokenUsually all model parametersSelected experts only
RoutingNot requiredRequired
Implementation complexityLowerHigher
Communication complexityModeratePotentially high
Parameter efficiencyStandardCan provide larger capacity per unit of active compute

Pre-Training a Small Language Model

The same principles can be demonstrated with a small educational model.

Prompt
# Convert training text into token identifiers
token_ids = tokenizer.encode(training_text)
# Divide tokens into fixed-length input sequences
sequences = create_sequences(token_ids, sequence_length=128)
# Initialize the language model
model = LanguageModel(vocab_size=tokenizer.vocab_size)
# Create the optimizer
optimizer = AdamW(model.parameters(), lr=0.0003)
# Train for the selected number of passes
for epoch in range(number_of_epochs):
    # Process one training sequence at a time
    for sequence in sequences:
        # Use all tokens except the final token as input
        inputs = sequence[:-1]
        # Use all tokens except the first token as targets
        targets = sequence[1:]
        # Clear gradients from the previous update
        optimizer.zero_grad()
        # Predict the next-token logits
        logits = model(inputs)
        # Compare predictions with actual next tokens
        loss = cross_entropy(logits, targets)
        # Calculate gradients
        loss.backward()
        # Update model parameters
        optimizer.step()

A production model would additionally require batching, distributed training, mixed precision, evaluation, checkpointing, fault tolerance, and optimized kernels.

Practical Example of Next-Token Learning

Consider the training sentence:

Java supports object-oriented programming.

The training pairs may be created as:

Input contextTarget token
Javasupports
Java supportsobject
Java supports objectoriented
Java supports object orientedprogramming
Java supports object oriented programmingperiod

The model updates its parameters after comparing each prediction with the correct target.

Across millions of examples, it learns that words such as Java, class, object, inheritance, and interface frequently appear in related contexts.

Practical Example of Generalization

Suppose the model sees many examples such as:

  • Python uses indentation to define blocks.
  • Java uses braces to define blocks.
  • JavaScript uses braces for many block structures.

Later, a user asks:

How are code blocks defined in Python?

The model can generate an answer based on patterns learned across many related documents.

It does not need to retrieve one exact sentence. It can combine learned relationships into a new response.

What Pre-Training Does Not Guarantee

Pre-training does not guarantee that the model will:

  • Always provide correct facts
  • Follow instructions correctly
  • Avoid harmful output
  • Understand current events
  • Explain its true internal reasoning
  • Distinguish truth from frequently repeated misinformation
  • Protect confidential data automatically
  • Perform reliable mathematical calculation
  • Remain unbiased
  • Know information created after its training data period

Additional systems are needed to improve reliability.

Limitations of Pre-Training

Major limitations include:

  1. High computational cost
  2. Large storage requirements
  3. Dependence on data quality
  4. Potential bias
  5. Privacy concerns
  6. Copyright concerns
  7. Possibility of memorization
  8. Static learned knowledge
  9. Difficult interpretability
  10. Expensive experimentation
  11. Training instability
  12. Environmental impact
  13. Benchmark contamination
  14. Hallucination risk
  15. Limited grounding in the physical world

Pre-Training and Hallucinations

A language model is trained to predict plausible tokens, not to guarantee factual truth.

If several continuations appear linguistically reasonable, the model may generate an incorrect one.

Hallucinations can happen because:

  • Training data contains conflicting claims
  • The model lacks sufficient information
  • Rare facts are weakly represented
  • The prompt is ambiguous
  • The model prioritizes fluent continuation
  • No external verification system is available

Retrieval, tool use, verification, and post-training can reduce this problem.

Pre-Training and Knowledge Cutoffs

A pre-trained model reflects information contained in its training data.

It does not automatically learn events occurring after training finishes.

Knowledge can be updated through:

  • Continued pre-training
  • Fine-tuning
  • Retrieval-augmented generation
  • Search tools
  • External databases
  • Application APIs

Retrieval is often more practical than repeatedly pre-training the entire model.

Evaluating a Pre-Trained Model

Evaluation should cover multiple capability areas.

Possible evaluation categories include:

  • Language modeling loss
  • Reading comprehension
  • Factual knowledge
  • Reasoning
  • Mathematics
  • Coding
  • Multilingual performance
  • Long-context understanding
  • Bias
  • Toxicity
  • Privacy leakage
  • Memorization
  • Robustness
  • Domain knowledge

No single benchmark provides a complete measurement of model quality.

Human Evaluation

Human evaluators can review outputs for:

  • Correctness
  • Relevance
  • Coherence
  • Fluency
  • Safety
  • Helpfulness
  • Completeness
  • Professional tone

However, human evaluation can be expensive and subjective.

Clear evaluation guidelines and multiple reviewers improve consistency.

Pre-Training Pipeline Summary

A complete pre-training pipeline generally follows this flow:

  1. Define the model’s target capabilities.
  2. Collect legally usable and relevant datasets.
  3. Clean and normalize the raw data.
  4. Filter low-quality and unsafe material.
  5. Remove exact and near duplicates.
  6. Build or select a tokenizer.
  7. Convert documents into token sequences.
  8. Design the model architecture.
  9. Initialize model parameters.
  10. Configure the optimizer and learning rate.
  11. Distribute the model across accelerators.
  12. Process token batches through the model.
  13. Calculate prediction loss.
  14. Run backpropagation.
  15. Synchronize gradients.
  16. Update parameters.
  17. Monitor stability and throughput.
  18. Save regular checkpoints.
  19. Evaluate on validation datasets.
  20. Continue until the token or compute budget is reached.
  21. Perform final capability and safety evaluations.
  22. Prepare the model for post-training.

Best Practices for Pre-Training

Use High-Quality Data

Prioritize meaningful, diverse, well-structured content rather than maximizing raw dataset size.

Remove Duplicates

Apply exact and semantic deduplication to reduce memorization and wasted compute.

Protect Validation Data

Keep evaluation datasets separate from training data.

Track Data Provenance

Maintain records describing where datasets came from and how they may be used.

Monitor Training Continuously

Track loss, gradients, throughput, memory, and hardware health.

Save Recoverable Checkpoints

Checkpoints should contain enough state to resume training accurately.

Balance the Data Mixture

Avoid allowing one domain or language to dominate unless specialization is intentional.

Evaluate During Training

Run validation and capability tests at regular intervals.

Use Stable Numerical Methods

Apply mixed precision, normalization, gradient clipping, and appropriate initialization carefully.

Plan Compute and Data Together

Choose model size and training-token count according to the available compute budget.

Common Pre-Training Mistakes

Using Unfiltered Data

Low-quality data can reduce model reliability and increase unsafe behavior.

Ignoring Duplicate Content

Duplicates waste computation and increase memorization risk.

Using an Excessive Learning Rate

An overly large learning rate can cause loss spikes and training failure.

Failing to Separate Evaluation Data

Evaluation contamination produces misleading performance scores.

Saving Incomplete Checkpoints

A checkpoint containing only model weights may not support exact training recovery.

Ignoring Data Distribution

Overrepresented sources can create strong domain or cultural biases.

Evaluating Only Training Loss

Low training loss does not guarantee useful, safe, or generalizable behavior.

Scaling Without Infrastructure Planning

A larger model requires stronger networking, storage, monitoring, and fault recovery.

Real-World Analogy

Pre-training can be compared to general education.

A student first studies:

  • Language
  • Mathematics
  • Science
  • History
  • General problem-solving

After building this foundation, the student specializes in a profession such as medicine, engineering, or law.

Similarly:

  • Pre-training provides broad foundational knowledge.
  • Fine-tuning provides specialization.
  • Instruction tuning teaches response behavior.
  • Reinforcement learning improves preferred behavior.
  • Retrieval systems provide updated external information.

Advantages of Pre-Training

  • Creates reusable foundation models
  • Reduces task-specific data requirements
  • Supports transfer learning
  • Enables many downstream applications
  • Learns from unlabeled data
  • Produces broad language capabilities
  • Improves few-shot and zero-shot performance
  • Supports domain adaptation
  • Reduces the need to train every application from scratch

Disadvantages of Pre-Training

  • Requires significant compute
  • Requires massive datasets
  • Can learn bias and misinformation
  • May memorize sensitive content
  • Produces static knowledge
  • Is difficult to interpret
  • Requires complex distributed systems
  • Can be environmentally expensive
  • Does not automatically create safe assistant behavior
  • Can produce fluent but incorrect outputs

Key Terms

TermMeaning
CorpusComplete collection of training data
TokenSmall unit of text processed by the model
VocabularySet of tokens recognized by the tokenizer
ParameterLearned numerical value in the model
GradientDirection and magnitude used to update a parameter
LossMeasurement of prediction error
OptimizerAlgorithm that updates parameters
BatchGroup of training sequences processed together
EpochOne complete pass through a dataset
CheckpointSaved model and training state
PerplexityMetric derived from language-modeling loss
Context lengthMaximum tokens processed in one sequence
Self-supervised learningLearning where labels are derived from the data
Fine-tuningAdapting a pre-trained model to a task
Foundation modelBroad model reusable across many applications

Final Summary

Pre-training is the foundational learning stage of a large language model.

During pre-training:

  • Large amounts of data are collected and processed.
  • Text is cleaned, filtered, deduplicated, and tokenized.
  • Token sequences are passed through a Transformer model.
  • The model predicts hidden or upcoming tokens.
  • Prediction errors are measured using a loss function.
  • Backpropagation calculates gradients.
  • An optimizer updates billions of parameters.
  • The process is distributed across many accelerators.
  • Checkpoints and validation metrics are used to track progress.
  • The resulting model learns broad patterns that can be transferred to many tasks.

Pre-training gives a model general capability, but it does not automatically make the model a reliable assistant. Fine-tuning, instruction tuning, preference optimization, retrieval, safety testing, and application-level controls are usually required before deployment.

Frequently Asked Questions

What is pre-training in artificial intelligence?

Pre-training is the process of training an AI model on a large and general dataset before adapting it to a specific task. It helps the model learn broad patterns, language structures, relationships, and reusable representations.

Why is it called pre-training?

It is called pre-training because it occurs before task-specific training, instruction tuning, preference optimization, or application-specific adaptation.

What does a language model predict during pre-training?

An autoregressive language model usually predicts the next token based on previous tokens. Other models may predict masked tokens, missing spans, corrupted text, or output sequences.

Does pre-training require manually labeled data?

Pre-training usually uses self-supervised learning, so manually created labels are not required for every example. Labels are automatically generated from the original text, such as using the next token as the prediction target.

What type of data is used for pre-training?

Pre-training may use books, articles, technical documentation, source code, research papers, educational content, web pages, licensed datasets, and organization-owned data.

What is a token in pre-training?

A token is a small unit of text processed by the model. It may be a word, part of a word, punctuation mark, number, code operator, byte, or special symbol.

What is a training corpus?

A training corpus is the complete collection of documents, code, text, and other data used to train a model.

Why is data cleaning important?

Data cleaning removes corrupted text, broken formatting, spam, invalid characters, and other low-quality content that could reduce model performance.

Why is deduplication necessary?

Deduplication removes repeated or nearly repeated content. It reduces memorization risk, improves data diversity, prevents overrepresentation, and avoids wasting compute.

What is the loss function in language model pre-training?

The loss function measures the difference between the model's prediction and the actual target token. Cross-entropy loss is commonly used for language modeling.

What is backpropagation?

Backpropagation is the algorithm used to calculate how each model parameter contributed to the prediction error. It produces gradients that guide parameter updates.

What is an optimizer?

An optimizer is an algorithm that uses gradients to update model parameters. AdamW is a commonly used optimizer in large language model training.

What is the learning rate?

The learning rate controls how much model parameters change during each optimization step. A rate that is too high can make training unstable, while a rate that is too low can make training inefficient.

What is a pre-trained model?

A pre-trained model is a model that has completed broad initial training and contains reusable knowledge representations. It can later be adapted for specific tasks.

Is a pre-trained model ready to work as a chatbot?

Not necessarily. A base pre-trained model may continue text effectively but may not reliably follow instructions. Instruction tuning and additional post-training are usually required for chatbot behavior.

What is the difference between pre-training and fine-tuning?

Pre-training develops general capabilities using a massive dataset. Fine-tuning adapts the pre-trained model to a narrower task, domain, format, or behavior using a smaller dataset.

What is continued pre-training?

Continued pre-training means taking an existing pre-trained model and training it further using a language-modeling objective, often with newer, domain-specific, or language-specific data.

Can pre-training update a model's knowledge?

Yes. Continued pre-training can add newer or specialized information. However, retrieval systems are often a more efficient way to provide frequently changing information.

What is perplexity?

Perplexity is a metric derived from language-modeling loss. It reflects how uncertain the model is when predicting evaluation text. Lower perplexity generally indicates better token prediction.

Why does pre-training require distributed computing?

Large models and their optimizer states may not fit on one device. Distributed computing divides data, parameters, layers, and calculations across multiple accelerators.

What is mixed-precision training?

Mixed-precision training uses lower-precision numerical formats for many calculations while retaining higher precision where needed. It improves speed and reduces memory usage.

Can a model memorize its pre-training data?

Yes. Models can memorize some sequences, especially duplicated, unique, or frequently repeated content. Data deduplication and memorization testing help reduce this risk.

Does pre-training guarantee factual accuracy?

No. Pre-training optimizes token prediction rather than factual verification. A model can generate a plausible but incorrect answer when it lacks reliable information.

What is catastrophic forgetting?

Catastrophic forgetting occurs when additional training on a narrow dataset weakens previously learned general capabilities. Mixing general data with specialized data can reduce this problem.

What happens after pre-training?

After pre-training, the model may undergo instruction tuning, supervised fine-tuning, preference optimization, reinforcement learning, safety evaluation, red-team testing, tool integration, retrieval integration, and application-specific deployment.