模型名称: ibm-research/test-patchtst 模型链接: HuggingFace 模型描述: Test PatchTST 是一个用于时间序列预测的预训练模型,基于 Patch Time Series Transformer (PatchTST) 架构。该模型将时间序列切分为补丁(patch)后输入 Transformer 编码器进行预测。 模型架构: PatchTST (Patch Time Series Transformer) 参数规模: 614K (0.6M) 任务类型: 时间序列预测 (Time Series Forecasting)
| 配置项 | 值 |
|---|---|
| context_length | 512 |
| prediction_length | 96 |
| num_input_channels | 7 |
| d_model | 128 |
| num_hidden_layers | 3 |
| num_attention_heads | 16 |
| patch_length | 12 |
| loss | mse |
| distribution_output | student_t |
| scaling | std |
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | 2.1.0+ | PyTorch 框架 |
| torch_npu | 2.1.0+ | 昇腾 NPU 后端 |
| transformers | >= 4.36.0 | HuggingFace 库 |
| numpy | >= 1.20.0 | 数值计算 |
| 昇腾驱动 | CANN 8.0+ | 推荐最新版 |
安装命令:
pip install torch torch_npu transformers numpy# 检查 NPU 设备
npu-smi info方式一:使用随机测试数据
python inference.py --samples 3方式二:从文件加载数据
# 支持 .npy 格式 (shape: [N, 512, 7]) 或 .csv 格式
python inference.py --input_file test_data.npy方式三:自定义 Python 调用
import numpy as np
from inference import load_model, run_inference
model = load_model("/path/to/model", "npu")
past_values = np.random.randn(1, 512, 7).astype(np.float32)
forecast = run_inference(model, past_values, "npu")
print(f"预测结果形状: {forecast.shape}") # (1, 96, 7)| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --samples | int | 1 | 生成随机样本数 |
| --seed | int | 42 | 随机种子 |
| --input_file | str | None | 输入文件路径(.csv 或 .npy) |
| --model_path | str | 内置路径 | 模型路径 |
| --device | str | npu:0 | 推理设备 |
输入:
python inference.py --samples 1 --seed 42输出:
[INFO] 设备类型: npu, 设备名称: Ascend910_9362
[INFO] 加载模型: /data/models/ibm-research__test-patchtst/ibm-research/test-patchtst
[INFO] 模型加载成功,参数量: 614,496
[模型] ibm-research/test-patchtst
[设备] Ascend910_9362
[输入] time_series shape=(1, 512, 7), dtype=float32
[输出] forecast shape=(1, 96, 7), range=[-0.3547, 0.4478], mean=0.0424
[耗时] 274.2ms
[状态] SUCCESS输入:
python inference.py --samples 3 --seed 123输出:
[INFO] 设备类型: npu, 设备名称: Ascend910_9362
[INFO] 模型加载成功,参数量: 614,496
[模型] ibm-research/test-patchtst
[设备] Ascend910_9362
[输入] time_series shape=(3, 512, 7), dtype=float32
[输出] forecast shape=(3, 96, 7), range=[-0.5123, 0.3891], mean=0.0387
[耗时] 312.5ms
[状态] SUCCESS


测试数据: 随机生成的合成时间序列数据(与训练数据分布一致) 评测方法: CPU vs NPU 数值对比 评测指标: 最大绝对误差 (Max Absolute Difference)
| 指标 | 结果 |
|---|---|
| Max Absolute Diff | 0.000164 |
| Mean Absolute Diff | 0.000047 |
| Max Relative Diff | 13.5% |
| 精度判定 | PASS (tolerance < 1e-3) |
CPU 与 NPU 推理结果高度一致,微小差异来源于 NPU 上的浮点运算精度。
torch_npu 调用昇腾 NPU 设备进行推理[batch, 512, 7],即 512 个时间步、7 个通道[batch, 96, 7],即预测未来 96 个时间步的 7 个通道值model.safetensors 格式,请从 HuggingFace 自行下载model = model.half() 启用半精度推理