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

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

任务ID d365c5581e84472e95af18e73690f55b · 原始权重 https://huggingface.co/ibm-research/patchtst-etth1-pretrain

#NPU

本仓库将 HuggingFace 上的 ibm-research/patchtst-etth1-pretrain(PatchTST ETTh1 Masked Pretraining, 6 层 Transformer, d_model 128, 16 heads, context 512, patch 12×42, 7 通道)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。通过 PatchTST encoder 的确定性前向在 npu:0 上完成时序表征推理,输出 last_hidden_state。

  • 模型原始仓库:https://huggingface.co/ibm-research/patchtst-etth1-pretrain
  • 权重:pytorch_model.bin 4.7 MB,config.json context 512, patch_length 12, stride 12, num_patches 42, d_model 128, encoder_layers 6, encoder_attention_heads 16, encoder_ffn_dim 512, num_input_channels 7, scaling mean, mask_ratio 0.2
  • 源实现:transformers PatchTSTModel / PatchTSTForPretraining(论文 https://arxiv.org/abs/2211.14730)
  • Revision:主分支(architectures ["PatchTSTForMaskPretraining"], model_type patchtst)
  • 数据集:ETTh1(HUFL, HULL, MUFL, MULL, LUFL, LULL, OT)7 通道小时级电力变压器数据

任务与路由

项值
模型ibm-research/patchtst-etth1-pretrain(~1.2M 参数)
架构PatchTST Encoder (Patchify + Positional Sincos + 6×EncoderLayer (Multi-Head Attention + BatchNorm + FFN))
任务时间序列掩码预训练表征 / 点预测基础模型(time-series-models.md 路线),验收 last_hidden_state 数值一致性
输入past_values shape [batch, time, channels](本仓 batch 2, time 512, channels 7, float32),past_observed_mask 自动 ones
输出last_hidden_state shape [batch, channels, num_patches, d_model]=[2,7,42,128],可接 PatchTSTMaskPretrainHead 重构 12 长度 patch
数据契约轴顺序明确为 [batch, time, channel] 输入 -> [batch, channel, patch, d_model] 输出;context 512, patch_length 12, stride 12, num_patches 42, 通道顺序固定 7,不可打乱;缺失值以 observed_mask=0 掩码;缩放经内置 Scaler(mean) 可逆归一
路由time-series 点预测/预训练(参考 time-series-models.md),验收 hidden 数值一致性与性能,吞吐量 windows/s

表格数据类型关联:本模型为多变量时序基础模型,适用于表格数据(CSV/Parquet)中多列数值时序的表征与预训练,任务映射为 forecasting,验收指标为 hidden 数值一致性而非表格分类 logits。表格行可视为时间步,列对应 7 个电力负荷通道。

环境

  • 操作系统:Linux aarch64,昇腾 910B(Ascend910_9362),CANN 8.5.1,npu-smi 25.5.5
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,numpy 1.26.4,huggingface_hub 0.36.2
  • 依赖见 requirements.txt:torch、torch_npu、transformers、numpy、safetensors、huggingface_hub

安装

pip install -r requirements.txt
# 昇腾 NPU 需确保 torch 与 torch_npu 版本匹配,并已加载 Ascend 驱动/固件
# 权重已缓存在 working/models/patchtst-etth1 或自动从 HuggingFace 拉取(约 4.7 MB)
# 旧 checkpoint 需 patch config: patch_stride=stride, num_hidden_layers=encoder_layers 等,inference.py 已内置兼容

NPU 推理

inference.py 为自包含脚本,默认使用合成可复现时序(seed 42)在 npu:0 上完成 CPU 基线、NPU 推理、一致性与性能基准。核心 forward 的所有参数与参与计算的 Tensor 均显式位于 npu:0(见 npu_device_call.png 与 model_result.png)。计时前后均调用 torch.npu.synchronize()。通过 copy.deepcopy 保持 CPU/NPU 权重一致;使用 batch=2, context=512, 7 channels,已禁用随机 masking 以保证确定性。

# 方式一:权重已缓存在本地(离线,推荐)
python inference.py

# 方式二:自动从 HuggingFace 下载(需联网,首次约 4.7 MB)
python inference.py

# 方式三:指定本地权重目录(可选)
MODEL_DIR=/path/to/patchtst-etth1-pretrain python inference.py

设备选择固定为 npu:0(可用时),输入输出均校验 device 均为 npu:0,无 CPU fallback。旧 checkpoint 的命名差异(model.encoder.w_p -> encoder.embedder.input_embedding, w_pos -> positional_encoder.position_enc, encoder.encoder.layers -> encoder.layers, norm_sublayer1.1 -> norm_sublayer1.batchnorm)已在脚本中 remap。

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

以合成时序 batch 2, time 512, channels 7 为例(seed 42,torch.randn):

  • 输入:past_values shape (2,512,7),均值 0.0083,标准差 1.0022,范围 [-3.8325, 3.4456],per-channel means [0.031, -0.039, 0.041, -0.003, -0.006, 0.029, 0.005]
  • CPU 基线:last_hidden_state shape (2,7,42,128) 均值 -0.003410 std 1.543690 min -9.841632 max 9.339795;样例 batch0 ch0 patch0 5 dims [0.322601,0.129104,1.836975,-0.295154,0.472823],batch0 ch0 patch1 [0.677183,-0.097816,0.823857,0.816857,-0.991188]
  • NPU 输出:相同输入在 npu:0 上 last_hidden_state shape (2,7,42,128) 均值 -0.0034 std 1.5437;样例 batch0 ch0 patch0 [0.322569,0.129033,1.836977,-0.295229,0.472915],batch0 ch0 patch1 [0.677110,-0.097810,0.823896,0.816714,-0.991107];device npu:0 已验证,all params on npu:0
  • 推理状态:[STATUS] SUCCESS - NPU inference completed without CPU fallback,首轮编译后稳定 ~3.7 ms

完整输出见 model_result.png。

CPU-NPU 一致性

使用相同权重(通过 deepcopy)、相同数据切片(seed 42)、相同预处理、相同随机种子与 float32 精度,分别在 CPU 与 npu:0 上运行并保存原始 hidden,用 scripts/compare_outputs.py 比较:

python scripts/compare_outputs.py --cpu /tmp/patchtst_etth1_cpu.npy --npu /tmp/patchtst_etth1_npu.npy --task forecasting --atol 2e-3 --rtol 1e-3
  • hidden(last_hidden_state):shape (2,7,42,128) 一致,max_abs 1.49e-03,mean_abs 6.44e-05,atol=2e-3 rtol=1e-3 PASS,有限值 True
  • 阈值说明:FP32 跨设备正常波动(6 层 Transformer + BatchNorm + fused Attention 在 NPU 上的累加顺序与量化差异);若收紧至 1e-4 则 max_abs 1.49e-03 失败,故以 2e-3 为合理边界(参考 validation.md 对 FP32 从 1e-4 起步、根据结构放宽)
  • 比较脚本:compare_outputs.py 返回 0 即 PASS,已保存 /tmp/patchtst_etth1_cpu.npy 与 /tmp/patchtst_etth1_npu.npy

性能(Ascend 910B,npu:0,batch 2, time 512, 7 channels, 42 patches)

  • 计时:torch.npu.synchronize() 包裹,预热 3 次、正式 10 次
  • 耗时:avg 3.71 ms, min 3.68, max 3.74, p50 3.71, p90 3.73, p95 3.73(含一次 6 层 encoder forward,不含权重加载与数据生成);首轮 172 ms 含 NPU 图编译,稳定后 ~3.7 ms
  • 吞吐:~540 windows/s(batch 2 / 0.00371s),~270 series/s per series
  • 显存:权重 4.6 MB + 激活 <30 MB,总计 <40 MB 单卡可容(HBM 64GB)
  • 说明:计时含模型 forward 全流程,不含权重加载与合成数据生成;禁用随机 masking 后为确定性推理

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成(非手工绘制),固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$,深灰背景白色等宽字体,展示真实设备与结果:

agent workflow npu device call model result

限制

  • 仅验证合成可复现时序的 smoke consistency(2×512×7 -> 2×7×42×128 hidden),非 ETTh1 全量 17420 行 benchmark;真实 ETTh1 需按时间划分、滑动窗口、归一/逆归一后计算 MSE,官方预训练 MSE 约 0.3881 (horizon 96)。
  • 单卡推理,未验证长 horizon (>512) 或多卡分布式;模型为 FP32,未量化,量化与图编译优化待后续。
  • 输入通道顺序固定 7 通道,改变 channels 需重训或配置 num_input_channels;缺失值依赖 observed_mask,未验证大规模缺失场景;掩码预训练的随机采样在 NPU 上需固定种子或禁用以保证可复现比较。