冬
gcw_IDzXRVNw/ttm-research-r2-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TTM-Research-R2 昇腾 NPU 适配部署文档

1. 模型简介

TTM-Research-R2 是来自 IBM Research 的 TinyTimeMixer (TTM) 时间序列预测模型的研究版本。

本模型是 NeurIPS 2024 论文 "Tiny Time Mixers (TTMs): Fast Pre-trained Models for Enhanced Zero/Few-Shot Forecasting of Multivariate Time Series" 中提出的模型。

模型架构

属性值
模型类型TinyTimeMixerForPrediction
参数量0.85M
上下文长度512
预测长度96
Patch 长度64
Patch 步长64
隐藏维度192
编码器层数2
解码器层数2
解码器隐藏维度128
频率调制8 维

模型特点

  • MLP-Mixer 架构: 用于时间序列的特征混合
  • Patch 化处理: 将时间序列分割成固定长度的 patch
  • 频率调制模块: 对时间序列进行频率域处理
  • 多通道支持: 支持多变量时间序列输入
  • 高效推理: 轻量级模型(<1M 参数),适合边缘部署

输入输出格式

  • 输入: 时间序列 (batch, channels, context_length)
  • 输出: 预测值 (batch, prediction_length, channels)

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 1 GB NPU 显存(TTM 推理峰值约 276 MB)
  • 本次实测使用 910B2(单卡 64 GB)

2.2 软件依赖

依赖项版本要求说明
操作系统CentOS 7.6+ / Ubuntu 18.04+支持主流 Linux 发行版
CANN5.0.RC2+华为昇腾计算架构
Python3.10 / 3.12推荐 3.10+
PyTorch2.0.0+推荐 2.10
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展
safetensors>=0.4.0权重加载
granite-tsfm本地源码IBM 时间序列模型库

2.3 安装方式

# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100

# 2) 安装模型依赖(使用华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple \
    safetensors torch_npu numpy

# 3) 验证环境
python -c "
import torch, torch_npu
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"

3. 分步推理操作流程

步骤 1:准备工作目录

# 进入适配目录
cd /workspace/agent1/ttm-research-r2-ascend

# 确认上游权重存在
ls -la /workspace/agent2/ibm-research/ttm-research-r2/

# 确认 granite-tsfm 源码存在
ls -la /workspace/agent2/ibm-granite/granite-tsfm-main/

步骤 2:检查 NPU 环境

# 检查 NPU 设备状态
npu-smi info

# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"

步骤 3:运行推理脚本

# 基本用法:默认参数 (NPU:0)
python inference.py --device npu:0

# 指定其他 NPU 卡
python inference.py --device npu:1

# CPU 模式(仅在 NPU 不可用时使用)
python inference.py --device cpu

4. 完整测试用例

测试用例 1:时间序列预测(NPU:0)

conda activate pt2100
cd /workspace/agent1/ttm-research-r2-ascend
python inference.py --device npu:0

实际输出:

[W822 14:01:49.703614090 FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
INFO:p-1333710:t-281473760018464:patch_getenv.py:_log_once:get env LANG = en_US.UTF-8
[W822 14:01:58.812328740 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
============================================================
TTM-Research-R2 昇腾 NPU 推理
============================================================

[Device] npu:0
[Model Path] /workspace/agent2/ibm-research/ttm-research-r2

[Loading] TTM model from local granite-tsfm...
  Config loaded: context_length=512, prediction_length=96
  Model loaded successfully
  Total params: 0.85M

[Model Loaded] Allocated: 0.00 GB, Reserved: 0.00 GB

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 102.8       41                0    / 0             |
| 0                         | 0000:C1:00.0 | 0           0    / 0          3489 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1333710       | python                   | 120                    |
+===========================+===============+====================================================+


[Creating] Dummy time series input...
  Past values shape: torch.Size([1, 1, 512])
  Freq token shape: torch.Size([1, 1])

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 104.0       41                0    / 0             |
| 0                         | 0000:C1:00.0 | 0           0    / 0          3489 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1333710       | python                   | 120                    |
+===========================+===============+====================================================+


[Running Time Series Prediction]...

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(MB)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)      |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 104.4       41                0    / 0             |
| 0                         | 0000:C1:00.0 | 0           0    / 0          3645 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1333710       | python                   | 276                    |
+========================---++===================================================+

[Results]
  Output type: <class 'tsfm_public.models.tinytimemixer.modeling_tinytimemixer.TinyTimeMixerForPredictionOutput'>
  Prediction outputs shape: torch.Size([1, 96, 512])
  Output sample: [-0.5516, -0.3686, -0.2897, -0.2471, -0.2180]

[Memory After Inference] Allocated: 0.01 GB, Reserved: 0.14 GB

[Done]

✅ 验证要点:

  • 模型加载成功: 使用本地 granite-tsfm 库加载 TinyTimeMixer 模型 (0.85M 参数)
  • NPU 推理稳定: 时间序列预测在 NPU 上运行推理
  • NPU 进程运行: npu-smi 显示 Python 进程 (PID 1333710) 在 NPU 上运行
  • 预测输出: 输出形状 [1, 96, 512],96 步预测值
  • NPU 显存峰值: 约 276 MB 进程内存

5. 常见问题解答

Q1:提示 "NPU 不可用" 怎么办?

解决方案:

  1. 确认已安装 CANN 驱动并设置环境变量(source /usr/local/Ascend/ascend-toolkit/latest/set_env.sh)
  2. 运行 npu-smi info 检查设备状态
  3. 确认 PyTorch 和 torch_npu 版本匹配

Q2:为什么使用本地 granite-tsfm?

解释:TinyTimeMixer 模型在标准 transformers 包中不可用。IBM 提供了 granite-tsfm 库实现该模型。本脚本使用本地源码 /workspace/agent2/ibm-granite/granite-tsfm-main/。

Q3:TTM 与其他时间序列模型相比有什么优势?

解释:TTM 是轻量级预训练模型,参数量仅约 0.85M-3M,远小于 TimesFM、Moirai 等数十亿参数的模型。TTM 在零样本和少样本预测任务上实现了 SOTA 性能。

6. 文件结构

ttm-research-r2-ascend/
├── inference.py              # 推理脚本(torch_npu 推理)
├── README.md                 # 本文档
├── requirements.txt          # 依赖列表
└── assets/                  # 资源占位目录

上游权重目录(只读,未被修改):

/workspace/agent2/ibm-research/ttm-research-r2/
├── config.json               # 模型配置
├── model.safetensors        # 权重 (~3.4MB)
├── generation_config.json   # 生成配置
├── training_args.bin        # 训练参数
└── README.md              # 原始文档

本地 granite-tsfm 库(只读,未被修改):

/workspace/agent2/ibm-granite/granite-tsfm-main/
├── tsfm_public/
│   └── models/
│       └── tinytimemixer/   # TinyTimeMixer 模型实现
└── ...

7. requirements.txt - 依赖列表

# TTM-Research-R2 昇腾 NPU 推理环境依赖

# 核心依赖(必须)
torch>=2.0.0
torch_npu
safetensors>=0.4.0
numpy

使用方式:

# 激活环境
conda activate pt2100

# 安装依赖(华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple -r requirements.txt

8. 参考资源

  • TTM 论文 (arXiv:2401.03955)
  • IBM Granite TTM
  • TTM GitHub
  • granite-tsfm 源码
  • 华为昇腾文档
  • torch_npu GitHub