waylong/ibm-granite-granite-timeseries-flowstate-r1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-granite/granite-timeseries-flowstate-r1 — Ascend NPU 适配

NPU Ascend910 #NPU

模型简介

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

  • Hugging Face: ibm-granite/granite-timeseries-flowstate-r1
  • Revision: 05effc6cb39ee16dce9dd0064ed1a76e4b8ff464 (main, config.json 2048 context, 35M safetensors)
  • 权重: model.safetensors 36.3 MB (~9.07M params, FP32, FlowStateForPrediction, torch_dtype float32)
  • 配置: 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] use_freq true with_missing true
  • 架构: FlowStateForPrediction (CausalRevIN + Linear embedding + 6×S5 encoder + Functional Basis Decoder LegS, prediction_type quantile→mean)
  • 任务: 概率预测(分位数),输出 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 @ /tmp/granite-tsfm2), einops 0.8.2
  • 依赖见 requirements.txt

安装

pip install -r requirements.txt
# FlowState 额外依赖 (tsfm_public) — 任选其一:
# 1) 在线安装 (需代理):
pip install git+https://github.com/ibm-granite/granite-tsfm.git
# 2) 本地 clone:
git clone https://github.com/ibm-granite/granite-tsfm.git && pip install ./granite-tsfm --no-deps
# 本仓 inference.py 已内置 datasets stub,无需额外 datasets 包

NPU 推理

python inference.py

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

============================================================
Model: ibm-granite/granite-timeseries-flowstate-r1 (revision 05effc6cb39ee16dce9dd0064ed1a76e4b8ff464)
Task: time-series probabilistic forecasting (FlowState quantile)
Backend: torch + torch_npu + tsfm_public (granite-tsfm)
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/.../working/model_snapshot
[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.9149s (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.0916 std 0.0731 min -0.2582 max 0.0359
[RESULT] pred[0, :5, 0] [-0.2002258151769638, -0.18122974038124084, -0.09574131667613983, -0.11032980680465698, -0.09660828113555908]
[RESULT] quant median (q=0.5) [0,:5] [-0.21478191018104553, -0.1903679221868515, -0.10500675439834595, -0.11949676275253296, -0.1025649905204773]
[RESULT] quant q10 [0,:5] [-0.9971801042556763, -0.9898408651351929, -0.9105710387229919, -0.901771068572998, -0.8781985640525818]
[RESULT] quant q90 [0,:5] [0.6718228459358215, 0.6976616382598877, 0.7760943174362183, 0.742037296295166, 0.7383202314376831]
[RESULT] quant monotonic min_diff 0.187052 (should be >=0)
[PERF] second run 1.6510s
[PERF] stable avg 1629.05 ms min 1621.35 max 1642.67 p50 1627.81 p90 1642.67 p95 1642.67 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.9149s

真实结果

  • 输入 context 统计: mean -0.0011 std 0.9961 (随机正态 seed42)
  • 预测 prediction_outputs [2,48,1] 均值 -0.0916, NPU 首样本头5 [-0.2002,-0.1812,-0.0957,-0.1103,-0.0966],尾5 [-0.0400,0.0041,-0.0038,0.0084,0.0043]
  • 分位数 quantile_outputs [2,9,48,1] 头5 中位数 [-0.2147,-0.1903,-0.1050,-0.1194,-0.1025],q10 vs q90 单调递增 min_diff 0.187 >0,finite True
  • 输出已内部 denorm,量纲与输入一致,可直接业务使用

一致性

  • 方法: 同一权重(model.safetensors 36.3MB, 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 2.41e-05 mean_abs 7.46e-06 allclose True (atol 1e-4 rtol 1e-3)
  • 分位数: cpu shape [2,9,48,1] npu shape [2,9,48,1] max_abs 3.19e-05 mean_abs 8.14e-06 finite True, monotonic True (min_diff 0.187) — 数值差异属 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.9149s (含首次编译) / second_run 1.6510s / stable_avg 1629.05ms / min 1621.35ms / max 1642.67ms / p50 1627.81ms / p90 1642.67ms / p95 1642.67ms (10次测试,3次 warmup)
  • 吞吐: ~1.2 windows/s (2 / 1.629), ~60 timesteps/s (96 /1.629 per batch), 首轮编译后稳定
  • 显存: 权重 36M + S5 激活 <2GB,单卡 64GB 充足,峰值 <4GB
  • 说明: 仅计时 model(past_values) 推理,不含预处理;2048 较大 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/model_snapshot 加载;首轮编译耗时计入 first_run
  • 推理依赖 torch_npu 与 tsfm_public + datasets stub,CPU fallback 视为失败