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

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

#NPU #Ascend #Ascend910

模型简介

  • 模型名称: ibm-research/patchtst-etth1-pretrain
  • 权重 URL: https://huggingface.co/ibm-research/patchtst-etth1-pretrain
  • 本地路径: ./model_cache/ibm-research-patchtst-etth1-pretrain (revision main, snapshot_download 固定 via HF_ENDPOINT=https://hf-mirror.com)
  • 模型类型: patchtst
  • 架构: PatchTSTForPrediction (checkpoint 为 PatchTSTForMaskPretraining, 共用 encoder; 适配验证使用 Prediction head)
  • 任务: 时间序列点预测 (time-series forecasting, point forecast, 512 -> 24, 7 channels)
  • 预训练数据: ETTh1 (Electricity Transformer Temperature hourly, 7 channels: HUFL, HULL, MUFL, MULL, LUFL, LULL, OT), 上下文 512 预测 24
  • 参数量: 663,704 (0.664M), float32, pytorch_model.bin 4.7 MB
  • 配置: context_length=512, prediction_length=24, num_input_channels=7, num_output_channels=1 (shared, 实际输出 7 via channel-independence), d_model=128, encoder_layers=6 (实际 3 layers + shared), encoder_attention_heads=16, patch_length=12, stride=12, num_patches=42, scaling=mean (PatchTSTMeanScaler), activation=gelu, norm=BatchNorm, positional_encoding=sincos, pooling=null

数据契约

  • 输入格式: past_values [batch, context_length, num_input_channels] 即 [B, T, C] = [2, 512, 7], dtype float32, 轴顺序 B-T-C (batch-time-channel), 严禁转置
  • 输出格式: prediction_outputs [B, prediction_length, num_input_channels] 即 [2, 24, 7] float32, 同时返回 loc [2,1,7] 与 scale [2,1,7] (PatchTSTMeanScaler 内部均值)
  • 预处理: PatchTSTMeanScaler (per-series mean, 内部缩放与逆缩放), 无频率 token, 无显式 observed_mask (模型内自动 ones_like), 无缺失值处理, 归一化必需
  • 示例数据: 合成数据 torch.randn seed 42, hash 3a1b5e462a04d7c4, 示例 [0,:3,0] = [1.9269, -1.6047, -0.7688], stats mean 0.0083 std 1.0022 min -3.8325 max 3.4456
  • 真实业务: ETTh1 小时级预测, 512 小时历史预测未来 24 小时, 7 通道 channel-independent (共享 embedding/Transformer 权重), 多变量通过独立通道推理, 96 步预测可通过滚动或切换 horizon 实现
  • 频率与轴: 无 frequency token, 小时频率, 轴顺序 B-T-C, mask/缩放/逆缩放均在模型内, 不得擅自改变列顺序 (HUFL, HULL, MUFL, MULL, LUFL, LULL, OT)

环境

  • NPU: Ascend910_9362, 2 cards, 使用 npu:0, CANN 8.5.1, npu-smi 25.5.5, HBM 64GB
  • 软件: python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, numpy 1.26.4, huggingface_hub
  • 显存: 模型 4.7MB 权重, NPU allocated 2.6 MB reserved 152 MB peak 128.1 MB, batch 2 远低于 64GB
  • HF镜像: HF_ENDPOINT=https://hf-mirror.com, snapshot_download 直连, 无 trust_remote_code (checkpoint 无自定义 Python)
  • 复现: torch.manual_seed(0) 固定权重初始化 (head 随机但 CPU/NPU 共享 via deepcopy), torch.manual_seed(42) 固定合成输入, eval mode, float32, torch_npu

安装

pip install -r requirements.txt
# requirements: torch==2.9.0 torch-npu==2.9.0.post1 transformers==4.57.6 numpy==1.26.4 huggingface_hub safetensors
# 若本地已有 CANN 与 torch_npu, 仅需 transformers 与 safetensors

NPU 推理

python inference.py
# 默认加载 ./model_cache/ibm-research-patchtst-etth1-pretrain (或 /tmp/patchtst_test, 或自动 snapshot_download) 到 npu:0
# 打印模型/revision/route/backend/device/dtype, 输入摘要, CPU vs NPU 预测, 同步耗时, PASS/FAIL
# 需先满足 HF_ENDPOINT=https://hf-mirror.com 已配置, 无需手动设置 http_proxy
  • 设备证明: first_param npu:0, input npu:0, prediction_outputs npu:0, loc npu:0, scale npu:0, 显式 .to("npu:0") + torch.npu.synchronize() 计时, 无 CPU fallback, 打印 npu-smi 与 torch_npu 版本
  • 加载后端: transformers PatchTSTForPrediction, from_pretrained 本地路径优先, 无网络隐式下载, 同权重 deepcopy 保证 CPU/NPU 一致性

真实结果

  • CPU: prediction_outputs [2,24,7] float32 cpu, sample [0,:3,0] [0.003800758393481374, 0.0010032316204160452, -0.008113279938697815], mean 0.000504 std 0.007063 min -0.011117 max 0.015988
  • NPU: 同形状 [2,24,7] float32 npu:0, sample [0,:3,0] [0.0038007537368685007, 0.0010032354621216655, -0.008113279938697815], mean 0.000504 std 0.007063 finite True, 无 NaN/Inf
  • 首轮编译: 171.98 ms (含图编译, past_observed_mask ones_like 动态形状首次编译)
  • 稳态: second_run 3.11 ms, benchmark avg 3.09 ms
  • 状态: [RESULT] PASS task=forecasting backend=PatchTST

一致性

  • 方法: 同权重 (deepcopy 共享随机 head)、同输入 (seed 42 合成 [2,512,7])、同预处理 (PatchTSTMeanScaler)、同 dtype float32、同 eval mode, 分别保存 /tmp/cpu_patchtst.npy /tmp/npu_patchtst.npy, 使用 scripts/compare_outputs.py --task forecasting --atol 1e-4 --rtol 1e-3
  • 结果: max_abs 1.54e-08 mean_abs 3.12e-09, allclose True, shape [2,24,7]==[2,24,7] finite True
  • 阈值: FP32 NPU 算子差异, 从 atol 1e-4 rtol 1e-3 起步, 实测 max 1.5e-08 < 1e-4, 已在 README 说明并通过校验, 预测回归比较 forecast
  • 单样本 smoke: 仅 batch 2 窗口一致性, 不称为完整 ETTh1 test MSE 排名 (官方 MSE 0.3881 for horizon 96 仅供参考, 本验证 horizon 24)
  • 无 fallback: 日志含 [NPU] device 证明, 含 ArgSort AiCpu 警告但非 fallback 失败, 未发生静默 CPU fallback (显式 npu:0 + synchronize)

性能

  • 计时: 每轮前后 torch.npu.synchronize(), warmup 3, 测试 10, 报告 compile/first_run 与稳态
  • 结果: compile/first_run 171.98 ms | avg 3.09 ms min 3.06 max 3.13 p50 3.08 p90 3.12 p95 3.12
  • 吞吐: 647.61 windows/s (batch 2, windows/s = batch*1000/avg), series/s 同值, 本仓报告 windows/s
  • 显存: allocated 2.6 MB reserved 152.0 MB peak 128.1 MB, batch 2 context 512 horizon 24 channels 7 dtype float32
  • 对比: 显式 NPU 推理, 预处理 excluded (synthetic tensor only), inverse scaling (mean) included in model, 适合端侧/资源受限部署
  • 频率: 支持 hourly 512->24, 滚动至 96 步可多次推理, ETTh1 全 7 通道单次推理

证据图

图片由 xterm.js 根据本次真实日志生成, 深灰背景白色等宽字体, 提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$, 非原始桌面截图

  • agent workflow
  • npu device call
  • model result

限制

  • 单卡 npu:0 即可, 资源 <150MB, 超出单卡 MoE 等模型不适用
  • 合成随机输入验证数值一致性, 非 ETTh1 完整 test 划分, 不报告 MSE/SMAPE 排名, 概率预测分布 student_t 未验证
  • Checkpoint 为 PatchTSTForMaskPretraining (预训练重构头), Prediction head 为随机初始化 (shared embedding/encoder 来自预训练, 但 head 需下游 fine-tune), 本仓通过固定种子与 deepcopy 保证 CPU/NPU 同权重一致性, 不声称已 fine-tune 到生产精度
  • 无 tokenizer, 无 remote code, 无训练, 仅推理迁移到 NPU, 动态 shape 首次编译后复跑一次, BatchNorm 在 eval 模式
  • 模型许可证 Apache-2.0, 权重遵循原 ibm-research, 单变量/多变量均 channel-independent

仓库结构

inference.py
readme.md
requirements.txt
assets/agent_workflow.png
assets/npu_device_call.png
assets/model_result.png