sheepss/ibm-research-patchtst-etth1-pretrain-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-research/patchtst-etth1-pretrain - NPU

#NPU

模型简介

ibm-research/patchtst-etth1-pretrain 是 IBM 发布的 PatchTST 时间序列掩码预训练模型,基于 Transformer 架构,针对 ETTh1 数据集(7 通道:HUFL/HULL/MUFL/MULL/LUFL/LULL/OT)预训练。模型将 512 长度的历史窗口切分为 42 个 patch(patch_length=12, stride=12),通过随机掩码重建学习时序表征,适用于下游预测、分类、回归等微调任务。

  • 架构:PatchTSTForPretraining(legacy PatchTSTForMaskPretraining),model_type=patchtst
  • 参数量:约 1.20M(1198220),权重 pytorch_model.bin 约 4.6 MB
  • 上下文长度 512,预测长度 24(预训练时掩码重建),d_model=128,encoder_layers=6,encoder_attention_heads=16,encoder_ffn_dim=512
  • 许可证:apache-2.0
  • 任务:时间序列掩码重建(pretraining reconstruction),本仓验证点为 prediction_output 重建张量
  • 权重地址:https://huggingface.co/ibm-research/patchtst-etth1-pretrain,revision 57adb20c250de139798567dd8a0b040390475717
  • 推理精度:FP32

NPU 适配说明:原始 checkpoint 为 transformers==4.33.0.dev0 旧版结构(model.encoder.w_p / model.encoder.encoder.layers.*),当前 transformers==4.57 已重命名为 model.encoder.embedder.input_embedding / model.encoder.layers.*。inference.py 在加载时自动映射旧键到新键并翻译 encoder_layers→num_hidden_layers 等配置,无需额外权重转换。

数据契约

采用 ETTh1 真实序列的 hf-internal-testing/etth1-hourly-batch(ETTh1 小时级 7 通道数据)作为可追溯输入:

  • 来源:https://huggingface.co/datasets/hf-internal-testing/etth1-hourly-batch 的 train-batch.pt(包含 past_values [64,512,7] 与 future_values [64,96,7])
  • 真实性:为 ETTh1 官方小时级数据的标准化切片,非合成数据
  • 默认窗口:取 past_values[0] 单窗口 [512,7],7 通道顺序与 ETTh1 一致(HUFL/HULL/MUFL/MULL/LUFL/LULL/OT)
  • 数据统计(默认窗口):mean -0.2537, std 0.8942, min -4.4812, max 2.6223
  • 轴顺序:[batch, time, channel],time=512, channel=7
  • 频率:小时级(ETTh1 hourly)
  • 预处理:模型内部 PatchTSTMeanScaler(scaling="mean")自动做 instance mean 归一化与逆归一化;输入无需额外缩放
  • 掩码:为保证 CPU-NPU 一致性,本仓将随机掩码固定为确定性掩码——掩码最后 2 个 patch([B,7,42] 中后 2 列置 True),mask_value=0,其余 40 个 patch 保持原始值。原始随机掩码(mask_ratio 0.2 / mask_patches [2,3])在 CPU 与 NPU 上因 torch.rand 实现差异会产生不同 mask,导致输出不可比;固定掩码后重建输出可严格对比。

环境

  • NPU:Ascend910 (Ascend910_9362),npu-smi 25.5.5,双卡 64GB HBM,实测 npu:0
  • CANN:8.5.1,torch 2.9.0+cpu,torch_npu 2.9.0.post1+gitee7ba04,transformers 4.57.6
  • Python 3.11.14,numpy 1.26.4,huggingface_hub

安装

pip install -r requirements.txt

模型权重通过 HuggingFace 镜像下载到本地目录后,从本地加载(inference.py 会优先搜索 /tmp/converted_patchtst 与 work_2fd820b9_patchtst/model,若不存在则自动 snapshot_download):

python -c "from huggingface_hub import snapshot_download; snapshot_download('ibm-research/patchtst-etth1-pretrain', local_dir='./patchtst-etth1-pretrain')"

NPU 推理

python inference.py --model-dir ./patchtst-etth1-pretrain --batch-size 1

inference.py 自包含:自动处理旧版权重映射、确定性掩码补丁、模型与输入显式迁移到 npu:0,打印模型/任务/backend/device/dtype/首个参数 device、输入与输出摘要、同步耗时和最终状态。输出 prediction_output 形状 [B,7,42,12],即每个通道 42 个 patch 的重建值。

CPU fallback 检查:脚本在 npu:0 上显式执行,若 torch.npu.is_available()==False 则直接退出非零,不静默回退到 CPU。

真实结果

使用 ETTh1 真实窗口 [1,512,7],确定性掩码(最后 2 patch),FP32,npu:0:

  • 输入:[1,512,7],mean -0.2537, std 0.8942
  • 输出:prediction_output [1,7,42,12],loss (masked MSE) 1.424148
  • 输出统计:mean 0.0216, std 0.8754, min -3.1100, max 4.0029
  • 示例(channel 0, patch 0, 12 值):[-0.0503, -0.2212, -0.3912, -0.1010, 0.3597, 0.7467, 1.1403, 0.8067, 0.0225, -0.5370, -1.0086, -0.9481]
  • 被掩码的最后 2 个 patch(channel 0)均值 -0.4537,示例 patch -2:[-0.2711, -0.2424, -0.1913, 0.1576, 0.3251, 0.7325, 0.6281, 0.2648, -0.7138, -1.6550, -2.3824, -2.6099]
  • 首轮(含编译)187.76 ms,稳定阶段 ~4.4 ms

完整终端输出见 assets/model_result.png。

一致性(CPU vs NPU)

同一权重、同一输入(ETTh1 past_values[:2],[2,512,7])、同一确定性掩码、同一 seed、eval 模式、FP32:

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu /tmp/cpu_patchtst_det.npy --npu /tmp/npu_patchtst_det.npy --task regression --atol 0.002 --rtol 0.001
  • CPU 输出 prediction_output [2,7,42,12],mean 0.007714,loss 1.488178
  • NPU 输出 prediction_output [2,7,42,12],mean 0.007732,loss 1.488031
  • 全部元素有限
  • max_abs_error 0.001321,mean_abs_error 0.000102
  • PASS(FP32,atol=2e-3, rtol=1e-3;原始随机掩码会因 CPU/NPU torch.rand 差异导致 max_abs ~7.0 不可比,本仓通过固定掩码消除伪不通过)

性能(npu:0,Ascend910,FP32,context=512,channel=7,batch=1)

计时前后均执行 torch.npu.synchronize(),仅计模型前向,不含权重加载与数据下载:

  • 首轮(含图编译):187.76 ms(inference.py)/ 4.48 ms(bench 纯前向,已 warmup)
  • 稳定阶段(预热 3 次 + 正式 10 次):avg 4.44 ms,min 4.39,max 4.48,p50 4.44,p90 4.47,p95 4.48
  • 吞吐:~225 windows/s(batch 1)/ ~235 windows/s(inference.py 10 次平均 4.25 ms)
  • NPU 峰值显存:~7.6 MB active + 8 MB reserved(torch_npu.memory_stats),模型权重 4.6 MB 驻留
  • dtype:FP32,BatchNorm

证据图

  • assets/agent_workflow.png:侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark、校验全流程
  • assets/npu_device_call.png:npu-smi、NPU 可用性、设备名、参数 device/dtype、输入输出 Tensor device
  • assets/model_result.png:默认 python inference.py 的真实完整输出

三张 PNG 均由 xterm.js 根据本次真实日志生成(非手工绘制、非桌面截图)。

限制

  • 单卡 npu:0 推理;未测试多卡或 BF16/FP16
  • 确定性掩码仅用于 CPU-NPU 一致性验证;原始论文随机掩码用于训练时应保留随机性
  • 模型为掩码预训练 checkpoint,PatchTSTForPretraining 输出为重建值,非直接未来预测;下游预测需微调为 PatchTSTForPrediction
  • 本仓不含权重与原始大数据集,仅含推理脚本与说明