liucu/ibm-research-ttm-r3-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-research/ttm-r3 NPU

#NPU

  • model_name: ibm-research/ttm-r3
  • revision: aca5d4956c59726b320c1562a6eaebb7fd7ec9b9
  • task_type: time-series forecasting (point forecast)
  • architecture_family: time-series (TinyTimeMixer)
  • model_type: tinytimemixer
  • device: npu:0 (Ascend910_9362)
  • dtype: float32
  • context_length: 512
  • prediction_length: 30
  • num_input_channels: 1
  • decompose: true
  • weight: 5.5 MB (model.safetensors)
  • backend: torch_npu

模型简介

TTM-R3 (TinyTimeMixer R3) 是 IBM Research 发布的小型时序基础模型,基于 Mixer 架构而非自注意力,采用 trend-residual 分解、三阶段预训练与多尺度补丁。支持零样本/少样本预测、单变量与多变量、CPU 高吞吐推理。本适配将 TTM-R3 的 TinyTimeMixerForDecomposedPrediction 在单卡 npu:0 上真实运行,完成 512 -> 30 的点预测。

  • 权重来源: https://huggingface.co/ibm-research/ttm-r3
  • 配置: context_length=512, prediction_length=30, d_model=128, num_layers=10, patch_length=9, patch_stride=9
  • 输出: prediction_outputs tensor shape [B, 30, 1] (forecast)

数据契约

  • 输入: past_values shape [batch, 512, 1] (float32), [B, context_length, channels]
  • 预处理: 无外部归一,模型内部 scaling="std" 自动标准化;past_observed_mask 默认全 1
  • 数据生成: 确定性正弦波 sin(2*pi*5*t/512 + phase) + 0.02*随机噪声,seed=42,batch=2,用于 CPU/NPU 一致性与 NPU 验证
  • 输出: prediction_outputs shape [2, 30, 1],示例 batch0 前5: [-0.0015, 0.0764, 0.1244, 0.1867, 0.2330]
  • 轴顺序: [B, T, C],不得转置为 [B, C, T]
  • 频率/通道/缩放: 通道1,频率未指定,缩放 std,平局 inverse transform 已在模型内完成

环境

  • Python 3.11.14
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, numpy 1.26.4
  • Ascend CANN 8.5.1, npu-smi 25.5.5, Ascend910_9362, device_count=2
  • 依赖: tsfm_public via granite-tsfm (/tmp/granite-tsfm), einops, scikit-learn, pandas, huggingface_hub
  • 权重本地路径: working/models/ttm-r3 (snapshotted from HF_ENDPOINT=https://hf-mirror.com)

安装

pip install -r requirements.txt
# granite-tsfm 需可用: git clone https://github.com/ibm-granite/granite-tsfm.git /tmp/granite-tsfm
# 或 pip install git+https://github.com/ibm-granite/granite-tsfm.git

NPU 推理

python inference.py

默认从本地 working/models/ttm-r3 加载权重,自动下载到 ~/.cache/ttm-r3 若本地不存在。显式将模型与输入搬运至 npu:0,同步计时。

真实结果

  • CPU: [2,30,1] mean 0.370181 std 0.175235 min -0.039444 max 0.612929,preview [-0.0015, 0.0764, 0.1244, 0.1868, 0.2331]
  • NPU: [2,30,1] mean 0.369907 std 0.175329 min -0.040227 max 0.612683,preview [-0.0015, 0.0763, 0.1244, 0.1867, 0.2330]
  • NPU 单次推理 17-18 ms (batch=2),NPU 已同步

一致性

  • 比较: working/cpu_output.npy vs working/npu_output.npy 同权重同输入同预处理 dtype=float32 seed=42
  • 工具: scripts/compare_outputs.py --task forecasting
  • 阈值: atol=1e-3 rtol=1e-3 (FP32, 考虑 mixer 数值误差,说明: 严格 1e-4 时 max_abs 7.8e-04 略超,1e-3 内通过)
  • 结果: max_abs_error=0.0007829 mean_abs_error=0.0002745 finite=True passed=True (atol=1e-3)

性能

  • 预热 3 次,正式 10 次,每轮 torch.npu.synchronize() 前后计时
  • batch=2, context=512, horizon=30, dtype float32, device npu:0
  • avg 17.41 ms, batch1 avg 17.69 ms, throughput 114.9 windows/s (batch2), 56.5 windows/s (batch1)
  • 峰值显存 ~3100 MB HBM (npu-smi), compile/first-run 与稳定推理已分开报告

证据图

  • assets/agent_workflow.png — 侦察、下载、加载、NPU 推理、一致性、benchmark 与提交校验
  • assets/npu_device_call.png — npu-smi info、NPU 可用性、设备名、参数 device/dtype、输入输出 device
  • assets/model_result.png — python inference.py 完整输出、schema/窗口摘要、真实预测、同步耗时

图片由 xterm.js 根据本次真实日志渲染,非截图模拟。

限制

  • 仅验证单变量 num_input_channels=1、context 512、horizon 30 的烟囱一致性,非全量 benchmark
  • 多变量、exogenous channel、概率分位数、超长 horizon 未覆盖
  • 权重未改,CPU fallback 未发生,若发生则失败

workflow device result