Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build_DeepSeek_Step_by_Step

Build_DeepSeek_Step_by_Step is an implementation-oriented project that breaks down the key modules behind modern LLMs from the ground up. It does not try to reproduce a full industrial training system. Instead, it decomposes a typical DeepSeek-like technical path into readable notebooks and teaching-oriented helper code, so readers can trace the full chain:

  • how text becomes tokens
  • how tokens become embeddings
  • how attention works
  • what modern structures such as GQA, MLA, and MoE solve in engineering terms
  • how training progresses from pretraining to SFT, reward models, PPO, and GRPO
  • how data moves from collection, cleaning, and deduplication to trainable corpora

The core goal of this project is not to stack terminology, but to connect modules, formulas, code, figures, and engineering meaning into one coherent learning path.

Current Coverage

The project currently covers five main tracks:

  1. Foundational Math and Input Representation

    • vectors, matrices, softmax, masks
    • tokenizers, BPE, embeddings
  2. Core Transformer Structure

    • self-attention
    • multi-head attention
    • RoPE
    • RMSNorm, residual connections, FFN, SwiGLU
    • basic decoder-only block
  3. Inference Efficiency and Capacity Scaling

    • KV cache
    • MQA / GQA
    • MLA
    • MoE
  4. Training and Alignment Pipeline

    • pretraining
    • continued training
    • SFT
    • reward model
    • RLHF
    • PPO / DPO / GRPO
  5. Data Engineering Pipeline

    • data collection / crawling
    • main-content extraction
    • cleaning / filtering
    • exact deduplication / near-duplicate detection
    • quality bucketing

An additional section covers:

  1. Position Encoding Visualization
    • learned absolute position embeddings
    • the original sinusoidal encoding from Attention Is All You Need
    • RoPE
    • relative bias
    • ALiBi
    • RoPE scaling intuition
    • 2D positional encoding

Project Characteristics

  • Each notebook focuses on one core question instead of mixing topics.
  • Explanations take priority over showing off technical complexity, and the code keeps intermediate results and tensor shapes visible where useful.
  • Important concepts are implemented minimally whenever possible instead of being left as definitions only.
  • Helper functions in utils/ are also written in a teaching style, with detailed docstrings and comments.
  • The training and data sections are part of the main learning path, not side material.
  • File names consistently use numbers and underscores, with no spaces.

Current Directory Structure

Build_DeepSeek_Step_by_Step/
├─ README.md
├─ PRD.md
├─ requirements.txt
├─ notebooks/
│  ├─ 01_python_and_matrix_foundations.ipynb
│  ├─ 02_tokenization_and_bpe.ipynb
│  ├─ 03_embeddings_and_language_model_inputs.ipynb
│  ├─ 04_self_attention_from_scratch.ipynb
│  ├─ 05_multi_head_attention.ipynb
│  ├─ 06_rope_and_position_encoding.ipynb
│  ├─ 07_rmsnorm_and_residual_connections.ipynb
│  ├─ 08_ffn_and_swiglu.ipynb
│  ├─ 09_build_a_basic_transformer_block.ipynb
│  ├─ 10_kv_cache_and_inference.ipynb
│  ├─ 11_mqa_and_gqa.ipynb
│  ├─ 12_mla_from_intuition_to_implementation.ipynb
│  ├─ 13_moe_routing_and_experts.ipynb
│  ├─ 14_pretraining_data_and_objective.ipynb
│  ├─ 15_sft_and_alignment_basics.ipynb
│  ├─ 16_reward_model_and_rl_intro.ipynb
│  ├─ 17_putting_everything_together.ipynb
│  ├─ 18_multi_stage_training_rlhf_ppo_grpo.ipynb
│  ├─ 19_data_collection_and_crawling.ipynb
│  ├─ 20_data_cleaning_filtering_and_dedup.ipynb
│  └─ 21_visualizing_position_encodings.ipynb
├─ utils/
│  ├─ tokenizer_utils.py
│  ├─ attention_utils.py
│  ├─ visualization_utils.py
│  └─ training_utils.py
└─ assets/
   ├─ figures/
   │  └─ README.md
   └─ sample_texts/
      ├─ tiny_corpus.txt
      ├─ instruction_examples.txt
      └─ raw_web_page_mock.html

Notebook Path

Foundations

  • 01_python_and_matrix_foundations.ipynb
  • 02_tokenization_and_bpe.ipynb
  • 03_embeddings_and_language_model_inputs.ipynb
  • 04_self_attention_from_scratch.ipynb
  • 05_multi_head_attention.ipynb

Core Transformer

  • 06_rope_and_position_encoding.ipynb
  • 07_rmsnorm_and_residual_connections.ipynb
  • 08_ffn_and_swiglu.ipynb
  • 09_build_a_basic_transformer_block.ipynb
  • 10_kv_cache_and_inference.ipynb

Efficient Attention and Capacity

  • 11_mqa_and_gqa.ipynb
  • 12_mla_from_intuition_to_implementation.ipynb
  • 13_moe_routing_and_experts.ipynb

Training and Alignment

  • 14_pretraining_data_and_objective.ipynb
  • 15_sft_and_alignment_basics.ipynb
  • 16_reward_model_and_rl_intro.ipynb
  • 17_putting_everything_together.ipynb
  • 18_multi_stage_training_rlhf_ppo_grpo.ipynb

Data Pipeline

  • 19_data_collection_and_crawling.ipynb
  • 20_data_cleaning_filtering_and_dedup.ipynb

Visualization

  • 21_visualizing_position_encodings.ipynb

What Is in utils/

utils/ is not a placeholder directory. It is a teaching-oriented helper layer reused by the notebooks:

These files intentionally use a teaching style, so their comments are more detailed than a normal utility library.

What Is in assets/

assets/ stores reusable static resources for the project.

Environment

Python 3.11 is recommended. The first version of the notebooks mainly depends on:

  • numpy
  • matplotlib
  • torch
  • jupyter

These dependencies are enough to support the project's minimal implementations, visualizations, and tensor experiments.

Scope Boundaries

The current version still does not aim to provide:

  • a full industrial-grade reproduction of the DeepSeek training stack
  • a distributed training system implementation
  • CUDA / Triton / FlashAttention kernel optimization
  • a complete inference-serving framework

This project is closer to a runnable, explainable, and extensible technical dissection notebook. The priority is to explain the full path from modules, to architecture, to training, to data, rather than copying repositories or summarizing concepts.

About

Notebook-first deep dive into modern LLM architecture, covering tokenization, embeddings, attention, GQA, MLA, MoE, training, and alignment from scratch.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages