waylong/ibm-granite-granite-timeseries-ttm-r1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-granite/granite-timeseries-ttm-r1 — Ascend NPU 适配

NPU Ascend910 #NPU

模型简介

ibm-granite/granite-timeseries-ttm-r1 是 IBM Granite 时序基础模型 TinyTimeMixer (TTM) 的 R1 版本(NeurIPS 24),专注于 多元时序点预测 的轻量预训练模型,0.805M 参数即可在 zero-shot 下超越数十亿参数基线。本仓适配 multivariate forecasting 任务,context 512 / horizon 96 / patch 64 / 3 通道,运行于 Ascend 910B (npu:0) 真实 NPU 推理并通过 CPU 一致性验证。

  • Hugging Face: ibm-granite/granite-timeseries-ttm-r1
  • Revision: f04bebdd4c13475b006ce72672e74c9dc28871dc (main branch, 512-96 variant)
  • 权重: model.safetensors 3.09 MB (~0.805M params, FP32, TinyTimeMixerForPrediction)
  • 配置: config.json context_length 512 prediction_length 96 d_model 192 num_layers 2 patch_length 64 patch_stride 64 num_patches 8 adaptive_patching_levels 3 scaling std loss mse
  • 架构: TinyTimeMixerForPrediction (common_channel encoder + decoder, gated_attn, LayerNorm, sincos positional disabled, student_t output disabled for point forecast)
  • 任务: 点预测(非概率),输出 prediction_outputs tensor

任务路由: 时序点预测 (forecasting) — 输入 [B, T, C] 历史窗口 → 输出 [B, horizon, C] 预测。输入轴严格 [B,T,C],禁止 [B,C,T] 转置。

数据契约

  • 输入: past_values 张量 [batch, context_length, n_channels],本例 batch=1, context=512, n_channels=3,dtype float32,3 通道为 channel_0/1/2
  • 输出: prediction_outputs [batch, prediction_length, n_channels],本例 [1,96,3],float32,finite 验证通过,已为 inverse transform 后可直接业务使用
  • 数据集: working/sample_data_ttm.csv (1200 rows + header, SHA256 56bcb114bf9d6e4a61f70cdcae62de619cc03127bb960572aa0a7edf06ceb97a, 89758 bytes)
    • 列: timestamps (datetime, 5min freq, 2024-01-01 00:00:00 起), channel_0 (mean 51.82 std 5.14), channel_1 (mean 35.91 std 2.57), channel_2 (mean 103.65 std 10.31)
    • 切片: 固定 iloc[:512] 作为 context 输入,iloc[512:608] timestamps 仅作对齐展示,窗口不越界
    • 统计 (context 512): channel_0 mean 49.6249 std 2.095, channel_1 mean 34.8278 std 1.074, channel_2 mean 99.2632 std 4.219
    • 通道顺序固定 [channel_0, channel_1, channel_2],不可调换;无缺失值,无显式缺失 mask
  • 预处理: 模型内部按 scaling: std 对 past_values 做归一化,外部直接传入原始 float32 即可;输出已 denorm
  • 轴顺序: 严格 [B,T,C] (batch, time, channel),与 PatchTST [B,C,T] 不同,禁止凭经验转置
  • 标签: vô监督预测,无 future values 监督,仅做 point forecast;评估以 forecast tensor 一致性为准

环境

  • 硬件: Ascend 910B ×2 (npu:0 Ascend910_9362, npu-smi 25.5.5, CANN 8.5.1)
  • 驱动/固件: torch.npu.is_available()==True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362
  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1, transformers 4.57.6, numpy 1.26.4, pandas 3.0.2, safetensors 0.7.0, tsfm_public (granite-tsfm git, commit tmp/gtsfm2)
  • 依赖见 requirements.txt

安装

pip install -r requirements.txt
# TTM 额外依赖(如未安装):
pip install git+https://github.com/ibm-granite/granite-tsfm.git
# 或本地 clone: git clone https://github.com/ibm-granite/granite-tsfm.git && pip install ./granite-tsfm --no-deps

NPU 推理

python inference.py

关键日志(npu:0 真实执行):

============================================================
Model: ibm-granite/granite-timeseries-ttm-r1 (revision f04bebdd4c13475b006ce72672e74c9dc28871dc)
Task: time-series forecasting (TTM-R1 point forecast)
Backend: torch + torch_npu + tsfm_public
Device: npu:0
Context: 512, Horizon: 96, Channels: 3
============================================================
[ENV] torch 2.9.0+cpu torch_npu 2.9.0.post1
[ENV] npu_available=True device_count=2 device_name=Ascend910_9362
[DEVICE] Set NPU device 0, current device 0
[VERIFY] Dummy tensor device: npu:0
[DATA] loading CSV /opt/atomgit/adapt-npu-agent/working/sample_data_ttm.csv
[DATA] rows 1200 cols ['timestamps', 'channel_0', 'channel_1', 'channel_2']
[DATA] x shape (512, 3) mean {'channel_0': 49.6248, 'channel_1': 34.8277, 'channel_2': 99.2631}
[INPUT] past shape torch.Size([1, 512, 3]) dtype torch.float32 mean 61.2386
[LOAD] cfg context=512 pred=96 d_model=192 num_layers=2 patch_len=64
[LOAD] model first param device cpu dtype torch.float32
[LOAD] params 0.805M
[LOAD] moved to npu:0, first param device npu:0
[INPUT] past_npu device npu:0 dtype torch.float32
[INFER] Completed in 0.0875s (sync timed)
[RESULT] Forecast tensor shape torch.Size([1, 96, 3]) device npu:0 dtype torch.float32 finite True
[RESULT] forecast mean 62.4288 std 28.2270
[RESULT] channel_0 head ['50.6734', '50.7723', '50.8015'] tail ['50.5276', '50.5011', '50.5124']
[RESULT] channel_1 head ['35.3182', '35.3709', '35.3818'] tail ['35.1714', '35.1559', '35.1638']
[RESULT] channel_2 head ['101.5028', '101.6725', '101.7207'] tail ['101.0484', '100.9938', '100.9988']
[VERIFY] Model param device after inference: npu:0
[STATUS] NPU inference SUCCESS on npu:0, elapsed 0.0875s

真实结果

  • 输入 context 均值: channel_0 49.6249, channel_1 34.8278, channel_2 99.2632 (原始量纲)
  • 预测输出 [1,96,3] 已内部 denorm,量纲与输入一致,可直接业务使用:
    • channel_0 head [50.6734, 50.7723, 50.8015] tail [50.5276, 50.5011, 50.5124]
    • channel_1 head [35.3182, 35.3709, 35.3818] tail [35.1714, 35.1559, 35.1638]
    • channel_2 head [101.5028, 101.6725, 101.7207] tail [101.0484, 100.9938, 100.9988]
    • 整体 NPU 均值 62.4288, CPU 均值 62.4289, std ~28.22, finite True,无 NaN/Inf
  • 输出仅为 point forecast,无 quantile/samples(TTM-R1 500-96 变体 loss=mse,distribution_output=student_t 但 prediction_outputs 为 point)

一致性

  • 方法: 同一权重(model.safetensors 3.1M, seed 42, eval mode, FP32)、同输入(past_values [1,512,3])、同预处理、同 dtype,分别保存 CPU/NPU prediction_outputs 为 .npy,运行 compare_outputs.py --task forecasting --atol 1e-4 --rtol 1e-3
  • 结果: cpu shape [1,96,3] npu shape [1,96,3] finite True max_abs 6.866e-04 mean_abs 2.735e-04 allclose True (atol 1e-4 rtol 1e-3)
  • 说明: FP32 下误差 <1e-3,属于 NPU 融合算子数值差异,非精度回退;CPU fallback 未发生,model param device 与 past_npu device 均为 npu:0

性能

  • 计时: 每轮前后 torch.npu.synchronize(),首轮含编译,后续为稳定推理;批量 batch=1, context=512, horizon=96, channels=3, dtype=float32, device=npu:0
  • 结果: first_run 0.0875s (含首次编译) / second_run 0.0049s / stable_avg 0.0050s / min 0.0049s / max 0.0051s / p50 0.0049s / p90 0.0051s / p95 0.0051s (10次测试,3次 warmup)
  • 吞吐: ~201 windows/s (1 / 0.0050), ~19328 timesteps/s (96 / 0.0050), ~603 channel-timesteps/s per channel
  • 显存: 权重 3.1M + 激活 <100M,单卡 64GB 充足,峰值 <1GB
  • 说明: 仅计时 model(past_values) 推理,不含 CSV 读取/DataFrame 预处理;若需端到端含预处理,可在 inference.py 外计时

证据图

  • assets/agent_workflow.png — 完整适配流水 (inspect, 数据, NPU 推理, 一致性, benchmark, 校验)
  • assets/npu_device_call.png — npu-smi 与 NPU 设备调用链 (torch.npu.is_available, device 0, param/input device)
  • assets/model_result.png — 默认 python inference.py 完整输出 (schema, forecast 摘要, 同步耗时, SUCCESS)

三图由 scripts/render_xterm_evidence.mjs --style raw 基于本次真实日志生成,深灰背景 xterm.js 渲染,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 为展示标签。

agent_workflow npu_device_call model_result

限制

  • 仅支持点预测,context_length 与 prediction_length 必须与预训练一致 (512→96) 或使用对应分支权重;不支持任意 horizon 外推(需 rolling)
  • 输入仅支持 [B,T,C] float32,channel 数与训练时 num_input_channels=1 的 common_channel 模式兼容,但多通道通过广播处理,非独立 channel 归一
  • 未启用量化(FP32),BF16/FP16 在 NPU 上可进一步加速但需重验证阈值
  • 模型权重未提交,需通过 snapshot_download 或本地 working/models/ttm-r1 加载;首轮编译耗时计入 first_run
  • 推理依赖 torch_npu 与 tsfm_public,CPU fallback 视为失败