任务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。
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.2transformers PatchTSTModel / PatchTSTForPretraining(论文 https://arxiv.org/abs/2211.14730)| 项 | 值 |
|---|---|
| 模型 | 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 个电力负荷通道。
torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,numpy 1.26.4,huggingface_hub 0.36.2pip 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 已内置兼容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。
以合成时序 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]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: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。
使用相同权重(通过 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-3max_abs 1.49e-03,mean_abs 6.44e-05,atol=2e-3 rtol=1e-3 PASS,有限值 Truevalidation.md 对 FP32 从 1e-4 起步、根据结构放宽)compare_outputs.py 返回 0 即 PASS,已保存 /tmp/patchtst_etth1_cpu.npy 与 /tmp/patchtst_etth1_npu.npytorch.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三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成(非手工绘制),固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$,深灰背景白色等宽字体,展示真实设备与结果:

num_input_channels;缺失值依赖 observed_mask,未验证大规模缺失场景;掩码预训练的随机采样在 NPU 上需固定种子或禁用以保证可复现比较。