#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=patchtstpytorch_model.bin 约 4.6 MB512,预测长度 24(预训练时掩码重建),d_model=128,encoder_layers=6,encoder_attention_heads=16,encoder_ffn_dim=512prediction_output 重建张量https://huggingface.co/ibm-research/patchtst-etth1-pretrain,revision 57adb20c250de139798567dd8a0b040390475717NPU 适配说明:原始 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])past_values[0] 单窗口 [512,7],7 通道顺序与 ETTh1 一致(HUFL/HULL/MUFL/MULL/LUFL/LULL/OT)-0.2537, std 0.8942, min -4.4812, max 2.6223[batch, time, channel],time=512, channel=7PatchTSTMeanScaler(scaling="mean")自动做 instance mean 归一化与逆归一化;输入无需额外缩放[B,7,42] 中后 2 列置 True),mask_value=0,其余 40 个 patch 保持原始值。原始随机掩码(mask_ratio 0.2 / mask_patches [2,3])在 CPU 与 NPU 上因 torch.rand 实现差异会产生不同 mask,导致输出不可比;固定掩码后重建输出可严格对比。Ascend910_9362),npu-smi 25.5.5,双卡 64GB HBM,实测 npu:0torch 2.9.0+cpu,torch_npu 2.9.0.post1+gitee7ba04,transformers 4.57.6pip 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')"python inference.py --model-dir ./patchtst-etth1-pretrain --batch-size 1inference.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.8942prediction_output [1,7,42,12],loss (masked MSE) 1.4241480.0216, std 0.8754, min -3.1100, max 4.0029[-0.0503, -0.2212, -0.3912, -0.1010, 0.3597, 0.7467, 1.1403, 0.8067, 0.0225, -0.5370, -1.0086, -0.9481]-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。
同一权重、同一输入(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.001prediction_output [2,7,42,12],mean 0.007714,loss 1.488178prediction_output [2,7,42,12],mean 0.007732,loss 1.488031max_abs_error 0.001321,mean_abs_error 0.000102atol=2e-3, rtol=1e-3;原始随机掩码会因 CPU/NPU torch.rand 差异导致 max_abs ~7.0 不可比,本仓通过固定掩码消除伪不通过)计时前后均执行 torch.npu.synchronize(),仅计模型前向,不含权重加载与数据下载:
187.76 ms(inference.py)/ 4.48 ms(bench 纯前向,已 warmup)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)~7.6 MB active + 8 MB reserved(torch_npu.memory_stats),模型权重 4.6 MB 驻留assets/agent_workflow.png:侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark、校验全流程assets/npu_device_call.png:npu-smi、NPU 可用性、设备名、参数 device/dtype、输入输出 Tensor deviceassets/model_result.png:默认 python inference.py 的真实完整输出三张 PNG 均由 xterm.js 根据本次真实日志生成(非手工绘制、非桌面截图)。
npu:0 推理;未测试多卡或 BF16/FP16PatchTSTForPretraining 输出为重建值,非直接未来预测;下游预测需微调为 PatchTSTForPrediction