jsjs1258/ibm-research-ttm-research-r2-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-research/ttm-research-r2-NPU

#NPU Ascend910 TinyTimeMixer

模型简介

  • 原始模型: ibm-research/ttm-research-r2
  • 模型类型: TinyTimeMixerForPrediction (tinytimemixer, 0.855M params, time-series forecasting)
  • 任务: 时间序列点预测 (point forecasting),上下文 512 -> 预测 96 (hourly)
  • Revision: main (config context_length 512, prediction_length 96, patch_length 64, d_model 192, num_layers 2)
  • 硬件: 华为昇腾 910B (Ascend910_9362) 单卡 npu:0,通过 torch_npu 显式迁移
  • 权重: 3.3 MiB model.safetensors,本地加载 /opt/atomgit/adapt-npu-agent/.cache_hf/ttm-research-r2

Tiny Time Mixer (TTM) 是 IBM Research 开源的轻量时序基础模型,NeurIPS 2024。支持 zero-shot/few-shot 预测,本适配使用 512-96 模型,StandardScaler 预处理 + freq_token (hourly=7) 前缀调优。

数据契约

项值
数据来源ETTh1 hourly (https://raw.githubusercontent.com/zhouhaoyi/ETDataset/main/ETT-small/ETTh1.csv) 公开可追溯
行数/列17420 行, 8 列 (date + 7 通道 HUFL/HULL/MUFL/MULL/LUFL/LULL/OT)
使用通道OT (Oil Temperature) 单变量,num_input_channels=1
时间列date (hourly), 已排序,测试集 20% (3484 行)
输入窗口最后 512 小时 (context)
预测长度96 小时 (horizon)
缩放StandardScaler per-channel (mean=9.3458, std=1.9074) 独立拟合窗口,推理后逆变换
频率hourly -> freq_token=7 (DEFAULT_FREQUENCY_MAPPING h=7)
轴顺序[batch, time, channel] = [1,512,1]
是否训练否,仅 zero-shot 推理,权重冻结

inference.py 校验缺列、非法值,复用同一 fitted mean/std,不得重新 fit。

环境

  • Python 3.11.14
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04
  • transformers 4.57.6, huggingface-hub 0.36.2, pandas 3.0.2, numpy 1.26.4
  • Ascend CANN 8.5.1, SOC ascend910_9391, NPU 2 device(s), name Ascend910_9362
  • 采集: python scripts/collect_env.py --output working/env.json

安装

pip install -r requirements.txt
# 或已在昇腾容器中,通过 torch_npu 直接可用

模型需先下载到本地(依赖环境变量 HF_ENDPOINT=https://hf-mirror.com 自动镜像):

python -c "from huggingface_hub import snapshot_download; snapshot_download('ibm-research/ttm-research-r2', local_dir='./ttm-research-r2')"
# 或使用 HF CLI

NPU 推理

# 默认 npu:0,ETTh1 自动下载到 data/ETTh1.csv 或 /opt/atomgit/adapt-npu-agent/ETTh1.csv
python inference.py
# 自定义
python inference.py --model-dir /path/to/ttm-research-r2 --data /path/to/ETTh1.csv --context 512 --prediction-length 96 --device npu:0 --iterations 10

关键日志 (节选):

[INFO] model_id      : ibm-research/ttm-research-r2
[INFO] device        : npu:0
[INFO] npu name     : Ascend910_9362
[INFO] params        : 0.855M
[INFO] first param   : device=npu:0 dtype=torch.float32
[INFO] input tensor  : past_values shape=(1, 512, 1) device=npu:0 dtype=torch.float32
[INFO] freq_token    : value=7 (hourly) shape=(1,) device=npu:0
[INFO] first run (compile+infer): 187.2 ms
[INFO] forecast tensor: shape=(1, 96, 1) device=npu:0 dtype=torch.float32
[STATUS] NPU inference PASS

所有模型参数与输入 Tensor 显式 .to(npu:0),计时前后 torch.npu.synchronize(),首轮编译与稳定推理分离。

真实结果

基于 ETTh1 OT 最后 512 小时的逆变换预测 (前12步):

step   1: 9.3668
step   2: 9.1558
step   3: 9.0187
step   4: 8.9667
step   5: 8.9550
step   6: 8.9499
step   7: 8.9500
step   8: 8.9498
step   9: 8.8968
step  10: 8.9191
step  11: 9.0361
step  12: 9.2043
  • Forecast scaled first 12: [0.0110, -0.0996, -0.1715, -0.1987, -0.2049, -0.2075 ...]
  • Forecast 逆变换均值: 9.2456, min 8.3833, max 10.4381
  • Peak NPU mem: 3.6 MiB
  • 编译+推理首轮 187 ms, 稳定后 avg 4.9 ms

一致性

同一权重、输入、预处理、dtype float32、seed 下:

  • CPU: python compare -> pred shape [1,96,1], mean -0.0525
  • NPU: 同上,device npu:0
  • 保存 .npy: compare_outputs.py --cpu cpu.npy --npu npu.npy --task forecasting --atol 1e-4 --rtol 1e-3
  • 结果: max_abs 0.000154, mean_abs 5.59e-05, passed true (FP32, TinyTimeMixer 含 LayerNorm/MLP,误差来自 NPU 浮点融合,阈值 1e-4/1e-3 在合理范围)
  • 无 CPU fallback

性能

预热 3 次,测试 10 次,同步后计时:

指标ms
compile+first187.2
avg4.9
min4.8
max4.9
p504.9
p904.9
p954.9
windows/s204.99
peak NPU mem3.6 MiB

Batch=1, context 512, horizon 96, dtype float32, 含 inverse scaling,不含 CSV 读取首轮下载。

证据图

三张 PNG 由 scripts/render_xterm_evidence.mjs --style raw 根据真实日志生成,深灰背景、白色等宽字体,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅展示标签:

agent workflow npu device call model result

图片由 xterm.js 根据本次真实日志生成,非手绘或 Matplotlib 模拟。

限制

  • 仅支持 minutely/hourly 分辨率 (freq_token 7),weekly/monthly 未验证
  • 仅验证 npu:0 单卡,需 context ∈ {512,1024,1536},本仓固定 512
  • 权重未改,仅推理迁移到 NPU,未验证训练
  • 随机输入仅 smoke,需 ETTh1 真实数据复现
  • 分段误差 atol 1e-4/1e-3,仅单窗口一致性,非全量 benchmark

许可证

原模型 CC-BY-NC-SA-4.0 (research-use),详见上游。