一个只有一千多万参数的 Transformer,训练目标不是做对话、不是识图,而是 学会当 Flappy Bird 的物理引擎。给它最近几帧的画面状态和你按不按空格, 它就吐出下一帧鸟在哪儿、管子到哪儿、缝隙在哪儿——循环跑起来,等于在模型 脑子里"想"着一局游戏。本文是把它弄到昇腾 910B 上用 vLLM-Ascend 服务化的 全过程记录。
一句话:decoder-only 的 nanoGPT,只不过"词表"是游戏状态量化出来的 token。
DreamGPT,来自 gmmeyer/gpt-bird(MIT)。bird_y(128 档)、pipe_dx(64 档)、gap_y(32 档)、status(活/死)。
词表总共 232 个 id,每个槽位的 id 区间互不重叠,天生就能靠掩码保证"生成
出来的一定是合法帧"。官方给了 small_pipes.pt(PyTorch 权重)和 model.onnx(给浏览器跑 WebGPU
用)。服务化推理用 PyTorch 权重。
这次实测跑通的环境,照抄即可:
| 东西 | 版本 |
|---|---|
| 芯片 | 昇腾 Ascend910(Atlas 800 A2) |
| 系统 | Linux aarch64 |
| CANN | 8.5.1 |
| Python | 3.11.14 |
| PyTorch / torch_npu | 2.9.0+cpu / 2.9.0.post1 |
| vLLM / vllm_ascend | 0.18.0 / 0.18.0 |
| transformers | 4.57.6 |
机上一共 2 张卡(torch.npu.device_count() 返回 2),我用的是其中一张
(ASCEND_RT_VISIBLE_DEVICES=0,对应 npu-smi 里 Phy-ID 6)。
优先走 GitCode 镜像,拉不到再换 ModelScope,最后才是 HuggingFace 直连:
export HF_ENDPOINT=https://ai.gitcode.com/hf_mirrors
python -c "from huggingface_hub import snapshot_download; snapshot_download('gmmeyer/gpt-bird', local_dir='<模型目录>')"小提示:GitHub 源码在容器里 git clone 老断流(HTTP/2 问题),改成直接下
tarball 就好:wget https://github.com/gmmeyer/gptbird/archive/refs/heads/main.tar.gz。
.pt 是自定义类序列化的,得先把源码包加进路径、把几个 config 类加入
safe_globals 才能 load:
import torch, torch.serialization
from dreaming_bird.config import ModelConfig, TokenizerConfig, EngineConfig
torch.serialization.add_safe_globals([ModelConfig, TokenizerConfig, EngineConfig])
ckpt = torch.load('small_pipes.pt', map_location='cpu', weights_only=True)这里踩了个坑:wte.weight 和 lm_head.weight 是同一个共享张量,直接转
safetensors 会报 "tensors share memory"。把 lm_head.weight clone() 一下
就好。转完得到 model.safetensors(约 45MB,fp32)。
模型目录还要有 config.json 和 tokenizer.json。vLLM 用 transformers 读
配置,所以 model_type 必须填 transformers 认识的——我用的 "gpt2"(反正
DreamGPT 就是 nanoGPT 血统),架构解析交给 architectures: ["DreamGPT"] 和
运行时注册。max_position_embeddings 填 1280(wpe 位置嵌入长度)。
vLLM 源码只读,不能往 registry.py 里加行,就运行时注册:
from vllm.model_executor.models.registry import ModelRegistry
ModelRegistry.register_model("DreamGPT", "gpt_bird:DreamGPTForCausalLM")模型类 DreamGPTForCausalLM 是标准 vLLM 接口(embed_input_ids / forward /
compute_logits / load_weights),内部用 vLLM 的 RMSNorm、QKVParallelLinear、
Attention、ParallelLMHead 搭出来,权重键名和 checkpoint 一一对应,
AutoWeightsLoader 直接吃。算子全是原生 PyTorch,昇腾没有兼容性障碍。
启动命令:
ASCEND_RT_VISIBLE_DEVICES=0 VLLM_ENABLE_V1_MULTIPROCESSING=0 \
PYTHONPATH=<插件目录> python3 start_server.py \
--model <模型目录> --served-model-name gpt-bird \
--dtype bfloat16 --max-model-len 1280 --max-num-seqs 8 \
--gpu-memory-utilization 0.08 --enforce-eager --port 8071VLLM_ENABLE_V1_MULTIPROCESSING=0 是为了让运行时注册的架构在主进程就能被
模型加载器看到,省得 worker 子进程找不到 DreamGPT。模型太小,
--max-model-len 1280 顶满,--gpu-memory-utilization 给 0.08 就够。
curl http://127.0.0.1:8071/v1/models
# 返回 "id": "gpt-bird","max_model_len": 1280,即就绪我写了个 inference.py:先在 CPU 上跑一段真实物理引擎回放当地真,把前几帧
当上下文发给服务,逐槽位(用 logit_bias 把非法 id 压到 -100)预测下一帧。
跑法:
python3 inference.py --endpoint http://127.0.0.1:8071 --model gpt-bird \
--context-frames 6 --compare --dream 8 --seed 7一次真实结果:
context: 31 tokens (BOS + 6 frames x 4 + 6 actions)
prompt ids: 001 068 182 208 228 230 068 181 208 228 230 068 180 208 228 230 ...
model output ids: [74, 177, 208, 228]
model frame : bird_y=282.0, pipe_dx=204.8, gap_y=200.0, alive=True
ground truth : bird_y=281.2, pipe_dx=204.0, gap_y=197.2, alive=True
exact token match: True latency: 57.3 ms (4 slots)exact token match: True —— 四个槽位的 token 和真值一模一样,就是官方说的
one-step 准确率 98.6% 的现场版。
再让它"做梦"(输出回灌,连续推 8 帧),行为自洽:鸟一直往下掉(282→290→298), 一按 FLAP 就往上窜(298→310→302),管子匀速左移(pipe_dx 每帧 -4.5 左右), 没出现飞出屏幕或莫名死亡的帧。
说实话这模型太小了,910B 对它来说是牛刀杀鸡,AICore 基本是个位数百分比。 关键是证明了"世界模型也能在昇腾上被 vLLM 服务化"这条路走得通。
| 项 | 数值 |
|---|---|
| 权重加载 | 0.0209 GB(bf16) |
| 单帧预测(4 槽位) | ~57 ms |
| 串行吞吐 | ~19.6 帧/秒 |
| 并发下 AICore | 3%-6% |
| 并发下 HBM | ~8.3 GB / 65.5 GB |
| 服务进程显存 | ~5.27 GB |
推理期间抓的 npu-smi info(原样):
+===========================+===============+====================================================+
| 3 Ascend910 | OK | 180.2 43 0 / 0 |
| 0 6 | 0000:0B:00.0 | 5 0 / 0 8324 / 65536 |
+------------------------------------------------------------------------------------------------+
| NPU Chip | Process id | Process name | Process memory(MB) |
+===========================+===============+====================================================+
| 3 0 | 31384 | VLLMEngineCor | 5270 |
+===========================+===============+====================================================+拿 CPU 物理引擎当裁判,把模型预测和真值摆在一起看:
| 槽位 | 模型 | 真值 | 差多少 |
|---|---|---|---|
| bird_y | 282.0 | 281.2 | 0.8 px |
| pipe_dx | 204.8 | 204.0 | 0.8 px |
| gap_y | 200.0 | 197.2 | 2.8 px |
| status | alive | alive | 0 |
几像素的偏差基本是量化粒度的锅(512px 分成 128 档,一档 4px),落在 1 档以内,
说明模型学到的"物理"和真引擎一致。换个 seed 再测(seed=42),输出
[71,179,217,228] 依旧和真值完全一致。
按被坑的顺序排,全是亲测:
git clone,直接下 tarball。.pt 报 Unsupported global → 把 ModelConfig 等类加进
torch.serialization.add_safe_globals。lm_head.weight 先 clone()。model_type: dreamgpt → 改成 model_type: gpt2,架构靠注册。gpt_bird 模块 → 设 PYTHONPATH 到插件目录,
同时 VLLM_ENABLE_V1_MULTIPROCESSING=0 走进程内引擎。logit_bias 逐槽位把非法 id 压下去,等价于上游
legal_mask。跑起来后用仓库里的 inference.py 就能玩:python3 inference.py --endpoint http://127.0.0.1:8071 --model gpt-bird --compare --dream 8。