메뉴
HN
Hacker News • 38일 전

순수 C 언어로 구현한 microGPT, 애플 M5에서 초당 1,000만 토큰 달성

IMP
6/10
핵심 요약

외부 의존성 없이 libc만으로 단일 C 파일에 GPT의 순전파, 역전파, Adam 옵티마이저, 샘플링을 모두 구현한 'microGPT-C'가 공개되었습니다. 약 3.2만 개의 이름 데이터를 몇 초 만에 학습해 새로운 이름을 생성하며, 애플 M5 Pro(NEON)에서 초당 약 1,017만 토큰, AMD 라이젠 5 5600H(AVX2)에서 초당 약 693만 토큰의 추론 속도를 기록했습니다. 학습이 아닌 일반화를 통해 파라미터 5배짜리 트라이그램 모델을 능가한다는 점이 인상적입니다.

번역된 본문

microGPT-C: 가장 원자적인(minimal) 방식으로 의존성 없는 순수 C 언어만으로 GPT를 학습하고 추론하는 프로젝트입니다. 문자 단위(character-level) 트랜스포머에 순전파(forward pass), 역전파(backprop), Adam, 샘플링이 모두 포함되어 있으며, libc 이외에는 아무것도 사용하지 않는 단일 C 파일로 구성되어 있습니다. 약 3.2만 개의 이름을 몇 초 만에 학습하고 새로운 이름을 생성합니다.

빌드 및 실행: make run 또는 한 줄에 하나의 항목이 있는 임의의 코퍼스에서 직접 실행: ./microgpt data/names.txt. macOS, Linux, Windows(MSYS2)에서 빌드 가능하며, ARM64에서는 NEON, x86-64에서는 AVX2를 사용합니다. Makefile이 호스트에 맞는 플래그를 자동으로 선택합니다.

학습 진행: step 5000 / 20000 | loss 2.6036 (평균 2.2940) step 10000 / 20000 | loss 1.9639 (평균 2.2564) step 15000 / 20000 | loss 2.7007 (평균 2.2151) step 20000 / 20000 | loss 2.3463 (평균 2.2201)

추론 샘플: kayley, maria, arana, shayan, jayden, saria, kaylen, amari, alina, mailyn

C fp32+NEON: 초당 10,168,430 토큰

참고 사항: 모델은 4,192개 파라미터를 가지며 암기가 아닌 일반화를 수행합니다. 32,033개 이름 중 20,000개로 학습했을 때, 학습에 사용한 이름에서는 문자당 2.2054 nats, 한 번도 보지 못한 12,033개 이름에서는 2.2039 nats를 기록하여, 파라미터가 거의 5배나 많은 보간 트라이그램(interpolated trigram) 모델을 능가합니다.

학습과 추론은 서로 다른 순전파 경로를 사용합니다. gpt_forward는 역전파를 위해 활성값(activations)을 저장하고, gpt_forward_infer는 단일 토큰 전용으로 특화된 경로로 fp32 반올림 오차 범위 내에서 로짓(logits)이 일치합니다. docs/PERFORMANCE.md에서 이 경로의 동작 원리와 한계를 다룹니다.

성능 비교(머신 / 백엔드 / 초당 토큰):

  • Apple M5 Pro / NEON / 10,168,430
  • AMD Ryzen 5 5600H / AVX2 / 6,927,775
원문 보기
원문 보기 (영어)
microGPT-C 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. Build and run make run 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 Notes 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
관련 소식