Documentation

Hesper.NN.MLP

Multi-Layer Perceptron (MLP) with GPU Backpropagation #

Complete implementation of 2-layer MLP for MNIST with GPU-accelerated forward and backward passes.

Architecture #

Input(784) → Dense(128) + ReLU → Dense(10) + Softmax → Output(10)

GPU Implementation #

Forward Pass:

  1. Layer 1: MatVec(784→128) + Bias + ReLU (fused kernel)
  2. Layer 2: MatVec(128→10) + Bias (fused kernel)
  3. Softmax(10)

Backward Pass:

  1. Softmax gradient: dL/dlogits = probs - one_hot(label)
  2. Layer 2 gradient: dW2 = h1^T @ dlogits, dB2 = dlogits, dH1 = W2^T @ dlogits
  3. ReLU gradient: dH1pre = dH1 * (h1 > 0)
  4. Layer 1 gradient: dW1 = input^T @ dH1pre, dB1 = dH1pre

SGD Update (GPU):

Type Definitions #

MLP parameters (weights and biases for 2 layers)

Instances For

    MLP input (single sample + label for training)

    Instances For

      MLP output (predictions + loss)

      Instances For

        MLP gradients (for backprop)

        Instances For

          CPU Reference Implementation #

          def Hesper.NN.MLP.cpuMatVec (mat vec : Array Float) (rows cols : Nat) :

          Matrix-vector multiplication on CPU

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For

            Add bias vector to result

            Equations
            Instances For

              ReLU activation

              Equations
              Instances For

                Softmax activation

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For

                  Cross-entropy loss (approximated as 1 - prob for simplicity)

                  Equations
                  Instances For

                    CPU forward pass

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For

                      CPU backward pass (returns gradients)

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For

                        GPU Kernels (using existing WGSL helpers) #

                        GPU Backward Pass Kernels (using WGSL DSL) #

                        Softmax gradient kernel: dLogits[i] = probs[i] - (i == label ? 1 : 0)

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          def Hesper.NN.MLP.genLayer2BackwardKernel (hiddenSize outputSize : Nat) :

                          Layer 2 backward kernel: computes dW2, dB2, dH1

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For

                            ReLU backward kernel: dH1pre[i] = dH1[i] * (h1[i] > 0)

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              def Hesper.NN.MLP.genLayer1BackwardKernel (inputSize hiddenSize : Nat) :

                              Layer 1 backward kernel: computes dW1, dB1

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For

                                SGD Update on GPU #

                                SGD kernel: param[i] -= lr * grad[i]

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  def Hesper.NN.MLP.genAdamKernel (size : Nat) (lr beta1 beta2 epsilon : Float) (step : Nat) :

                                  Generate GPU kernel for Adam optimizer update.

                                  Adam (Adaptive Moment Estimation) combines momentum and RMSprop.

                                  Algorithm:

                                  m[i] = beta1 * m[i] + (1 - beta1) * grad[i]           # Update first moment
                                  v[i] = beta2 * v[i] + (1 - beta2) * grad[i]^2         # Update second moment
                                  m_hat = m[i] / (1 - beta1^step)                       # Bias correction
                                  v_hat = v[i] / (1 - beta2^step)                       # Bias correction
                                  params[i] -= lr * m_hat / (sqrt(v_hat) + epsilon)    # Update parameters
                                  

                                  Parameters:

                                  • size: Number of parameters
                                  • lr: Learning rate (typical: 0.001)
                                  • beta1: Momentum decay rate (typical: 0.9)
                                  • beta2: Variance decay rate (typical: 0.999)
                                  • epsilon: Numerical stability constant (typical: 1e-8)
                                  • step: Current step number (for bias correction)

                                  Buffers:

                                  • @binding(0): params (read-write) - parameters to update
                                  • @binding(1): grads (read-only) - gradients
                                  • @binding(2): m (read-write) - first moment estimates
                                  • @binding(3): v (read-write) - second moment estimates

                                  Example:

                                  let kernel := genAdamKernel 1000 0.001 0.9 0.999 1e-8 10
                                  
                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For

                                    SGD Update on CPU #

                                    CPU SGD parameter update

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For

                                      VerifiedOpFusion Instance #

                                      Note: Full VerifiedOpFusion instance would require:

                                      1. Kernel type signatures matching the pattern
                                      2. WGSL expression builders
                                      3. Proper workgroup dimensions

                                      For now, we provide the CPU spec which can be called directly from the training loop. GPU kernels can be invoked via Compute API.

                                      Equations
                                      • One or more equations did not get rendered due to their size.

                                      Training Helpers #

                                      Single training step: forward + backward + update

                                      Equations
                                      Instances For