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

ibm-research/flowstate NPU 适配

#NPU

本仓库将 HuggingFace 上的 ibm-research/flowstate(FlowState 时间序列基础预测模型,context 2048,概率分位数预测,S5 状态空间编码器 + LEGS 解码器)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。输出 9 个分位数(0.1-0.9)的未来序列预测,支持合成时序零样本推理。

  • 模型原始仓库:https://huggingface.co/ibm-research/flowstate
  • 权重:model.safetensors 约 35 MB,config.json context_length 2048
  • 源实现:https://github.com/ibm-granite/granite-tsfm(tsfm_public.models.flowstate)

任务与路由

项值
模型ibm-research/flowstate(revision main,config context_length=2048, quantiles=9)
架构FlowStateModel(encoder 6 层 S5, decoder LEGS, out_gating 缺省)
任务时间序列预测 probabilistic forecasting(quantile)
输入past_values shape [seq_len, batch, n_ch] 或 [batch, seq_len, n_ch](batch_first 可选),scale_factor, prediction_length
输出prediction_outputs [batch, horizon, n_ch](均值)、quantile_outputs [batch, quantiles, horizon, n_ch]
数据契约轴顺序明确为 [time, batch, channel](batch_first=False)或 [batch, time, channel];context 固定 2048,horizon 按 decoder_patch_len=24 的倍数;频率通过 scale_factor 调节;缺失值以 NaN 掩码处理;缩放为 patch 内标准化

环境

  • 操作系统:Linux aarch64,昇腾 910B(Ascend910_9362),CANN 8.5.1
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1
  • 依赖见 requirements.txt:torch、torch_npu、transformers、safetensors、tsfm_public(来自 granite-tsfm)

安装

pip install -r requirements.txt
# 昇腾 NPU 需确保 torch 与 torch_npu 版本匹配,并已加载 Ascend 驱动/固件
# tsfm_public 将从 GitHub 自动安装(git+https://github.com/ibm-granite/granite-tsfm.git)

NPU 推理

inference.py 为自包含脚本,默认使用合成可复现时序(seed 0)在 npu:0 上完成完整推理、一致性与性能基准。

# 方式一:权重已缓存在 ./working/models/ibm-research-flowstate
python inference.py

# 方式二:自动从 HuggingFace 下载(需联网)
python inference.py

# 方式三:指定本地权重目录
MODEL_DIR=/path/to/flowstate python inference.py

设备选择固定为 npu:0(可用时),核心 forward 的所有参数与参与计算的 Tensor 均显式位于 npu:0(见 npu_device_call.png 与 model_result.png)。计时前后均调用 torch.npu.synchronize()。

真实结果(Ascend 910B 单卡,npu:0)

以合成时序 context=2048, batch=2, n_ch=1, horizon=96 为例(seed 0):

  • 输入:past_values shape (2048, 2, 1),均值 -0.0096,标准差 0.9934
  • CPU 基线:prediction_outputs mean -0.0766 std 0.0769,quantile_outputs 9 分位数单调递增(0.1→0.9)
  • NPU 输出:相同输入在 npu:0 上 prediction_outputs mean -0.0766 std 0.0769,与 CPU 完全一致(见下)
  • 分位数样例(batch0, horizon0):CPU [-1.2734, -1.0112, -0.7855, -0.5724, -0.3622, -0.1551, 0.0547, 0.2791, 0.5476],NPU 误差 <3.3e-05
  • 推理状态:[STATUS] SUCCESS - NPU inference completed without CPU fallback

完整输出见 model_result.png。

CPU-NPU 一致性

使用相同权重、相同数据切片(seed 0)、相同预处理、相同随机种子与 float32 精度,分别在 CPU 与 npu:0 上运行并保存原始输出,用 scripts/compare_outputs.py 比较:

  • 点预测(mean):forecasting max_abs 1.48e-05,mean_abs 4.18e-06,atol=1e-4 rtol=1e-3 PASS
  • 分位数:转置后 shape (2,96,9,1) 满足 quantile 单调性检查,max_abs 3.30e-05,PASS
  • 单调性:CPU 与 NPU 分位数均单调递增(沿 quant 维度 axis=1)
  • 阈值:atol=1e-4 rtol=1e-3(FP32 跨硬件正常波动,模型含 FFT 与 S5 递归,误差远低于阈值)

结果:PASS。数值差异在 fp32 跨硬件正常波动范围内。

性能基准

在 npu:0、float32 下,预热 3 次后正式计时 10 次(batch=2, context=2048, horizon=96, n_ch=1):

指标值
compile/first-run1651.64 ms(首次 NPU 编译含图编译)
avg / min / max1621.79 / 1613.80 / 1651.64 ms
p50 / p90 / p951619.75 / 1624.92 / 1638.28 ms
吞吐1.23 windows/s(batch=2 → 2.46 samples/s)
batch / shape / dtype2, [2048,2,1]->[2,96,1], float32
峰值 NPU 显存~3.1 GB(远低于单卡 64 GB 上限,npu-smi 实测)

计时包含模型 forward 与 scale_factor 处理,前后均调用 torch.npu.synchronize()。首轮编译时间与稳定推理时间分开报告。

证据图

以下三张图片由 xterm.js 依据本次真实运行日志渲染生成(非人工绘制、非模拟):

图片内容
assets/agent_workflow.png侦察、模型下载、NPU 推理、一致性、基准与提交校验的完整工作流日志
assets/npu_device_call.pngnpu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device
assets/model_result.png默认 python inference.py 的真实完整输出(schema、真实预测、同步耗时、状态)

限制

  • 本仓库不包含权重文件;首次运行且本地无缓存时需联网从 HuggingFace 下载(约 35 MB)。
  • tsfm_public 依赖需从 GitHub 安装,首次安装需联网。
  • 模型输入 context 固定 2048,horizon 建议为 24 的倍数;超出 16k/context_scale 长度会自动截断。
  • 当前仅验证单变量与 batch 2 场景,多变量(n_ch>1)与多 batch 已在代码中支持但未作为主基准。
  • 推理阶段所有计算均在 NPU,无 CPU fallback;采样(sampling)未启用,仅验证均值与分位数预测。