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

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

任务ID a1d608a124fd4f32a397b3fda742c7d3 · 原始权重 https://huggingface.co/ibm-granite/granite-timeseries-patchtst

#NPU

本仓库将 HuggingFace 上的 ibm-granite/granite-timeseries-patchtst(PatchTSTForPrediction,ETTh1 7通道,context 512,prediction 96,patch 12/12,d_model 128,3层 Transformer,16头,约614K参数,~2.4 MB model.safetensors)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。支持点预测时间序列预测,输出预报张量,指标 MAE/RMSE 可在下游评估。

  • 模型原始仓库:https://huggingface.co/ibm-granite/granite-timeseries-patchtst
  • 权重:model.safetensors 2.4 MB,config.json context 512 prediction 96 patch 12(_name_or_path namctin/patchtst_etth1_forecast),transformers 4.36.0 版配置移植
  • 源实现:transformers 4.57.6 的 PatchTSTForPrediction(无需 trust_remote_code)
  • Revision:7fe295d8bc8fbac8041b60ab351882634165517f(config + model.safetensors)
  • 许可证:Apache-2.0

任务与路由

项值
模型ibm-granite/granite-timeseries-patchtst(revision 7fe295d, PatchTST, 614K, ETTh1)
架构PatchTSTForPrediction(channel-independent patch embedding + 3层 Transformer encoder + prediction head)
任务时间序列点预测 forecasting(time-series-models.md 路线),验收 forecast tensor
输入past_values [batch, time, channel] = [batch, 512, 7],7通道 ETTh1(HUFL,HULL,MUFL,MULL,LUFL,LULL,OT),float32,scaling std,past_observed_mask 默认为全1
输出prediction_outputs [batch, prediction_length, num_input_channels] = [batch, 96, 7],点预测 float32,已 inverse scale
数据契约轴顺序固定 [B,T,C](不可转置),context 512,horizon 96,patch 12/12→42 patches,通道 7,频率小时级 ETTh1,缩放 std(mean 0 std 1 逆变换已合并到 head),无辅助协变量,输入有限值校验
路由点预测(参考 time-series-models.md),保存 forecast 并比较 MAE/RMSE,有标签可用 SMAPE

环境

  • 操作系统:Linux aarch64,昇腾 910B(Ascend910_9362),CANN 8.5.1,npu-smi 25.5.5,2卡(使用 npu:0)
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,numpy 1.26.4,huggingface_hub
  • 依赖见 requirements.txt:torch、torch_npu、transformers、numpy、huggingface_hub、safetensors
  • 权重缓存:/tmp/models/granite-timeseries-patchtst(或 hub cache models--ibm-granite--granite-timeseries-patchtst),首次约 2.4 MB + 331 KB 图片

安装

pip install -r requirements.txt

NPU 推理

inference.py 为自包含脚本,默认使用确定性合成 ETTh1-like 多变量时序(seed 42)在 npu:0 上完成 CPU 基线、NPU 推理、一致性与性能基准。核心 forward 的所有参数与参与计算的 Tensor 均显式位于 npu:0(见 npu_device_call.png 与 model_result.png)。计时前后均调用 torch.npu.synchronize()。

python inference.py
MODEL_DIR=/tmp/models/granite-timeseries-patchtst python inference.py

真实结果(Ascend 910B 单卡,npu:0)

以确定性合成 ETTh1 多变量时序(seed 42,past_values [2,512,7] 均值 0.0083 std 1.0022)为例:

  • 输入:past_values shape (2,512,7) float32,patching 42 patches(12/12),scaling std,axis [B,T,C]。
  • CPU 基线:forecast shape (2,96,7) 均值 0.037661 std 0.164059,样例 batch0 ch0 前5 [0.20202, 0.176456, 0.153266, 0.171155, 0.181743];batch1 ch0 前5 [-0.140478, -0.189977, -0.213437, -0.11593, -0.078209]。
  • NPU 输出:相同输入在 npu:0 上 forecast shape (2,96,7) 均值 0.0377,样例 batch0 ch0 前5 [0.20214, 0.176585, 0.153398, 0.171286, 0.18185];batch1 ch0 前5 [-0.140393, -0.189878, -0.213325, -0.115818, -0.078109]。
  • 推理状态:[STATUS] SUCCESS - NPU inference completed without CPU fallback

完整输出见 model_result.png。

CPU-NPU 一致性

python .claude/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu /tmp/cpu_granite.npy --npu /tmp/npu_granite.npy --task forecasting --atol 5e-4 --rtol 1e-3
  • 点预测 forecast:shape (2,96,7) 一致,max_abs 1.64e-04,mean_abs 4.54e-05,atol=5e-4 rtol=1e-3 PASS,有限值 True
  • 阈值说明:atol=5e-4 为 FP32 跨设备正常波动

性能(Ascend 910B,npu:0,batch 2,512→96,7通道)

  • 耗时:warmup 首轮 199.73 ms 后稳定 avg 2.47 ms, min 2.45, max 2.52, p50 2.46, p90 2.52, p95 2.52(batch 2)
  • 吞吐:808.52 windows/s(2 windows / 0.00247s)
  • 显存:权重 2.4 MB + 激活 <100 MB

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成:

agent workflow npu device call model result

限制

  • 仅验证合成 ETTh1-like 随机时序的 smoke consistency(2 windows,context 512 horizon 96),非 ETTh1 test split 全量 MSE 0.3881 benchmark。
  • 单卡推理,未验证多卡/分布式;未量化(FP32)。