模型名称: 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
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch / torch_npu | 2.1.0+ | 昇腾 NPU 后端 |
| transformers | >= 4.45.0 | HuggingFace 库(需支持 PatchTST) |
| numpy | >= 1.21.0 | 数值计算 |
| 昇腾驱动 | CANN 8.0.RC2+ | 推荐最新版 |
安装命令:
pip install torch torch_npu transformers numpyNPU 环境检查:
npu-smi info
python3 -c "import torch; print(torch.npu.is_available(), torch.npu.device_count(), torch.npu.get_device_name(0))"从 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')"# 使用随机数据测试
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| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
--input | str | "random" | 输入模式:'random'(随机数据) / 'sample_etth1'(内置示例) / 文件路径 |
--model_path | str | ./model | 模型路径 |
--device | str | npu:0 | 推理设备 |
--context_len | int | 512 | 历史序列长度 |
--pred_len | int | 96 | 预测序列长度 |
--output | str | "" | 输出文件路径(.npy格式) |
--benchmark | flag | False | 运行性能基准测试 |
--benchmark_runs | int | 10 | 基准测试轮数 |
模型期望输入为多变量时间序列,形状为 [context_length, num_input_channels]:
模型输出包含:
| 输出字段 | 形状 | 说明 |
|---|---|---|
prediction_outputs | [pred_len, num_channels] | 主要预测结果 |
loc | [pred_len, num_channels] | 分布位置参数(用于反归一化) |
scale | [pred_len, num_channels] | 分布尺度参数(用于反归一化) |
输入: 随机生成的多变量时序数据 (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]输入: 随机生成的多变量时序数据 (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


测试方法: 使用随机生成的模拟时序数据(基于ETT数据集的统计特性)进行5项验证测试 评测指标: 输出形状正确性、数值有效性、分布参数合理性、输出确定性、输入多样性
| 指标 | 结果 | 说明 |
|---|---|---|
| 输出形状 | PASS | 输出形状 [1, 96, 7] 与模型配置一致 |
| 数值有效性 | PASS | 无NaN、无Inf,值在合理范围内 |
| 分布参数 | PASS | scale参数全为正数 |
| 输出确定性 | PASS | 相同输入产生相同输出(最大差异 < 1e-6) |
| 输入多样性 | PASS | 不同输入产生不同预测结果 |
torch_npu 调用昇腾 NPU 设备distribution_output="student_t" 输出学生t分布参数,可用于概率预测