Pular para conteúdo

01 - Foundations: Large Language Models

Estimated time to read: 5 minutes

Welcome to the Foundations of Large Language Models module. This module explores the transition from mathematical operations to emergent AI phenomena, focusing on subword segmentation, vector spaces, and latent manifold representations.


🧭 Module Roadmap & Laboratories

To keep the material focused and structured, we have divided this module into four distinct, comprehensive laboratories:

1. 📊 LAB-01: Tokenization Math of Compression

Benchmarks BPE vs WordPiece tokenizers, analyzes the character footprint of structured data (JSON), and quantifies the multilingual "Token Tax" on non-English text.

Explores the Manifold Hypothesis in high-dimensional vector spaces, implements Cosine Similarity manually, and visualizes word clusters in 2D and 3D.

  • Core concepts: metric spaces, cosine vs. Euclidean distances, PCA, t-SNE, semantic search indexing.
  • Interactive Playground: embeddings_playground.ipynb

3. 🧠 LAB-03: Attention from Scratch: PyTorch Linear Algebra

Implements scaled dot-product attention and multi-head attention from scratch using PyTorch linear algebra. Computes bidirectional vs. causal masked matrices.

  • Core concepts: Scaled Dot-Product, Multi-Head Attention, causal masking, KV projection.
  • Interactive Playground: attention_playground.ipynb

4. 🎲 LAB-04: Inference Math: Sampling, Quantization & Loss Analysis

Implements autoregressive token sampling strategies (Temperature, Top-k, Top-p), evaluates Cross-Entropy Loss & Perplexity, and quantifies GPU VRAM & KV-Cache memory footprint across FP16, INT8, and INT4 quantization schemes.

  • Core concepts: Temperature scaling, Top-k/Top-p Nucleus sampling, Perplexity, Int8/Int4 quantization math, KV Cache memory overhead.
  • Interactive Playground: inference_playground.ipynb

🛠️ Resources & References

Below are the academic references and technical guides used in this module, styled in a technical post-card format:

📄 Academic & Seminal Papers

📖 Technical References & Guides

Hugging Face Tokenizers Guide

Hugging Face Team · 2020-03-10
#tokenization #tooling #guide

Practical implementation details, benchmarks, and algorithms for BPE, WordPiece, and SentencePiece tokenizers.

Ollama & LiteLLM Integration Docs

LiteLLM Team · 2023-10-01
#local-inference #apis #tooling

Guide on running local LLM inference engines and wrapping them with OpenAI-compatible routing for development fallbacks.

The Manifold Hypothesis (Wikipedia)

Wikipedia & Community · Mathematical Concept
#math #topology #manifold-hypothesis #reference

Deep dive into why high-dimensional data distributions tend to concentrate near lower-dimensional, non-linear manifolds, which forms the mathematical foundation of neural embeddings.

Principal Component Analysis (PCA)

Karl Pearson · 1901-07-01
#linear-algebra #dimensionality-reduction #pca #reference

Historical paper and mathematical proof detailing the process of projecting high-dimensional points onto orthogonal eigenvectors to maximize variance.

Cosine Similarity & Vector Spaces

Wikipedia & Community · Linear Algebra Metric
#linear-algebra #metric-space #cosine-similarity #reference

Overview of cosine similarity, its mathematical formulation, and its application in high-dimensional information retrieval and metric vector spaces.

The Curious Case of Neural Text Degeneration

Holtzman et al. · 2019-04-22
#sampling #nucleus-sampling #decoding #paper

Landmark paper introducing Nucleus (Top-p) sampling, demonstrating how dynamic probability truncation prevents repetitive loops in neural text generation.

LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale

Dettmers et al. · 2022-08-15
#quantization #int8 #vram-optimization #paper

Introduces vector-wise quantization and outlier feature extraction enabling 8-bit transformer matrix multiplication with zero performance degradation.