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 一致性验证。
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 mseTinyTimeMixerForPrediction (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/2prediction_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 仅作对齐展示,窗口不越界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],不可调换;无缺失值,无显式缺失 maskscaling: std 对 past_values 做归一化,外部直接传入原始 float32 即可;输出已 denorm[B,T,C] (batch, time, channel),与 PatchTST [B,C,T] 不同,禁止凭经验转置torch.npu.is_available()==True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362requirements.txtpip 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-depspython 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.0875schannel_0 49.6249, channel_1 34.8278, channel_2 99.2632 (原始量纲)[1,96,3] 已内部 denorm,量纲与输入一致,可直接业务使用:
[50.6734, 50.7723, 50.8015] tail [50.5276, 50.5011, 50.5124][35.3182, 35.3709, 35.3818] tail [35.1714, 35.1559, 35.1638][101.5028, 101.6725, 101.7207] tail [101.0484, 100.9938, 100.9988]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-3cpu 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)model param device 与 past_npu device 均为 npu:0torch.npu.synchronize(),首轮含编译,后续为稳定推理;批量 batch=1, context=512, horizon=96, channels=3, dtype=float32, device=npu:0first_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 channelmodel(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:~$为展示标签。

context_length 与 prediction_length 必须与预训练一致 (512→96) 或使用对应分支权重;不支持任意 horizon 外推(需 rolling)[B,T,C] float32,channel 数与训练时 num_input_channels=1 的 common_channel 模式兼容,但多通道通过广播处理,非独立 channel 归一snapshot_download 或本地 working/models/ttm-r1 加载;首轮编译耗时计入 first_runtorch_npu 与 tsfm_public,CPU fallback 视为失败