Skip to content

NitsuaLu/microgpt-experiments

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

microgpt-experiments

Some attempts, based on microgpt.
一些尝试,基于 MicroGPT。


MicroGpt — 两个版本

文件 说明
MicroGpt/microgpt_original.py Karpathy 原版,纯 Python 手写 autograd(Value 类)和 Adam 优化器
MicroGpt/microgpt_pytorch.py PyTorch 迁移版,用 torch.Tensor + torch.optim.Adam 替代手写组件

核心差异:

原版 PyTorch 版
自动微分 手写 Value 类,构建计算图 PyTorch autograd
线性层 嵌套循环 sum(wi * xi ...) tensor 运算 x @ w.T
优化器 手写 Adam torch.optim.Adam

注:microgpt_original.py 仅修改了数据集路径(../input.txtinput.txt),其余与 Karpathy 原版一致。microgpt_pytorch.py 是 PyTorch 迁移尝试的最初版本。

快速开始

python MicroGpt/microgpt_original.py   # 纯 Python,零依赖
python MicroGpt/microgpt_pytorch.py    # 需要 torch

数据集 input.txt 首次运行时会自动下载。


KV Cache Benchmark

KV Cache 性能对比实验,比较 attention 计算在有无缓存下的复杂度差异(O(n) vs O(n²))。

文件说明

文件 作用
KV_Cache/microgpt_kv_cache.py 带 KV Cache 的完整训练 + 推理,每步打印耗时
KV_Cache/microgpt_no_kv_cache.py 无缓存版本,推理时每生成一个新 token 都从头重跑全序列
KV_Cache/microgpt_benchmark.py 训练一次,用同一份权重对比两种推理方式,画图验证
KV_Cache/attention_complexity.py 纯 micro-benchmark,不训模型,直接测 attention 的 O(n) vs O(n²) 复杂度

快速开始

pip install torch matplotlib seaborn

# 1. 复杂度对比(最快,秒出图)
python KV_Cache/attention_complexity.py

# 2. 实际模型对比(训练 + benchmark + 画图)
python KV_Cache/microgpt_benchmark.py

# 3. 单独跑两个版本看每步耗时
python KV_Cache/microgpt_kv_cache.py
python KV_Cache/microgpt_no_kv_cache.py

数据集 input.txt 首次运行时会自动下载。

预期结果

  • attention_complexity.py 的 log-log 图上,无缓存斜率为 2(O(n²)),有缓存斜率为 1(O(n))
  • microgpt_benchmark.py 验证两个版本生成的名字完全一致,KV Cache 版本快 8-10 倍(取决于 block_size)

KV Cache: O(n) vs O(n²)

About

Some attempts, based on MicroGPT.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages