模型: ibm-granite/granite-timeseries-patchtst-fm-r1 (PatchTST-FM, ~260M params, d_model=1024, n_layer=20, n_head=16, d_patch=16, context_length=8192, prediction_length=64, 99 quantiles)
任务: 时间序列概率预测(time-series-forecasting, probabilistic quantile forecasting)
硬件: 单卡 Ascend 910 (npu:0) 真实推理验证
权重: https://huggingface.co/ibm-granite/granite-timeseries-patchtst-fm-r1 (revision 151f9c6d576281b95c2ff784d0863bd3f12c80f1)
标签: #NPU #Ascend #Ascend910 #time-series #forecasting #PatchTST
Granite TimeSeries PatchTST-FM-r1 是 IBM 基于 PatchTST 的时序基础模型,结构保持 PatchTST 简洁性但加入 residual blocks、quantile head、分片连续掩码与预测期随机掩码、重构损失训练,推理时将预测视为掩码重构。context 包含历史与预测总长 8192,不足时前置均值填充并视为 masked。在 GIFT-Eval zero-shot 可复现模型中位列 Top5。
| 项 | 值 |
|---|---|
| 输入 | past_values torch.Tensor shape [B, T, C],B≥1, T≤8192, C≥1(单变量时 C=1),示例 B=2 T=512 C=2 |
| T 约束 | 建议被 16 整除,否则内部 padded 并自动截断 |
| 频率信息 | 无需 freq_token(PatchTST-FM 不使用 frequency token,区别于 TTM) |
| 缺失值 | NaN 先以均值填充 |
| 输出 | quantile_outputs shape [B, 99, 64, C],quantile_levels 0.01..0.99,median 为 index 49 (0.5) |
| 缩放 | RevIN 内部归一化,输出已 inverse |
| 评估 | quantile 单调性、median 的形状与有限值 |
本仓默认演示使用合成数据:B=2, T=512, C=2 的正弦/余弦序列,seed 42,保证可复现 NPU 推理。
pip install -r requirements.txt
# granite-tsfm (若 pip 索引无此版本,从源码安装)
pip install git+https://github.com/ibm-granite/granite-tsfm.git权重下载(首次):
hf download ibm-granite/granite-timeseries-patchtst-fm-r1 --local-dir ./.cache/granite-timeseries-patchtst-fm-r1python inference.py默认从 LOCAL_CANDIDATES 查找权重:/tmp/models/granite-direct、/opt/atomgit/adapt-npu-agent/working/models/granite-timeseries-patchtst-fm-r1、./.cache/granite-timeseries-patchtst-fm-r1,均不存在时自动从 HF 镜像下载。模型与输入显式 to("npu:0"),日志打印 first param device after to(npu:0): npu:0、output ... device npu:0。
真实输出示例(本次 NPU 运行,B=2 T=512 C=2):
[INFO] output quantile shape (2, 99, 64, 2) device npu:0 dtype torch.float32
[INFO] inference time (synchronized): 0.0116s
[INFO] median preview batch0 first5 C0: [0.0200, 0.0807, 0.1395, 0.2014, 0.2611]
[INFO] median preview batch0 first5 C1: [1.0009, 0.9976, 0.9940, 0.9932, 0.9903]
[SUCCESS] NPU inference completed on npu:0 with PatchTSTFMForPrediction同一权重/输入/预处理/精度(float32)/seed 下 CPU vs NPU 对比(scripts/compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3):
报告为单窗口 smoke consistency,非全数据集 benchmark。
单卡 npu:0,同步计时(torch.npu.synchronize()):
预处理(numpy 合成)不计入推理计时,已说明。
三图由 scripts/render_xterm_evidence.mjs --style raw 基于真实日志生成,深灰背景、xterm.js 渲染,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$:
assets/agent_workflow.png:侦察-下载-加载-NPU-CPU一致性-benchmarkassets/npu_device_call.png:npu-smi 与 torch.npu 可用性、参数 device、输入输出 deviceassets/model_result.png:python inference.py 完整输出
