CUDA Building Blocks
Implement the core CUDA primitives used everywhere in the layer: matmul variants, bias add and reduction, elementwise ops, activations, and row-wise softmax with their backward passes.
Build CUDA kernels for a complete sparse mixture-of-experts forward, backward, and training loop.
Every source step is its own lesson with intuition, concepts, correctly rendered MathJax mathematics, implementation, tests, mistakes, and a checkpoint.
Implement the core CUDA primitives used everywhere in the layer: matmul variants, bias add and reduction, elementwise ops, activations, and row-wise softmax with their backward passes.
Implement reusable kernels for selecting the top-k gate values per token and normalizing those gates to a probability distribution, including the backward through that normalization.
Build the router that maps tokens to experts: compute logits via a gate matmul, turn them into probabilities, pick the top-k experts per token, and propagate gradients back to the gate weights.
Implement the sparse plumbing of MoE: counting tokens per expert, computing slot offsets, gathering tokens into per-expert buffers, scattering gradients back, and combining expert outputs weighted by gate values (with backward).
Implement the two-layer expert MLP that runs on each expert's slice of tokens, including up/down projections, biases, activation, and the full set of backward kernels for inputs, weights, and biases.
Implement the MoE load-balancing auxiliary loss: dispatch fractions, mean router probabilities, the aux loss itself, and the gradient back to router probabilities.
Add the task loss (MSE), gradient-zeroing and SGD update kernels, and host-side orchestration for the full forward pass, backward pass, single training step, and overall training loop.