纯 C 语言编写的 Microgpt 在苹果 M5 芯片上达到每秒 1000 万 token 的处理速度。
Microgpt in pure C hits 10M tps on Apple m5

原始链接: https://github.com/vixhal-baraiya/microgpt-c

本项目提供了一个极简的、无依赖的字符级 GPT Transformer 实现,完全使用标准 C 语言编写(仅依赖标准库)。该模型专为训练和推理设计,在单个源文件中完成了反向传播、Adam 优化和采样功能。 主要特性包括: * **高效性**:模型仅包含 4,192 个参数,却能实现有效的泛化,性能优于更大的三元组模型,且能在几秒钟内完成对数千个名字的训练。 * **可移植性**:构建系统支持 macOS、Linux 和 Windows,并可自动针对 ARM64 (NEON) 和 x86-64 (AVX2) 架构进行优化。 * **高性能**:该实现针对训练和推理的前向传播进行了优化,在 Apple M5 Pro 上速度可超过每秒 1000 万个标记 (tokens)。 由于不依赖任何外部库,该实现成为了理解 Transformer 架构核心机制的极佳参考。代码库中还附带了关于性能表现及专用单标记推理路径的详细文档。

Hacker News | 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 | 登录 在 Apple M5 上,纯 C 语言编写的 Microgpt 达到每秒 1000 万次请求 (github.com/vixhal-baraiya) 13 分,由 dhorthy 发布于 1 小时前 | 隐藏 | 过往 | 收藏 | 1 条评论 help throwa356262 4 分钟前 [–] 5 年前的 AMD Ryzen 5 5600H 能达到 700 万次?我没看错吧?那我也得在 Strix Halo 上试试。 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

The most atomic way to train and inference a GPT in pure, dependency-free C.

A character-level transformer with forward pass, backprop, Adam and sampling, in one C file with nothing beyond libc. It trains on ~32k names in a couple of seconds and generates new ones.

Or run it directly, on any corpus with one item per line:

./microgpt data/names.txt

Builds on macOS, Linux and Windows (MSYS2), on ARM64 with NEON and x86-64 with AVX2. The Makefile picks the flags for the host.

step 5000 / 20000 | loss 2.6036  (avg 2.2940)
step 10000 / 20000 | loss 1.9639  (avg 2.2564)
step 15000 / 20000 | loss 2.7007  (avg 2.2151)
step 20000 / 20000 | loss 2.3463  (avg 2.2201)

inference
sample  1: kayley
sample  2: maria
sample  3: arana
sample  4: shayan
sample  5: jayden
sample  6: saria
sample  7: kaylen
sample  8: amari
sample  9: alina
sample 10: mailyn
  c fp32+NEON       10168430 tok/sec

The model has 4192 parameters and generalises rather than memorises. Trained on 20000 of the 32033 names, it scores 2.2054 nats per character on those and 2.2039 on the 12033 it never saw, beating an interpolated trigram that has nearly five times as many parameters.

Training and inference use separate forward passes. gpt_forward stores activations for backprop; gpt_forward_infer is a specialised single-token path whose logits match it to within fp32 rounding. docs/PERFORMANCE.md covers how that path works and what limits it.

machine backend tok/sec
Apple M5 Pro NEON 10,168,430
AMD Ryzen 5 5600H AVX2 6,927,775
联系我们 contact @ memedata.com