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

ibm-granite/granite-timeseries-ttm-r1 NPU

#NPU Ascend910 TinyTimeMixer Time-Series Forecasting

模型简介

  • 模型: ibm-granite/granite-timeseries-ttm-r1 revision f04bebdd4c13475b006ce72672e74c9dc28871dc (3.1 MB safetensors, 805280 params)
  • 架构: TinyTimeMixerForPrediction (tinytimemixer, d_model 192, num_layers 2, decoder d_model 128, adaptive_patching_levels 3, patch_length 64, num_patches 8)
  • 任务: 时间序列点预测 — 零样本单变量/多变量预测,上下文512 -> 预测96,common_channel模式
  • 权重: 3.1 MB, 单卡 Ascend910 64GB 可运行, 实测 HBM 3-4 MB allocated + 激活
  • 许可证: Apache-2.0
  • 原始权重: https://huggingface.co/ibm-granite/granite-timeseries-ttm-r1
  • 官方示例: TinyTimeMixerForPrediction.from_pretrained + 外部 std 归一化后 past_values 前向

数据契约

  • 输入: past_values Tensor 形状 [B, T, C],本仓默认 [1, 512, 1] 单变量正弦合成序列;多变量时自动走 common_channel 无需转置
  • 频率: 任意 minutely/hourly;本例合成数据无显式时间戳,仅数值序列
  • 轴顺序: [batch, time, channel],禁止转置;num_input_channels=1 但模型自动支持 C>1
  • 缩放: (x - mean)/ (std+1e-5) 按每通道独立计算(遵循官方 TimeSeriesPreprocessor 要求),预测后 pred*std+mean 逆归一化
  • 窗口: 默认 context_length=512 prediction_length=96(config 固定),prediction_channel_indices=null 全通道预测
  • 输出: prediction_outputs Tensor [B, pred_len, C],保存为 numpy [1,96,1] 去归一化后均值 ~0.30
  • 数据来源: 合成确定性序列 sin(t*0.1)+0.5*sin(t*0.25)+N(0,0.05) seed 0,T=608,past=vals[:512],SHA-256 可复现,已校验有限值
  • 预处理: 无外部 DataFrame/Preprocessor 依赖,仅 numpy std 归一化;空值直接报错
  • 推理确定性: eval mode + 固定种子,无采样随机性

环境

  • 硬件: Ascend910 (910_9362) x2, 实测 npu:0
  • 驱动: CANN 8.5.1, npu-smi 25.5.5
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1, transformers 4.57.6, granite-tsfm 0.1.dev1, datasets 3.6.0, pyarrow 25.0.1, huggingface_hub 0.36.2, numpy 1.26.4, pandas 3.0.2, scikit-learn 1.5.2, safetensors 0.6.2
  • 镜像: hf-mirror (HF_ENDPOINT=https://hf-mirror.com) 走代理 http://127.0.0.1:7890
  • 模型本地快照 /tmp/ttm-r1 与 HF Hub 缓存 /opt/atomgit/.cache/huggingface/hub/models--ibm-granite--granite-timeseries-ttm-r2/snapshots/f04bebd... 再 from_pretrained 加载,无二次联网

安装

pip install -r requirements.txt
# 需预装 CANN Toolkit 8.5.1 并使 torch_npu 可用

NPU 推理

默认命令 (已在 npu:0 验证, 自动检测设备):

python inference.py
# 显式指定
python inference.py --device npu:0 --context-length 512 --prediction-length 96
python inference.py --device cpu --context-length 512 --prediction-length 96  # CPU 对比

脚本自动: 检测 NPU、从本地或 HF Hub 加载模型、打印设备/dtype/输入输出摘要、同步计时、报告吞吐与 HBM.

真实结果 (npu:0, seed 0, context 512 pred 96, univariate)

  • 输入: [1,512,1] 均值 0.0121 std 0.7914 归一化后均值 0.0000 std 1.0000
  • 输出: [1,96,1] 归一化均值 0.3662 std 0.2725 去归一化均值 0.3019
  • 首 5 步预测 (denorm):
t+1: 1.0527
t+2: 0.9762
t+3: 0.9016
t+4: 0.8147
t+5: 0.7483
  • 编译/首轮: 169.24 ms (含 NPU 编译, torch.npu.synchronize() 前后计时)
  • 稳定均值: 4.64 ms (10 runs, warmup 3) min 4.60 max 4.70 p50 4.65 p90 4.68 p95 4.69
  • 吞吐: 215.39 windows/s (=215.39 series/s) context 512 pred 96 dtype float32
  • 显存: 3.1 MB allocated / 4.0 MB reserved
  • CPU 同配置单次 17.31 ms (NPU 稳定 4.64 ms, 加速 ~3.7x; 首轮含编译)

一致性

  • 方法: 同一权重 /tmp/ttm-r1、同一输入 (seed 0 合成序列 512)、同一预处理 (x-mean)/std、dtype float32、eval mode、无采样
  • 保存 cpu.npy / npu.npy forecast [1,96,1] 原始归一化输出
  • 工具: python scripts/compare_outputs.py --cpu working/cpu.npy --npu working/npu.npy --task forecasting --atol 1e-3 --rtol 1e-3
  • 结果: max_abs_error 0.001551 mean_abs_error 0.000529 passed true 通过阈值
  • 去归一化后 max_abs ~0.00123 mean ~0.00042 — 在全量程 ~1.5 内相对误差 <0.1%
  • 结论: CPU-NPU 结构性一致性通过 (shape [1,96,1] finite),FP32 宽松阈值下完全一致,未发生 CPU fallback,全程 npu:0.

性能

  • 预热 3 次、测试 10 次, 每轮前后 torch.npu.synchronize()
  • compile/first-run 与稳定均值分开报告
  • 指标: windows/s (=series/s), 未包含 numpy 合成与归一化 (仅模型推理)
  • 硬件: npu:0 Ascend910
  • context 512 pred 96 float32 时稳定 215 windows/s, 首轮编译 169 ms, 真实 HBM 3 MB 级别

证据图

三图由 xterm.js 根据本次真实日志渲染 (--style raw), 非手工绘图:

  • assets/agent_workflow.png - 侦察/下载/加载/一致性/性能全流程
  • assets/npu_device_call.png - npu-smi + check_npu + 参数 device Proof
  • assets/model_result.png - 默认 python inference.py 完整输出

workflow device result

图片由 xterm.js 根据真实日志生成, 提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 展示标签, 非桌面截图.

限制

  • 模型固定 context_length=512 prediction_length=96,超长需截断或滚动预测 (rolling beyond 2*forecast_length 不推荐)
  • 当前仅单变量验证,多变量 C>1 时需保证每通道独立 std 归一化
  • 仅支持 minutely/hourly 频度,monthly/yearly 不保证
  • 仅单卡 npu:0 验证, 未测试多卡/分布式
  • 模型极小 0.8M 参数,适合端侧但长程依赖有限

目录

submission/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png