Skip to main content

Building Language Models from Scratch

From the chain rule to a deployable model — no black boxes.

Instead of calling a library and treating the model as magic, you build every piece yourself: byte-level BPE, causal self-attention, the Transformer block, a training loop with warmup-cosine and diagnostics, a KV-cache inference engine, an evaluation harness, and a fine-tuning stack (SFT, LoRA, DPO). Each chapter is paired with a notebook that runs end-to-end on a single GPU and produces every figure the chapter cites — you see the real outputs, not idealized diagrams. The library is small (~5,000 LOC), inspectable, and backed by 190 tests; the GRU is verified against PyTorch's reference, and DPO comes with a proof.

Part of the "Beyond … and Pray" series: governed agents · trustworthy RAG · test & validate · LLMs from scratch

What's inside

  • Tokenization — character / word / byte + byte-level BPE from scratch
  • Transformers — attention, multi-head, RoPE / ALiBi / sinusoidal / learned, TinyGPT
  • Training — Trainer, scaling laws, mixed precision, diagnostics
  • Inference — greedy / beam / top-k / top-p + a KV-cache engine
  • Evaluation — perplexity, calibration, LLM-as-judge, contamination checks
  • Fine-tuning — SFT (loss-masked), LoRA from scratch, DPO with a proof
  • 190 tests, 33 notebooks, one per chapter

Install

pip install llm-from-scratch              # core: torch, numpy, scikit-learn
pip install "llm-from-scratch[notebooks]" # + jupyterlab, matplotlib, pandas
pip install "llm-from-scratch[all]"       # everything

Python 3.12+. A CUDA-capable GPU is recommended for the training chapters.

License

Apache-2.0. © 2026 Knowlytix.

Metadata

Release files for llm-from-scratch 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-from-scratch 0.1.0
File Size Uploaded
llm_from_scratch-0.1.0.tar.gz 89.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-from-scratch 0.1.0
File Interpreter ABI Platform
llm_from_scratch-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 189.2 kB

Release files / llm_from_scratch-0.1.0.tar.gz

Download URL llm_from_scratch-0.1.0.tar.gz
Size 89.6 kB
Tags Source
SHA-256 checksum
How to use checksums
636fa67dc27da4ed04ea3c4472dddb927b96e5f408ebb0a2c64a53d5e1163774
BLAKE2b-256 checksum
How to use checksums
bbd2428d524af043418c6fccf66e90e0a3524927459e238bbd848714d15c6dc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 9, 2026.

Transparency log

Release files / llm_from_scratch-0.1.0-py3-none-any.whl

Download URL llm_from_scratch-0.1.0-py3-none-any.whl
Size 99.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e52e51b14e474a45cc9b5ee3e1b635e3af460f2a020ce85aa2880d149d85871
BLAKE2b-256 checksum
How to use checksums
faa9c653eb00c128a40cc6c5efdba96d434fba2a482c2edc3faefd239d18efd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 9, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page