g
gcw_coj3XaOd/ibm-granite-granite-timeseries-patchtst
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PatchTST 时间序列预测模型 昇腾NPU部署文档

1. 模型简介

模型名称: ibm-granite/granite-timeseries-patchtst 模型链接: HuggingFace 模型描述: PatchTST (Patch Time Series Transformer) 是一种用于长时间序列预测的Transformer架构。该模型将时间序列切分为patch子序列,通过Transformer编码器提取时序特征,并输出未来时间步的预测值。本模型针对多变量时间序列预测任务训练,使用7个输入通道,预测未来96个时间步。 模型架构: PatchTSTForPrediction (基于Patch的时序Transformer) 参数规模: 614K (614,496 参数) 输入长度: 512 时间步 (context_length) 预测长度: 96 时间步 (prediction_length) 输入通道数: 7


2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch / torch_npu2.1.0+昇腾 NPU 后端
transformers>= 4.45.0HuggingFace 库(需支持 PatchTST)
numpy>= 1.21.0数值计算
昇腾驱动CANN 8.0.RC2+推荐最新版

安装命令:

pip install torch torch_npu transformers numpy

NPU 环境检查:

npu-smi info
python3 -c "import torch; print(torch.npu.is_available(), torch.npu.device_count(), torch.npu.get_device_name(0))"

3. 推理步骤

3.1 模型下载

从 HuggingFace 或 ModelScope 下载模型权重:

# 方式1: HuggingFace (需科学上网)
git lfs install
git clone https://huggingface.co/ibm-granite/granite-timeseries-patchtst

# 方式2: ModelScope (国内镜像)
python3 -c "from modelscope import snapshot_download; snapshot_download('ibm-granite/granite-timeseries-patchtst', cache_dir='./model')"

3.2 运行推理

# 使用随机数据测试
python inference.py --input "random"

# 使用内置示例数据
python inference.py --input "sample_etth1"

# 性能基准测试(10轮)
python inference.py --input "random" --benchmark --benchmark_runs 10

# 保存预测结果
python inference.py --input "random" --output predictions.npy

3.3 推理参数说明

参数类型默认值说明
--inputstr"random"输入模式:'random'(随机数据) / 'sample_etth1'(内置示例) / 文件路径
--model_pathstr./model模型路径
--devicestrnpu:0推理设备
--context_lenint512历史序列长度
--pred_lenint96预测序列长度
--outputstr""输出文件路径(.npy格式)
--benchmarkflagFalse运行性能基准测试
--benchmark_runsint10基准测试轮数

3.4 输入数据格式

模型期望输入为多变量时间序列,形状为 [context_length, num_input_channels]:

  • context_length: 512(历史窗口大小)
  • num_input_channels: 7(输入变量数量)
  • 数据应进行标准化处理(模型内部使用std标准化)

3.5 输出说明

模型输出包含:

输出字段形状说明
prediction_outputs[pred_len, num_channels]主要预测结果
loc[pred_len, num_channels]分布位置参数(用于反归一化)
scale[pred_len, num_channels]分布尺度参数(用于反归一化)

4. 测试样例及输出结果

样例 1:随机数据单次推理

输入: 随机生成的多变量时序数据 (512x7),seed=42 命令:

python inference.py --input "random"

输出:

[INFO] 设备类型: npu
[INFO] 设备名称: Ascend910_9362
[INFO] 加载模型: ./granite-timeseries-patchtst
[INFO] 模型加载完成,参数量: 614,496
[模型] ibm-granite/granite-timeseries-patchtst
[设备] Ascend910_9362 (npu:0)
[输入] 随机生成时序数据 (512x7)
[输出] 预测序列 pred_len=96 channels=7 value_range=[-1.7416, 0.6052] mean=[-0.8366, -0.6609, -0.5749, -0.5988, -0.6207, -0.6386, -0.6563]
[耗时] 264.6ms
[状态] SUCCESS

[预测结果详情]
  预测步长: 96
  预测通道: 7
  前5步预测值 (第1通道): [-0.906962  -0.9324515 -0.9775879 -1.0106386 -1.0335951]
  分布参数 loc (第1通道): [-0.1390575  -0.18435241 -0.21115349 -0.21822277 -0.23534966]
  分布参数 scale (第1通道): [1.4158298  0.94090396 0.70675915 0.5658347  0.46882603]

样例 2:性能基准测试

输入: 随机生成的多变量时序数据 (512x7),seed=42 命令:

python inference.py --input "random" --benchmark --benchmark_runs 10

输出:

[INFO] 运行基准测试 (10 轮)...
[INFO] 基准测试结果:
  平均: 3.7ms
  标准差: 0.7ms
  最小: 3.3ms
  最大: 5.7ms
[模型] ibm-granite/granite-timeseries-patchtst
[设备] Ascend910_9362 (npu:0)
[输入] 随机生成时序数据 (512x7)
[输出] 预测完成 pred_len=96 channels=7 avg_latency=3.7ms pred_range=[-1.6454, 0.2436]
[耗时] 3.7ms
[状态] SUCCESS

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果


6. 精度评测

测试方法: 使用随机生成的模拟时序数据(基于ETT数据集的统计特性)进行5项验证测试 评测指标: 输出形状正确性、数值有效性、分布参数合理性、输出确定性、输入多样性

指标结果说明
输出形状PASS输出形状 [1, 96, 7] 与模型配置一致
数值有效性PASS无NaN、无Inf,值在合理范围内
分布参数PASSscale参数全为正数
输出确定性PASS相同输入产生相同输出(最大差异 < 1e-6)
输入多样性PASS不同输入产生不同预测结果

7. 注意事项

  • 推理脚本通过 torch_npu 调用昇腾 NPU 设备
  • 模型使用 distribution_output="student_t" 输出学生t分布参数,可用于概率预测
  • 输入数据应进行标准化处理(模型内部使用标准差标准化)
  • 模型权重从 HuggingFace 或 ModelScope 下载,不包含在仓库中
  • 首次推理包含模型加载时间(约260ms),后续推理仅需约3-4ms(batch=1)
  • 本模型仅支持固定输入长度 (512) 和预测长度 (96),与训练配置一致