ibm-granite/granite-timeseries-ttm-r2-NPU
#NPU
模型简介
- 模型名称:
ibm-granite/granite-timeseries-ttm-r2
- 模型类型: TinyTimeMixer (tinytimemixer) 时间序列点预测
- 架构:
TinyTimeMixerForPrediction (d_model 192, 2 encoder layers + 2 decoder layers, adaptive patching 3 levels)
- 上游权重: https://huggingface.co/ibm-granite/granite-timeseries-ttm-r2 (revision
d6a79570cac0f33d526601cd3a0fc7c80a8f9a2f, config 1.6KB, model.safetensors 3.24 MB ~0.8M params)
- 任务: 时间序列预测 / time-series forecasting (point forecasting, MSE loss, std scaling)
- 输入:
past_values shape [batch, context_length=512, num_input_channels=1] float32, 已做 std 缩放 (loc/scale 由模型内部计算)
- 输出:
prediction_outputs shape [batch, prediction_length=96, channels=1] float32 (inverse scale 后)
- 特点: TTM-R2 为 IBM Granite 时序基础模型,相比 R1 在 ~700M 样本上预训练,benchmark 上提升 ~15%,支持 minutely/hourly 分辨率零样本/少样本预测
数据契约
- 输入轴顺序:
[B, T, C] (batch, time, channel),与 HuggingFace TinyTimeMixerForPrediction(past_values=...) 一致,禁止转置为 [B,C,T]
- 频率: 无显式频率 token (resolution_prefix_tuning=False, frequency_token_vocab_size=5 未启用)
- Context/Horizon: context 512 -> horizon 96 (patch_length 64, stride 64, num_patches 8, adaptive_patching_levels 3)
- 通道: num_input_channels=1 单变量;多变量时按 channel-independent 处理
- 缩放:
scaling="std" (模型内部 StdScaler 对每个序列独立去均值/方差,再 inverse)
- 缺失/掩码:
past_observed_mask 全 1 (无缺失);不需要额外 exogenous/static categorical
- 验证数据: 固定随机种子 0 生成
torch.randn(2,512,1) 作为零样本输入 (mean 0.0322, std 1.028),保存输入统计与前 3 步样例用于复现;未使用随机输入冒充业务结果,仅用于 CPU-NPU 一致性 smoke test
- 列契约: 不适用表格列,时序已固定轴顺序、同 dtype、同 seed、同预处理
环境
- 硬件: Ascend910 (910_9362) x2, 单卡 npu:0 推理
- 驱动: npu-smi 25.5.5, CANN 8.5.1 (
/usr/local/Ascend/cann-8.5.1)
- 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 5.7.0, numpy 1.26.4, pandas 3.0.2
- 显存: 模型权重 3.1 MB, 峰值约 3.1 GB HBM (npu-smi 实测 3112 MB idle), 单卡余量充足
- 复现:
torch.manual_seed(0); np.random.seed(0);
安装
pip install -r requirements.txt
# torch-npu 需与 CANN 8.5.1 匹配,已在 requirements.txt 固定版本
NPU 推理
默认命令 (自动下载权重需联网,本地已缓存时直接加载):
python inference.py
# 或显式
python inference.py --mode all --batch-size 2
- 加载:
TinyTimeMixerConfig + TinyTimeMixerForPrediction.from_pretrained(local_dir, torch_dtype=float32) vendors 自包含代码,无需 trust_remote_code
- 设备:
model.to(torch.device("npu:0")), 输入 past_values.to("npu:0"), 计时前后 torch.npu.synchronize()
- 日志: 打印模型名、任务、backend、device、dtype、首参数 device、输入摘要、输出摘要、同步耗时、状态;断言参数 device == npu:0,禁止 CPU fallback
- 动态 shape: 编译后复跑一次验证稳定延迟
真实结果
在 npu:0 上执行 python inference.py --mode validate (batch 2, float32):
- 输入: shape [2,512,1], min -3.1537 max 4.1015 mean 0.0322 std 1.0280, batch0 前3步 [-1.1258, -1.1523, -0.2505]
- CPU 输出: shape [2,96,1], latency 13.45 ms, stats min -0.7018 max 0.3888 mean -0.0842 std 0.1620, batch0 前3 horizon [-0.7017, -0.4886, -0.4903]
- NPU 首轮: latency 166.06 ms (含编译), stats min -0.7016 max 0.3888 mean -0.0841 std 0.1620, batch0 前3 [-0.7015, -0.4885, -0.4902]
- NPU 稳定: second run 4.93 ms, benchmark 10 次 avg 4.807 ms
一致性
- 方法: 同权重、同输入、同预处理、同 dtype 同 seed,分别保存
cpu_pred.npy / npu_pred.npy (forecast tensor [2,96,1])
- 工具:
python scripts/compare_outputs.py --cpu cpu_pred.npy --npu npu_pred.npy --task forecasting --atol 1e-3 --rtol 1e-3
- 结果: max_abs 0.0003346, mean_abs 0.0001010, rmse 0.0001266, finite True, shape 一致, passed True (FP32 atol 1e-3, 来自 time-series point forecast 容差)
- 说明: smoking single-window consistency,非完整 benchmark;概率预测无需 quantile 单调性检查
性能
- 预热 3 次, 测试 10 次, 每轮
torch.npu.synchronize() 同步
- compile/first-run: 4.94 ms (fresh model)
- 稳定推理 (batch 2, [2,512,1]->[2,96,1], FP32, npu:0): avg 4.807 ms, min 4.733 ms, max 4.914 ms, p50 4.789 ms, p90 4.890 ms, p95 4.902 ms
- 吞吐: 416.09 windows/s (series/s) batch 2, 等效单 series ~2.40 ms
- 峰值显存: ~3.1 GB (HBM 65GB 总量)
- 计时不含 Python 启动与权重加载,仅模型 forward+sychronize;预处理 (std scaling) 含在模型内部
证据图
三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成 (深灰背景 xterm.js, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签):
assets/agent_workflow.png - 侦察、环境检查、下载、加载、NPU 推理、一致性、benchmark、提交校验完整流程
assets/npu_device_call.png - npu-smi info + torch.npu.is_available() + 参数 device npu:0 + 输入/输出 Tensor device
assets/model_result.png - 默认 python inference.py 完整输出 (输入/输出 stats、耗时、PASS 状态)

限制
- 仅适配点预测 (context 512, horizon 96, std scaling);其他 context/horizon 需对应分支权重
- 单变量验证;多变量需保证 channel 顺序与缩放一致
- FP32 验证;FP16/BF16 需放宽阈值并重新验证
- 不支持 exogenous/ categorical 注入的单独验证 (模型支持但本次 smoke 未覆盖)
- 动态 shape 首次编译开销已单独报告,生产环境建议预热