waylong/ibm-research-ttm-r3-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-research/ttm-r3 — Ascend NPU 适配

#NPU

模型简介

  • 模型名称: ibm-research/ttm-r3 (TinyTimeMixer R3, decompose: trend + residual)
  • 权重 URL: https://huggingface.co/ibm-research/ttm-r3
  • Revision: aca5d4956c59726b320c1562a6eaebb7fd7ec9b9
  • 架构: TinyTimeMixerForDecomposedPrediction (trend_forecaster + residual_forecaster), context 512 / prediction 30, d_model 128 / trend_d_model 8, 10+10 layers, 1.415M 参数
  • 任务: 多元时序点预测 (point forecasting), 支持零样本/少样本, 频率 5min 示例
  • 许可证: cc-by-nc-sa-4.0
  • 关联技能: npu-adapt-tabular (ibmr TTM R3 通过 tsfm_public / granite-tsfm 加载)

数据契约

  • 数据来源: working/sample_data_ttm.csv (合成示例, 可追溯) 与 submission fallback 合成
  • SHA256: 56bcb114bf9d6e4a61f70cdcae62de619cc03127bb960572aa0a7edf06ceb97a (sample_data_ttm.csv, 1200 rows)
  • Schema: timestamps: datetime64[ns] (5min freq, 2024-01-01 起), channel_0: float64, channel_1: float64, channel_2: float64; 推理使用 channel_0, channel_1 (2 channels)
  • 缺失值: 无; 预处理: 无标准化外部输入, 模型内部 StdScaler (scaling: std); 列顺序固定为 channel_0, channel_1
  • 输入契约: past_values: [B, T=512, C=2] float32 (common_channel 模式, batch 1)
  • 输出契约: prediction_outputs: [B, 30, 2] float32 (点预测), trend_prediction_outputs: [B,30,2], residual_prediction_outputs: [B,30,2]
  • 轴顺序验证: 禁止转置, 已按 [B,T,C] 验证
  • 时序参数: context_length=512, prediction_length=30, patch_length=9/64, frequency_token_vocab_size=5

环境

  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, pandas 3.0.2, numpy 1.26.4, einops, scikit-learn, huggingface_hub, safetensors
  • Ascend CANN 8.5.1, NPU: Ascend910_9362 ×2 (910B), 已验证 torch.npu.is_available()==True, device_count=2, 目标设备 npu:0
  • 依赖安装: pip install -r requirements.txt (含 granite-tsfm 源码 tsfm_public 位于 /tmp/gtsfm2, 需 datasets mock)
  • 权重体积: 5.5 MiB (model.safetensors SHA256 74dcec10fa75f334f6e6ddb8f6df19452de1c70a1ad43b87e5064c4e30f930cc), 单卡显存预估 < 1GB (含激活)

安装

pip install -r requirements.txt
# 若环境无 granite-tsfm, 从本地源码安装:
pip install -e /tmp/gtsfm2 --no-deps --no-build-isolation

权重获取

  • 官方: https://huggingface.co/ibm-research/ttm-r3 (config.json + model.safetensors)
  • 本次验证本地快照: working/model_ttm_r3/ (已通过 huggingface-cli download ibm-research/ttm-r3 --local-dir working/model_ttm_r3)
  • 推理优先本地目录, 失败回退 snapshot_download (需联网)

NPU 推理

默认命令 (无需参数, 自动选数据与设备):

python inference.py

行为:

  1. 检查 torch.npu.is_available(), npu-smi info, 设定 torch.npu.set_device(0)
  2. 加载 TinyTimeMixerForDecomposedPrediction.from_pretrained(local_dir).to(npu:0) 并打印首参数 device/dtype
  3. 加载 working/sample_data_ttm.csv 或合成 512+30 窗口
  4. 构造 past_values [1,512,2] 并显式迁移到 npu:0, 执行 torch.npu.synchronize() 计时
  5. 输出 forecast shape/有限值/首尾 5 行、loc/scale、耗时与设备验证

真实结果

来自本次 npu:0 真实推理 (python inference.py 完整日志见 assets/model_result.png):

[INPUT] past tensor shape torch.Size([1, 512, 2]) device npu:0 dtype torch.float32
[INFER] Completed in 0.2136s (sync timed)
[RESULT] Forecast tensor shape (1, 30, 2), finite True
[RESULT] Trend tensor shape (1, 30, 2)
[RESULT] pred head:
         timestamps  channel_0  channel_1
2024-01-02 18:40:00  50.824959  35.490074
2024-01-02 18:45:00  50.823193  35.503796
2024-01-02 18:50:00  50.811890  35.508495
2024-01-02 18:55:00  50.791489  35.506134
2024-01-02 19:00:00  50.769341  35.517082
[RESULT] channel means ['channel_0:50.5372', 'channel_1:35.4849']
[VERIFY] Model param device after inference: npu:0
[STATUS] NPU inference SUCCESS on npu:0, elapsed 0.2136s
[PERF] second run 0.0187s

CPU 对照 (相同权重/输入/seed, TinyTimeMixerForDecomposedPrediction):

  • prediction_outputs mean 43.0106, head [50.824707, 35.490063] (首点)
  • 已保存 working/cpu_output.npy 与 working/npu_output.npy 用于一致性校验

一致性

同一权重、输入、预处理、dtype=float32、seed=42、eval mode:

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_output.npy --npu working/npu_output.npy --task forecasting --atol 1e-4 --rtol 1e-3

结果:

{
  "task": "forecasting",
  "cpu_shape": [1, 30, 2],
  "npu_shape": [1, 30, 2],
  "finite": true,
  "atol": 0.0001,
  "rtol": 0.001,
  "max_abs_error": 0.001041412353515625,
  "mean_abs_error": 0.0004620234171549479,
  "passed": true
}
trend 校验: max_abs 0.000282 mean_abs 0.000124 passed true

阈值解释: FP32 点预测, 输出尺度 ~50, 1e-4/1e-3 严格阈值下仍通过; 误差来源为 NPU/CPU 浮点累积差异, 无 fallback。
单样本 smoke consistency, 非全量 benchmark。

性能

NPU npu:0 同步计时 (torch.npu.synchronize() 前后), batch 1, shape [1,512,2] -> [1,30,2], float32:

  • 首轮编译+推理: 0.2085 s (含 graph 编译)
  • 稳定阶段 (3 warmup + 10 measured):
    • avg 0.0173 s, min 0.0171 s, max 0.0181 s, p50 0.0172 s, p90 0.0174 s, p95 0.0177 s
    • second run 0.0187 s
    • 吞吐: 57.8 windows/s (1/0.0173), 115.6 series·s? 按 windows/s 报告
    • 含预处理否: 否 (仅模型 forward, 不含 CSV 读取)
    • 峰值显存: < 350 MB HBM (npu-smi 3106 MB 常驻含基础占用)
    • dtype/batch/shape 已在日志与 perf.json 记录

详见 working/perf.json 与 assets/model_result.png。

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成 (深灰背景、白色等宽、提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • assets/agent_workflow.png — 侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark 与提交校验
  • assets/npu_device_call.png — npu-smi info、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device
  • assets/model_result.png — 默认 python inference.py 完整 stdout/stderr、UTC 时间与退出码

README 仅引用图片, 渲染工具见技能文档。

agent workflow npu device call model result

限制

  • 仅验证单样本 (batch 1, context 512, horizon 30, 2 channels) 的数值一致性; 未做全量数据集精度评测
  • 需 torch_npu 与 Ascend 910B 环境, 默认 npu:0, 不支持 CPU fallback (检测到 fallback 即失败)
  • 模型为点预测, 无概率 quantiles; prediction_length 固定 30, 改变需重载或插值
  • 权重未提交, 运行时需本地 working/model_ttm_r3 或联网下载
  • 未启用 estimate_errors / multi_quantile_head 的额外不确定性头, 仅主预测