femtoGPT is a minimal generative pretrained Transformer (GPT) project implemented in pure Rust, usable for inference and training of GPT-style language models, with support for CPU and GPU.
Purpose: This project is mainly aimed at learners and developers interested in large language models (LLMs) who want to deeply understand their underlying working principles. It implements tensor processing logic and training/inference code for a minimal GPT architecture from scratch, and the architecture is very similar or almost identical to Andrej Karpathy's nanoGPT video lecture.
Main capabilities: Supports training and inference of GPT-style language models; provides both CPU and GPU execution modes, with the GPU implementation based on OpenCL and able to run on NVIDIA and AMD graphics cards without installing the heavyweight CUDA toolkit, requiring only an OpenCL runtime; model saving and loading use serde/bincode; parallel computing uses rayon. The project depends only on random generation libraries (rand/rand-distr), data serialization libraries (serde/bincode), and a parallel computing library (rayon).
Usage: The Rust toolchain must first be installed with the command curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh. The training and inference commands are cargo run --release -- train and cargo run --release -- infer, respectively; adding --features gpu enables GPU acceleration. If using GPU training, ensure that the GPU driver is correctly installed and an OpenCL runtime is available; on Debian systems, the OpenCL runtime can be installed via sudo apt install ocl-icd-opencl-dev. Training data must be placed in dataset.txt, and the number of unique characters should be kept small (the example dataset uses only 65 distinct characters). Training data will be saved in the train_data directory, and training can be stopped and then resumed.
Applicable scenarios: Suitable for learning the underlying implementation of LLMs, conducting small-scale model experiments, and teaching demonstrations. The project author trained a 300,000-parameter model on the Shakespeare dataset; after several hours of training, the output quality was poor but it could generate easy-to-pronounce words; subsequently, on the Reddit dataset, a 5-layer model was trained for about 10 hours (32-core CPU) using the sentencepiece tokenizer (vocabulary size 500), and on the TinyStories dataset, an 8-layer, 8-head, 128-embedding-dimension model was trained with vocabulary size 1000, with some generated samples shown.
Limitations: The project explicitly states that it runs extremely slowly (relatively speaking, it is fast on CPU), and most basic operations (such as matrix multiplication) use the simplest implementation; gradient correctness is verified through gradient checking methods, but some layer implementations are still very likely to contain errors. The project does not provide more information about fees, pricing, platform support scope, performance guarantees, etc.