try microgpt.pdf
(Note: The browser's native PDF viewer will run the Javascript. GitHub's built-in repo viewer will not.)
This is a PDF file that trains a Transformer language model locally in the PDF viewer's JavaScript sandbox. It requires no servers or external APIs.
Note
The entire neural network training loop (forward pass, backward pass, gradient descent) runs locally in your browser or PDF viewer.
PDFs support an interactive scripting layer called Acrobat JS. Modern browsers (like Chrome via PDFium) implement a sandbox for this standard.
The sandbox restricts DOM access and cross-origin network requests, but it supports floating-point math, array manipulation, and timers. That's enough to run a Transformer.
- The Model: I transpiled Karpathy's pure Python MicroGPT and some JavaScript reference implementations into standalone ES5 JavaScript. This includes an autograd engine (
Valueclass), the Transformer architecture, and Box-Muller Gaussian weight initializations. - The UI: The Javascript execution context is bound to interactive AcroForm fields (buttons and text boxes).
- Execution Limits: Running the training loop synchronously would freeze the browser UI thread. To fix this, the training runs in an asynchronous chunked loop that yields to
setTimeout, streaming the loss outputs into the PDF text box. - Dataset Fetching: The script detects GitHub
blobURLs in the dataset input box and converts them toraw.githubusercontent.comURLs to attempt afetch. If the sandbox blocks the network request, it asks the user to paste the raw text directly.
This project uses uv for lightning-fast dependency management.
- Clone this repository.
- Install
uvif you haven't already. - Run the following commands:
uv sync
uv run build_pdf.py [optional_dataset.txt]The script generates microgpt.pdf in the root directory.
- MicroGPT Architecture:Andrej Karpathy
- Inspiration: Inspired by doompdf and pdftris.
This repository is licensed under the GNU GPL v3.
Rahul Kumar/microgpt.pdf - MicroGPT running inside a PDF file
Copyright (C) 2026 Rahul Kumar
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.