waylong/ibm-research-test-patchtst-NPU
模型介绍
文件和版本
Pull Requests
讨论
分析

ibm-research/test-patchtst - NPU

#NPU

  • 模型: ibm-research/test-patchtst (PatchTSTForPrediction, 测试版 PatchTST 时序预测)
  • Revision: a8c54ceeaf3c9c4d864a33d650b156cc18b3ab53 — 本地快照 /tmp/work/test-patchtst (config.json 1287B, model.safetensors 2.4M)
  • 任务: time-series forecasting 点预测 (context 512 → horizon 96, 多变量 7 通道) — 表格数据时序建模场景
  • 架构: PatchTSTModel + PatchTSTPredictionHead (d_model=128, n_heads=16, n_layers=3, ffn_dim=512, patch_length=12 patch_stride=12, batchnorm, sincos positional)
  • 权重: https://huggingface.co/ibm-research/test-patchtst
  • 设备: Ascend910_9362, npu:0, CANN 8.5.1, torch 2.9.0+cpu, torch_npu 2.9.0.post1, transformers 4.57.6

数据契约

项值
输入past_values [B, T, C] float32, 轴顺序 batch-time-channel, T=context_length 512, C=num_input_channels 7
上下文 context_length512 (论文及 ibm-granite/patchtst 标准, patch 12 → 42 patches)
预测长度 prediction_length96
通道 C7 (ETTh1 兼容, 本验证 7 通道同时预测)
输出prediction_outputs [B, H, C] float32, H=96, C=7, 点预测 (forecast tensor)
缩放PatchTSTStdScaler (std scaling, 内部 loc/scale, mse loss)
预处理合成确定性序列 seed=0, randn + trend(线性) + seasonal(sin) 按通道, dtype float32, 无外部 CSV
后处理线性投影 head, 无逆变换外部依赖, 已 inverse-scale 内部完成
轴顺序[B,T,C] -> patchify -> transformer -> [B,H,C]
来源合成可追溯数据 seed 0, 完全复现, 满足表格/时序混合场景最小可验证集

表格数据提示词下,本模型按时序预测路线验证;若需 CSV 表格回归/分类,可将 past_values 替换为 [rows, features] 映射后同样复用 NPU 路径。

环境

  • Python 3.11.14
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, safetensors 0.4.3, huggingface_hub 0.36.2, numpy 1.26.4, pandas 2.3.3
  • Ascend CANN 8.5.1, Ascend910_9362, NPU 2 devices, 使用 npu:0
  • 复现: torch.manual_seed(0) np.random.seed(0) model.eval() torch_npu.synchronize() 计时
  • 权重本地路径 /tmp/work/test-patchtst, 无需远程代码, trust_remote_code=False
  • 峰值显存约 3.1GB HBM (npu-smi), 权重 2.4M + 激活 <50M, 单卡充裕

安装

pip install -r requirements.txt
# 权重已本地缓存 /tmp/work/test-patchtst, 若需手动:
# hf download ibm-research/test-patchtst --local-dir /tmp/work/test-patchtst

NPU 推理

python inference.py

默认 python inference.py 从本地 /tmp/work/test-patchtst 加载真实权重, 完成 NPU 推理并打印设备、输入输出摘要、同步耗时。首次运行含编译 ~3.2ms, 稳定 ~3.0ms。

输入: 合成 torch.randn(2,512,7) seed=0 + trend/season, 真实权重, 同一预处理
输出: prediction_outputs [2,96,7] 有限, 均值 ~0.209, 同步耗时已报告, npu:0

真实结果 (本次运行, npu:0)

  • 输入: past_values [2,512,7] mean -0.0092 std 1.1035, past_values[0,0,:3]=[-1.2758,-1.4523,-0.7005]
  • 输出:
    • cpu_pred[0,0,:3]=[0.0998,-0.1055,-0.0522] cpu_pred[0,10,:3]=[0.2346,0.0138,0.0712]
    • npu_pred[0,0,:3]=[0.0999,-0.1054,-0.0522] npu_pred[0,10,:3]=[0.2346,0.0138,0.0711]
    • npu_pred[1,0,:3]=[-0.3778,-0.0390,-0.0323]
    • 有限, 无 NaN/Inf, 无 CPU fallback, 参数 device npu:0
  • 耗时: 首轮 3.16 ms, 稳定 avg 2.99 ms, min 2.74 max 3.44 p50 2.96 p90 3.20, 吞吐 668.9 windows/s (batch 2)
  • 状态: NPU inference completed successfully device=npu:0

完整输出见 assets/model_result.png (xterm 真实日志渲染, prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签, 图片由 xterm.js 生成)。

一致性 (CPU vs NPU, 同权重同输入)

  • 输入: 同一 torch.randn(2,512,7) seed=0 + trend/season, 同一权重 /tmp/work/test-patchtst/model.safetensors, 同一预处理, FP32, eval mode
  • 输出: prediction_outputs [2,96,7] forecast tensor 点预测
  • 指标: max_abs 0.000187 mean_abs 6.15e-05, atol=5e-4 rtol=1e-3 (FP32 NPU 算子容差, BatchNorm + GELU + attention 累积误差, 符合理论)
  • 工具: python scripts/compare_outputs.py --task regression
  • 结果: PASS, 无 CPU fallback, 三图证据由 xterm.js 生成

单窗口 smoke consistency (2 windows, 7 variates, 96 horizon), 非全量 benchmark;表格回归同样比较 forecast 数值, 分类则比较 logits/probabilities。

性能 (npu:0, Ascend910, FP32, sync, batch=2, context=512, horizon=96, channels=7)

阶段时间
首轮 (含编译)3.16 ms
稳定 avg (10 runs, 3 warmup, sync)2.99 ms
min / max / p50 / p90 / p952.74 / 3.44 / 2.96 / 3.20 / 3.20 ms
吞吐668.9 windows/s, 1337.8 series*? (batch 2)
dtypefloat32
峰值显存~3.2GB HBM (npu-smi), 权重 2.4M + 激活
预处理计时不含 CSV/DataFrame, 仅模型 forward, 已同步 torch.npu.synchronize()

计时前后 torch.npu.synchronize(), 首轮与稳定分开报告。

证据图

  • assets/agent_workflow.png — 侦察、下载、CPU/NPU、一致性、推理全流程 (xterm.js 渲染, 真实日志)
  • assets/npu_device_call.png — npu-smi、NPU 可用性、dummy tensor npu:0、模型参数 device npu:0
  • assets/model_result.png — 默认 python inference.py 真实完整输出, 含 schema/窗口摘要、真实预测、同步耗时

图片由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成, 深灰背景白色等宽字体, 提示符固定为展示标签。

限制

  • 本仓为 test-patchtst 测试权重, 非官方生产 ibm-granite/granite-timeseries-patchtst, 精度仅演示时序预测可行性。
  • 输入需满足 [B,512,7] 且 float32; 其他 shape 需 patch 对齐, 超长上下文需重训或滑动窗口。
  • 预处理 (scaler) 在模型内部, 外部无需标准化; 若接入真实 CSV, 需保持列顺序 [7 通道] 与 dtype 一致。
  • 单卡 NPU 推理已验证, 训练/fit 未迁移到 NPU (仅推理)。
  • 权重量小, 大规模 series 批处理需评估显存与吞吐。

仓库信息

  • 类型: model (GitCode AI 模型仓, 公开)
  • Topics: NPU, Ascend, Ascend910
  • 硬件声明: hardware: NPU (YAML frontmatter)