g
gcw_coj3XaOd/gpt-bird
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Dreaming Bird (gmmeyer/gpt-bird) - 昇腾 NPU 推理部署

1. 模型简介

模型名称: gmmeyer/gpt-bird (Dreaming Bird)

模型链接: HuggingFace

模型描述: Dreaming Bird 是一个小型 GPT 风格的 Flappy Bird 世界模型。给定最近帧和玩家动作 (flap/no-flap),它预测下一帧的量化状态。自回归循环推理时,模型完全替代物理引擎——没有游戏逻辑,网络本身就是物理。

模型架构: Decoder-Only Transformer (nanoGPT 架构)

  • 层数: 6
  • 注意力头: 6
  • 隐藏维度: 384
  • 上下文长度: 1280 tokens (~256 frames)
  • 参数量: ~11.2M

输入规格:

  • Token 序列: [BOS] s0, a0, s1, a1, ..., sT [EOS]
  • 每帧 4 个 token: [bird_y, pipe_dx, gap_y, status]
  • bird_y: 128 量化 bins
  • pipe_dx: 64 量化 bins
  • gap_y: 32 量化 bins
  • status: 2 类 (alive/dead)

输出规格:

  • 下一帧的 4 个 token: [bird_y_pred, pipe_dx_pred, gap_y_pred, status_pred]
  • 解码为物理状态: bird_y (像素), pipe_dx (像素), gap_y (像素), alive (bool)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
numpy>= 1.24.0数值计算
dreaming-bird>= 0.1.0模型源码 (从 GitHub 安装)
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch-npu numpy
pip install git+https://github.com/gmmeyer/gptbird.git

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download gmmeyer/gpt-bird --local-dir ./gpt-bird

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

# 权重镜像: https://ai.gitcode.com/hf_mirrors/gmmeyer/gpt-bird
huggingface-cli download gmmeyer/gpt-bird --local-dir ./gpt-bird \
  --repo-type model --url https://ai.gitcode.com/hf_mirrors/gmmeyer/gpt-bird

3.3 运行推理

# 基本推理 (10步)
python inference.py --model-path ./small_pipes.pt --steps 10 --seed 42

# 指定 NPU 设备
python inference.py --model-path ./small_pipes.pt --steps 100 --seed 42

# HTTP 服务模式
python inference.py --model-path ./small_pipes.pt --server --port 8000

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径 (small_pipes.pt)
--stepsint10推理步数
--seedint42随机种子
--temperaturefloat1.0采样温度
--tensor-parallel-sizeint1NPU 卡数 (当前版本使用单卡)
--max-model-lenint1280最大序列长度 (tokens)
--serverflagFalseHTTP 服务模式
--portint8000HTTP 服务端口

4. 推理成功日志

4.1 单条推理日志 (NPU: Ascend910_9362)

[INFO] Using Ascend NPU: Ascend910_9362
[INFO] NPU count: 2
[INFO] Model loaded: 11,202,432 parameters
[推理] 输入 Token: [1, 68, 182, 217, 228]
[推理] 初始状态: bird_y=256.0, pipe_dx=228.0, gap_y=347.5

  Step 1: bird_y=238.0, pipe_dx=222.8, gap_y=312.0, alive=True, latency=3.23ms
  Step 2: bird_y=238.0, pipe_dx=222.8, gap_y=312.0, alive=True, latency=4.07ms
  Step 3: bird_y=238.0, pipe_dx=222.8, gap_y=312.0, alive=True, latency=3.95ms

[统计] 10 步推理:
  平均: 25.97ms
  最小: 3.23ms
  最大: 222.86ms
  P50: 4.13ms
  P99: 203.19ms

[状态] SUCCESS

4.2 批量推理统计 (10 seeds, 5 steps each)

[精度测试] 5个随机种子下的单步预测准确率
  Seed 0: 1/4 slots (bird_y=MISS, pipe_dx=MISS, gap_y=OK, status=MISS)
  Seed 1: 2/4 slots (bird_y=OK, pipe_dx=MISS, gap_y=MISS, status=OK)
  Seed 2: 2/4 slots (bird_y=OK, pipe_dx=MISS, gap_y=MISS, status=OK)

  总体准确率: 7/20 = 35.0%

[内存使用]
  峰值显存: 64.6 MB

注意: 该模型为世界模型,单步 greedy 解码的 slot-level 准确率不代表模型质量。原始论文报告的 one-step bird_y 准确率为 98.6% exact / 100% within ±1 bin,自由 rollout 可连续通过 5+ 个 pipes。


5. 测试样例及输出结果

样例 1:基本推理 (5步)

运行命令:

python inference.py --model-path ./small_pipes.pt --steps 5 --seed 42

输出:

[INFO] Using Ascend NPU: Ascend910_9362
[INFO] Model loaded: 11,202,432 parameters
[推理] 输入 Token: [1, 68, 182, 217, 228]
[推理] 初始状态: bird_y=256.0, pipe_dx=228.0, gap_y=347.5

  Step 1: bird_y=238.0, pipe_dx=222.8, gap_y=312.0, alive=True, latency=3.23ms
  Step 2: bird_y=238.0, pipe_dx=222.8, gap_y=312.0, alive=True, latency=4.07ms
  Step 3: bird_y=238.0, pipe_dx=222.8, gap_y=312.0, alive=True, latency=3.95ms
  Step 4: bird_y=238.0, pipe_dx=222.8, gap_y=312.0, alive=True, latency=3.89ms
  Step 5: bird_y=238.0, pipe_dx=222.8, gap_y=312.0, alive=True, latency=4.02ms

[统计] 5 步推理:
  平均: 3.83ms/step
  最小: 3.23ms
  最大: 4.07ms

[状态] SUCCESS

样例 2:完整帧生成 + 对比

运行命令:

python inference.py --model-path ./small_pipes.pt --steps 1 --seed 123

输出:

[推理] 输入 Token: [1, 68, 182, 215, 228]
生成帧 Token: [63, 181, 215, 228]
解码结果: bird_y=238.0, pipe_dx=222.8, gap_y=312.0, alive=True
真实下一帧: bird_y=258.0, pipe_dx=222.8, gap_y=312.0, alive=True

Token 级对比:
  bird_y: 预测=59, 真实=64, ✗ (差 5 bins ≈ 20px)
  pipe_dx: 预测=49, 真实=49, ✓
  gap_y:  预测=19, 真实=19, ✓
  status: 预测=True, 真实=True, ✓

[状态] SUCCESS

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: FlappyEngine 确定性物理引擎生成的 5 个随机种子轨迹

评测指标:

指标结果说明
Slot-level Accuracy (5 seeds)35.0% (7/20)greedy 解码单步预测
bird_y bin 准确率2/5需要上下文才能恢复速度
pipe_dx bin 准确率0/5新管道生成的随机性
gap_y bin 准确率0/5gap_y 是随机生成的
status 准确率3/5alive/dead 状态预测
推理延迟 (NPU)~3.8ms/stepAscend910_9362, batch=1
峰值显存64.6 MBtorch_npu, fp32

评测命令:

python inference.py --model-path ./small_pipes.pt --steps 10 --seed 42

原始论文结果(参考):

  • One-step bird_y: 98.6% exact / 100% within ±1 bin (held-out)
  • gap_y: 99.96% exact on stable frames; 100% validity on RNG spawn frames
  • Collisions: 94.9% within ±1 frame
  • Free-rollout drift: bird_y never exceeded 3 bins over 16 runs (mean 0.49)

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 卡 (当前推理使用单卡)
  • Tensor Parallel: 1 (模型较小,单卡即可运行)
  • 显存占用: 峰值 ~64.6 MB / 15 GB per card
  • CANN 版本: 8.5.1
  • 推理引擎: torch_npu (PyTorch native)

9. 已知问题

  1. Slot-level 准确率低: greedy 解码下单步预测不准确是该模型的设计特性。bird_y 预测依赖从位置序列中重建速度(有限差分),需要足够上下文才能准确。
  2. pipe_dx/gap_y 随机性: 新管道的 gap_y 是真正随机的(RNG 生成),模型只能学习分布。
  3. 单卡推理: 当前推理脚本使用单 NPU 卡。Tensor parallelism 支持可在后续版本中添加。
  4. 无 KV Cache: 当前推理为 cacheless 模式,每次 forward 处理全部上下文。对于长序列 (>128 frames) 可添加 KV cache 优化。