DeepSeek V3 JAX: A 190-Line Model Explained13 min read2,563 words

DeepSeek V3 JAX: A 190-Line Model Explained

See how a 190-line pure JAX implementation brings DeepSeek V3’s MLA, MoE and training ideas to a MacBook Air—and what its results actually prove.

DeepSeek V3 JAXJAX language modelDeepSeekMoEmulti-head latent attentionmachine learning on MacBook Air
DeepSeek V3 JAX: A 190-Line Model Explained

DeepSeek V3 in 190 Lines: What a JAX Demo Really Shows

DeepSeek V3 is known for its size and technical ambition. But this small JAX project takes another approach: it implements a handful of the architecture's core ideas in about 190 lines, then trains a small model from scratch on a MacBook Air. It's a genuinely useful engineering exercise - just don't mistake it for a full "DeepSeek V3, but smaller" story.

The model has 36.2 million parameters, with 13.6 million active per token. It's trained on public-domain detective fiction, not on the broad mix of data a production assistant would use. The goal here isn't to match DeepSeek V3's scale or capabilities. It's to make design choices inspectable and testable.

Below, I break down what the implementation includes, how the attention and expert-routing pieces work, what the reported training results actually support, and how to reproduce the run without turning it into a misleading comparison.

What does this DeepSeek V3 JAX project implement?

This is a small, readable implementation of selected DeepSeek V3 architectural ideas. It's pure JAX - parameters are plain dictionaries of arrays, and the model is written as functions rather than through a higher-level framework like Flax, Equinox, or PyTorch.

That framing changes what the project is "for." It's not primarily a deployment-ready package. It's an engineering artifact where you can follow the forward pass, routing decisions, cache handling, and training logic directly. You don't have to reverse-engineer how a framework hides the mechanics.

The implementation includes several core components:

  • Multi-head Latent Attention (MLA): caches key/value information in a compressed per-token latent form.
  • Decoupled rotary position embeddings: keeps the rotary key separate from the compressed latent and shares it across attention heads.
  • DeepSeekMoE: runs one shared expert plus routed experts chosen per token.
  • Auxiliary-loss-free balancing: adjusts routing to avoid persistent expert imbalance.
  • Sequence-wise balance loss: adds a complementary term to the training objective.
  • Multi-token prediction (MTP): predicts the token after the next in addition to the immediate next token.
  • Training and generation: uses bfloat16 computation with float32 master weights, with prefill and decoding that use the MLA cache.

The default configuration uses 16 routed experts and selects four per token, plus the shared expert. The reported total is 36.2 million parameters, with 13.6 million active per token. In mixture-of-experts models, that split matters: total parameter count doesn't tell you how much computation each token actually triggers.

Also, the "190-line" claim should be taken literally and narrowly. It describes the model implementation itself, not every script or file you'd need to prepare data, run tests, train, and generate samples. The repo has the extra machinery elsewhere. The compact code is there to keep the architecture legible, not to cram the entire experiment into one file.

What it is - and what it is not

Think of it as a working illustration of how multiple DeepSeek V3 ideas can fit together in JAX. It's not the full DeepSeek V3 model, not a drop-in replacement for a production inference stack, and not evidence that a MacBook Air can train a frontier-scale model. The point is to let you study the mechanics without the infrastructure that usually hides them.

How does the compact model reproduce DeepSeek's key ideas?

The most useful parts aren't just individual features. They're the way memory use, compute, and training behavior interact. A compact implementation makes those trade-offs easier to see, but it also limits what you can claim about performance at production scale.

MLA reduces what attention needs to cache

In autoregressive generation, each new token extends the sequence. During decoding, the model must keep information from earlier tokens, and caching full keys and values across all layers can add up quickly as context grows.

MLA aims to reduce that cost by caching a compressed latent representation and reconstructing what's needed from it.

Here, the project reports that the cached representation is 144 numbers per token instead of 480 for the full key-and-value representation. That's a concrete example of the intended memory-saving trade-off. It's not a universal ratio - the exact numbers depend on the chosen dimensions in this particular small model.

The implementation also includes decoupled RoPE. Instead of putting all positional information inside the compressed latent, it keeps a rotary key separate and shared across heads. Practically, it means compression has to work alongside positional information, not erase it.

MoE routes tokens to a subset of experts

A dense model runs the same feed-forward network for every token. An MoE model instead keeps multiple expert networks and uses a router to pick which ones run per token.

In this project, one shared expert runs for every token. The router then selects four out of 16 routed experts.

This creates a real distinction between capacity and active computation. Parameters exist across all experts, but only a subset runs per token. That can raise capacity without making every token pay the compute for all experts. It also introduces a systems risk: if routing over-sends tokens to a few experts, they become bottlenecks while others do less.

Balancing changes routing, not expert weights

The "auxiliary-loss-free" balancing mechanism uses a per-expert bias to nudge routing toward less-used experts. It changes which experts get selected; it doesn't directly change the weights of the experts that are already chosen. The training also includes a small sequence-wise balance loss as an extra signal.

That separation matters when you interpret results. A routing-balancing method should be judged on whether it actually spreads load, and whether it hurts the learning objective.

In the reported run, expert load with bias was 5.7-6.9% per expert at step 2,000, close to the 6.25% expected under even distribution. Without the bias, expert load ranged from 0.09% to 18.6%.

At the same step, validation loss was essentially the same with and without balancing: 3.921 vs 3.923. For this run, that supports the practical claim that the bias improved load distribution without an obvious validation-loss penalty. It still doesn't guarantee the same behavior for every model size, dataset, or training setup.

MTP adds a second prediction target

Multi-token prediction adds an auxiliary objective: alongside predicting the next token, the model predicts the token after that. This project weights that extra objective at λ = 0.3. It gives training another signal about what follows in the sequence, but the outcome depends on how the objective is implemented and tuned.

Taken together, these mechanisms are why the small implementation is interesting: they're real operations and measurable system behaviors, not just names in an architecture diagram.

What did training on a MacBook Air prove?

The published run used an Apple Silicon MacBook Air M5 with 24 GB of memory. It trained for 12,000 steps, processed roughly 98 million tokens, and took about 2.6 hours. Reported median throughput was about 10,700 tokens per second. Those numbers are useful for understanding what "local experimentation" looks like, but they're tightly tied to the machine, software versions, configuration, and workload.

The training data was 207 public-domain books, mostly detective fiction. The run reports 21.7 million training tokens, with one Hammett title repeated three times, plus a held-out crime-fiction validation split. The subject matter is deliberately narrow. A model trained on this material is likely to reflect it - it's not a general-purpose language model benchmark.

Validation loss went from 5.02 at step 250 down to 3.51 by the end of training. That at least shows the model learned patterns within its training distribution. It doesn't, by itself, prove broad reasoning, factual reliability, or usefulness on unrelated tasks. To evaluate those kinds of claims, you'd need other tests and data.

The sample generation also shows the limitation clearly. For instance, given "The fog came in off the", the model continues with a fluent-sounding sentence about the bay, trees, and stars. That demonstrates generation, not a controlled evaluation of instruction following or handling unfamiliar topics. One plausible completion isn't enough.

Read the results as engineering evidence

The strongest conclusions here are about implementation behavior:

  • The architecture can be implemented and trained in a small, inspectable JAX project.
  • MLA reduces the size of the demonstrated attention cache representation.
  • Expert-balance bias can make routing much more even in this configuration.
  • In the reported balancing run, validation loss didn't show a meaningful difference at step 2,000.
  • A modest model can complete a multi-hour training run on a consumer laptop with supported software.

The weaker conclusions are the ones that try to generalize beyond the experiment. This run doesn't prove the design is better than alternatives, doesn't show the same balancing behavior at larger scale, and doesn't establish that the model competes with commercial assistants. Those claims would require different experiments, bigger and more representative datasets, and evaluations aligned to whatever claim you're trying to make.

Why the distinction matters

"Trained on a laptop" can sound like a breakthrough in hardware capability. What it actually means is that a small, carefully configured experiment ran locally. The takeaway is about learning and testing architecture mechanics at a tolerable cost - not about removing compute requirements for large-scale pretraining.

How can you reproduce the experiment safely?

Reproducing the run is a good exercise if your goal is to inspect the implementation or see how the components interact. Treat it like a controlled experiment. Don't treat it like a benchmark you can compare directly with different machines, datasets, or training setups.

Check compatibility before changing dependencies

The setup expects an Apple Silicon Mac, Python 3.12, and the project's pinned dependencies. The README calls out that jax-metal 0.1.1 works with jax==0.4.34; upgrading JAX without checking compatibility can break the accelerator path.

It's tempting to dismiss this as "just packaging," but it's part of the experiment. When accelerator support depends on a particular library combination, changing one piece can affect whether training works at all. If you want to test alternatives, start from the pinned environment and change one thing at a time.

The repo includes CPU tests and an optional Metal compatibility smoke test. The README also notes that a gradient through a specific two-axis contraction fails on the cited Metal version, and that the model avoids that operation. That's a clear example of the backend shaping the implementation.

Rebuild the data and compare the inputs

The data prep script downloads 207 books from the repo's source list, cleans the text, trains an 8,192-token BPE tokenizer, and writes training, validation, and tokenizer files. The README reports about 120 MB of source downloads and gives expected document, character, and token counts.

If your goal is to reproduce the published result, matching the data matters as much as matching the code. The run reports checksums for the generated files. That lets you confirm your inputs match before blaming differences in loss or speed on the model.

If you alter the corpus, tokenizer, or split, record those changes. Otherwise you've stopped doing reproduction and started doing something adjacent.

The dataset is public-domain detective fiction, but "public domain" doesn't guarantee every downloaded file is identical across sources or perfectly clean. Keep the data preparation steps inspectable, and verify that your local files match the intended inputs.

Handle checkpoints as executable input

The released checkpoint is stored as a Python pickle. Pickle files can execute code when loaded, so loading an untrusted checkpoint is a security risk. Only use checkpoints from sources you trust, and don't treat "model weights" as inert just because they're labeled that way.

After installation and data preparation, you can run the provided sampling script to generate text from a prompt. Keep the purpose in mind: sampling checks that inference works; it doesn't assess quality.

If you want to compare variants, fix the prompt and decoding settings, and use a broader evaluation set rather than picking the completion you like best.

A practical reproduction checklist

  • Use the documented operating system, Python version, and pinned packages.
  • Run the supplied tests before starting a long training job.
  • Confirm the dataset and tokenizer outputs, including checksums where available.
  • Record hardware, dependency versions, configuration, and training steps.
  • Treat generated text as an example, not a benchmark.
  • Load pickle checkpoints only when you trust their origin.

These steps address the most common failure modes: backend incompatibility, changed training data, and results that can't be compared because the setup wasn't recorded.

Conclusion

This JAX implementation is valuable because it connects several DeepSeek V3 ideas to concrete, measurable behavior: a smaller attention cache representation, sparse expert activation, and improved expert-load balance in a small training run. It also makes the limits obvious. This is a narrow 36.2-million-parameter architecture demo, not a laptop-scale replica of a production language model.

Use it to understand how the pieces fit together, test local implementation choices, and see how JAX and Apple Silicon constraints show up in practice. When you evaluate the results, separate evidence about the mechanics from claims about model capability - the experiment is useful because that distinction is built into what it can and can't show.

Frequently Asked Questions

No. It is a small implementation of selected DeepSeek V3 architectural ideas, with 36.2 million total parameters and 13.6 million active per token. It does not reproduce the original model’s scale, training data, compute or capabilities.
It can train this small model locally; the reported run took about 2.6 hours on an M5 MacBook Air with 24 GB of memory. This demonstrates a local experiment, not that a laptop can train a frontier-scale model.
A per-expert bias nudges routing toward less-used experts. In the reported run, it substantially evened out expert load without a meaningful validation-loss difference at step 2,000. This result is specific to the tested configuration.
A Python pickle can execute code when loaded. Only load the checkpoint if you trust its source and understand the risk; pickle files should not be treated as inert model-weight data.

Related articles