m
mengsihan/ibm-research-test-patchtst-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PatchTST NPU 适配 — ibm-research/test-patchtst

#NPU

将 HuggingFace 权重 ibm-research/test-patchtst(PatchTST,PatchTSTForPrediction)真实运行在单卡 昇腾 Ascend910(npu:0),完成 ETTh1 数据集 7 通道、context=512、horizon=96 的 多变量时间序列点预测,并完成 CPU-NPU 一致性验证与性能基准。

  • 模型 / revision:ibm-research/test-patchtst(main,safetensors,FP32)
  • 架构:PatchTSTForPrediction(d_model=128,layers=3,heads=16,patch=12/stride=12)
  • 任务类型:时间序列预测(点预测),输出 [batch, horizon, channels] 的逆缩放预测
  • 权重来源:https://huggingface.co/ibm-research/test-patchtst
  • 后端:transformers.PatchTSTForPrediction + torch_npu,模型与输入 Tensor 显式位于 npu:0

数据契约

默认输入为真实可追溯的 ETTh1(电变压器温度,小时频率)测试窗口:

  • 来源:https://raw.githubusercontent.com/zhouhaoyi/ETDataset/main/ETT-small/ETTh1.csv
  • 源文件 SHA-256:f18de3ad269cef59bb07b5438d79bb3042d3be49bdeecf01c1cd6d29695ee066
  • 列顺序固定:HUFL, HULL, MUFL, MULL, LUFL, LULL, OT(7 通道)
  • 输入窗口:ETTh1 第 16813–17324 行(1 起始,共 512 行),即 past_values
  • 验证真值:ETTh1 第 17325–17420 行(共 96 行),用于计算 MAE/RMSE
  • 缩放:PatchTSTStdScaler(模型内部,窗口内按通道 std 缩放并逆缩放)
  • 自定义数据:--csv <file>,文件需含同样的 7 列且列序一致;--context/--horizon 控制切分
  • 时间轴 / 频率:小时(1h),无时区,输入窗口不得越过数据边界

环境

项值
设备Ascend910(npu-smi 显示 Ascend910,2 卡,使用 npu:0)
驱动 / CANNCANN 8.5.1,Ascend 驱动 25.5.5
torch / torch_nputorch 2.9.0+cpu / torch_npu 2.9.0.post1+gitee7ba04
transformers4.57.6
numpy / pandas1.26.4 / 2.3.3

安装

pip install -r requirements.txt
# NPU 侧需先安装与 CANN 匹配的 torch_npu(Ascend PyTorch 适配器,非 PyPI 发行)

NPU 推理

# 默认:从 HF 拉取权重(HF_ENDPOINT 默认 hf-mirror.com,可覆盖),使用内嵌 ETTh1 窗口,在 npu:0 推理
python inference.py

# 使用本地权重目录
python inference.py --model-path /path/to/model_dir

# 使用自定义 CSV(列序同 ETTh1)
python inference.py --csv /path/to/data.csv --context 512 --horizon 96

脚本为自包含实现:模型权重从本地目录 / 缓存 / HF 仓库解析;无 NPU 时明确报错并返回非零, 不做静默 CPU fallback;默认执行 3 次预热 + 10 次计时推理,输出同步耗时与状态。

真实结果

默认 python inference.py 在 npu:0 的真实输出(ETTh1 最后 96 步,逆缩放原始单位):

[output] prediction_outputs shape=(1, 96, 7)
[output] OT(7th) channel first_12=[6.299, 6.152, 6.0873, 6.1828, 6.2289, 6.2734, 6.4228, 6.3995, 6.6339, 6.8307, 6.8432, 6.9466]
[result] mae_vs_truth=1.398693 rmse_vs_truth=2.162873
  • 任务指标(相对 ETTh1 真值):MAE 1.3987,RMSE 2.1629
  • 输出 dtype=float32,loc/scale 与预测均以原始单位给出

一致性(CPU-NPU)

同一权重、同一窗口、同一 seed(0)、eval 模式、FP32。输出保存为 .npy 后使用 compare_outputs.py --task forecasting --atol 1e-3 --rtol 1e-2 校验:

cpu_shape=[1,96,7] npu_shape=[1,96,7] finite=true
max_abs_error=0.001003  mean_abs_error=0.000194  passed=true

FP32 下 CPU/NPU 最大绝对误差约 1e-3、平均约 2e-4,在浮点计算差异范围内。

性能

预热 3 次后正式计时 10 次(每轮前后 torch.npu.synchronize()),batch=1,shape=[1,512,7],FP32:

avg=2.444ms  min=2.413ms  max=2.521ms  p50=2.439ms  p90=2.458ms  p95=2.49ms
  • 首轮(含 NPU 图编译)单独计,不计入稳定阶段
  • 吞吐约为 409 windows/s;未计入 CSV 读取与 DataFrame 预处理
  • 峰值显存约 5.1MB,远低于单卡 64GB(模型约 614K 参数 / 2.4MB)

证据图

以下三张 PNG 由 xterm.js 根据本次真实日志渲染(render_xterm_evidence.mjs --style raw), 仅展示本次会话真实 stdout/stderr,非手工补写或绘图库模拟:

  • assets/agent_workflow.png:侦察、数据检查、下载、加载、NPU 推理、一致性、性能与提交校验
  • assets/npu_device_call.png:npu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device
  • assets/model_result.png:默认 python inference.py 的完整真实输出(schema、真实预测、同步耗时、状态)

限制

  • 内嵌演示窗口来自 ETTh1 公开数据集;生产使用时请通过 --csv 提供业务数据
  • 权重需联网从 HF(或其镜像)首次拉取;网络不可用时需预先提供 --model-path
  • 仅完成推理侧适配;若需在 NPU 上完成训练/微调需另行评估
  • test-patchtst 为 ibm-research 提供的测试型 checkpoint,官方完整预训练权重见 https://huggingface.co/ibm-granite/granite-timeseries-patchtst