Leanpub Header

Skip to main content

LLMs in Go. From scratch

Build, train and fine-tune a GPT model with no other dependencies except Go's standard library

Interested in this book? Show your support by saying what you'd like to pay for it!

$
About

About

About the Book

This book is about building a language model from nothing, in Go, with the standard library only: no PyTorch, no BLAS, no cgo. Our journey starts with a tensor and a matrix product, and it ends with GPT-2's own weights running in our code and a chat model fine-tuned from them.

Each chapter follows one step of a real project, my language model Tolk, in the order it was built. First comes the math, derived by hand: both the forward pass and the gradient. Next comes the Go code that mirrors it, and last an experiment you can run: a test, a benchmark, or a falling loss.

The stops along the way:

- backpropagation, from a scalar autograd to a hand-written backward pass for each layer, each one checked by finite differences;

- a bigram model, Bengio's neural language model, and what makes deep networks train;

- a byte-level BPE tokenizer, attention, and the GPT;

- making it fast on the CPU, and training it on TinyStories;

- sampling and a KV cache;

- GPT-2 (124M) loaded into our own code and checked against known results, Hugging Face's greedy text and lm-eval's LAMBADA score, still without PyTorch;

- the GPU through Apple's Metal, called directly from Go;

- RMSNorm, SwiGLU, RoPE, grouped-query attention, bf16, and scaling laws, each one measured;

- GPT-2 fine-tuned into a chat model on OASST1, in full and with LoRA.

To follow the same journey, you need Go at the level of the Tour of Go and high-school derivatives. The rest, including the matrix product and probability, is taught where it is first needed.

I build Tolk with Claude Opus, Anthropic's AI model. I design the plan, and I review, verify, and edit. Claude writes the code and drafts the text, and I edit every draft into the final text. It is a joint effort.

I am publishing this book chapter by chapter, while it is being written. The first release covers chapters 1–3, and chapter 3, on backpropagation on scalars, is the free sample. Once you buy the book, every later chapter comes free. Sign up to hear when the first chapters are out.

Share this book

Author

About the Author

Szymon Walczak

I'm a software engineer with over 20 years of experience in designing and building complex web and cross-platform mobile applications. I hold a PMP certification (#2115680) and a PhD in Mathematics, which together shape how I approach software – with both engineering rigor and product thinking.

I'm a big enthusiast of AI augmented development and use language models in my everyday work. Now, I want to know how they work, so I am building one from scratch in Go, in the open: Tolk. This book is what I have learned. More at szymonwalczak.dev.

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub