# no-magic-ai/no-magic architecture

> Architecture diagram of the GitHub repository no-magic-ai/no-magic, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/no-magic-ai/no-magic
- Repository: https://github.com/no-magic-ai/no-magic

## Overview

#### 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: [`challenges/README.md`](https://github.com/no-magic-ai/no-magic/blob/main/challenges/README.md) (Conceptual Challenges), [`01-foundations/microgpt.py`](https://github.com/no-magic-ai/no-magic/blob/main/01-foundations/microgpt.py) (Autoregressive LM), [`03-systems/micropaged.py`](https://github.com/no-magic-ai/no-magic/blob/main/03-systems/micropaged.py) (Paged KV Storage), [`01-foundations/microtokenizer.py`](https://github.com/no-magic-ai/no-magic/blob/main/01-foundations/microtokenizer.py) (Tokenizer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_foundations["01 Foundations"]
  node_tokenizer["Tokenizer<br/>standalone script<br/>[microtokenizer.py]"]
  node_gpt["Autoregressive LM<br/>standalone script<br/>[microgpt.py]"]
  node_rag["Retrieval-Augmented Generation<br/>standalone script<br/>[microrag.py]"]
  node_foundation_lifecycle{{"Representation → Train → Predict<br/>learning pattern"}}
end

subgraph group_alignment["02 Alignment"]
  node_adapters["LoRA Adaptation<br/>standalone script<br/>[microlora.py]"]
  node_qlora["Quantized LoRA<br/>standalone script<br/>[microqlora.py]"]
  node_dpo["Direct Preference Optimization<br/>standalone script<br/>[microdpo.py]"]
  node_alignment_signals{{"Adaptation &amp; Reward Signals<br/>learning pattern"}}
end

subgraph group_systems["03 Systems"]
  node_attention["Attention Execution<br/>standalone script<br/>[microattention.py]"]
  node_kv_cache[("KV Cache<br/>standalone script<br/>[microkv.py]")]
  node_paged_cache[("Paged KV Storage<br/>standalone script<br/>[micropaged.py]")]
  node_quantization["Quantization<br/>standalone script<br/>[microquant.py]"]
  node_optimized_inference{{"Memory / Compute Tradeoffs<br/>systems pattern"}}
end

subgraph group_agents["04 Agents"]
  node_react["ReAct Loop<br/>standalone script<br/>[microreact.py]"]
  node_mcts["MCTS Planning<br/>standalone script<br/>[micromcts.py]"]
  node_memory[("Agent Memory<br/>standalone script<br/>[micromemory.py]")]
  node_agent_loop{{"Observe → Act → Evaluate → Update<br/>decision pattern"}}
end

subgraph group_curriculum["Curriculum &amp; Distribution"]
  node_challenges["Conceptual Challenges<br/>curriculum content<br/>[README.md]"]
  node_anki["Anki Deck Generator<br/>content pipeline<br/>[generate_anki.py]"]
  node_epub["EPUB Builder<br/>distribution pipeline<br/>[generate-epub.sh]"]
  node_verification["Repository Verification<br/>quality automation<br/>[verify.py]"]
  node_verify_ci["Verification CI<br/>GitHub workflow<br/>[verify.yml]"]
end

node_tokenizer -->|"token IDs"| node_gpt
node_gpt -->|"demonstrates"| node_foundation_lifecycle
node_rag -->|"demonstrates"| node_foundation_lifecycle
node_foundation_lifecycle -.->|"conceptual progression"| node_alignment_signals
node_adapters -->|"demonstrates"| node_alignment_signals
node_qlora -->|"demonstrates"| node_alignment_signals
node_dpo -->|"uses preferences"| node_alignment_signals
node_foundation_lifecycle -.->|"conceptual progression"| node_optimized_inference
node_attention -->|"optimizes"| node_optimized_inference
node_kv_cache -->|"paged storage"| node_paged_cache
node_paged_cache -->|"reduces memory pressure"| node_optimized_inference
node_quantization -->|"precision tradeoff"| node_optimized_inference
node_optimized_inference -.->|"conceptual progression"| node_agent_loop
node_react -->|"implements"| node_agent_loop
node_mcts -->|"implements"| node_agent_loop
node_memory -->|"state read/write"| node_agent_loop
node_challenges -.->|"exercises concepts"| node_foundation_lifecycle
node_anki -.->|"study material"| node_challenges
node_epub -->|"packages curriculum"| node_challenges
node_verification -->|"executed by"| node_verify_ci

click node_tokenizer "https://github.com/no-magic-ai/no-magic/blob/main/01-foundations/microtokenizer.py"
click node_gpt "https://github.com/no-magic-ai/no-magic/blob/main/01-foundations/microgpt.py"
click node_rag "https://github.com/no-magic-ai/no-magic/blob/main/01-foundations/microrag.py"
click node_adapters "https://github.com/no-magic-ai/no-magic/blob/main/02-alignment/microlora.py"
click node_qlora "https://github.com/no-magic-ai/no-magic/blob/main/02-alignment/microqlora.py"
click node_dpo "https://github.com/no-magic-ai/no-magic/blob/main/02-alignment/microdpo.py"
click node_attention "https://github.com/no-magic-ai/no-magic/blob/main/03-systems/microattention.py"
click node_kv_cache "https://github.com/no-magic-ai/no-magic/blob/main/03-systems/microkv.py"
click node_paged_cache "https://github.com/no-magic-ai/no-magic/blob/main/03-systems/micropaged.py"
click node_quantization "https://github.com/no-magic-ai/no-magic/blob/main/03-systems/microquant.py"
click node_react "https://github.com/no-magic-ai/no-magic/blob/main/04-agents/microreact.py"
click node_mcts "https://github.com/no-magic-ai/no-magic/blob/main/04-agents/micromcts.py"
click node_memory "https://github.com/no-magic-ai/no-magic/blob/main/04-agents/micromemory.py"
click node_challenges "https://github.com/no-magic-ai/no-magic/blob/main/challenges/README.md"
click node_anki "https://github.com/no-magic-ai/no-magic/blob/main/resources/flashcards/generate_anki.py"
click node_epub "https://github.com/no-magic-ai/no-magic/blob/main/scripts/generate-epub.sh"
click node_verification "https://github.com/no-magic-ai/no-magic/blob/main/scripts/verify.py"
click node_verify_ci "https://github.com/no-magic-ai/no-magic/blob/main/.github/workflows/verify.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_tokenizer,node_gpt,node_rag,node_foundation_lifecycle toneBlue
class node_adapters,node_qlora,node_dpo,node_alignment_signals toneAmber
class node_attention,node_kv_cache,node_paged_cache,node_quantization,node_optimized_inference toneMint
class node_react,node_mcts,node_memory,node_agent_loop toneRose
class node_challenges,node_anki,node_epub,node_verification,node_verify_ci toneIndigo
```

## Components

### 01 Foundations

Independent scripts teaching core ML lifecycles.

- [Tokenizer](https://github.com/no-magic-ai/no-magic/blob/main/01-foundations/microtokenizer.py): standalone script · Turns text into token IDs. · `01-foundations/microtokenizer.py`
- [Autoregressive LM](https://github.com/no-magic-ai/no-magic/blob/main/01-foundations/microgpt.py): standalone script · Owns text setup, training, and generation. · `01-foundations/microgpt.py`
- [Retrieval-Augmented Generation](https://github.com/no-magic-ai/no-magic/blob/main/01-foundations/microrag.py): standalone script · Retrieves context before generation. · `01-foundations/microrag.py`
- **Representation → Train → Predict**: learning pattern · Shared conceptual lifecycle, not shared code.

### 02 Alignment

Standalone adaptation and preference-learning examples.

- [LoRA Adaptation](https://github.com/no-magic-ai/no-magic/blob/main/02-alignment/microlora.py): standalone script · Low-rank parameter adaptation. · `02-alignment/microlora.py`
- [Quantized LoRA](https://github.com/no-magic-ai/no-magic/blob/main/02-alignment/microqlora.py): standalone script · Quantized base with low-rank adapters. · `02-alignment/microqlora.py`
- [Direct Preference Optimization](https://github.com/no-magic-ai/no-magic/blob/main/02-alignment/microdpo.py): standalone script · Optimizes a policy from preference signals. · `02-alignment/microdpo.py`
- **Adaptation & Reward Signals**: learning pattern · Preferences, rewards, regularization, and routing.

### 03 Systems

Independent serving and performance primitives.

- [Attention Execution](https://github.com/no-magic-ai/no-magic/blob/main/03-systems/microattention.py): standalone script · Attention computation and positional encoding. · `03-systems/microattention.py`
- [KV Cache](https://github.com/no-magic-ai/no-magic/blob/main/03-systems/microkv.py): standalone script · Stores decoding state for inference. · `03-systems/microkv.py`
- [Paged KV Storage](https://github.com/no-magic-ai/no-magic/blob/main/03-systems/micropaged.py): standalone script · Pages cache storage for serving. · `03-systems/micropaged.py`
- [Quantization](https://github.com/no-magic-ai/no-magic/blob/main/03-systems/microquant.py): standalone script · Trades numerical precision for efficiency. · `03-systems/microquant.py`
- **Memory / Compute Tradeoffs**: systems pattern · Equivalent or accelerated execution strategies.

### 04 Agents

Stateful decision and planning loops.

- [ReAct Loop](https://github.com/no-magic-ai/no-magic/blob/main/04-agents/microreact.py): standalone script · Sequences thought, action, and observation. · `04-agents/microreact.py`
- [MCTS Planning](https://github.com/no-magic-ai/no-magic/blob/main/04-agents/micromcts.py): standalone script · Searches and updates a planning tree. · `04-agents/micromcts.py`
- [Agent Memory](https://github.com/no-magic-ai/no-magic/blob/main/04-agents/micromemory.py): standalone script · Reads and writes persistent decision state. · `04-agents/micromemory.py`
- **Observe → Act → Evaluate → Update**: decision pattern · Stateful agent control cycle.

### Curriculum & Distribution

Learning content and repository automation.

- [Conceptual Challenges](https://github.com/no-magic-ai/no-magic/blob/main/challenges/README.md): curriculum content · Exercises map concepts to algorithms. · `challenges/README.md`
- [Anki Deck Generator](https://github.com/no-magic-ai/no-magic/blob/main/resources/flashcards/generate_anki.py): content pipeline · Generates flashcards from CSV decks. · `resources/flashcards/generate_anki.py`
- [EPUB Builder](https://github.com/no-magic-ai/no-magic/blob/main/scripts/generate-epub.sh): distribution pipeline · Aggregates source into an EPUB. · `scripts/generate-epub.sh`
- [Repository Verification](https://github.com/no-magic-ai/no-magic/blob/main/scripts/verify.py): quality automation · Catalog and validation checks. · `scripts/verify.py`
- [Verification CI](https://github.com/no-magic-ai/no-magic/blob/main/.github/workflows/verify.yml): GitHub workflow · Runs repository verification. · `.github/workflows/verify.yml`

## Connections

- 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
