SPB Git

spb/forge Public MIT

Forge — LLM training from scratch in pure C++20 + Metal on Apple Silicon.

C++ 61.2% C 23% Python 7.6% TeX 7.2% CMake 1.1%
519 B · 25 lines cpp
Raw Blame History
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2#include "core/autograd.h"34#include <cstdio>5#include <cstdlib>67namespace forge {89Tape& Tape::get() {10    static Tape tape;11    return tape;12}1314void Tape::backward(const Var& loss) {15    if (loss.value().numel() != 1) {16        std::fprintf(stderr, "forge: backward() needs a scalar loss\n");17        std::abort();18    }19    loss.grad().fill_(1.0f);20    for (auto it = nodes_.rbegin(); it != nodes_.rend(); ++it) (*it)();21    clear();22}2324} // namespace forge25