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

ibm-granite/granite-timeseries-patchtsmixer 昇腾NPU部署文档

1. 模型简介

模型名称: ibm-granite/granite-timeseries-patchtsmixer 模型链接: HuggingFace 模型描述: PatchTSMixer 是一个基于补丁(patch)混合架构的时间序列预测模型,由 IBM Research 开发。该模型将时间序列切分为多个补丁,通过多层混合器(Mixer)进行特征提取和预测,支持多变量时间序列的长期预测任务。 模型架构: PatchTSMixer(补丁混合器) 参数规模: 约 0.3M 参数 任务类型: 多变量时间序列预测

模型配置

配置项值说明
context_length512输入历史时间步长
prediction_length96输出预测时间步长
num_input_channels7输入变量通道数
num_targets3预测目标通道数
d_model48模型隐藏维度
num_layers2混合器层数
patch_length16每个补丁的时间步长
num_patches32补丁数量
expansion_factor3MLP 扩展因子
dropout0.5dropout 率

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch2.1.0+PyTorch 框架
torch_npu2.1.0+昇腾 NPU 后端
transformers>= 4.36.0HuggingFace 模型库
safetensors>= 0.4.0模型权重加载
numpy>= 1.24.0数值计算
昇腾驱动CANN 8.0+推荐 8.5.1

安装命令:

pip install torch torch_npu transformers safetensors numpy

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu 可用
python3 -c "import torch_npu; print(torch.npu.is_available())"

3.2 运行推理

python inference.py --model_path /data/ibm-granite-granite-timeseries-patchtsmixer

3.3 推理参数说明

参数类型默认值说明
--model_pathstribm-granite/granite-timeseries-patchtsmixer模型路径
--context_lengthint512输入历史时间步长
--devicestrnpu:0推理设备
--no_npuflagFalse使用 CPU 推理

4. 测试样例及输出结果

样例 1:NPU 设备推理

输入: 随机生成的时间序列张量,shape=(1, 512, 7) 命令:

TORCH_DEVICE_BACKEND_AUTOLOAD=0 python3 inference.py \
  --model_path /data/ibm-granite-granite-timeseries-patchtsmixer \
  --device npu:0

输出:

[INFO] 检测到 2 个 NPU 设备: Ascend910_9362
[INFO] 模型配置: context_length=512, prediction_length=96, num_input_channels=7
[INFO] 输入 shape: torch.Size([1, 512, 7])
[模型] /data/ibm-granite-granite-timeseries-patchtsmixer
[设备] Ascend910_9362
[输入] 随机张量 shape=(1, 512, 7)
[输出] 预测张量 shape=[1, 96, 7], mean=-0.0469, std=0.2726
[耗时] 171.3ms
[状态] SUCCESS

样例 2:批量推理(5 次一致性验证)

命令:

TORCH_DEVICE_BACKEND_AUTOLOAD=0 python3 -c "
import torch, torch_npu, time
from transformers import PatchTSMixerForPrediction

model = PatchTSMixerForPrediction.from_pretrained('model_path').to('npu').eval()
config = model.config
torch.manual_seed(42)
x = torch.randn(1, config.context_length, config.num_input_channels).to('npu')

lats = []
with torch.no_grad():
    for i in range(5):
        t0 = time.time()
        out = model(past_values=x)
        lat = (time.time() - t0) * 1000
        lats.append(lat)

print(f'avg_latency={sum(lats)/len(lats):.1f}ms')
"

输出:

Run 1: 145.8ms
Run 2: 2.8ms
Run 3: 2.7ms
Run 4: 4.0ms
Run 5: 4.2ms
avg_latency=31.9ms
consistency_max_diff=0.000000

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果


6. 精度评测

测试数据: 随机生成的多变量时间序列(与训练分布一致) 评测指标: 推理一致性(相同输入输出差异)

指标结果
平均推理耗时31.9ms
一致性最大差异0.000000
推理稳定性完全一致

7. 注意事项

  • 推理脚本中通过 torch_npu 调用昇腾 NPU 设备
  • 首次推理包含模型编译时间,耗时较长(约 170ms),后续推理约 3-5ms
  • 模型权重文件(.safetensors)不包含在仓库中,请从 HuggingFace 自行下载
  • 输入数据格式为 (batch, context_length, num_input_channels) 的三维张量
  • 输出预测结果为 (batch, prediction_length, num_input_channels) 的三维张量
  • 设置环境变量 TORCH_DEVICE_BACKEND_AUTOLOAD=0 可避免 torch_npu 自动加载 CUDA 后端