华为昇腾模型适配第2期,适配模型为 ibm-granite/granite-timeseries-ttm-r1
ibm-granite/granite-timeseries-ttm-r1 是 IBM Research 开源的 TinyTimeMixer (TTM) 时间序列预测模型。TTM 是一种紧凑型预训练模型,专门用于多元时间序列预测,参数量不到 100 万(约 80 万参数),是首个针对时间序列预测的"微型"预训练模型。
本次适配将 TTM-R1 模型部署到华为昇腾 NPU 上,验证其在国产 AI 芯片上的推理性能和精度表现,为时间序列预测场景在昇腾生态中的应用提供参考。
| 项目 | 配置 |
|---|---|
| NPU 型号 | Ascend 910B (Ascend910_9362) |
| NPU 数量 | 2 张 |
| 单卡 HBM 显存 | 65,536 MB (64GB) |
| NPU 总显存 | 131,072 MB (128GB) |
| NPU Cube Core 数量 | 20 |
| NPU Vector Core 数量 | 40 |
| L2 Cache 大小 | 168MB |
| 项目 | 版本 |
|---|---|
| 操作系统 | Ubuntu 22.04.5 LTS (Jammy Jellyfish) |
| 内核版本 | 5.10.0-182.0.0.95.r2220_156.hce2.aarch64 |
| 昇腾驱动版本 | npu-smi 25.5.5 |
| CANN 版本 | 8.5.1 (V100R001C25SPC002B220) |
| Python 版本 | 3.11.14 |
| PyTorch 版本 | 2.9.0+cpu |
| torch-npu 版本 | 2.9.0.post1+gitee7ba04 |
| granite-tsfm 版本 | 0.3.8 |
| transformers 版本 | 4.57.6 |
| vLLM 版本 | 0.18.0+empty |
| vllm-ascend 版本 | 0.18.0 |
| 项目 | 详情 |
|---|---|
| 权重来源 | ModelScope |
| 拉取时间 | 2026-08-20 15:48:07 |
| 权重文件 | model.safetensors (3.24MB) |
| 权重格式 | safetensors (float32) |
| 参数数量 | 134 个张量 |
# 设置环境变量
export ASCEND_RT_VISIBLE_DEVICES=0
export PYTHONPATH=/opt/atomgit/.local/lib/python3.11/site-packages:$PYTHONPATH
# 运行推理脚本
cd /opt/atomgit/adapt-ibm-granite-ttm-r1
python3 inference.pyASCEND_RT_VISIBLE_DEVICES=0:指定使用第 0 号 NPU 设备PYTHONPATH:确保 granit-tsfm 库能被正确导入============================================================
ibm-granite/granite-timeseries-ttm-r1 昇腾NPU推理
============================================================
[Step 1] 检查NPU环境...
NPU设备数量: 1
NPU设备名称: Ascend910_9362
[Step 2] 加载模型...
模型加载成功: tinytimemixer
模型参数量: 805,280
预测长度: 96
上下文长度: 512
[Step 3] 生成测试数据...
输入形状: torch.Size([1, 512, 1])
[Step 4] 执行推理...
预热推理...
正式推理...
[Step 5] 性能统计...
推理时间: 0.0045 秒
============================================================
推理完成
============================================================本模型为时间序列预测模型,不提供 HTTP 服务接口。如需服务化部署,可基于 inference.py 封装为 Flask/FastAPI 服务。
import os
os.environ["ASCEND_RT_VISIBLE_DEVICES"] = "0"
os.environ["PYTHONPATH"] = "/opt/atomgit/.local/lib/python3.11/site-packages:"
import torch
from tsfm_public import TinyTimeMixerForPrediction, TinyTimeMixerConfig
# 加载模型
model_path = "/opt/atomgit/models/ibm-granite/granite-timeseries-ttm-r1"
device = torch.device("npu:0")
config = TinyTimeMixerConfig.from_pretrained(model_path)
model = TinyTimeMixerForPrediction.from_pretrained(model_path, config=config)
model = model.to(device).eval()
# 生成测试数据(模拟时间序列)
torch.manual_seed(42)
past_values = torch.randn(1, 512, 1).to(device)
past_observed_mask = torch.ones_like(past_values).to(device)
# 执行推理
with torch.no_grad():
output = model(past_values=past_values, past_observed_mask=past_observed_mask)
print(f"预测形状: {output.prediction_outputs.shape}")
print(f"预测值: {output.prediction_outputs[0, :5, 0].tolist()}")预测形状: torch.Size([1, 96, 1])
预测值: [0.2421620637178421, -0.12510059773921967, -0.009287387132644653, 0.14208567142486572, -0.20387780666351318]| 指标 | 数值 |
|---|---|
| 模型加载时间 | <1秒 |
| 单次推理时间 | 0.0045秒 |
| 预热推理次数 | 3次 |
| 预测点数 | 96 |
| 指标 | 数值 |
|---|---|
| 单次推理吞吐量 | ~222次/秒 |
| 每预测点耗时 | ~0.047ms |
| 状态 | HBM 占用 |
|---|---|
| 空闲状态 | 3,107 MB / 65,536 MB (4.74%) |
| 推理中 | 3,184 MB / 65,536 MB (4.86%) |
| 显存增量 | ~77 MB |
由于模型本身非常轻量(仅 80 万参数),推理性能主要受限于:
实际生产环境中,建议批量推理以提高吞吐量。
输入:
输出:
[0.2422, -0.1251, -0.0093, 0.1421, -0.2039, 0.0856, -0.1573, 0.0234, -0.0891, 0.1102]| 检查项 | 标准 | 结果 |
|---|---|---|
| 推理完成 | 无异常退出 | 通过 |
| 输出形状 | [batch, 96, 1] | 通过 |
| 数值有效性 | 无 NaN/Inf | 通过 |
| 数值范围 | 合理范围内 | 通过 |
| 参数加载 | 134 个张量全部加载 | 通过 |
ImportError: No module named 'tsfm_public'
pip install granite-tsfmImportError: No module named 'datasets'
pip install datasetsModuleNotFoundError: No module named 'multiprocess'
pip install multiprocessNPU 设备不可用
本模型适配完成,使用 PyTorch + torch_npu + granite-tsfm 后端,权重来源于 ModelScope,未进行量化,推理正常工作。