Code

The whole stack, in the open.

Everything that trains Apex-3, from the model to the data pipeline.

Repository
DW-dev-UE/LLM-from-scratch
Path
apex3/
Commit
1d491e2
Language
Python · PyTorch
Runs on
1× GH200 (ARM64, NGC PyTorch 26.09 container)
Trains
Apex-3

The complete pretraining stack behind Apex-3, released with the paper at a pinned commit.

It is the Python package apex3/: the mixture-of-experts model, the Muon optimizer, one-GPU offload into Grace memory, FP8 and a fused loss, DiLoCo for a second GH200, the data pipeline and an agent runtime.

Comments and docstrings are stripped. With docstrings excluded, every Python file parses to the same syntax tree as the server code behind the paper's step 10 benchmark and its training numbers after 10B tokens. Clone it below, or read it file by file in the explorer.

Clone
git clone https://github.com/DW-dev-UE/LLM-from-scratch.git
cd LLM-from-scratch/apex3

What’s inside

Eight parts of the stack, each with the files that implement it.

ComponentWhereWhat it does
One-GPU offloadMemory The FP32 masters and optimizer state live in pinned Grace memory, reached over NVLink-C2C. Chosen activations are stored on the CPU too.
Fine-grained MoEModel A sigmoid router picks the top experts, and a per-expert bias balances the load without a loss term. Experts run as grouped GEMMs.
Muon + AdamWOptimizer Muon on the hidden matrices, AdamW on the embeddings, LM head, router and one-dimensional parameters, with the WSD schedule.
FlashAttention-3, varlenAttention Document boundaries become varlen sequence lengths, so attention never crosses documents. The build script makes the FlashAttention-3 wheel.
FP8 and a fused lossPrecision FP8 through torchao, and a Triton kernel for the cross-entropy gradient that includes the z-loss term.
DiLoCo for a second GH200Scale-out Each worker publishes its difference from the shared weights, the differences are averaged, and an outer update with momentum follows.
Data pipelineData One module per stage: ingest, cleaning, decontamination, deduplication, redaction, repository packing and tokenization.
Agent runtimeTools Chat template, tools, a Docker sandbox, an MCP client, Skills, a KV-cache backend and the agent loop.

Select a path to open it in the file explorer below.

Find your way around

Select a file to read what it does, then open it at the pinned commit.

Start here

Paths are relative to the repository root. Every link opens the pinned commit, 1d491e2.

Run it yourself

The container, the benchmark and the training command, converted from the paper’s appendix. Run them from the apex3/ directory.

Placeholders are underlined in blue.

docker
# GH200 is ARM64: use the NGC PyTorch container.
# Mount your code, data and checkpoint directories. The paper ran its commands with docker exec.
docker run --gpus all --ipc=host --cap-add SYS_NICE \
  --ulimit memlock=-1 --ulimit stack=67108864 \
  -v CODE_DIR:CODE_DIR -v DATA_DIR:DATA_DIR -v CKPT_DIR:CKPT_DIR \
  nvcr.io/nvidia/pytorch:26.09-py3

Image digest sha256:6e8ccc607fc2a51e3741667b86316a0889418ba8b78ed0ba96e8d282b648a7a6. FlashAttention-3 is built from source with server/build_fa3.sh, and the installation steps are in server/b_setup.sh. The default seccomp profile blocks set_mempolicy, so the container needs --cap-add SYS_NICE. Do not use --cpuset-mems 0: the GPU is no longer visible.

Linux exposes the GH200's HBM as NUMA node 1. Launch every process under numactl --membind=0 and drop the page cache before each run, or pinned host memory can spill into node 1 and end in GPU out-of-memory errors.

Questions

What hardware do I need?

One GH200. It is ARM64, so use the NGC PyTorch 26.09 container shown above, with FlashAttention-3 built from server/build_fa3.sh. A second GH200 is optional: it joins through DiLoCo, as Apex-3’s second GPU worker did at 4B tokens. The step 10 benchmark needs no corpus.

Is the training data included?

No. The 2.2 TB training corpus is not released. The data pipeline code is: apex3/datapipe/ and apex3/scripts/data/pipeline.py cover download, ingest, decontam, clean, dedup, repo-pack, tokenize, long and stats. The Stage 1 mixture is listed under the Train tab above, and the benchmark draws random tokens from a fixed seed, so it runs without any data.

Where are the logs and the paper?

Every step of the run is on Training logs. The 20-page paper is on Research, and it also reports what did not work. The evidence bundles and figure scripts are in the repository under apex3/paper/.