liucu/Datadog-Toto-2.0-313m-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-granite/granite-timeseries-patchtst-fm-r1 NPU 适配

模型: 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 推理。

环境

  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1, transformers 4.57.6, einops
  • CANN 8.5.1, Ascend910_9362, npu-smi 25.5.5
  • granite-tsfm 0.3.9.dev4 (tsfm_public.models.patchtst_fm)
  • HF_ENDPOINT=https://hf-mirror.com,权重本地加载不联网

安装

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-r1

NPU 推理

python 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):

  • 输入 B=2 T=512 C=2, pred_len=64, Q=99
  • max_abs_error 5.96e-06, mean 9.59e-07, passed true
  • 输出有限值、quantile 单调性 True

报告为单窗口 smoke consistency,非全数据集 benchmark。

性能

单卡 npu:0,同步计时(torch.npu.synchronize()):

  • 预热 3 次,测试 10 次,B=2 T=512 C=2:avg 11.77ms, min 11.56ms, max 11.94ms, p50 11.77ms, p90 11.93ms, p95 11.94ms, 吞吐 ~170 windows/s
  • 首轮含编译 ~277ms(冷启动)
  • 不同 ctx:8192→13.29ms/seq, 2048→11.53ms, 512→11.53ms(B=1 C=1,5次平均)
  • 显存峰值 ~3GB(重量 984MB, d_model 1024, 20层)

预处理(numpy 合成)不计入推理计时,已说明。

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 基于真实日志生成,深灰背景、xterm.js 渲染,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$:

  • assets/agent_workflow.png:侦察-下载-加载-NPU-CPU一致性-benchmark
  • assets/npu_device_call.png:npu-smi 与 torch.npu 可用性、参数 device、输入输出 device
  • assets/model_result.png:python inference.py 完整输出

agent_workflow npu_device_call model_result

限制

  • 仅验证推理,不含训练;fit() 区分说明(本模型 zero-shot,无需 fit)
  • 输入超过 8192 时需截断;未测试极大 batch (>8) 与多通道 (>8) 显存边界
  • FP32 精度,BF16/FP16 未验证
  • 未接入 GIFT-Eval 全量数据,仅合成数据 smoke 测试