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

Granite TimeSeries PatchTST-FM r1 (NPU)

#NPU 真实推理:Granite-TimeSeries-PatchTST-FM-r1 已在单卡 Ascend 910 (npu:0) 上完成零样本概率预测验证。

  • 模型:ibm-granite/granite-timeseries-patchtst-fm-r1
  • 架构:PatchTSTFMForPrediction(PatchTST-FM,IBM granite-tsfm),约 258M 参数,fp32
  • 任务:时间序列点预测 + 99 分位概率预测
  • 配置:context_length=8192, prediction_length=64, n_patch=512, d_model=1024, n_layer=20, num_quantile=99

数据契约

  • 输入:past_values,形状 (batch, context, num_features),fp32
  • 模型直接接受缺失值(NaN),支持不等长序列(左端用序列均值填充并标记 pad)
  • 本次验证输入:ETTm1 数据集(7 通道)最后 512 步 → 输出 64 步
  • 输出:
    • prediction_outputs:(batch, prediction_length, num_features) 点预测(对 99 分位做梯形积分得到均值)
    • quantile_outputs:(batch, num_quantiles, prediction_length, num_features) 99 分位预测
  • 轴顺序为 [batch, time, channel],输出已做 inverse transform(RevIN)

环境

  • Ascend CANN 8.5.1,torch 2.9.0,torch_npu 2.9.0.post1,transformers 4.57.6,Python 3.11
  • NPU:2 × Ascend910(HBM 64GB/卡),验证使用 npu:0

依赖

pip install torch==2.9.0 torch-npu==2.9.0.post1
pip install transformers einops huggingface_hub numpy

模型代码已内嵌在 inference.py(来自 ibm-granite/granite-tsfm,Apache-2.0),无需额外安装 tsfm 包。

NPU 推理

# 默认 npu:0;权重自动从 HF(国内镜像直连)下载到 ./granite-patchtst-fm-model
python inference.py

# 使用本地权重 + 真实 CSV 数据 + 性能基准
python inference.py --model-dir /path/to/model --data /path/to/ETTm1.csv --bench

# CPU 基线(一致性对比用)
python inference.py --device cpu

首次运行会触发算子编译(首轮约 270ms),随后为稳定推理。

真实结果(npu:0,ETTm1 最后 512 步 → 64 步)

model: ibm-granite/granite-timeseries-patchtst-fm-r1 | params: 257,895,552
first param device: npu:0 | dtype: torch.float32
input: series shape=(1, 512, 7) context=512 -> horizon=64
prediction_outputs shape: (1, 64, 7) | quantile_outputs shape: (1, 99, 64, 7)
first-run latency: 268.02 ms
bench (after 10 runs, ms): avg=12.87 min=12.13 max=12.19 p50=13.03 p90=13.08 p95=13.09
point forecast [first 8 steps]: [12.8158, 12.9343, 13.0778, 13.1547, 13.2580, 13.4052, 13.4824, 13.5122]
quantile forecasts (q=0.05/0.50/0.95) step[0]: [10.2918, 12.8799, 14.4520]
STATUS: PASS

峰值显存:约 1073 MB(模型权重 ~1.03GB fp32 + 激活)。

一致性(CPU vs NPU)

同一权重、同一输入(ETTm1 末 512 步,7 通道)、同一 dtype(fp32)对比:

  • prediction_outputs:max_abs_err = 2.098e-5(atol=1e-4, rtol=1e-3)✅
  • 输入输出形状、有限性一致,分位数顺序单调

性能(npu:0)

项目数值
首轮(含编译)268.02 ms
稳定 avg12.87 ms
min / max12.13 / 13.09 ms
p50 / p90 / p9513.03 / 13.08 / 13.09 ms
batch1
context / horizon / channels512 / 64 / 7
dtypefp32
峰值显存~1073 MB

测试方法:每轮前后 torch.npu.synchronize(),预热 3 次,正式 10 次。计时仅含模型 forward,不含数据读取与预处理。

证据图

三张截图由 xterm.js 根据本次真实日志渲染(非手工绘制):

  • assets/agent_workflow.png:侦察、权重下载、CPU 基线、NPU 推理、一致性、性能、校验全流程
  • assets/npu_device_call.png:npu-smi info、torch.npu 可用性、模型与输入/输出 Tensor device/dtype
  • assets/model_result.png:默认推理真实完整输出(shape、真实预测、同步耗时、STATUS)

限制

  • 验证数据为公开 ETTm1 的固定窗口切片;输出是模型真实零样本预测,非完整 benchmark 排名
  • 支持多通道输入,但本次验证以 ETTm1 7 通道为准
  • model.safetensors 不随仓提交,运行时自动下载(约 1.03GB)