Appearance
Module 1.1: The Non-Deterministic Engine & Parameter Knobs β
Curriculum Alignment: [
docs/plan/01_phase1_engine_and_prompting.md](file:///Users/huychau/Documents/working/training/ai/docs/plan/01_phase1_engine_and_prompting.md)
Topic Scope: Autoregressive Next-Token Mechanics, Spiky Intelligence, Sampling Knobs (, Top- , Penalties)
Level: Senior Architect / Advanced AI Engineering
1. Architectural Mental Model: LLMs as Stochastic Microservices β
In traditional distributed systems, microservices are deterministic: for a given idempotent request
An Autoregressive Large Language Model (LLM), however, is a stochastic next-token predictor:
Where
The "Spiky Intelligence" Phenomenon β
As software architects, we expect systems to exhibit continuous capability curves (e.g., if a system can solve complex graph optimization, it can surely perform elementary string reversing). LLMs break this intuition due to Byte-Pair Encoding (BPE) tokenization and non-symbolic weight representation:
- Why Tokenization Causes Spikes: An LLM does not see characters or bytes; it sees token IDs. The word
"strawberry"might be tokenized as["str", "aw", "berry"]. Asking a frontier model how many'r's are in"strawberry"requires it to reconstruct character boundaries from abstract vector embeddings, leading to elementary failures despite solving graduate-level distributed consensus problems. - Architecture Takeaway: Never rely on an LLM for operations that a deterministic compiler, regex, or Python standard library function can perform in
time. Treat the model as a probabilistic semantic reasoning engine, wrapped by deterministic harness middleware.
2. Mathematical Mechanics of the Engine Knobs β
Before an LLM emits a token, the final linear layer produces a raw vector of unnormalized scores called logits
1. Temperature ( ) β
Temperature controls the "entropy" or spread of the probability distribution:
(Greedy Decoding / Argmax): The model deterministically selects the highest-probability token. Use case: Structured JSON extraction, code compilation, database query generation, classification. (Neutral Softmax): The probability distribution reflects the true pre-trained distribution. (High Entropy / "Creative"): The logit differences are flattened, raising the probability of low-ranked tokens. Leads to hallucinations, diversity, and linguistic drift.
2. Top- (Nucleus Sampling) β
Instead of considering all
Tokens outside
- Top-
: Restricts the model to the top probability mass. If the top 2 tokens already account for mass, only those 2 tokens are considered. - Top-
: Extremely narrow selection (almost greedy).
IMPORTANT
Architectural Rule: Change either Temperature or Top-P, but rarely both simultaneously in production pipelines. Altering both simultaneously introduces uncalibrated sampling variance.
3. Presence and Frequency Penalties β
These modify logits at runtime based on the historical token counts
- Frequency Penalty (
): Scaled by token frequency count. Discourages repeating specific words/phrases. - Presence Penalty (
): Flat penalty applied if a token has appeared at least once. Encourages topical exploration.
3. Python Simulation: Logit Manipulation & Sampling β
The following script simulates the exact mathematical pipeline inside an LLM's decoding loop:
python
"""
Logit Sampling Simulator
Simulates Softmax Temperature scaling, Top-P nucleus truncation, and categorical sampling.
"""
from typing import Sequence
import numpy as np
import numpy.typing as npt
def softmax(logits: npt.NDArray[np.float64], temperature: float) -> npt.NDArray[np.float64]:
"""Applies numerically stable softmax with temperature scaling."""
if temperature <= 1e-5:
# Emulate greedy argmax
probs = np.zeros_like(logits)
probs[np.argmax(logits)] = 1.0
return probs
scaled_logits = logits / temperature
# Subtract max for numerical stability (avoids overflow in exp)
shifted = scaled_logits - np.max(scaled_logits)
exp_logits = np.exp(shifted)
return exp_logits / np.sum(exp_logits)
def apply_top_p(probs: npt.NDArray[np.float64], top_p: float) -> npt.NDArray[np.float64]:
"""Truncates probabilities to the nucleus mass defined by top_p."""
sorted_indices = np.argsort(probs)[::-1]
sorted_probs = probs[sorted_indices]
cumulative_probs = np.cumsum(sorted_probs)
# Identify indices to remove (tokens beyond the threshold)
cutoff_mask = cumulative_probs > top_p
# Keep at least the highest probability token
cutoff_mask[0] = False
truncated_probs = sorted_probs.copy()
truncated_probs[cutoff_mask] = 0.0
# Renormalize
return truncated_probs / np.sum(truncated_probs)
def simulate_sampling(
vocabulary: Sequence[str],
logits: list[float],
temperature: float,
top_p: float = 1.0,
) -> dict[str, float]:
arr_logits = np.array(logits, dtype=np.float64)
probs = softmax(arr_logits, temperature)
if top_p < 1.0:
probs = apply_top_p(probs, top_p)
return {token: round(float(prob), 4) for token, prob in zip(vocabulary, probs)}
# Execution Example
if __name__ == "__main__":
vocab = ["microservice", "monolith", "serverless", "event-driven", "batch"]
mock_logits = [5.2, 4.8, 2.1, 3.5, 1.0]
print("--- Greedy (T = 0.01) ---")
print(simulate_sampling(vocab, mock_logits, temperature=0.01))
print("\n--- Balanced (T = 0.7) ---")
print(simulate_sampling(vocab, mock_logits, temperature=0.7))
print("\n--- High Entropy (T = 1.8) ---")
print(simulate_sampling(vocab, mock_logits, temperature=1.8))
print("\n--- Nucleus Filtering (T = 1.0, Top-P = 0.75) ---")
print(simulate_sampling(vocab, mock_logits, temperature=1.0, top_p=0.75))4. Curated Reading & Canonical References β
Review these primary sources specified in the curriculum plan:
| Resource | Canonical Reference & Link | Specific Focus Areas |
|---|---|---|
| Primary Curriculum Book | AI Agents and Applications (Google Drive) | Part 1, Chapters 1β2: Foundations of LLM engine behavior, stochastic generation, and agent primitives. |
| Foundational Course | DeepLearning.AI: Generative AI for Everyone | Intuitive mental models for autoregressive token mechanics and enterprise capability boundaries. |
| Industry Standard Guide | Prompt Engineering Guide β Settings | Deep dive into Temperature, Top_P, and token sampling mechanics. |
| Interactive Playgrounds | OpenAI Playground & Hugging Face Flan-T5 | Comparative parameter experimentation across closed (GPT) and open-source (Flan-T5) architectures. |
| Government Security Standard | NIST AI 100-2e2025: Adversarial Machine Learning | Section 2.1: Taxonomy of evasion, poisoning, and stochastic variance vulnerabilities. |
5. Hands-on Engineering Exercises β
Exercise 1.1: Temperature Variance Profiler β
- Task: Write a Python script using
pytestthat submits the same multi-step logic problem (e.g.,"Reverse the words in: 'microservices domain driven design' and count the total vowels") to an LLM across 20 iterations atand 20 iterations at . - Verification Metric: Calculate:
- Percentage of identical responses at
(should be ). - Entropy and output variance at
.
- Percentage of identical responses at
- Deliverable Link: This builds the foundation for your Parameter Stability Report required in Phase 1.