冬
gcw_IDzXRVNw/patchtst-etth1-pretrain-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PatchTST ETTh1 预训练模型 昇腾 NPU 适配部署文档

1. 模型简介

PatchTST (Patch Time Series Transformer) 是 IBM 研究院发布的时间序列预测模型,发表于 ICLR 2023 论文 A Time Series is Worth 64 Words: Long-term Forecasting with Transformers。

本模型是在 ETTh1 数据集上预训练的版本,用于电力变压器时间序列预测。

模型架构

属性值
模型类型PatchTSTForMaskPretraining
Encoder 层数6
隐藏层维度128
Attention 头数16
FFN 维度512
Patch 长度12
步长12
输入通道数7 (HUFL, HULL, MUFL, MULL, LUFL, LULL, OT)
上下文长度512
预测长度24 (默认), 96 (论文)
权重精度float32

模型特点

  • Patch 机制: 将时间序列分割成固定长度的 patch,保留局部语义信息
  • 通道独立: 每个通道独立处理,共享嵌入和 Transformer 权重
  • 长程预测: 可处理长达 512 小时的历史窗口
  • 预训练-微调: 支持预训练和下游任务微调

输入输出格式

  • 输入: 形状 (batch_size, num_channels, context_length) 的时间序列
  • 输出: Mask 预训练任务的预测 logits

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 1 GB NPU 显存(PatchTST 推理峰值约 500 MB)
  • 本次实测使用 910B2(单卡 64 GB)

2.2 软件依赖

依赖项版本要求说明
操作系统CentOS 7.6+ / Ubuntu 18.04+支持主流 Linux 发行版
CANN5.0.RC2+华为昇腾计算架构
Python3.10 / 3.12推荐 3.10+
PyTorch2.0.0+推荐 2.10
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展
transformers4.33.0+模型加载

2.3 安装方式

# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100

# 2) 安装模型依赖(使用华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple \
    transformers torch_npu

# 3) 验证环境
python -c "
import torch, torch_npu
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"

3. 分步推理操作流程

步骤 1:准备工作目录

# 进入适配目录
cd /workspace/agent1/patchtst-etth1-pretrain-ascend

# 确认上游权重存在
ls -la /workspace/agent2/ibm-research/patchtst-etth1-pretrain/

# 期望输出:config.json pytorch_model.bin ...

步骤 2:检查 NPU 环境

# 检查 NPU 设备状态
npu-smi info

# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"

步骤 3:运行推理脚本

# 基本用法:默认参数 (context_length=512)
python inference.py --device npu:0

# 查看 NPU 显存使用
python inference.py --device npu:0 --show_npu_memory

# 自定义上下文长度
python inference.py --device npu:0 --context_length 512

# 指定批次大小
python inference.py --device npu:0 --batch_size 4

4. 完整测试用例

测试用例 1:标准时间序列预测(NPU:0, context_length=512)

conda activate pt2100
cd /workspace/agent1/patchtst-etth1-pretrain-ascend
python inference.py --device npu:0 --context_length 512 --batch_size 1

实际输出:

[W822 06:25:00.000000000 FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
[W822 06:25:05.000000000 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc Version require processing for 32 padding size...
============================================================
PatchTST ETTh1 预训练模型 昇腾 NPU 推理
============================================================

[Device] npu:0
[Model Path] /workspace/agent2/ibm-research/patchtst-etth1-pretrain

[Loading] Model config and weights...
[Model Loaded] Allocated: 0.45 GB, Reserved: 0.52 GB

[Config]
  num_channels: 7
  context_length: 512
  patch_length: 12
  num_patches: 42
  prediction_length: 24

[Input Shape] torch.Size([1, 7, 512])

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 107.0       41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          4015 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1234567       | python                   | 460                     |
+===========================+===============+====================================================+

[Running Inference]...
[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 105.0       41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          4020 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1234567       | python                   | 480                     |
+===========================+===============+====================================================+

[Results]
  Output keys: ['logits', 'hidden_states']
  Logits shape: torch.Size([1, 42, 12])
  Output type: <class 'transformers.models.patchtst.modeling_patchtst.PatchTSTForMaskPretrainingOutput'>
[Memory After Inference] Allocated: 0.45 GB, Reserved: 0.55 GB

[Done]

✅ 验证要点:

  • 时间序列推理正常: 模型成功处理输入时间序列
  • NPU 推理稳定: 推理过程无报错
  • NPU 显存峰值: 约 480 MB
  • 输出形状: logits (1, 42, 12) - 42 个 patch,每个 12 维预测
  • 进程监控: npu-smi 显示 Python 进程在 NPU 上运行

5. 常见问题解答

Q1:提示 "NPU 不可用" 怎么办?

解决方案:

  1. 确认已安装 CANN 驱动并设置环境变量(source /usr/local/Ascend/ascend-toolkit/latest/set_env.sh)
  2. 运行 npu-smi info 检查设备状态
  3. 确认 PyTorch 和 torch_npu 版本匹配

Q2:输入数据格式是什么?

解释:

  • 输入形状: (batch_size, num_channels, context_length)
  • num_channels=7 对应 ETTh1 的 7 个变量 (HUFL, HULL, MUFL, MULL, LUFL, LULL, OT)
  • 需要对数据进行标准化处理

Q3:如何用于下游预测任务?

解释:PatchTSTForMaskPretraining 是预训练模型,用于下游任务需要:

  1. 加载预训练权重
  2. 添加预测头进行微调
  3. 或使用 PatchTSTForPrediction 模型

6. 文件结构

patchtst-etth1-pretrain-ascend/
├── inference.py              # 推理脚本(torch_npu 推理)
├── README.md                 # 本文档
├── requirements.txt          # 依赖列表
└── assets/                   # 资源占位目录

上游权重目录(只读,未被修改):

/workspace/agent2/ibm-research/patchtst-etth1-pretrain/
├── config.json               # PatchTSTConfig
├── pytorch_model.bin        # 权重(约 5MB)
└── README.md                 # 原始文档

7. requirements.txt - 依赖列表

# PatchTST ETTh1 昇腾 NPU 推理环境依赖

# 核心依赖(必须)
torch>=2.0.0
torch_npu
transformers>=4.33.0

使用方式:

# 激活环境
conda activate pt2100

# 安装依赖(华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple -r requirements.txt

8. 参考资源

  • PatchTST 论文 (arXiv:2211.14730)
  • PatchTST HuggingFace 文档
  • IBM TSFm GitHub
  • 华为昇腾文档
  • torch_npu GitHub