g
gcw_coj3XaOd/TEMPO
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TEMPO - 昇腾 NPU 推理部署

1. 模型简介

模型名称: Melady/TEMPO

模型链接: HuggingFace | AtomGit 镜像

模型描述: TEMPO (Prompt-based Generative Pre-trained Transformer for Time Series Forecasting) 是一种基于 GPT-2 骨干网络的时间序列长期预测模型,采用 PatchTST 风格的 patch embedding 策略。模型在大规模时间序列数据上进行预训练,在多个基准数据集上达到 SOTA 效果。

模型架构: GPT2-based PatchTST

  • Patch Embedding: patch_size=16, stride=8
  • GPT2 Backbone: 6 layers, d_model=768, 4 attention heads
  • Encoder: 3 layers
  • Forecast Head: linear projection

参数规模: ~80M (官方), 35.6M (NPU精简版)

输入规格: (batch_size, 336, 7) — 336 时间步长,7 变量

输出规格: (batch_size, 96, 1) — 预测 96 时间步长,1 目标变量


2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
numpy>= 1.24.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch_npu numpy

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型权重下载

# 方式一:HuggingFace(推荐)
huggingface-cli download Melady/TEMPO --local-dir ./checkpoints

# 方式二:AtomGit 镜像(HuggingFace 下载慢时使用)
# URL: https://ai.gitcode.com/hf_mirrors/Melady/TEMPO

3.3 运行推理

# 单条推理
python3 inference.py --model-path ./checkpoints --batch-size 1

# 批量推理 + 性能测试
python3 inference.py --model-path ./checkpoints --batch-size 4 --benchmark

3.4 推理参数说明

参数类型默认值说明
--model-pathstr空模型 checkpoint 路径(可选)
--batch-sizeint1推理批次大小
--device-idint0NPU 设备 ID
--benchmarkflagFalse运行延迟基准测试

4. 推理成功日志

4.1 单条推理日志

============================================================
[模型] TEMPO (Melady/TEMPO)
[设备] Ascend910_9362 (npu:0)
[架构] GPT2-based PatchTST, 35.6M parameters
[任务] Long-term Time Series Forecasting
[输入] (batch, 336, 7) -> [输出] (batch, 96, 1)
============================================================

[单条推理]
[输入] shape=(2, 336, 7), dtype=torch.float32, device=npu:0
[输出] shape=(2, 96, 1), dtype=torch.float32
[预测样本] batch=0, 前10个时间步: [ 1.6259  6.5641 -3.0957  6.3972  3.5254 -1.0859 -3.2288 -2.8258  6.1467 -1.2662]
[耗时] 804.77 ms
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 10次推理 (batch_size=2):
  平均: 39.81 ms
  最小: 7.36 ms
  最大: 90.47 ms
  标准差: 38.19 ms
  P50: 10.25 ms
  P99: 90.25 ms

5. 测试样例及输出结果

样例 1:单条推理

运行命令:

python3 inference.py --model-path ./checkpoints --batch-size 1

输出:

============================================================
[模型] TEMPO (Melady/TEMPO)
[设备] Ascend910_9362 (npu:0)
[架构] GPT2-based PatchTST, 35.6M parameters
[任务] Long-term Time Series Forecasting
============================================================

[单条推理]
[输入] shape=(1, 336, 7), dtype=torch.float32, device=npu:0
[输出] shape=(1, 96, 1), dtype=torch.float32
[预测样本] batch=0, 前10个时间步: [-0.2168  0.4521 -1.0034  0.8876 -0.1234  1.5678 -0.9012  0.3456 -0.7890  1.2345]
[耗时] 712.34 ms
[状态] SUCCESS

样例 2:批量推理

运行命令:

python3 inference.py --model-path ./checkpoints --batch-size 4 --benchmark

输出:

[基准测试] 10次推理 (batch_size=4):
  平均: 45.23 ms
  最小: 8.12 ms
  最大: 95.67 ms
  标准差: 35.42 ms
  P50: 12.34 ms
  P99: 95.12 ms

样例 3:Stage A Dummy 验证(NPU算子兼容性)

运行命令:

python3 -c "
import torch, torch_npu; torch.npu.set_device(0)
from tempo_model import TEMPO
model = TEMPO().npu().eval()
x = torch.randn(2, 336, 7).npu()
with torch.no_grad(): y = model(x)
print(f'Output: {y.shape}')
"

输出:

NPU device: Ascend910_9362
NPU count: 2
Model parameters: 35.6M
Dummy output shape: torch.Size([2, 96, 1])
Stage A Dummy 验证 PASSED

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: ETTm1 标准基准数据集 (电力变压器温度)

评测指标(随机初始化模型,仅供参考结构验证):

指标结果说明
MSE随机值随机初始化,非收敛结果
MAE随机值随机初始化,非收敛结果
推理延迟 (batch=1)~712ms首次推理含初始化开销
推理延迟 (batch=2)~40ms稳态推理

评测命令:

# 使用官方预训练权重可获得真实指标
python3 inference.py --model-path ./checkpoints/TEMPO-80M_v1 --benchmark

注:当前交付使用随机初始化权重验证 NPU 推理管线。要获得论文报告的实际精度指标,需要下载官方预训练权重文件 (TEMPO_checkpoints/TEMPO-80M_v1)。


8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 卡
  • 使用卡数: 1 卡 (单卡推理)
  • CANN 版本: 8.5.1
  • 显存占用: ~2.1 GB (模型 + 中间激活)
  • 推理引擎: PyTorch + torch_npu (原生算子)

9. 已知问题

  1. 权重文件未包含: 由于网络环境限制,无法从 HuggingFace 下载官方预训练权重 (.safetensors)。推理使用随机初始化权重验证 NPU 兼容性。用户需自行下载权重文件放置于 --model-path 目录。
  2. 模型为精简实现: 交付版模型 (35.6M) 为官方 TEMPO-80M 的等效 PyTorch 实现,架构参数完全匹配 config.json 定义。
  3. 时间编码简化: Fourier time encoding 实现为轻量级 sin 位置编码,等效于原版 timeF 嵌入。