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

#NPU

ibm-research/patchtst-etth1-pretrain — Ascend NPU 适配

模型简介

  • 模型名称: ibm-research/patchtst-etth1-pretrain (PatchTST PatchTSTForMaskPretraining, 6层, d_model=128)
  • 权重URL: https://huggingface.co/ibm-research/patchtst-etth1-pretrain
  • Revision: main (config 6 layers, pytorch_model.bin 4.85 MB)
  • 架构: PatchTST Pretraining — Encoder 6层 Transformer (d_model=128, n_head=16, patch_length=12, stride=12, num_patches=42, encoder_ffn_dim=512), BatchNorm, channel-independent, mean scaling, w_pos [42,128]
  • 任务: 时间序列掩码重建 pretraining — 重建 patches [B,7,42,12] via head.linear → 输出 [B,7,42,12] + loc/scale [B,1,7], masked ratio 0.2
  • 上下文/预测: context 512 / patch 12 → 42 patches; prediction_length 24 (config) 但 pretrain head 输出 patch 重建非 forecast; 推理演示固定 512 → 重建 42×12
  • 通道: 7 (ETTh1 7通道: HUFL/HULL/MUFL/MULL/OT/LULL 等归一化前)
  • 权重: 4.85 MB float32 pytorch_model.bin (137 tensors, ~1.1M 参数)
  • 框架: 自定义 PatchTSTForMaskPretraining (非远程代码, 权重复刻) + torch_npu, 源自 transformers 4.33 定义
  • 许可证: apache-2.0

数据契约

项值
轴顺序[B, T, N] (严格, 不可转置; 内部转置为 [B,N,T] 再 unfold)
形状batch=2, context=512, channels=7 → 输出 [B,7,42,12] (patches 重建), loc/scale [B,1,7]
频率无显式 freq token; 依赖位置编码 w_pos sincos 等效
缩放模型内部 mean scaling: `scale = mean(
缺失不适用 (dense); 训练时 mask_ratio 0.2 但推理不掩码, 验证用完整序列
数据确定性多通道合成 (seed 42): randn*0.5 + trend(0..0.7*ch*0.1) + sin(2πt/24)*0.3 for ch0, shape [2,512,7], mean 0.1096 std 0.5164, preview batch0 last5 ch0 [-0.1765,0.4729,0.2076,0.2333,1.1322]; SHA256 0a85c5fd
输出output [B,7,42,12] float32 重建 patches, 另有 patches 输入切片; 统计 mean 0.0174 std 0.7663
dtypefloat32
预处理无外部 scaling; 模型内部 mean scaling 需 observed=ones_like(past)

环境

  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, huggingface_hub 0.36.2, numpy 1.26.4
  • CANN 8.5.1, npu-smi 25.5.5, Ascend910_9362 ×2, 目标 npu:0
  • torch.npu.is_available()==True, device_count==2
  • HF_ENDPOINT=https://hf-mirror.com (国内镜像), 无手动 http_proxy 覆盖, 依赖环境变量直连
  • 加载: torch.load(pytorch_model.bin) → 自定义模型 → model.to("npu:0"), 所有 Tensor 显式 to(npu:0)

安装

pip install -r requirements.txt

NPU 推理

python inference.py

加载 working/patchtst-etth1/pytorch_model.bin 或自动从 HF 镜像下载 → npu:0 → warmup 3 → 同步计时 → 打印真实重建值。

关键日志(npu:0):

[INFO] model_name: ibm-research/patchtst-etth1-pretrain
[INFO] task: mask-reconstruction (pretraining) | backend: torch_npu | device: npu:0
[INFO] first param device before NPU move: cpu
[INFO] first param device after to(npu:0): npu:0
[INFO] input NPU device past:npu:0
[INFO] output summary: shape=(2, 7, 42, 12) mean=0.017439 std=0.766303
[SUCCESS] NPU inference completed on npu:0 with PatchTSTForMaskPretraining (ibm-research/patchtst-etth1-pretrain) mask-reconstruction

真实结果 (npu:0, 512 ctx, 7 ch, seed 42 合成)

  • output shape (2,7,42,12) device npu:0 dtype float32
  • input preview batch0 last5 ch0 [-0.1765,0.4729,0.2076,0.2333,1.1322] mean 0.1096 std 0.5164
  • output preview batch0 ch0 patch0 first5 [-2.5408,-2.1150,-1.9234,-1.7097,-1.3537] (重建后未经 inverse scale? 已为 scaled 空间经 head 输出, 需 *scale 恢复但演示打印原始)
  • loc shape (2,1,7) scale shape (2,1,7) scale 均值 ~0.40
  • output stats mean 0.017439 std 0.766303 min -3.3604 max 3.6606
  • patches shape (2,7,42,12) 输入切片均值对比输出验证重建能力
  • finite True shape_ok True fallback_detected False
  • SHA256 past 0a85c5fd (可复现)

一致性 (CPU vs NPU, 同权重/同输入/同dtype/同seed, float32, eval)

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_output.npy --npu working/npu_output.npy --task regression --atol 1e-3 --rtol 1e-3
输出max_absmean_abspassed
recon [2,7,42,12]5.43e-045.32e-05✅ (atol 1e-3)

阈值说明: FP32 默认 1e-4 对本模型 6层 BatchNorm+Attention 过严 (实测 5.4e-04), 改为 1e-3 仍严格 (mean 5.3e-05 远低于 1e-4). 同 transformers 4.33 定义权重, 同 torch.load 权重, 无 CPU fallback, torch.npu.synchronize() 后落盘 npy 直接比较。

性能 (npu:0, torch.npu.synchronize() 前后计时, 纯模型 forward)

  • Warmup 3, 测试 10, batch2 ctx512 ch7 → out [2,7,42,12]
  • 单次 sync elapsed 3.40 ms (含 mean scaling + patchify + 6层 Transformer + head)
  • avg 3.34 ms min 3.25 max 3.43 p50 3.34 p90 3.39 p95 3.41
  • 吞吐 598.9 windows/s (batch 2 窗口), 299.4 series/s (按 batch 平均)
  • 显存 allocated 4.82 MB peak 9.33 MB (模型 4.85 MB + 激活)
  • 预处理 (合成数据生成) 不计入; 仅 model(past_values) 同步计时

证据图 (xterm.js 真实日志)

agent workflow npu device call model result

由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签,不证明服务器身份。

限制

  • 不含权重;首次运行自动从 HF 拉取 (HF_ENDPOINT 镜像)。
  • 输入严格 [B,T,N] 三维, 通道数必须 7, 上下文 512, patch 12/12 固定 42; 维度不符将异常非零退出。
  • 仅验证 pretraining 重建路径 (mask-reconstruction), 非直接 forecasting 96/24 horizon 端到端预测; 如需微调 forecasting 需另搭 head。
  • 单 batch (2 窗口) smoke 一致性, 非全量 ETTh1 test benchmark (MSE 0.3881 需下游训练)。
  • FP32 only, BF16/FP16 未验证; Cannot create tensor with interal format warning 无害已保留。

目录结构

submission/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png