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 自回归生成。
time-series-forecasting, tag Wind Power)model/module.py, model/windfm.py)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=20WindFMTokenizer = 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] 重构| 项 | 值 |
|---|---|
| 输入 | [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 展示用;一致性比较在归一化空间进行 |
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 下载)pip install -r requirements.txt
# 另需 torch_npu 与 CANN 8.5.1 (Ascend 910)默认 python inference.py 自动完成真实 NPU 推理 (CPU 基线 + NPU,对比,性能):
python inference.py行为:
torch.npu.is_available() 与 npu-smi info,设置 torch.npu.set_device(0),dummy torch.randn(2,3).to("npu:0") 验证链路/tmp/models/WindFM-Tokenizer 优先,否则 hf hub)WindFMTokenizer(**config.json),从 model.safetensors 加载 3.95M 参数,CPU 与 NPU 同权重、同输入、同预处理、同 dtype121522.csv 前 240 步 6 特征 -> 标准化 clip -> [1,240,6]forward -> (z_pre,z), encode(half) -> s1/s2, decode -> 重构 [1,240,6], MAE 0.063.to("npu:0"),torch.npu.synchronize() 前后计时,首轮 198 ms (含图编译),稳定 9.12 ms
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]npu:0,输入 x_npu device npu:0,输出 dec_npu device npu:0,无 CPU fallbackcpu.npy (decode [1,240,6]) 与 npu.npy,执行 python scripts/compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3max_abs_error 1.907e-06, mean_abs_error 2.89e-07, finite true, passed trueatol=1e-4, rtol=1e-3 (参考 validation.md FP32 起步阈值),分类无需 argmax,回归比较数值| 项 | 值 |
|---|---|
| 设备 | 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 推理、一致性、benchmarkassets/npu_device_call.png — npu-smi info、NPU 可用性、参数 device、输入/输出 device、同步耗时assets/model_result.png — 默认 python inference.py 的真实完整输出README 引用图片,无需披露具体渲染工具。图片由真实日志尾部连续截取生成,token 已脱敏。

npu:0 推理,seq_len 受 max_context (官方未显式限制) 与显存约束;240 已验证,1024+ 需测 KV 缓存与 RoPEWindFM 预测模型;零样本风电预测需配套 NeoQuasar/WindFM (8.1M) + WindFMPredictor.auto_regressive_inferencetorch.compile;FP16/BF16 精度未验证121522.csv 单站 smoke test,未在全 126k 站点 150B steps 上评测