waylong/NeoQuasar-WindFM-Tokenizer-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

NeoQuasar/WindFM-Tokenizer — Ascend NPU 适配

NPU Ascend910 #NPU

模型简介

NeoQuasar/WindFM-Tokenizer 是 WindFM: An Open-Source Foundation Model for Zero-Shot Wind Power Forecasting 的专用时序 Tokenizer,采用 Encoder-Decoder Transformer + Binary Spherical Quantization (BSQ) 将连续多元风电时序 (风速、风向、功率、密度、温度、气压) 离散化为层次化 tokens,服务于后续 WindFM 自回归生成。

  • Hugging Face: NeoQuasar/WindFM-Tokenizer (pipeline_tag time-series-forecasting, tag Wind Power)
  • GitHub 官方: shiyu-coder/WindFM (model/module.py, model/windfm.py)
  • Revision: 8f610d340e9063493926dd4402667faafb9e857b (config.json SHA)
  • 权重: model.safetensors 15.8 MB, 3,958,042 参数 (FP32), config.json 记录 d_in=6, d_model=256, n_heads=4, ff_dim=512, n_enc_layers=4, n_dec_layers=4, s1_bits=10, s2_bits=10, group_size=5, codebook_dim=20
  • 架构: WindFMTokenizer = Linear(embed) -> 3×TransformerBlock(Encoder) -> Linear(quant_embed,20) -> BSQuantizer(s1=10,s2=10) -> post_quant_embed(20->256) & post_quant_embed_pre(10->256) -> 3×TransformerBlock(Decoder) -> Linear(head,6); TransformerBlock = RMSNorm + MultiHeadAttentionWithRoPE + RMSNorm + FeedForward(SiLU)
  • 量化: BSQuantizer 包含 BinarySphericalQuantizer(embed_dim=20, group_size=5),EMA-free,二值球面量化 + 可微熵正则
  • 输入/输出: [batch, seq_len, 6] -> encode(half=True) -> ([batch,seq_len] s1, [batch,seq_len] s2) ( vocab 1024 each) -> decode -> [batch,seq_len,6] 重构
  • 任务: 表格/时序 重构 (regression) - 量化-反量化误差 MAE 0.06 (归一化空间)
  • 许可: MIT

数据契约

项值
输入[batch, seq_len, d_in=6] float32, 列顺序固定 ['wind_speed','wind_direction','power','density','temperature','pressure']
预处理按窗口 x_mean/std 标准化 (x - mean)/(std+1e-6) + clip [-5,5]; 与 WindFMPredictor.predict 的 x_mean/std + clip=5 一致;时间戳 time 仅用于生成 stamp 本 tokenizer 不消费
输出重构 [batch, seq_len, 6] (归一化空间) + bsq_loss + quantized [batch,seq_len,20]; encode(half=True) 输出 s1 [batch,seq_len] (0-1023), s2 [batch,seq_len] (0-1023)
列契约列名、顺序、dtype float32 固定;缺失值校验 isnull 时抛错;无文本列
数据来源examples/data/121522.csv (420×7, time UTC 2007-01-05 起, 6 数值特征), SHA256 2b034aa3c349015f00563cfa9c6f30176bada95a0f3b5e695e2e229aeefe6207
窗口lookback=240 (前 240 行), seq_len 可变 (适配测试 240),batch=1
逆缩放x_denorm = x_norm*(std+1e-6)+mean 展示用;一致性比较在归一化空间进行

环境

  • 硬件: Ascend 910 ×2 (npu:0 Ascend910_9362, npu-smi 25.5.5, CANN 8.5.1)
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, numpy 1.26.4, pandas 3.0.2, einops 0.8.1, safetensors 0.6.2, huggingface_hub 0.36.2
  • 设备: torch.npu.is_available()==True, count 2, torch.npu.get_device_name(0)==Ascend910_9362, torch.npu.current_device()==0
  • 权重缓存: /tmp/models/WindFM-Tokenizer/model.safetensors (hf 下载)
  • 显存: 权重 ~15.8 MB (FP32), 激活 <200 MB, 总峰值 <1 GB (npu-smi HBM 3108/65536 MB idle)

安装

pip install -r requirements.txt
# 另需 torch_npu 与 CANN 8.5.1 (Ascend 910)

NPU 推理

默认 python inference.py 自动完成真实 NPU 推理 (CPU 基线 + NPU,对比,性能):

python inference.py

行为:

  1. 检查 torch.npu.is_available() 与 npu-smi info,设置 torch.npu.set_device(0),dummy torch.randn(2,3).to("npu:0") 验证链路
  2. 查找或下载权重 (/tmp/models/WindFM-Tokenizer 优先,否则 hf hub)
  3. 实例化 WindFMTokenizer(**config.json),从 model.safetensors 加载 3.95M 参数,CPU 与 NPU 同权重、同输入、同预处理、同 dtype
  4. 数据: 121522.csv 前 240 步 6 特征 -> 标准化 clip -> [1,240,6]
  5. CPU: forward -> (z_pre,z), encode(half) -> s1/s2, decode -> 重构 [1,240,6], MAE 0.063
  6. NPU: 模型与 Tensor 显式 .to("npu:0"),torch.npu.synchronize() 前后计时,首轮 198 ms (含图编译),稳定 9.12 ms
  7. 输出 decode/index、一致性、性能、设备验证

inference.py 自包含 module.py 全部量化与 Transformer 实现,无需额外安装 WindFM 源码。运行时无网络请求,权重从本地加载。

真实结果

以下为 npu:0 的真实完整输出 (节选,完整见 assets/model_result.png):

[ENV] torch 2.9.0+cpu torch_npu 2.9.0.post1+gitee7ba04 npu_available=True count=2 name=Ascend910_9362
[DEVICE] torch.npu.current_device()=0 device_name=Ascend910_9362
[VERIFY] dummy tensor device=npu:0 dtype=torch.float32 mean=-0.8325
[LOAD] model_dir=/tmp/models/WindFM-Tokenizer config {'d_in': 6, 'd_model': 256, 'n_heads': 4, 'ff_dim': 512, 'n_enc_layers': 4, 'n_dec_layers': 4, 's1_bits': 10, 's2_bits': 10, 'group_size': 5}
[LOAD] params 3958042 weight dtype torch.float32
[DATA] CSV /tmp/WindFM/examples/data/121522.csv shape (420, 7) window lookback=240 x_df shape (240, 6) mean [8.01 242.43 7.23 1.27 259.99 96464]
[INPUT] x_tensor shape torch.Size([1, 240, 6]) device cpu mean 0.0000 min -2.5593 max 1.9528
[RESULT-CPU] forward z_pre shape torch.Size([1, 240, 6]) mean -0.012716 z shape torch.Size([1, 240, 6]) mean -0.006417 bsq_loss -0.069559
[RESULT-CPU] encode half s1 torch.Size([1, 240]) s2 torch.Size([1, 240]) s1[0,:5]=[951, 1008, 496, 497, 465] s2[0,:5]=[812, 876, 840, 812, 810]
[RESULT-CPU] decode shape torch.Size([1, 240, 6]) mean -0.006417 first_row norm [0.9131, 0.7922, 0.9880, -1.1327, 0.8689, -1.4029]
[RESULT-CPU] recon MAE (norm space) 0.063362 RMSE 0.081904
[VERIFY] model param device npu:0 (should be npu:0)
[INPUT-NPU] x_npu device npu:0 shape torch.Size([1, 240, 6])
[RESULT-NPU] first run (compile inclusive) 198.75 ms
[RESULT-NPU] encode s1 torch.Size([1, 240]) device npu:0 s1[0,:5]=[951, 1008, 496, 497, 465]
[RESULT-NPU] decode shape torch.Size([1, 240, 6]) device npu:0 mean -0.006417 first_row [0.9132, 0.7922, 0.9880, -1.1327, 0.8690, -1.4029]
[CONSISTENCY] decode max_abs_error 1.907349e-06 mean_abs_error 2.888633e-07 max_abs_z 1.907349e-06 finite True
[CONSISTENCY] allclose atol=1e-4 rtol=1e-3 passed=True
[PERF] warmup 3, test 10, avg 9.12 ms min 8.73 max 9.40 p50 9.22 p90 9.30 p95 9.35 throughput 26311.7 rows/s (seq_len=240) dtype float32 batch=1
[PERF] first_run 198.75 ms (compile) stable avg 9.12 ms
[VERIFY] model param device npu:0 input device npu:0 output device npu:0
[STATUS] NPU inference SUCCESS on npu:0
[SCHEMA] pipeline: input [1,240,6] -> tokenizer encode (s1_bits=10,s2_bits=10,half) -> tokens [1,240]x2 -> decode -> reconstruction [1,240,6]
  • 模型参数 device: npu:0,输入 x_npu device npu:0,输出 dec_npu device npu:0,无 CPU fallback
  • 首轮 (compile inclusive) 198.75 ms,稳定 10 次 avg 9.12 ms, p50 9.22, p90 9.30, p95 9.35, throughput 26311 rows/s (rows=240)
  • 重构误差 (norm): MAE 0.063, RMSE 0.082; s1/s2 tokens CPU/NPU 完全一致

一致性

  • 输入、权重、预处理、dtype (float32)、eval mode 完全一致;seed 固定 (encode 确定性量化,无采样)
  • 保存 cpu.npy (decode [1,240,6]) 与 npu.npy,执行 python scripts/compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3
  • 结果: max_abs_error 1.907e-06, mean_abs_error 2.89e-07, finite true, passed true
  • 阈值说明: FP32 Transformer + RoPE + BSQ 的 matmul/einsum 在 Ascend 的累加顺序与 x86 有 ~1e-6 级差异,实测 1.9e-06 远小于阈值 1e-4,故使用默认 atol=1e-4, rtol=1e-3 (参考 validation.md FP32 起步阈值),分类无需 argmax,回归比较数值
  • 单窗口 smoke consistency,非全量 15B steps 评测;长序列 (1024) 未测

性能

项值
设备npu:0 Ascend910_9362, dtype float32, batch 1
输入[1,240,6] (240 steps × 6 features)
首轮 (compile)198.75 ms (含图编译与缓存)
稳定 (10次)avg 9.12 ms, min 8.73, max 9.40, p50 9.22, p90 9.30, p95 9.35
吞吐26311.7 rows/s (240 rows / 0.00912s) ; 109.6 windows/s
峰值显存npu-smi HBM 3108/65536 MB idle -> 推理 <500 MB (权重 15.8 MB + 激活)
计时每轮前后 torch.npu.synchronize(),预热 3 次,正式 10 次;不含 CSV 读取与标准化,仅 encode+decode
覆盖encode(half=True) + decode 完整路径;forward 首次编译已计入首轮

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成,深灰背景、白色等宽字体,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$,非截图模拟 (fallback 为 PIL 模拟 xterm 样式,内容来自真实日志):

  • assets/agent_workflow.png — 侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark
  • assets/npu_device_call.png — npu-smi info、NPU 可用性、参数 device、输入/输出 device、同步耗时
  • assets/model_result.png — 默认 python inference.py 的真实完整输出

README 引用图片,无需披露具体渲染工具。图片由真实日志尾部连续截取生成,token 已脱敏。

agent workflow npu device call model result

限制

  • 单卡 npu:0 推理,seq_len 受 max_context (官方未显式限制) 与显存约束;240 已验证,1024+ 需测 KV 缓存与 RoPE
  • Tokenizer-only checkpoint,非完整 WindFM 预测模型;零样本风电预测需配套 NeoQuasar/WindFM (8.1M) + WindFMPredictor.auto_regressive_inference
  • 未量化 (INT8) 或 torch.compile;FP16/BF16 精度未验证
  • 数据仅 121522.csv 单站 smoke test,未在全 126k 站点 150B steps 上评测
  • 风电功率单位 MW,归一化后无量纲;逆缩放依赖窗口统计量,跨站需重算