每帧编码为 4 个 token —— bird_y, pipe_dx, gap_y, status,采用 slot-constrained 解码(逐字段掩码到合法 id 区间),因此畸变帧在构造上不可能出现。gap_y(新露出管道的间隙)是采样生成(真正不可预测,梦境自行"发明"),其余槽位贪心。
| 组件 | 版本 |
|---|---|
| torch | 2.9.0 |
| torch-npu | 2.9.0.post1 |
| fastapi | 0.123.10 |
| uvicorn | 0.46.0 |
| CANN | 8.5.1 |
| NPU | Ascend 910(64GB HBM) |
本模型为自定义 decoder-only transformer(非标准 LLM),vllm-ascend 无法直接服务,未使用 vLLM。
每个状态场占用互不相交的连续 id 区间(vocab_size = 232):
| 字段 | id 区间 | 量化 bin | 物理范围 |
|---|---|---|---|
PAD/BOS/EOS/SEP | 0 ~ 3 | - | - |
bird_y | 4 ~ 131 | 128 bins | 0 ~ 512 px |
pipe_dx | 132 ~ 195 | 64 bins | 0 ~ 288 px |
gap_y | 196 ~ 227 | 32 bins | 0 ~ 512 px |
status | 228 ~ 229 | 2 类 | alive / dead |
action | 230 ~ 231 | 2 类 | noflap / flap |
流顺序为 S_t, A_t, S_{t+1}, ...:动作跟在它作用的帧之后。推理时由玩家提供动作 token(不生成),模型生成下一帧的 3 个世界 token。
| 配置项 | 值 |
|---|---|
| 模型类型 | DreamGPT(nanoGPT 风格 decoder) |
| n_layer / n_head / d_model | 6 / 6 / 384 |
| block_size | 1280 token(256 帧) |
| 归一化 | RMSNorm(pre-norm,残差前) |
| 注意力 | 因果 SDPA,qkv 合一投影(无 bias) |
| 前馈 | 4×d_model GELU + 投影 |
| 词表 | 232(token 化状态) |
| 权重绑定 | wte.weight ≡ lm_head.weight |
| 参数量 | 11,202,432 |
| 权重大小 | small_pipes.pt ≈ 44.8MB(float32) |
reset:用确定性物理引擎生成一局初始帧,编码为 [BOS] + 4 个状态 token 作为上下文;step(action):把动作 token 追加到上下文 → 逐槽位生成 3 个世界 token(bird_y 贪心、pipe_dx 贪心、gap_y 采样,每个槽位掩码到合法区间)→ 反量化得到下一帧观测;collides()(鸟-管道碰撞几何)判定,比模型自身 status token 在长自由 rollout 中更可靠,并把几何正确的 status token 追加回上下文保持 on-distribution;| # | 适配点 | 说明 |
|---|---|---|
| 1 | 非 vLLM,不走 vLLM-Ascend | gpt-bird 是自定义架构的 decoder transformer(vocab 232、状态 token 流、slot-constrained 解码),vllm-ascend 只能服务标准 LLM 架构,无法直接加载;采用 torch_npu + FastAPI 方案在昇腾 NPU 上服务化 |
| 2 | checkpoint 反序列化 shim | small_pipes.pt 内 pickle 以全局引用 dreaming_bird.config.ModelConfig 等保存配置;inference.py 内置自包含 dataclass 并注册 dreaming_bird.config 命名空间 shim,无需安装源码包即可 torch.load |
| 3 | 完全自包含 | 模型结构 / Tokenizer / 物理引擎 / 逐帧自回归循环全部内嵌于 inference.py,无第三方模型库依赖(不依赖 transformers/multimolecule) |
| 4 | 槽位约束解码 | 每帧 4 个 token 逐槽位 logits[~legal_mask] = -inf,生成的帧永远是合法字段 token |
| 5 | 几何守卫 status | 长 rollout 中不用模型学的 alive/dead token,改用 collides() 几何判定,再回填上下文 |
| 6 | 服务化并发 | FastAPI 端点内用 threading.Lock 保护有状态的 stepper(上下文在多次 /step 间累积) |
| 7 | 推理设备 | 通过 torch_npu 注册 npu 后端,模型 .to("npu:0") 在 Ascend 910 上计算;权重 float32(44.8MB),峰值 HBM 约 2.2GB |
模型验证(seed=0,脚本控制器在"梦境"中自由飞行,昇腾 NPU):
pip install -r requirements.txttorch-npu 需与 torch 版本严格匹配(本仓库验证 torch 2.9.0 + torch-npu 2.9.0.post1)。
# 方式一:GitCode 镜像(用户提供的权重来源)
git clone https://ai.gitcode.com/hf_mirrors/gmmeyer/gpt-bird.git
# (权重为 Git LFS:small_pipes.pt ≈ 44.8MB,需 git-lfs pull 或经 LFS batch API 下载)
# 方式二:HuggingFace 直连
git clone https://huggingface.co/gmmeyer/gpt-bird
# 方式三:经 LFS batch API 下载权重(无 git-lfs 时)
python3 - <<'EOF'
import json, urllib.request
repo = "https://gitcode.com/hf_mirrors/gmmeyer/gpt-bird.git"
body = {"operation":"download","transfers":["basic"],
"objects":[{"oid":"1f82c66937abf2946c771cd1ccdd7b657dab83c31a4b94f6f552f7665d37eaf5","size":44826338}]}
req = urllib.request.Request(f"{repo}/info/lfs/objects/batch", data=json.dumps(body).encode(),
headers={"Content-Type":"application/vnd.git-lfs+json","Accept":"application/vnd.git-lfs+json"})
href = json.load(urllib.request.urlopen(req))["objects"][0]["actions"]["download"]["href"]
urllib.request.urlretrieve(href, "small_pipes.pt")
EOF# 缺省:脚本控制器驱动世界模型自由飞行 300 帧
python3 inference.py --checkpoint small_pipes.pt --device npu:0 --seed 0 --frames 400 --policy scripted
# 随机策略 / 不操作策略
python3 inference.py --checkpoint small_pipes.pt --device npu:0 --policy random
python3 inference.py --checkpoint small_pipes.pt --device npu:0 --policy noop
# 探测 NPU 设备
python3 inference.py --probe# 启动服务(默认 0.0.0.0:8000,此处演示用 127.0.0.1:8016;多卡时可用 ASCEND_RT_VISIBLE_DEVICES 选择空闲芯片)
export ASCEND_RT_VISIBLE_DEVICES=1
python3 inference.py --serve \
--checkpoint small_pipes.pt \
--device npu:0 \
--host 127.0.0.1 --port 8016调用示例:
# 健康检查
curl http://127.0.0.1:8016/health
# 模型信息
curl http://127.0.0.1:8016/info
# 重置一局梦境(seed 决定初始管道间隙)
curl -X POST http://127.0.0.1:8016/reset -H "Content-Type: application/json" -d '{"seed": 0}'
# 单步推理(玩家动作:flap / noflap,返回下一帧梦到的状态)
curl -X POST http://127.0.0.1:8016/step -H "Content-Type: application/json" -d '{"action": "flap"}'
# 自由 rollout(脚本控制器"在梦里飞行")
curl -X POST http://127.0.0.1:8016/rollout \
-H "Content-Type: application/json" \
-d '{"seed": 0, "frames": 300, "policy": "scripted"}'
命令行推理输出(seed=0,脚本策略,昇腾 NPU):
================================================================
gpt-bird (Dreaming Bird) — 昇腾 NPU 神经世界模型
模型: gmmeyer/gpt-bird
checkpoint: small_pipes.pt
设备: npu:0 (Ascend910_9362)
================================================================
正在加载模型(首次加载会编译算子,可能较慢)...
模型加载完成,耗时 1.44s,参数量 11.202M,block_size=1280
[rollout] seed=0 policy=scripted 目标 400 帧
survived_frames: 400
pipes_passed: 8
alive: True
avg fps: 48.1
avg ms/frame: 20.79
示例轨迹(前 10 帧):
frame action bird_y pipe_dx gap_y alive
0 noflap 258.00 222.75 296.00 True
1 noflap 258.00 218.25 296.00 True
2 noflap 262.00 218.25 296.00 True
3 noflap 270.00 213.75 296.00 True
4 noflap 274.00 209.25 296.00 True
5 noflap 282.00 204.75 296.00 True
6 noflap 290.00 200.25 296.00 True
7 flap 278.00 195.75 296.00 True
8 noflap 270.00 191.25 296.00 True
9 noflap 262.00 186.75 296.00 True
SUCCESS服务化推理返回(POST /rollout,seed=0,脚本策略,昇腾 NPU):
{
"model": "gmmeyer/gpt-bird",
"seed": 0,
"policy": "scripted",
"frames": 205,
"survived_frames": 205,
"pipes_passed": 3,
"alive": false,
"died_in_dream": true,
"fps": 53.7,
"ms_per_frame": 18.63,
"trajectory": [{"frame": 0, "action": "noflap", "bird_y": 258.0, "pipe_dx": 222.75, "gap_y": 296.0, "alive": true}, "..."]
}
gap_y槽位为采样生成(新露出管道的间隙不可预测),因此不同 seed / 不同运行间轨迹有随机性。 预热后单步推理约 16 ms(首次含算子编译约 260 ms);服务化 rollout 平均 53.7 fps。

服务健康检查返回:
{
"status": "ok",
"model": "gmmeyer/gpt-bird",
"device": "npu:0",
"npu": {"available": true, "device_count": 1, "names": ["Ascend910_9362"]}
}模型信息(GET /info):
{
"object": "list",
"data": [{
"id": "gpt-bird",
"task": "neural-world-model",
"library_name": "pytorch",
"pipeline_tag": "text-generation",
"params": 11202432,
"arch": "DreamGPT (nanoGPT-style decoder, RMSNorm, weight-tied)",
"model_cfg": {"n_layer": 6, "n_head": 6, "d_model": 384, "block_size": 1280},
"vocab_size": 232,
"tokens_per_frame": 4,
"checkpoint": "small_pipes.pt",
"load_seconds": 1.4
}]
}峰值 HBM 占用约 2.2GB(npu-smi 实测,进程内存)。

# 1. 环境准备(依赖 + 权重)
pip install -r requirements.txt
git clone https://ai.gitcode.com/hf_mirrors/gmmeyer/gpt-bird.git # 下载 LFS 权重 small_pipes.pt
# 2. 命令行推理
python3 inference.py --checkpoint small_pipes.pt --device npu:0 --seed 0 --frames 400 --policy scripted
# 3. 服务化推理
python3 inference.py --serve --checkpoint small_pipes.pt --device npu:0 --host 127.0.0.1 --port 8016
curl http://127.0.0.1:8016/health
curl -X POST http://127.0.0.1:8016/rollout -H "Content-Type: application/json" -d '{"seed": 0, "frames": 300}'贡献者: liuhongwei-2026 | 赛道: 模型适配赛道