How no-magic-ai/no-magic works
Purpose and Execution Model
no-magic is an educational Python repository of dependency-free, standalone AI algorithm implementations. Its primary executable entry points are the individual micro*.py scripts; each owns its data setup, training loop, and inference/demo path and can run directly with Python 3.10+ on CPU. There is deliberately no shared application runtime, package, or internal import graph.
Grounding: 01-foundations/microgpt.py, 02-alignment/microdpo.py, 03-systems/microflash.py.
Four-Tier Conceptual Architecture
The repository is organized as a learning dependency progression rather than a deployable system:
1. Foundations establish representations, model architectures, optimization, and generative learning.
2. Alignment adds adaptation, regularization, preference optimization, and RL objectives.
3. Systems isolates serving/performance/storage primitives.
4. Agents demonstrates decision, search, memory, and tool-use loops.
These boundaries are conceptual only: scripts remain independently runnable.
Grounding: 01-foundations/README.md, 02-alignment/README.md, 03-systems/README.md, 04-agents/README.md.
Foundation Model and Data Stages
Foundation scripts expose end-to-end ML lifecycles: transform inputs into representations, perform forward computation, calculate loss, update parameters, then generate or predict. Key families include text tokenization/embeddings and language models, recurrent and attention models, vision models, retrieval augmentation, and generative models.
Representative flows include tokenizer → token IDs → autoregressive prediction in microgpt, embedding/retrieval → augmented context → generation in microrag, and image/latent/noise transformations in vision and generative scripts.
Grounding: 01-foundations/microtokenizer.py, 01-foundations/microgpt.py, 01-foundations/microrag.py.
Alignment and Adaptation Stage
Alignment scripts operate conceptually on pretrained-policy/model parameters and training signals. They illustrate efficient parameter adaptation through low-rank adapters and quantized bases; supervised preference optimization; and policy-gradient variants using rewards, clipping, or group-relative comparisons. Regularization and routing mechanisms are separately demonstrated.
Important boundary: these are pedagogical implementations, not reusable training extensions to foundation scripts.
Grounding: 02-alignment/microlora.py, 02-alignment/microqlora.py, 02-alignment/microdpo.py.
Inference and Systems Primitives
Systems scripts decompose model-serving constraints into independent mechanisms: attention computation and positional encoding, KV-cache storage/paging, memory-efficient attention/checkpointing, quantization, decoding, parallel execution, retrieval, and state-space alternatives. The primary control flow is typically model state/input → optimized representation or execution strategy → equivalent/accelerated output, with explicit memory/compute tradeoffs.
Grounding: 03-systems/microattention.py, 03-systems/microkv.py, 03-systems/micropaged.py, 03-systems/microquant.py.
Agent and Planning Loops
Agent scripts model stateful decision cycles rather than conventional supervised training alone: select actions from observations, evaluate rewards or outcomes, update search/bandit/memory state, and repeat. ReAct specifically represents thought/action/observation sequencing; MCTS and minimax implement tree-based planning; memory models expose read/write state.
Grounding: 04-agents/microreact.py, 04-agents/micromcts.py, 04-agents/micromemory.py.
Content, Validation, and Distribution Boundaries
Supporting content turns scripts into a curriculum: conceptual exercises map to algorithms, CSV decks generate Anki material, and an EPUB pipeline aggregates source. Catalog and verification automation are repository-level quality/discovery infrastructure, not runtime dependencies. Scripts may download small datasets on first run; otherwise the project uses only the Python standard library, CPU, and local execution. External visualization assets are hosted/rendered by the separate no-magic-viz project using Manim; releases distribute videos and prebuilt EPUBs.
Grounding: challenges/README.md, resources/flashcards/generate_anki.py, scripts/generate-epub.sh, scripts/verify.py, .github/workflows/verify.yml.
Main entry points: README.md Conceptual Challenges · microgpt.py Autoregressive LM · micropaged.py Paged KV Storage · microtokenizer.py Tokenizer
Components 22
01 Foundations
Independent scripts teaching core ML lifecycles.
- Tokenizer · Turns text into token IDs.
01-foundations/microtokenizer.py - Autoregressive LM · Owns text setup, training, and generation.
01-foundations/microgpt.py - Retrieval-Augmented Generation · Retrieves context before generation.
01-foundations/microrag.py - Representation → Train → Predict · Shared conceptual lifecycle, not shared code.
02 Alignment
Standalone adaptation and preference-learning examples.
- LoRA Adaptation · Low-rank parameter adaptation.
02-alignment/microlora.py - Quantized LoRA · Quantized base with low-rank adapters.
02-alignment/microqlora.py - Direct Preference Optimization · Optimizes a policy from preference signals.
02-alignment/microdpo.py - Adaptation & Reward Signals · Preferences, rewards, regularization, and routing.
03 Systems
Independent serving and performance primitives.
- Attention Execution · Attention computation and positional encoding.
03-systems/microattention.py - KV Cache · Stores decoding state for inference.
03-systems/microkv.py - Paged KV Storage · Pages cache storage for serving.
03-systems/micropaged.py - Quantization · Trades numerical precision for efficiency.
03-systems/microquant.py - Memory / Compute Tradeoffs · Equivalent or accelerated execution strategies.
04 Agents
Stateful decision and planning loops.
- ReAct Loop · Sequences thought, action, and observation.
04-agents/microreact.py - MCTS Planning · Searches and updates a planning tree.
04-agents/micromcts.py - Agent Memory · Reads and writes persistent decision state.
04-agents/micromemory.py - Observe → Act → Evaluate → Update · Stateful agent control cycle.
Curriculum & Distribution
Learning content and repository automation.
- Conceptual Challenges · Exercises map concepts to algorithms.
challenges/README.md - Anki Deck Generator · Generates flashcards from CSV decks.
resources/flashcards/generate_anki.py - EPUB Builder · Aggregates source into an EPUB.
scripts/generate-epub.sh - Repository Verification · Catalog and validation checks.
scripts/verify.py - Verification CI · Runs repository verification.
.github/workflows/verify.yml
Connections 20
- Tokenizer → Autoregressive LM: token IDs · Representative text-generation flow.
- Autoregressive LM → Representation → Train → Predict: demonstrates
- Retrieval-Augmented Generation → Representation → Train → Predict: demonstrates
- Representation → Train → Predict → Adaptation & Reward Signals: conceptual progression · No reusable training dependency.
- LoRA Adaptation → Adaptation & Reward Signals: demonstrates
- Quantized LoRA → Adaptation & Reward Signals: demonstrates
- Direct Preference Optimization → Adaptation & Reward Signals: uses preferences
- Representation → Train → Predict → Memory / Compute Tradeoffs: conceptual progression · Systems isolates execution concerns.
- Attention Execution → Memory / Compute Tradeoffs: optimizes
- KV Cache → Paged KV Storage: paged storage
- Paged KV Storage → Memory / Compute Tradeoffs: reduces memory pressure
- Quantization → Memory / Compute Tradeoffs: precision tradeoff
- Memory / Compute Tradeoffs → Observe → Act → Evaluate → Update: conceptual progression · Agent examples remain independent scripts.
- ReAct Loop → Observe → Act → Evaluate → Update: implements
- MCTS Planning → Observe → Act → Evaluate → Update: implements
- Agent Memory → Observe → Act → Evaluate → Update: state read/write
- Conceptual Challenges → Representation → Train → Predict: exercises concepts
- Anki Deck Generator → Conceptual Challenges: study material
- EPUB Builder → Conceptual Challenges: packages curriculum
- Repository Verification → Verification CI: executed by