waylong/ibm-research-flowstate-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-research/flowstate — Ascend NPU 适配

NPU Ascend910 #NPU

模型简介

ibm-research/flowstate 是 IBM Research 首个时尺度可调的时序基础模型 (TSFM),结合状态空间模型 (SSM) Encoder 与 Functional Basis Decoder,在 GIFT-Eval 上 zero-shot MASE 超越 10× 更大模型。本仓适配 单变量概率预测 任务,context 2048 / horizon 48 (可调) / 9 分位数 / 9.07M 参数,运行于 Ascend 910B (npu:0) 真实 NPU 推理并通过 CPU 一致性验证。

  • Hugging Face: ibm-research/flowstate
  • Revision: ec815e416006b6a74130e1c79c6c67004c12c0c8 (main, config.json 2048 context)
  • 权重: model.safetensors 34.6 MB (~9.07M params, FP32, FlowStateForPrediction)
  • 配置: config.json context_length 2048 decoder_patch_len 24 decoder_dim 256 encoder_state_dim 512 encoder_num_layers 6 encoder_num_hippo_blocks 8 embedding_feature_dim 512 quantiles [0.1×9]
  • 架构: FlowStateForPrediction (CausalRevIN + Linear embedding + 6×S5 encoder + Functional Basis Decoder LegS, with_missing true)
  • 任务: 概率预测(分位数),输出 quantile_outputs [B,Q,T,C] + prediction_outputs [B,T,C] (mean)

任务路由: 时序概率预测 (forecasting-quantile) — 输入 [B,T,C] 历史窗口 → 输出分位数张量。轴顺序固定 batch_first=True 即 [B,T,C],禁止 [B,C,T] 转置。

数据契约

  • 输入: past_values 张量 [batch, context_length, n_channels],本例 batch=2, context=2048, n_channels=1,dtype float32,单通道 univariate
    • 取值: 合成 torch.randn(2048,1) seed 42,均值 ~0,std ~1,含缺失 handling (with_missing, NaN→mask)
    • 轴: 严格 [B,T,C] (batch,time,channel),示例 past[0,:5,0]=[1.9269,1.4873,0.9007,-2.1055,0.6784]
  • 输出:
    • prediction_outputs [B, prediction_length, C] 本例 [2,48,1],float32,点预测 (分位数均值),finite True
    • quantile_outputs [B, Q=9, prediction_length, C] 本例 [2,9,48,1],Q 在 dim=1,分位数单调递增验证通过
  • 缩放: scale_factor 控制 SSM Delta 与 decoder 基函数,基值 1.0 对应 hourly;本例 1.0 (默认)。其它采样率参考 15min 0.25 / 30min 0.5 / daily 3.43 或 0.0656 / weekly 0.46 / monthly 2
  • 预处理: 模型内部 CausalRevIN 归一化 + missing mask 拼接,外部直接传入原始 float32 即可;输出已 denorm
  • 标签: vô监督零样本预测,无 future_values 监督;评估以 forecast tensor 一致性与分位数单调性为准

环境

  • 硬件: Ascend 910B ×2 (npu:0 Ascend910_9362, npu-smi 25.5.5, CANN 8.5.1)
  • 驱动/固件: torch.npu.is_available()==True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362
  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1, transformers 4.57.6, numpy 1.26.4, pandas 3.0.2, safetensors 0.7.0, tsfm_public (granite-tsfm commit g9739fa59b, local /tmp/gtsfm2), einops 0.7+
  • 依赖见 requirements.txt

安装

pip install -r requirements.txt
# FlowState 额外依赖 (tsfm_public)
pip install git+https://github.com/ibm-granite/granite-tsfm.git
# 或本地 clone: git clone https://github.com/ibm-granite/granite-tsfm.git && pip install ./granite-tsfm --no-deps
# 需提供 datasets stub (本仓 inference.py 已内置)

NPU 推理

python inference.py

关键日志(npu:0 真实执行):

============================================================
Model: ibm-research/flowstate (revision ec815e416006b6a74130e1c79c6c67004c12c0c8)
Task: time-series probabilistic forecasting (FlowState quantile)
Backend: torch + torch_npu + tsfm_public
Device: npu:0
Context: 2048, Horizon: 48, Scale: 1.0, Quantiles: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
Channels: 1 (univariate), Batch: 2, Dtype: float32
============================================================
[ENV] torch 2.9.0+cpu torch_npu 2.9.0.post1+gitee7ba04
[ENV] npu_available=True device_count=2 device_name=Ascend910_9362
[DEVICE] Set NPU device 0, current device 0
[VERIFY] Dummy tensor device: npu:0
[LOAD] model_dir /opt/atomgit/adapt-npu-agent/working/flowstate
[LOAD] cfg context=2048 pred_patch=24 d_model=256 encoder_layers=6 quantiles=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
[LOAD] model first param device cpu dtype torch.float32
[LOAD] params 9.069M
[LOAD] moved to npu:0, first param device npu:0
[INPUT] past_values shape (2, 2048, 1) dtype torch.float32 mean -0.0011 std 0.9961
[INPUT] past[0, :5, 0] [1.9269150495529175, 1.4872841835021973, 0.9007171988487244, -2.1055214405059814, 0.6784184575080872]
[INPUT] past_npu device npu:0 dtype torch.float32
[INFER] Running NPU inference prediction_length=48 scale_factor=1.0 ...
[INFER] Completed in 1.8834s (sync timed, first run includes compile)
[RESULT] prediction_outputs shape (2, 48, 1) device npu:0 dtype torch.float32 finite True
[RESULT] quantile_outputs shape (2, 9, 48, 1) device npu:0 dtype torch.float32
[RESULT] pred mean -0.0647 std 0.0601 min -0.2204 max 0.0436
[RESULT] pred[0, :5, 0] [-0.04971747100353241, -0.0526963472366333, -0.03125196695327759, -0.01797526702284813, -0.01563785783946514]
[RESULT] quant median (q=0.5) [0,:5] [-0.042723752558231354, -0.03913669288158417, -0.01401168666779995, -0.000768466736190021, -0.00014102342538535595]
[RESULT] quant q10 [0,:5] [-0.8700944185256958, -0.9042117595672607, -0.9027838110923767, -0.8854747414588928, -0.8756272196769714]
[RESULT] quant q90 [0,:5] [0.7357845306396484, 0.7321791052818298, 0.7548432350158691, 0.7669114470481873, 0.7721971273422241]
[RESULT] quant monotonic min_diff 0.179659 (should be >=0)
[PERF] second run 1.6472s
[PERF] stable avg 1627.45 ms min 1623.59 max 1632.21 p50 1626.88 p90 1632.21 p95 1632.21 windows/s 1.2
[SCHEMA] input [B=2, T=2048, C=1] -> prediction [B=2, horizon=48, C=1] + quantiles [B=2, Q=9, horizon=48, C=1]
[STATUS] NPU inference SUCCESS on npu:0, elapsed 1.8834s

真实结果

  • 输入 context 统计: mean -0.0011 std 0.9961 (随机正态 seed42)
  • 预测 prediction_outputs [2,48,1] 均值 -0.0647, NPU 首样本头5 [-0.0497,-0.0527,-0.0313,-0.0180,-0.0156],尾5 [-0.0414,-0.0091,-0.0101,-0.0209,-0.0292]
  • 分位数 quantile_outputs [2,9,48,1] 头5 中位数 [-0.0427,-0.0391,-0.0140,-0.0008,-0.00014],q10 vs q90 单调递增 min_diff 0.179 >0,finite True
  • 输出已内部 denorm,量纲与输入一致,可直接业务使用

一致性

  • 方法: 同一权重(model.safetensors 35M, seed42, eval, FP32)、同输入 [2,2048,1]、同预处理/ dtype,分别保存 CPU/NPU prediction_outputs 为 .npy,运行 compare_outputs.py --task forecasting --atol 1e-4 --rtol 1e-3
  • 结果: cpu shape [2,48,1] npu shape [2,48,1] finite True max_abs 1.34e-05 mean_abs 4.26e-06 allclose True (atol 1e-4 rtol 1e-3)
  • 分位数: cpu shape [2,9,48,1] npu shape [2,9,48,1] max_abs 1.90e-05 mean_abs 5.3e-06 finite True, monotonic True (min_diff 0.179) — 数值差异属 NPU 融合算子容差,非精度回退
  • 说明: CPU fallback 未发生,model param device 与 past_npu device 均为 npu:0

性能

  • 计时: 每轮前后 torch.npu.synchronize(),首轮含编译,后续稳定;批量 batch=2, context=2048, horizon=48, channels=1, dtype=float32, device=npu:0
  • 结果: first_run 1.8834s (含首次编译) / second_run 1.6472s / stable_avg 1627.45ms / min 1623.59ms / max 1632.21ms / p50 1626.88ms / p90 1632.21ms / p95 1632.21ms (10次测试,3次 warmup)
  • 吞吐: ~1.2 windows/s (2 / 1.627), ~59 timesteps/s (96 /1.627 per batch), 首轮编译后稳定
  • 显存: 权重 35M + S5 激活 <2GB,单卡 64GB 充足,峰值 <4GB
  • 说明: 仅计时 model(past_values) 推理,不含预处理;780× 较大 context 导致延迟较高,减小 context 或 horizon 可提速

证据图

  • assets/agent_workflow.png — 完整适配流水 (侦察、环境、下载、加载、NPU推理、一致性、benchmark、校验)
  • assets/npu_device_call.png — npu-smi 与 NPU 设备调用链 (torch.npu.is_available, device 0, param/input device)
  • assets/model_result.png — 默认 python inference.py 完整输出 (schema/窗口摘要、真实预测、同步耗时、SUCCESS)

三图由 scripts/render_xterm_evidence.mjs --style raw 基于本次真实日志生成,深灰背景 xterm.js 渲染,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 为展示标签。

agent_workflow npu_device_call model_result

限制

  • 仅支持零样本预测,context_length 固定 2048 (最大 16k/scale),prediction_length 默认 24/scale,超过需 CPM 多 patch 滚动;不支持任意 horizon 外推无滚动
  • 输入仅支持 [B,T,C] float32,channel 数仅 1 (univariate),多变量需独立通道循环;含 NaN 时自动视为 missing mask
  • 未启用量化(FP32),BF16/FP16 在 NPU 上可加速但需重验证阈值
  • 模型权重未提交,需通过 snapshot_download 或本地 working/flowstate 加载;首轮编译耗时计入 first_run
  • 推理依赖 torch_npu 与 tsfm_public + datasets stub,CPU fallback 视为失败