Chapter 8
Large Language Models and Generative Processes
Advance Diploma in Artificial Intelligence and Machine Learning
Natural Language Processing & Generative AI
•
Advance Diploma
Large Language Models and Generative Processes
Introduction
Large language models (LLMs) are neural language systems trained to model sequences of tokens. Their central task is deceptively simple: given a context, estimate the probability of the next token. Yet large-scale training, transformer architectures, instruction tuning, human preference optimisation, and sophisticated inference procedures enable these models to translate, summarise, write software, answer questions, and interact conversationally.
An LLM is not a database in the ordinary sense. It stores statistical regularities in distributed parameters and generates outputs by repeatedly sampling or selecting tokens. Consequently, fluency does not guarantee truth. Understanding the complete lifecycle—from data collection and pre-training to deployment, monitoring, and retirement—is essential for responsible engineering.
Learning Objectives
By the end of this chapter, students should be able to:
Explain the transformer-based architecture and scaling behaviour of LLMs.
Distinguish pre-training, instruction tuning, preference alignment, and inference.
Analyse the effect of context windows and positional representations.
Compare decoding strategies and select an appropriate method for an application.
Explain why hallucinations occur and design mitigation and evaluation procedures.
Describe the technical, operational, ethical, and governance implications of deploying LLMs.
1. The Language-Modelling Problem
Let a tokenised sequence be x1, x2, …, xT, where T is the sequence length. An autoregressive language model factorises the probability of the sequence as:
P(x₁, x₂, …, xT) = ∏t=1T P(xt | x₁, …, xt−1)
Here, P(xt | x<t) is the probability assigned to token xt given all preceding tokens. The model is trained by minimising negative log-likelihood, commonly called cross-entropy loss:
L(θ) = −(1/T) ∑t=1T log P_θ(xt | x<t)
θ denotes the model parameters. The logarithm is normally natural logarithm, and the loss is measured in nats per token; with logarithm base 2 it is measured in bits per token. This objective assumes that training examples are representative of the intended distribution and that token order is meaningful. It does not directly require the model to be truthful, helpful, or safe.
During generation, the model produces a probability distribution over its vocabulary, appends a selected token, and repeats the process. A stopping token, a maximum length, or an application-defined rule terminates generation.
Tokens and representations
Tokens may be words, subwords, characters, punctuation marks, or byte sequences. Subword tokenisation balances vocabulary size and the ability to represent rare words. Token count is operationally important: billing, memory use, context capacity, and latency are often measured in tokens rather than words.
Each token is mapped to a vector called an embedding. Transformer layers repeatedly transform these vectors using attention and feed-forward networks. The final hidden representation is projected to vocabulary logits, which are converted into probabilities.
2. Transformer Foundations
The transformer uses self-attention to allow each position to integrate information from other positions. For a sequence represented by matrices Q, K, and V, attention is:
Attention(Q, K, V) = softmax(QKᵀ / √dk)V
dk is the dimension of each key vector. The square-root scaling prevents dot products from becoming excessively large, which would make the softmax distribution too concentrated and gradients less useful. In a decoder-only, autoregressive transformer, a causal mask prevents position t from attending to future positions.
Multi-head attention runs several attention operations with different learned projections. Some heads may specialise in syntactic relationships, positional patterns, or long-range associations, although such interpretations must be established empirically rather than assumed. Feed-forward sublayers apply nonlinear transformations independently to each position. Residual connections and normalisation improve optimisation in deep networks.
A simplified decoder layer therefore contains:
token and positional representation;
masked multi-head self-attention;
residual connection and normalisation;
position-wise feed-forward transformation;
another residual connection and normalisation.
Modern systems may add grouped-query attention, mixture-of-experts routing, improved normalisation, rotary or other positional representations, and specialised hardware kernels. These design choices affect memory, throughput, and quality but do not change the basic autoregressive principle.
3. Scaling Laws and Model Capacity
Scaling refers to increasing model parameters, training tokens, computation, or data quality. Larger models often achieve lower average loss, but gains are not automatic. They depend on sufficient data, optimisation quality, architecture, and evaluation validity.
A commonly used empirical relationship expresses loss approximately as a power-law function of model size, data, and compute over a limited regime:
L ≈ L∞ + A N^(−α) + B D^(−β) + C C^(−γ)
Here, L is observed loss; L∞ is an irreducible or asymptotic term; N is parameter count; D is the number of training tokens; C is compute; and A, B, C (when the symbols are distinguished in implementation), α, β, and γ are empirically fitted constants. This is not a universal law or a guarantee of capability. It is an approximation valid within a measured scaling regime.
The compute required for a dense transformer is influenced by parameter count, token count, sequence length, and architecture. In broad terms, training compute is often approximated as proportional to parameter count multiplied by training tokens, though exact estimates depend on attention implementation, sparsity, activation recomputation, precision, and hardware utilisation.
Scaling produces both predictable and emergent-looking effects. Some capabilities improve smoothly; others appear suddenly because a benchmark uses a thresholded metric or because several weak abilities combine. Claims of emergence should therefore be tested with multiple metrics and carefully controlled baselines.
Scaling also creates risks: data duplication, environmental cost, concentration of infrastructure, memorisation of personal information, and increased capacity to generate harmful content. More parameters do not automatically produce better reasoning, factuality, or social judgement.
4. The Training and Inference Lifecycle
Lifecycle of a large language model from data to monitored deployment
Figure: Lifecycle of a large language model from data to monitored deployment
Figure: The lifecycle of an LLM is iterative rather than a one-time training event.
Data preparation
Data pipelines acquire, license, filter, deduplicate, classify, and document text. Quality is more important than raw volume. Filtering may address malware, personal data, illegal content, low-quality boilerplate, and benchmark contamination. However, aggressive filtering can remove valuable minority-language or specialised material. Dataset documentation should record provenance, consent or licensing basis, transformations, known gaps, and intended use.
Pre-training
Pre-training uses self-supervised examples created from raw sequences. Distributed training divides computation across data, tensor, pipeline, or expert dimensions. Engineers monitor loss, throughput, numerical stability, gradient norms, hardware failures, and validation performance. Checkpoints are essential for recovery and reproducibility.
Instruction tuning
A base model predicts continuations but may not reliably follow requests. Supervised fine-tuning (SFT) trains on prompt–response examples written or curated to demonstrate desired behaviour. It teaches formats, task following, refusal styles, and conversational conventions. SFT data quality, diversity, and annotation consistency are crucial; narrow examples can cause overfitting or undesirable stylistic bias.
Preference alignment
Alignment methods optimise responses according to preferences about helpfulness, harmlessness, truthfulness, or style. In reinforcement learning from human feedback (RLHF), a reward model is trained from comparisons, and the language model is optimised against that reward while constrained to remain near a reference policy. Direct preference optimisation (DPO) and related methods use preference pairs without requiring the same explicit reinforcement-learning loop. These methods encode human and institutional values; they do not solve value disagreement or factual verification.
A useful distinction is:
Stage
Main objective
Typical weakness
Pre-training
Predict tokens
Fluent but not reliably task-oriented
SFT
Follow instructions
Can imitate annotation errors
Preference optimisation
Prefer selected behaviours
Reward hacking or over-refusal
Retrieval/tool integration
Ground or act on external information
Tool errors, access and security risks
Inference
Produce a response under constraints
Sampling variability and hallucination
5. Context Windows and Long-Context Behaviour
The context window is the maximum token sequence processed in one request, including system instructions, user input, retrieved documents, conversation history, and generated output. A larger window permits more information, but it increases computational and financial cost and does not ensure that every included fact receives equal attention.
In standard full self-attention, interactions among n tokens have approximately quadratic pair count:
number of token pairs = n²
This describes attention interaction scaling, not necessarily total wall-clock time, because kernels, sparsity, batching, and hardware change practical performance. During autoregressive generation, key–value (KV) caching avoids recomputing earlier states but consumes memory proportional to layers, heads, head dimension, sequence length, and numerical precision.
Long-context systems may use sliding windows, sparse attention, recurrence, retrieval, compression, or position interpolation. Evaluation must test retrieval at different locations, interference among documents, instruction priority, and performance near the maximum length. The “lost in the middle” phenomenon describes cases where information placed between the beginning and end of a long prompt is used less reliably.
Professional practice is to keep prompts compact, structure evidence with headings and citations, place critical constraints clearly, and retrieve only relevant passages. Context expansion should not replace information architecture.
6. Decoding Strategies
The model produces logits zi for each vocabulary token i. Temperature modifies the distribution:
PT(i) = exp(zi / T) / ∑j exp(zj / T)
T is a positive temperature. T = 1 leaves the distribution unchanged; lower values concentrate probability around high-logit tokens, while higher values increase diversity. Temperature does not add knowledge or guarantee creativity.
Greedy decoding selects the highest-probability token at every step. It is deterministic and often suitable for short factual formats, but it can produce repetitive or locally optimal text.
Beam search maintains several partial sequences and expands them according to cumulative score, sometimes with length normalisation. It is useful in some structured generation and translation settings, but for open-ended dialogue it can produce bland or repetitive outputs.
Top-k sampling restricts sampling to the k most probable tokens. Top-p (nucleus) sampling chooses the smallest candidate set whose cumulative probability reaches p, then samples from that set. Top-p adapts the candidate-set size to uncertainty. Typical settings must be validated empirically; there is no universally optimal value.
Typical, contrastive, and constrained decoding are additional approaches. Constrained decoding is appropriate when outputs must conform to a grammar, schema, or list of permitted values. Structured decoding improves syntactic validity but cannot guarantee semantic correctness.
Decoding should match the task. Deterministic settings support reproducible extraction; controlled sampling supports ideation; constrained decoding supports machine-readable outputs. Always record model version, prompt, decoding parameters, tools, and random seed where reproducibility matters.
7. Hallucination, Reliability, and Grounding
A hallucination is an output claim that is unsupported, fabricated, or inconsistent with the relevant evidence. It may arise from incomplete training data, ambiguous prompts, distribution shift, next-token optimisation, retrieval failures, context overload, or pressure to answer every question. Hallucinations are not simply “random mistakes”; they are often highly fluent predictions.
Important forms include:
fabricated citations, quotations, laws, or references;
incorrect dates, calculations, names, or causal claims;
unsupported synthesis across documents;
false claims about actions, tool use, or source consultation;
omission of uncertainty and inappropriate confidence.
Mitigation is layered. Prompting can request evidence, uncertainty, and explicit refusal when information is insufficient. Retrieval-augmented generation (RAG) supplies relevant documents, but retrieval quality, chunking, ranking, access control, and citation mapping determine its value. Tools can perform calculations, query databases, or validate schemas. Post-generation checks can compare claims with sources, run tests, or require human approval.
A useful evaluation distinguishes factuality from groundedness. Factuality asks whether a claim is true in the world; groundedness asks whether it is supported by the supplied evidence. A response may be grounded in an erroneous document, or factually correct without citing the available source.
Evaluation sets should include ordinary cases, adversarial prompts, ambiguous requests, rare entities, multilingual inputs, long contexts, and high-impact scenarios. Metrics may include exact match, task success, citation precision, citation recall, calibration, refusal appropriateness, latency, cost, and harmful-output rates. Aggregate averages can hide severe subgroup failures.
8. Applied Case: A University Policy Assistant
The following is an illustrative case.
Background. A university wants an assistant that answers questions about examination rules, appeals, accessibility arrangements, and deadlines. Incorrect advice could affect student rights.
Problem. A general LLM produces plausible answers but occasionally cites obsolete policies and invents deadlines. Students also ask questions outside the university’s jurisdiction.
Analysis. The risk is not solved by lowering temperature. The system needs authoritative, versioned documents, access controls, provenance, and escalation. Policy language may be conditional, so the assistant must preserve exceptions rather than compressing them into simplistic rules.
Approach. The university creates a document registry with effective dates and owners. A retrieval layer filters by policy status and user context, retrieves relevant passages, and attaches document identifiers. The prompt requires the model to answer only from retrieved evidence, quote the applicable clause, identify the policy date, and state when evidence is insufficient. A deterministic calculator handles date arithmetic. High-impact cases—appeals, disability accommodations, disciplinary matters, and complaints—are routed to trained staff. Evaluation uses historical questions, synthetic edge cases reviewed by policy experts, and red-team tests for prompt injection in uploaded documents.
Outcome. The system reduces unsupported answers and improves traceability, but it does not replace institutional decision-makers. Monitoring detects retrieval failures, outdated documents, unequal performance across language groups, and attempts to exfiltrate confidential records.
Learning points. Grounding is an end-to-end property; citations alone are not proof of correctness. Domain ownership, document governance, escalation, and continuous evaluation are as important as model selection.
9. Governance, Ethics, and Professional Practice
LLM deployment raises questions of copyright, privacy, accountability, safety, accessibility, labour, and environmental impact. Organisations should identify the legal basis for data processing, minimise personal information, restrict retention, and prevent confidential prompts from entering unauthorised training pipelines. They should assess licences and provenance for both training and retrieval data.
A risk-based governance process should document intended use, prohibited use, affected stakeholders, foreseeable harms, human oversight, evaluation results, incident procedures, and change control. High-impact applications require stronger validation and meaningful human review. Human oversight is ineffective when reviewers lack time, authority, expertise, or the ability to challenge the system.
Security threats include prompt injection, data poisoning, sensitive-information disclosure, insecure tool use, model extraction, and supply-chain compromise. External documents must be treated as untrusted input. Tools should use least privilege, explicit schemas, sandboxing, authentication, rate limits, and audit logs.
Energy and hardware impacts should be measured across training and inference. Smaller specialised models, quantisation, batching, caching, and efficient retrieval may reduce cost without sacrificing the required capability. The appropriate model is the smallest system that meets validated requirements.
10. Contemporary Directions and Best Practices
Current research explores multimodal models, mixture-of-experts architectures, small high-quality models, synthetic-data refinement, test-time reasoning, agents, tool use, continual adaptation, and mechanistic interpretability. These directions introduce new failure modes: tool-mediated harm, coordination errors, data feedback loops, and more difficult evaluation.
A robust engineering workflow is:
Define the task, users, failure costs, and acceptance criteria.
Establish a clean, versioned evaluation set before optimisation.
Begin with retrieval, tools, prompts, or a smaller model before fine-tuning.
Separate factuality, helpfulness, safety, latency, and cost metrics.
Test distribution shift, adversarial inputs, subgroup performance, and long context.
Log model version, prompt version, evidence, tools, outputs, and decisions while protecting privacy.
Introduce human escalation for consequential cases.
Monitor after deployment and reassess after data, model, policy, or interface changes.
Chapter Summary
Large language models estimate the next-token distribution over token sequences, usually using decoder-only transformers with masked self-attention. Pre-training develops broad statistical competence, while supervised instruction tuning and preference optimisation shape task following and behavioural priorities. Scaling model size, data, and compute can improve loss and capability, but it has costs and does not guarantee truthfulness.
Context windows define the information available during inference, yet longer context can increase cost and create retrieval and attention problems. Decoding methods—including greedy decoding, beam search, temperature, top-k, top-p, and constrained decoding—control the trade-off between determinism, diversity, and validity. Hallucination is a central reliability problem requiring layered mitigation through high-quality retrieval, tools, verification, calibrated uncertainty, and human oversight.
The LLM lifecycle includes data governance, pre-training, post-training, deployment, monitoring, evaluation, and controlled improvement. Responsible practice treats the model as one component of a socio-technical system whose safety depends on evidence, access control, policy ownership, security, transparency, and accountability.
Key Terms
Token: A unit produced by a tokenizer and processed by the model.
Autoregressive model: A model that predicts each token conditioned on preceding tokens.
Cross-entropy loss: The average negative log probability assigned to target tokens.
Transformer: A neural architecture based primarily on attention and feed-forward layers.
Self-attention: A mechanism allowing sequence positions to combine information from other positions.
Causal mask: A mask preventing a token from attending to future tokens.
Scaling law: An empirical relationship between performance and resources such as parameters, data, or compute.
Pre-training: Broad self-supervised training on large collections of sequences.
Instruction tuning: Supervised fine-tuning on examples of desired task-following behaviour.
Alignment: Methods intended to make model behaviour conform to specified human or institutional preferences.
RLHF: Reinforcement learning from human feedback.
Preference optimisation: Training using comparisons or rankings of alternative outputs.
Context window: The maximum token sequence available to a model in one request.
KV cache: Stored attention keys and values used to accelerate autoregressive generation.
Temperature: A parameter controlling the concentration of a probability distribution during decoding.
Top-p sampling: Sampling from the smallest token set whose cumulative probability reaches a selected threshold.
Hallucination: An unsupported, fabricated, or incorrect generated claim.
Grounding: Connecting an output to relevant external evidence or data.
RAG: Retrieval-augmented generation, in which retrieved information supports generation.
Prompt injection: An attempt to manipulate instructions through user or external content.
Review Questions
Short Answer Questions
What does an autoregressive language model estimate?
Why is the factor √dk used in scaled dot-product attention?
Distinguish pre-training from instruction tuning.
What is the function of a context window?
Define hallucination and give one example.
Long Answer Questions
Explain the lifecycle of an LLM from data curation through post-deployment monitoring.
Compare greedy decoding, beam search, top-k sampling, and top-p sampling.
Discuss how scaling can improve capability while creating new technical and social risks.
Explain how retrieval-augmented generation can reduce, but not eliminate, hallucinations.
Analyse the roles of preference data, reward models, and human oversight in alignment.
Analytical / Application Questions
Design an evaluation plan for an LLM used to summarise clinical documents. Identify risks, metrics, evidence requirements, and escalation rules.
A chatbot must return valid JSON records for a database. Which decoding and validation methods would you use, and why?
A long-context assistant frequently ignores policy clauses placed in the middle of uploaded documents. Diagnose possible causes and propose an improved architecture and test programme.
Multiple Choice Questions with Answers
Question 1
Which objective is directly optimised during standard autoregressive pre-training?
A. Maximising human preference scores
B. Predicting the next token given preceding tokens
C. Minimising inference latency
D. Retrieving documents from a database
Correct Answer: B. Predicting the next token given preceding tokens
Question 2
In scaled dot-product attention, why are attention scores divided by √dk?
A. To remove positional information
B. To reduce vocabulary size
C. To control score magnitude and support stable softmax gradients
D. To guarantee factual outputs
Correct Answer: C. To control score magnitude and support stable softmax gradients
Question 3
Which stage primarily teaches a base model to follow explicit user instructions?
A. Tokenisation
B. Instruction tuning
C. KV caching
D. Quantisation
Correct Answer: B. Instruction tuning
Question 4
What does lowering temperature generally do during decoding?
A. Makes the probability distribution more concentrated
B. Increases the context-window size
C. Adds external evidence
D. Guarantees grammatical correctness
Correct Answer: A. Makes the probability distribution more concentrated
Question 5
What is the main purpose of a causal mask in a decoder-only transformer?
A. To hide unsafe words
B. To prevent attention to future tokens
C. To compress the vocabulary
D. To select retrieved documents
Correct Answer: B. To prevent attention to future tokens
Question 6
Which statement best describes top-p sampling?
A. It always selects the single most probable token
B. It samples only from a fixed number of tokens
C. It samples from the smallest set whose cumulative probability reaches a threshold
D. It searches all possible complete sequences deterministically
Correct Answer: C. It samples from the smallest set whose cumulative probability reaches a threshold
Question 7
Which is an example of a hallucination?
A. A response that quotes a supplied document accurately
B. A response that refuses an unsupported request
C. A fabricated legal citation presented as genuine
D. A response generated with a fixed random seed
Correct Answer: C. A fabricated legal citation presented as genuine
Question 8
Why does retrieval-augmented generation not guarantee factual answers?
A. Retrieval can return irrelevant, outdated, or incorrect evidence
B. Retrieval removes all context from the prompt
C. Retrieval prevents the model from generating text
D. Retrieval always increases temperature
Correct Answer: A. Retrieval can return irrelevant, outdated, or incorrect evidence
Question 9
Which practice best supports reproducibility of an LLM application?
A. Changing prompts after every request without recording them
B. Recording model, prompt, decoding, tool, and seed information
C. Using the largest available model regardless of task
D. Deleting evaluation data after deployment
Correct Answer: B. Recording model, prompt, decoding, tool, and seed information
Question 10
Which control is most appropriate when an LLM may affect student appeals or disability accommodations?
A. Allowing autonomous decisions with no audit trail
B. Using only a higher temperature
C. Human escalation and review by authorised domain experts
D. Removing policy dates from retrieved documents
Correct Answer: C. Human escalation and review by authorised domain experts
Natural Language Processing & Generative AI
Chapter 8
of 15