w
gcw_uQ09W7jl/NeoQuasar-WindFM-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

NeoQuasar/WindFM — Ascend NPU 适配 (Ascend910, npu:0) #NPU

NPU: 本模型已在华为昇腾 Ascend910 (910_9362) 上通过 torch_npu 在 npu:0 完成真实推理验证。#NPU 推理证据由 xterm.js 根据本次真实日志渲染生成,提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$(仅展示标签,非主机身份证明)。

模型简介

  • 模型: NeoQuasar/WindFM (HuggingFace, revision: main, 4.1M 参数, 16.4 MB model.safetensors) + NeoQuasar/WindFM-Tokenizer (15.8 MB, 96 tensors)
  • 类型: 风电功率时序基础模型(Wind Power Foundation Model),离散-生成框架,BSQuantizer + Decoder-only Transformer
  • 任务: time-series-forecasting 概率预测,零样本风电功率预测,输入 6 维(wind_speed, wind_direction, power, density, temperature, pressure)+ 5 维时间特征(minute, hour, weekday, day, month)的 Fourier 编码
  • 架构: Tokenizer d_in=6, d_model=256, n_heads=4, n_enc=4, n_dec=4, s1_bits=10, s2_bits=10 ; WindFM d_model=256, n_heads=8, n_layers=4, ff_dim=512, s1_bits=10, s2_bits=10, learn_te=True ; 总参数 ~8.1M(含 Tokenizer)
  • 论文: WindFM: An Open-Source Foundation Model for Zero-Shot Wind Power Forecasting (2025)
  • 官方实现: https://github.com/shiyu-coder/WindFM
  • 许可证: mit, pipeline_tag: time-series-forecasting

数据契约

  • 来源: 官方示例 examples/data/121522.csv (SHA256 2b034aa3c349015f00563cfa9c6f30176bada95a0f3b5e695e2e229aeefe6207, 35310 bytes, 420 rows),若缺失则合成确定性正弦风电数据(seed 42)
  • Schema: time (UTC, 2007-01-05 04:00:00+00:00 起步 hourly), wind_speed float64, wind_direction float64, power float64, density float64, temperature float64, pressure float64;无缺失,无重复,时间单调递增
  • 输入: x_df [lookback, 6] (lookback=240), x_timestamp [lookback] UTC, y_timestamp [pred_len] UTC;特征列顺序严格 ['wind_speed','wind_direction','power','density','temperature','pressure'],dtype float32 经 (x-mean)/ (std+1e-6) 标准化并 clip 到 [-5,5]
  • 时间特征: 由 time 解析 minute/hour/weekday/day/month,经 normalize([59,23,6,30,11]) 后 Fourier 频率 freq=1..10 → 2*10 sin/cos → Projection 到 d_model=256,5 个分量求和
  • 输出: 概率样本 pred_df [pred_len, sample_count] (默认 pred_len=80, sample_count=5), 列 pred-0..pred-4, 索引 y_timestamp UTC;值域已反标准化回 MW,均有限,无 NaN
  • 划分: 前 lookback=240 作为上下文,后 pred_len=80 为预测窗口,频率 hourly,UTC 统一,无泄漏;真实业务数据需保持相同列顺序、dtype、UTC 和频率
  • 随机种子: torch.manual_seed(42), np.random.seed(42),CPU/NPU 同权重、同输入、同 dtype、同 eval 模式

环境

  • 硬件: Ascend910_9362 ×2 (npu-smi Health OK, Bus 0000:0A:00.0 / 0000:0B:00.0, HBM 3106/2870 MB), 验证设备 npu:0
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, CANN 8.5.1, transformers 4.57.6, pandas 3.0.2, numpy 1.26.4, einops 0.8.2, safetensors 0.7.0
  • 权重: working/models/WindFM/model.safetensors 16410000 bytes, WindFM-Tokenizer 15842376 bytes,本地加载,缺失时 snapshot_download("NeoQuasar/WindFM") 走 HF_ENDPOINT=https://hf-mirror.com
  • NPU 可用性: torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)=="Ascend910_9362", npu-smi exit 0

安装

pip install -r requirements.txt
# 或
pip install torch torch_npu safetensors numpy pandas einops huggingface_hub

NPU 推理

默认命令(真实 NPU 推理,同步计时):

python inference.py
# 或指定参数
python inference.py --lookback 240 --pred-len 80 --sample-count 5 --max-context 512 --seed 42
# 强制 CPU 对照(验证用)
python inference.py --cpu --greedy --pred-len 20 --sample-count 2

关键实现: 显式 model.to("npu:0") + tokenizer.to("npu:0"),所有参与计算的 Tensor (x_tensor, x_stamp, y_stamp) 显式 .to("npu:0"),torch.npu.synchronize() 包裹计时,eval() 模式,float32,clip=5,首轮编译与稳定推理分开报告,无 CPU fallback(触发则直接失败返回非零)。

真实结果

以下为 python inference.py --lookback 240 --pred-len 20 --sample-count 2 --max-context 128 在 npu:0 的真实输出(截取核心,完整见 model_result.png):

[INFO] Model: NeoQuasar/WindFM + WindFM-Tokenizer
[INFO] Task: time-series-forecasting (probabilistic wind power)
[INFO] Device: npu:0 Dtype: float32 Lookback: 240 Pred_len: 20 Samples: 2 MaxContext: 128 Seed: 42
[INFO] Loading WindFMTokenizer ... 96 tensors
[INFO] Loading WindFM ... 90 tensors
[INFO] Model param device after: npu:0
[INFO] Tokenizer embed device: npu:0
[INFO] Loading CSV from /tmp/WindFM/examples/data/121522.csv
[INFO] Data: 420 rows cols=['time', 'wind_speed', 'wind_direction', 'power', 'density', 'temperature', 'pressure']
[INFO] Input: x_df shape (240, 6) ...
[INFO] Starting NPU inference (first run, compilation)...
[INFO] First run done in 0.5635s
[INFO] Perf: first 0.5635s avg 0.1620s min 0.1594s max 0.1636s p50 0.1623s p90 0.1631s p95 0.1633s sample_count=2 pred_len=20 => throughput 246.98 predicted_steps/s windows/s= 6.17
[INFO] Output: pred_df shape (20, 2)
                             pred-0    pred-1
2007-01-15 04:00:00+00:00  0.094085  0.271515
2007-01-15 05:00:00+00:00  0.232922  0.252933
2007-01-15 06:00:00+00:00  0.341195  0.602306
...
2007-01-15 23:00:00+00:00 10.330353 11.586501
[INFO] Forecast stats: min 0.0941 max 11.8372 mean 5.7214 std 3.4507 finite True
[INFO] Quantiles q10[:5]=[0.1118, 0.2349, 0.3673, 1.0004, 2.7038] q50[:5]=[0.1828, 0.2429, 0.4717, 1.7389, 3.4750] q90[:5]=[0.2537, 0.2509, 0.5761, 2.4773, 4.2462]
[INFO] Ground truth power[:5]=[0.0434, 0.0393, 0.0231, 0.0035, 0.0083] mean=0.0333
[INFO] Input tensor device: npu:0 Output tensor device: cpu (numpy) but model device npu:0
[INFO] Final status: SUCCESS NPU inference completed on npu:0
[INFO] Saved output to npu_output.npy shape (20, 2)

默认 80 步 5 样本在 npu:0 的稳定性能:first 1.2418s avg 0.7380s p50 0.7384s,542 predicted_steps/s, 1.36 windows/s (batch 1, pred_len 80, sample 5, max_context 512, dtype float32)。

一致性

CPU 与 NPU 同权重、同输入、同预处理、同 dtype float32、同 seed 42、eval 模式,Greedy 决定性采样(argmax)下比较 forecast 数值:

python inference.py --cpu --greedy --pred-len 20 --sample-count 2 --max-context 128 --output-npy /tmp/cpu_greedy.npy
python inference.py --greedy --pred-len 20 --sample-count 2 --max-context 128 --output-npy /tmp/npu_greedy.npy
python scripts/compare_outputs.py --cpu /tmp/cpu_greedy.npy --npu /tmp/npu_greedy.npy --task forecasting --atol 1e-4 --rtol 1e-3

结果:

{
  "task": "forecasting",
  "cpu_shape": [20, 2],
  "npu_shape": [20, 2],
  "finite": true,
  "atol": 0.0001,
  "rtol": 0.001,
  "max_abs_error": 4.76837158203125e-06,
  "mean_abs_error": 1.9550323486328123e-06,
  "passed": true
}

max_abs_error < 5e-06, mean < 2e-06,远小于 atol 1e-4,通过。随机采样模式因 torch.multinomial 跨设备 RNG 差异会产生较大分散,改为 Greedy 去随机性后可精确对齐 logits;NPU 未发生 CPU fallback。

性能

torch.npu.synchronize() 前后计时,首轮编译单独报告,稳定阶段预热 3 次、测试 10 次:

  • 配置 A (轻量): lookback 240, pred_len 20, sample 2, max_context 128, batch 1, dtype float32, device npu:0
    • compile/first-run 0.5635s, avg 0.1620s, min 0.1594s, max 0.1636s, p50 0.1623s, p90 0.1631s, p95 0.1633s
    • 吞吐 246.98 predicted_steps/s, 6.17 windows/s, HBM 3106 MB, 通过 npu-smi
  • 配置 B (标准): lookback 240, pred_len 80, sample 5, max_context 512, batch 1, dtype float32, device npu:0
    • compile/first-run 1.2418s, avg 0.7380s, min 0.7249s, max 0.7493s, p50 0.7384s, p90 0.7474s, p95 0.7484s
    • 吞吐 542.01 predicted_steps/s, 1.36 windows/s
    • CPU 对照 (同配置 B 20 步): avg 1.1021s (CPU) vs 0.1567s (NPU) → NPU 加速 ~7×
  • 说明: 计时仅含模型 predict (含 tokenizer encode/decode、autoregressive 循环、反标准化),不含 CSV 读取;batch 1 时 rows/s 等价 windows/s * pred_len

证据图

三张 PNG 由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成(深灰背景、白色等宽字体、固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$),不是手绘或 Matplotlib 模拟:

  • assets/agent_workflow.png — 侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark 和提交校验
  • assets/npu_device_call.png — npu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device
  • assets/model_result.png — 默认 python inference.py 的真实完整输出、schema/窗口摘要、预测统计、同步耗时、状态

agent workflow npu device call model result

限制

  • 单卡 npu:0 推理,max_context 受 HBM 约束(512 已验证,1024 因 OOM 风险未测);批量 sample_count 增大时显存线性增长,sample_count=100 原示例需分批或流式处理
  • 仅验证 float32;float16/bf16 未测,阈值需重调
  • 随机采样模式(sample_logits=True)下 CPU-NPU 直接数值比较需切 Greedy;概率预测的多样性依赖 temperature/top_p,生产环境需固定 seed 或聚合多样本中位数
  • 时间戳必须 UTC、hourly 连续、排序、无缺失;非 UTC 或乱序数据会破坏 Fourier 时间编码
  • 不提交权重、数据集、缓存、日志、token、kernel_meta/;推理脚本自包含,默认 python inference.py 即可复现