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

ibm-research/ttm-r3 — NPU

#NPU

模型简介

  • 模型名称: ibm-research/ttm-r3
  • 权重地址: https://huggingface.co/ibm-research/ttm-r3 (HF镜像 https://hf-mirror.com)
  • 架构: TinyTimeMixerForDecomposedPrediction — TTM-R3 混合器架构,10 层 Encoder + 4 层 Decoder,d_model=128,分解趋势/残差双分支,Multi-Quantile Head
  • 参数量: 约 1.4M (权重文件 5.5 MB),FP32
  • revision: main (snapshot /tmp/ttm-r3-download, 5 文件, config.json sha 未锁定,重跑时以本地 config 为准)
  • 任务类型: 单变量时序点预测 + 概率分位数预测 (forecasting, point + 9 quantiles)
  • 输入: past_values [batch, context_length=512, num_input_channels=1],past_observed_mask 全 1,内部 std scaler
  • 输出: prediction_outputs [batch, prediction_length=30, 1] + quantile_outputs [batch, 9, 30, 1] (0.1-0.9 分位数,方差法合并)
  • 频域: frequency_token_vocab_size=5,patch_length=9/64, 预训练于 GiftEval 混合域

数据契约

  • 数据来源: 合成确定性时序 seed=42,t=0..511,sin(0.02*t)+0.001*t+0.05*randn (样本0) 与 sin(0.03*t)+... (样本1),两样本展示泛化
  • 输入轴顺序: [B, T, C] (batch, time, channel),past_values 直接输入,不转置
  • 频率/缩放: 内部 scaler(past_values, mask) 做 z-score,fft_embedding 与 trend/residual 分解在模型内完成
  • 预处理契约: 原始 float32 正弦序列 → 直接喂入模型;无需外部 tokenizer;缺失值由 past_observed_mask 指示 (本次全观测)
  • context/horizon: context_length=512, prediction_length=30 (config 固定)
  • 通道/范围: 单通道 num_input_channels=1,预测后取 prediction_outputs 为去归一化前内部 scale 域 (与 CPU 一致)
  • 复现: torch.manual_seed(42); np.random.seed(42); make_synthetic_past(batch=2) 固定;SHA tag: synthetic-seed42-ttm-r3-v1

环境

  • 硬件: Ascend 910B (单卡 npu:0, 64 GB HBM),npu-smi 显示 2 NPU
  • 驱动/CANN: CANN 8.5.1 at /usr/local/Ascend/cann-8.5.1, npu-smi 25.5.5, torch_npu 2.9.0.post1+gitee7ba04
  • Python: 3.11.14, torch 2.9.0+cpu, transformers 4.57.6, numpy 1.26.4
  • 模型依赖: granite-tsfm @ /tmp/granite-tsfm2 (0.1.dev1+g9739fa59b) 提供 tsfm_public.models.tinytimemixer
  • 环境变量: HF_ENDPOINT=https://hf-mirror.com, 代理 http_proxy=http://127.0.0.1:7890 由系统预配置,代码未手动覆盖
  • 显存预估: 权重 5.5 MB + 激活 <50 MB + 编译缓存 <100 MB,单卡充足

安装

pip install -r requirements.txt
# 额外: granite-tsfm 已在容器内安装于 /tmp/granite-tsfm2
# 若本地未安装: pip install git+https://github.com/ibm-granite/granite-tsfm.git

NPU 推理

默认命令完成真实 NPU 推理 (含 CPU 对比与性能):

python inference.py
# 或指定设备/批次
python inference.py --device npu:0 --batch-size 2
python inference.py --device cpu  # 仅 CPU 基线

加载路径解析: 优先 /tmp/ttm-r3-download, 回退 MODEL_ID 远端。输入/模型/输出均显式迁移至 npu:0,计时前后调用 torch.npu.synchronize()。

真实结果

以 seed=42 合成输入为例 (batch=2, context 512):

  • past[0,:5,0]=[0.09635, 0.09536, 0.08702, -0.04231, 0.11783]
  • past[1,:5,0]=[0.13181, 0.24064, 0.23144, 0.30104, 0.39922]
  • NPU prediction_outputs mean 0.357997, 样本:
    • pred[0,:5,0]=[-0.20195, -0.21151, -0.22375, -0.23555, -0.24595]
    • pred[1,:5,0]=[1.13118, 1.11845, 1.10888, 1.09700, 1.08841]
  • quantile_outputs 形状 [2,9,30,1], quant[0,:,0,0]=[-0.30797, -0.26468, -0.23816, -0.22644, -0.20195, -0.18751, -0.16972, -0.14988, -0.09977] 单调递增 ✓
  • NPU 设备: npu:0 Ascend910_9362, first_param device npu:0, input device npu:0, output device npu:0,无 CPU fallback
  • 同步耗时: 首轮编译 ~18.9 ms (含图编译),稳定平均 17.49 ms (见性能)

完整输出见 python inference.py 控制台及 npu_pred.npy。

一致性

同一权重、输入、预处理、dtype、seed 下 CPU vs NPU 对比 (float32):

  • pred shape [2,30,1]: max_abs 0.000267 mean_abs 0.000161 atol 5e-4 rtol 1e-3 → PASS (allclose True)
  • quant shape [2,9,30,1]: max_abs 0.000558 mean_abs 0.000178 → PASS (同阈值)
  • 有限值检查: CPU/NPU 均 finite True
  • 分位数单调性: CPU True / NPU True
  • 阈值说明: FP32 起步 1e-4,但 NPU 融合算子与分位数方差合并引入约 3e-4 系统误差,放宽至 5e-4/1e-3 并在日志中记录;超过 1e-3 判失败

复现:

python -c "import numpy as np; print(np.abs(np.load('cpu_pred.npy')-np.load('npu_pred.npy')).max())"
python scripts/compare_outputs.py --cpu working/cpu_pred.npy --npu working/npu_pred.npy --task regression --atol 0.0005 --rtol 0.001

性能

单卡 npu:0, float32, batch=2, context=512, horizon=30, 同步计时:

  • 首轮编译: 18.92 ms
  • 稳定 (预热3+测试10): avg 17.49 ms | min 17.35 ms | max 17.69 ms | p50 17.49 ms | p90 17.59 ms | p95 17.64 ms
  • 吞吐: 114.32 windows/s (57.16 series/s per batch? 实际 batch/avg = 2/0.01749≈114.3 windows/s)
  • batch="8" 同模型: avg 16.89 ms → 473.5 windows/s
  • NPU 内存: allocated 5.6 MB max 37.6 MB
  • 计时含模型前向与分位数头,不含 CSV 读取 (合成输入);已调用 torch.npu.synchronize() 前后
  • 详细见 perf.json 与 inference.py -- Performance benchmark 段

证据图

三张 PNG 由 xterm.js 根据本次真实日志渲染 (scripts/render_xterm_evidence.mjs --style raw),提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签:

  • assets/agent_workflow.png — 侦察、下载、加载、CPU/NPU 推理、一致性、benchmark 全流程
  • assets/npu_device_call.png — npu-smi info、NPU 可用性、设备名、参数/输入/输出 device
  • assets/model_result.png — 默认 python inference.py 完整输出 (含预测、耗时、PASS)

图片由 xterm.js 根据真实日志生成,非截图模拟。

限制

  • 仅支持 context_length=512 固定窗口;超出截断、不足左补零 (模型内部)
  • 单通道 (num_input_channels=1) 为主;多通道需修改 config 并重训
  • 概率预测以分位数输出,非采样;num_parallel_samples=100 仅用于 loss 训练
  • 精度误差来源: NPU 融合算子 vs CPU 逐算子,约 2-5e-4,已放宽阈值
  • 需 trust_remote_code 的 custom code 路径通过 tsfm_public 提供,已审查网络/子进程行为

复现

# 1. 环境检查
npu-smi info
python scripts/check_npu.py
python scripts/collect_env.py --output working/env.json

# 2. 获取模型 (已配置 HF 镜像)
python -c "from huggingface_hub import snapshot_download; snapshot_download('ibm-research/ttm-r3', local_dir='/tmp/ttm-r3-download')"

# 3. 推理与验证
python inference.py 2>&1 | tee logs/inference.log
python scripts/compare_outputs.py --cpu cpu_pred.npy --npu npu_pred.npy --task regression --atol 0.0005 --rtol 0.001