模型名称: ibm-granite/granite-timeseries-patchtsmixer 模型链接: HuggingFace 模型描述: PatchTSMixer 是一个基于补丁(patch)混合架构的时间序列预测模型,由 IBM Research 开发。该模型将时间序列切分为多个补丁,通过多层混合器(Mixer)进行特征提取和预测,支持多变量时间序列的长期预测任务。 模型架构: PatchTSMixer(补丁混合器) 参数规模: 约 0.3M 参数 任务类型: 多变量时间序列预测
| 配置项 | 值 | 说明 |
|---|---|---|
| context_length | 512 | 输入历史时间步长 |
| prediction_length | 96 | 输出预测时间步长 |
| num_input_channels | 7 | 输入变量通道数 |
| num_targets | 3 | 预测目标通道数 |
| d_model | 48 | 模型隐藏维度 |
| num_layers | 2 | 混合器层数 |
| patch_length | 16 | 每个补丁的时间步长 |
| num_patches | 32 | 补丁数量 |
| expansion_factor | 3 | MLP 扩展因子 |
| dropout | 0.5 | dropout 率 |
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | 2.1.0+ | PyTorch 框架 |
| torch_npu | 2.1.0+ | 昇腾 NPU 后端 |
| transformers | >= 4.36.0 | HuggingFace 模型库 |
| safetensors | >= 0.4.0 | 模型权重加载 |
| numpy | >= 1.24.0 | 数值计算 |
| 昇腾驱动 | CANN 8.0+ | 推荐 8.5.1 |
安装命令:
pip install torch torch_npu transformers safetensors numpy# 检查 NPU 设备
npu-smi info
# 验证 torch_npu 可用
python3 -c "import torch_npu; print(torch.npu.is_available())"python inference.py --model_path /data/ibm-granite-granite-timeseries-patchtsmixer| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model_path | str | ibm-granite/granite-timeseries-patchtsmixer | 模型路径 |
| --context_length | int | 512 | 输入历史时间步长 |
| --device | str | npu:0 | 推理设备 |
| --no_npu | flag | False | 使用 CPU 推理 |
输入: 随机生成的时间序列张量,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命令:
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


测试数据: 随机生成的多变量时间序列(与训练分布一致) 评测指标: 推理一致性(相同输入输出差异)
| 指标 | 结果 |
|---|---|
| 平均推理耗时 | 31.9ms |
| 一致性最大差异 | 0.000000 |
| 推理稳定性 | 完全一致 |
.safetensors)不包含在仓库中,请从 HuggingFace 自行下载(batch, context_length, num_input_channels) 的三维张量(batch, prediction_length, num_input_channels) 的三维张量TORCH_DEVICE_BACKEND_AUTOLOAD=0 可避免 torch_npu 自动加载 CUDA 后端