jsjs1258/ibm-granite-granite-timeseries-patchtst-fm-r1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Granite TimeSeries PatchTST-FM r1(昇腾 NPU)

#NPU

模型简介

ibm-granite/granite-timeseries-patchtst-fm-r1 是基于 PatchTST 架构的零样本时间序列基础模型,带 99 分位数的分位数头,支持点预测与概率预测。架构 PatchTSTFMForPrediction 由 IBM granite-tsfm(tsfm_public)提供:

  • 模型类型:patchtst_fm
  • 参数量:约 2.58 亿(257,895,552)
  • 权重:FP32(model.safetensors,约 1.0 GB)
  • context_length:8192(输入不足时自动均值填充并视为掩码)
  • prediction_length:64
  • d_model:1024,n_layer:20,n_head:16,d_patch:16
  • quantile_levels:0.01 ~ 0.99(99 个分位数)
  • revision:main
  • 许可证:apache-2.0

数据契约

本演示使用可复现的合成时间序列(固定 seed=42),构造规则与契约如下:

字段值
输入张量 past_values[batch, context, channels],float32
batch4
context512(模型上限 8192)
channels(特征维度)1
prediction_length64
数据生成10 + 5*sin(2π(3+b)t + b) + 0.02bt + N(0,0.3)

模型输入为 [B, T, C](batch × time × channel),输出:

  • prediction_outputs(点预测/均值积分):[B, 64, C]
  • quantile_outputs(概率预测):[B, 99, 64, C]

缺失值以 NaN 表示,模型自动按均值填充并生成掩码;点预测通过分位数对概率积分得到(含尾部扩展)。

环境

  • 硬件:华为 Ascend 910(本验证运行于单卡 npu:0)
  • 系统:Linux aarch64
  • CANN:8.5.1
  • Python:3.11
  • torch:2.9.0 + torch_npu 2.9.0

安装

pip install torch torch-npu transformers>=4.57.6 datasets einops numpy pandas scikit-learn huggingface_hub
pip install --no-deps --no-build-isolation "git+https://github.com/ibm-granite/granite-tsfm.git"

权重先下载到本地目录(model/),推理时不隐式联网:

python -c "from huggingface_hub import snapshot_download; snapshot_download('ibm-granite/granite-timeseries-patchtst-fm-r1', local_dir='model')"

NPU 推理

python inference.py

inference.py 自包含:加载本地权重,模型与全部 Tensor 显式迁移到 npu:0,在 eval 模式(torch.no_grad())下完成 99 分位数 + 点预测,输出输入/输出摘要与同步耗时。权重目录不存在时会先下载一次到本地 model/。

真实结果

在 npu:0(Ascend910_9362)上使用与 CPU 完全相同的权重、输入、seed 与 dtype=float32 推理:

  • 点预测(首序列前 8 步,真实输出): 9.9602, 10.1635, 10.3492, 10.5372, 10.7216, 10.9184, 11.0989, 11.2859
  • 分位数(首序列第 0 步):q01=9.3870,q50=9.9500,q99=10.6246
  • 分位数顺序单调且有限,点预测位于 q50 附近,符合概率预测契约。

一致性

同一权重、输入、seed、eval 模式与 dtype 下 CPU 与 NPU 输出对比:

输出最大绝对误差平均绝对误差判定
point_forecast [4,64,1]7.63e-061.98e-06PASS
quantiles [4,99,64,1]1.14e-052.04e-06PASS

阈值:atol=1e-3, rtol=1e-3(FP32)。未发生 CPU fallback,全部算子均在 npu:0 执行。

性能

  • 预热 3 次,正式测试 10 次,每轮前后 torch.npu.synchronize()。
  • batch=4,context=512,horizon=64,channels=1,dtype=float32。
指标值
NPU 平均单次推理12.14 ms
NPU 单序列耗时约 3.04 ms/series
CPU 平均单次推理(参考)1517 ms

首轮含编译/加载开销,报告为稳定推理阶段耗时。

证据图

  • assets/agent_workflow.png:侦察、下载、加载、NPU 推理、一致性、benchmark 与校验的完整日志
  • assets/npu_device_call.png:npu-smi、NPU 可用性、设备名与 Tensor device/dtype
  • assets/model_result.png:默认 python inference.py 的真实完整输出

三张证据图由 xterm.js 依据本次真实日志渲染生成(提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$),不是手工绘制的示意图。

限制

  • 本仓提交可复现的 NPU 推理与 CPU-NPU 一致性证据,属于窗口级数值一致性验证,不是 GIFT-Eval 全榜单精度评测。
  • 演示数据为固定 seed 的合成序列;对真实业务数据请替换 past_values 张量,并沿用相同的 [B,T,C] 契约。
  • 点预测通过对 99 分位数积分得到(含尾部扩展),不是模型直接回归输出。