本文档记录 gpt-bird 在华为 Ascend NPU 上的推理适配与验证结果。gpt-bird 是由 Gregory Meyer 开发的神经网络 Flappy Bird 世界模型,一个纯基于神经网络的游戏物理引擎替代品。
该模型是一个 6 层 Decoder-only Transformer(d_model=384),输入为 Flappy Bird 游戏的帧状态 token 和玩家动作(flap/noflap),输出为下一帧的量化状态。循环自回归运行(~30-60 fps)时,模型完全替代了游戏物理引擎。
模型链接(HuggingFace):https://huggingface.co/gmmeyer/gpt-bird
参考文档:
| 组件 | 版本 |
|---|---|
torch | 2.9.0+cpu |
torch_npu | 2.9.0.post1+gitee7ba04 |
numpy | 1.26.4 |
onnx | 1.22.0 |
| CANN | 8.5.1 |
Ascend 910(单卡)/opt/atomgit/0821/gpt-bird3.11pip install -r requirements.txt如需使用阿里云镜像:
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ --extra-index-url https://pypi.org/simple/推理脚本 inference.py 自动检测可用设备(优先 NPU),从 ONNX 权重加载并执行自回归解码:
python inference.py指定设备:
python inference.py --device npu:0
python inference.py --device cpupython inference.py验证结果:
推理日志(run_npu.log)中包含完整终端输出和 npu-smi 进程占用采样:
[INFO] 模型加载成功 | 参数量: 12,087,168 | 耗时: 1.30s
[INFO] 生成 token 数: 51
[INFO] 生成序列: [1, 85, 187, 187, 227, 187, 187, 187, 227, 227, ...]NPU 进程占用:
| NPU Chip | Process id | Process name | Process memory(MB) |
| 7 0 | 14135 | python3 | 182 || 组件 | 参数 |
|---|---|
| 嵌入层 | vocab=232, d_model=384 |
| 位置编码 | 可学习绝对位置编码,block_size=1280 |
| Transformer 层数 | 6 |
| 注意力 | Gated Linear Attention(6 头,d_kv=64) |
| MLP | SwishGLU(d_ff=1536) |
| 输出层 | 权重绑定(lm_head = wte.weight.T) |
总计约 12M 参数。
MLP 采用 SwishGLU 变体,公式为:
output = 0.5 * (1 + erf(x @ w_fc / sqrt(2))) * (x @ w_fc) @ w_down验证自 ONNX 图节点序列:fc/MatMul → Div(sqrt(2)) → Erf → Add(1) → Mul(fc_out) → Mul(0.5) → proj/MatMul
每帧包含 4 个 token:
| Slot | 含义 | 范围 |
|---|---|---|
bird_y | 小鸟垂直位置 | 128 bins |
pipe_dx | 管道水平距离 | 64 bins |
gap_y | 管道间隙垂直位置 | 32 bins |
status | 存活状态 | alive/dead |
输入:[BOS token = 1]
输出 token 序列(部分):[1, 85, 187, 187, 227, 187, 187, 187, 227, 227, 227, ...]
status_dead token,表示游戏结束model.onnx 提取并加载到 PyTorch nn.Module,避免使用 onnx2torch GraphModule(后者存在 torch_npu 兼容问题)onnx2torch 仅用于参考验证,已移除)onnxruntime-web + WebGPU,无需服务器端推理