ibm-granite/granite-timeseries-ttm-r2 是 IBM Granite 时序基础模型 TinyTimeMixer (TTM) 的 R2 版本,专为多元时序点预测设计,论文见 NeurIPS 24。本仓为 R2 的 context 512 / prediction 96 分支,参数量 0.8M,支持 minutely/hourly 分辨率,基于 common_channel 混合与 std 缩放,适配 Ascend 910B (npu:0) 的真实推理与 CPU 一致性验证。
d6a79570cac0f33d526601cd3a0fc7c80a8f9a2fmodel.safetensors 3.1 MB, 0.805M params, FP32, TinyTimeMixerForPredictionconfig.json context 512 patch 64 stride 64 num_patches 8 d_model 192 decoder 128 2层 fcm 3层, scaling std, loss msegranite-tsfm (tsfm_public), transformers>=4.37.2, 需本地安装(见安装)任务路由: 时序点预测 (forecasting) — 输入
[B, T, C]历史窗口 → 输出[B, horizon, C]预测。分类/回归的 logits 比較不適用,本倉以 forecast tensor 的 MAE/一致性為驗證。
past_values 张量 [batch, context_length, num_channels],本示例 batch=1, context=512, channels=3,列 ["channel_0","channel_1","channel_2"],dtype float32,time 维度为 5min 频率prediction_outputs [batch, prediction_length, channels],本例 [1,96,3],已做 inverse std 缩放(与输入同量纲),finite 验证通过/opt/atomgit/adapt-npu-agent/working/sample_data_ttm.csv 合成序列 1200 行,5min 频率,种子 42,3 通道随机游走;SHA-256 56bcb114bf9d6e4a61f70cdcae62de619cc03127bb960572aa0a7edf06ceb97a,无缺失,时间排序
timestamps (datetime), channel_0 (~50±2), channel_1 (~34±1), channel_2 (~99±4){channel_0:49.62, channel_1:34.82, channel_2:99.26};后 96 timestamp 作 forecast horizonTinyTimeMixerStdScaler 按 past_values 计算 loc/scale (loc ~[49.6,34.8,99.2] scale~[2.09,1.07,4.21]) 并在预测后逆变换;用户无需外部 StandardScaler,输入原始 float32 即可(与 README 的外部标准化说明等价,由模型内部完成)[B,T,C],未做 [B,C,T] 转置;缺失值通过 mask 标记(本例全观测)torch.npu.is_available()==True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362granite-tsfm 0.3.9.dev4 (从 https://github.com/ibm-granite/granite-tsfm 本地安装,因 pypi 代理限制)requirements.txtpip install -r requirements.txt
# granite-tsfm 需从源码安装(pypi 镜像暂不可达时)
git clone https://github.com/ibm-granite/granite-tsfm /tmp/granite-tsfm
pip install /tmp/granite-tsfm --no-deps
# 或
pip install git+https://github.com/ibm-granite/granite-tsfm.gitpython inference.py关键日志(npu:0 真实执行):
============================================================
Model: ibm-granite/granite-timeseries-ttm-r2 (revision d6a79570cac0f33d526601cd3a0fc7c80a8f9a2f)
Task: time-series forecasting (TTM point forecast)
Backend: torch + torch_npu
Device: npu:0
Context: 512, Horizon: 96, Channels: ['channel_0', 'channel_1', 'channel_2']
============================================================
[ENV] torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04
[ENV] npu_available=True, device_count=2, device_name=Ascend910_9362
[ENV] npu-smi exit 0
...
[DEVICE] Set NPU device 0, current device 0
[VERIFY] Dummy tensor device: npu:0
[LOAD] Using model dir /opt/atomgit/adapt-npu-agent/working/models/ttm-r2
[LOAD] cfg context=512 pred=96 d_model=192 num_layers=2 mode=common_channel scaling=std
[LOAD] model first param device: npu:0
[LOAD] model param count 0.805M dtype torch.float32
[INPUT] past tensor shape torch.Size([1, 512, 3]) device npu:0 dtype torch.float32
[INFER] Completed in 0.0871s (sync timed)
[RESULT] Forecast tensor shape (1, 96, 3), finite True
[RESULT] pred head:
timestamps channel_0 channel_1 channel_2
2024-01-02 18:40:00 50.736332 35.398720 101.735817
2024-01-02 18:45:00 50.845657 35.477028 101.869629
...
[VERIFY] Model param device after inference: npu:0
[STATUS] NPU inference SUCCESS on npu:0, elapsed 0.0871schannel_0 49.62, channel_1 34.82, channel_2 99.26,loc/scale 与之一致[1,96,3] 已逆缩放,量纲与输入一致,可直接业务使用:
[0,0]: [50.736, 35.398, 101.735][0,95]: [50.179, 35.180, 100.622]channel_0 50.6475, channel_1 35.3896, channel_2 101.4912同一权重、输入、seed、dtype(float32)、eval 模式,CPU vs NPU:0 对比 forecast 张量:
python scripts/compare_outputs.py --cpu working/cpu_output.npy --npu working/npu_output.npy --task forecasting --atol 1e-4 --rtol 1e-3cpu_shape [1,96,3] npu_shape [1,96,3] finite true max_abs 0.000847 mean_abs 0.000261 PASSED设备 npu:0,FP32,batch=1, context=512, horizon=96, channels=3,计时含 torch.npu.synchronize(),预热 3 次,测试 10 次:
三图由 xterm.js 根据本次真实日志渲染(scripts/render_xterm_evidence.mjs --style raw):

提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅作展示标签。
prediction_length=96 单步预测;滚动预测需外部循环(不推荐超过 2*horizon)