GPT-2 Runs on Osprey’s GPU Backend, in the Python Flavor Too
Osprey can run GPT-2, the 124-million-parameter language model, on a GPU. It loads the weights exactly as Hugging Face publishes them, generates text and runs a training step. The model itself is Osprey code: attention, matrix multiplication, predictions and weight updates.
The model is also written in Osprey’s Python syntax flavor. A syntax flavor is a way to write Osprey: this one uses def, indentation, f-strings and decorated effect declarations. The Osprey compiler builds it as a native program. The Python-flavor GPT-2 imports no Default-flavor code and matches the Default version’s generated text on host lists, Metal and OpenCL in the recorded runs.
On an Apple M4 Max, the training step takes 7.6 seconds on Metal versus 25.2 seconds on the CPU: about 3.3× faster. Metal and OpenCL run the same model source.
Written in the Python flavor
Default, ML and Python syntax flavors lower into one shared AST, with the same type inference, effects, modules and code generation. Here is the GPT-2 effect and GPU handler from the Python-flavor port. The first-line marker tells the compiler to parse these Osprey source files using Python-style syntax:
# osprey: flavor=python
@static_effect
class Tensor[B]:
def build(size: int, kernel: Kernel) -> B: ...
def mat_mul(a: B, b: B, rows: int, inner: int, cols: int) -> B: ...
@static_handler(Tensor[GpuBuffer[float]])
class on_gpu:
def build(n, f):
match gpuIota(n):
case Success(value=value):
return Success(value=gpuMap(value, f))
case Error(message=message):
return Error(message=toString(message))
def mat_mul(a, b, rows, inner, cols):
return or_abort(gpuMatMul(a, b, rows, inner, cols))
def product(a, b, rows, inner, cols):
return perform(Tensor.mat_mul(a, b, rows, inner, cols))
The Python flavor guide covers functions, records, match, effects, handlers and mixed-flavor projects. Calls to CPython libraries use an explicit Python-hosted bridge; this GPT-2 port runs natively without CPython. Generation is available in both syntax flavors; the training example is currently Default-only.
Write the model once
Osprey provides typed GPU buffers and operations that apply ordinary Osprey functions to their contents. The compiler turns supported functions into GPU kernels—the small programs a GPU executes. You write the calculation in Osprey; you do not need a separate CUDA or Metal version of the model.
GPT-2 describes its calculations through a small set of array operations, declared as the algebraic effect Tensor. Two handlers answer those operations: one with CPU lists, one with GPU buffers executed through Metal or OpenCL. The model performs the operations and never names a buffer type, so the handler the program installs decides where the arithmetic runs. Choosing the handler is the only change; the model’s calculations stay the same. The effect is static, so the compiler resolves the handler before the program runs and nothing is looked up at run time. Run the same model on CPU or GPU walks through it, and both Default and Python sources are available to read.
What actually runs on the GPU?
The CPU turns your prompt into tokens and loads the weights. The GPU handles the model’s layers, attention calculations, predictions and training calculations.
Weights upload once. Intermediate results stay on the GPU for the next operation, avoiding a trip back to the CPU after every calculation. The CPU coordinates the work, handles text and reads back the results the program needs.
Generation reuses earlier calculations as it adds tokens. The training example updates the weights once and checks the prediction error again.
An independent reference implementation, reference.py, is real Python, run by CPython, and checks the calculations of both Osprey versions. Recorded CPU and GPU runs generated the same text, with closely agreeing training results. The verification details explain the small numerical differences.
Metal, OpenCL and CUDA
Osprey’s GPU interface has no CUDA dependency.
| Execution path | Current support |
|---|---|
| CPU | GPU-buffer operations on the host, plus a list implementation for comparison |
| Metal | Implemented for Apple GPUs |
| OpenCL | Implemented; provides a route to compatible AMD, Intel and NVIDIA devices with suitable drivers |
| CUDA | No dedicated CUDA backend today |
These measurements used an Apple M4 Max through Metal and OpenCL, not every supported GPU vendor.
Where acceleration helps today
| Workload | CPU | Metal | OpenCL |
|---|---|---|---|
| One training step | 25.2 s | 7.6 s | 7.8 s |
| Generate 20 tokens | 6.5 s | 6.9 s | 6.8 s |
These recorded measurements are from 16 October 2026: Apple M4 Max, release build, ARC memory mode, a five-token prompt and the median of three runs. Times include loading the weights.
Training gives the GPU larger pieces of work and gets a clear speedup. Short text generation still pays for many small GPU launches. Combining those jobs and adding faster numerical formats are further work; this version uses carefully checked 64-bit arithmetic.
Run it
From the repository root, after building Osprey:
# Choose the backend available on your machine: metal, opencl or host.
make -C src/toolchain/targets/llms gpt2 GPU=metal PROMPT="The capital of France is" TOKENS=20
make -C src/toolchain/targets/llms gpt2-train GPU=metal PROMPT="The capital of France is"
# The same generator, written in the Python flavor:
make -C src/toolchain/targets/llms gpt2 FLAVOR=python GPU=metal PROMPT="The capital of France is" TOKENS=20
The first run downloads the weights and tokenizer data. The program prints its actual backend and reports unsupported GPU work without silently substituting CPU execution.
Try the complete example, and compare its CPU and GPU runs. The model and its GPU calculations are Osprey code you can read and change.