gcw_6a3w2yXz/ibm-research-test-ttm-v1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-research/test-ttm-v1 NPU 适配

#NPU

本仓库将 HuggingFace 上的 ibm-research/test-ttm-v1(TinyTimeMixer 测试版,TinyTimeMixerForPrediction,context 512 / prediction 96,d_model 192,decoder d_model 128,2 encoder layers + 2 decoder mixers,0.805M 参数,3.1 MB safetensors,std scaling,common_channel 模式)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库,验证点预测时间序列推理与 CPU-NPU 一致性。

  • 模型原始仓库:https://huggingface.co/ibm-research/test-ttm-v1
  • 权重:model.safetensors 3.24 MB(805,280 参数,float32,safetensors),config.json context_length 512, prediction_length 96, patch_length 64, patch_stride 64, num_patches 8, adaptive_patching_levels 3, d_model 192, decoder_d_model 128, num_layers 2, decoder_num_layers 2, mode common_channel, scaling std, distribution_output student_t, transformers 4.37.2
  • 源实现:https://github.com/ibm-granite/granite-tsfm(tsfm_public.models.tinytimemixer.TinyTimeMixerForPrediction),无需 trust_remote_code,权重经 safetensors 直接加载
  • Revision:d41dd386e08192aca42157b757df887c98de0c3e

[!WARNING] Test checkpoint:官方 README 标注 For the official TTM model, please see: https://huggingface.co/ibm-granite/granite-timeseries-ttm-v1,本 test-ttm-v1 为 0.8M 参数的测试/演示权重,性能不代表官方 TTM。本仓验证该测试权重的 NPU 真实推理、可复现性与一致性,适用于表格数据中多列数值时序的零样本预测演示。

任务与路由

项值
模型ibm-research/test-ttm-v1(revision d41dd386, 0.805M params, 3.24 MB fp32)
架构TinyTimeMixerForPrediction(Patcher 192×64 + 3级 Mixer 48/96/192 通道 + Decoder Adapter 128 + Forecast Head 96×1024)
任务时间序列点预测 point forecasting
输入past_values shape [batch, time, num_input_channels](本仓 batch 2, time 512, channels 1, float32, seed 0 随机正态),past_observed_mask 自动为全1
输出prediction_outputs shape [batch, horizon, channel](本例 [2,96,1]),loc [2,1,1],scale [2,1,1]
数据契约轴顺序 [batch, time, channel] 输入 -> [batch, horizon, channel] 输出;context 512(patch 64 stride 64 -> 8 patches × 3 adaptive levels),horizon 96,channels 1,scaling std,频率隐式,缺失值以 mask 标记(全观测),dtype float32
路由time-series 点预测,验收 forecast 数值一致性与有限值

表格数据类型关联:适用于表格数据(CSV/Parquet)中单变量或多变量数值时序的预测任务,任务映射为 forecasting,验收指标为 forecast 数值一致性、有限值与性能(windows/s)。

环境

  • 操作系统:Linux aarch64,昇腾 910B(Ascend910_9362),CANN 8.5.1,npu-smi 25.5.5,Ascend toolkit /usr/local/Ascend/cann-8.5.1
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1+gitee7ba04,transformers 4.57.6,safetensors 0.7.0,numpy 1.26.4,huggingface_hub,einops
  • 硬件:单卡 npu:0,HBM 64 GB,NPU 3 物理ID 6(本次),空闲显存约 3113 MB 起

依赖见 requirements.txt,安装:

pip install -r requirements.txt

NPU 推理

默认命令(真实 NPU 推理,含编译):

python inference.py

关键逻辑:

  • 通过 tsfm_public.models.tinytimemixer 源码(/tmp/granite/granite-tsfm 自动 clone)加载 TinyTimeMixerConfig/TinyTimeMixerForPrediction,规避 tsfm_public.toolkit 的 datasets/pyarrow 重依赖
  • 本地优先 /tmp/ttm_test → working/ttm_model → huggingface_hub.snapshot_download 回退下载
  • model.to("npu:0"),first_param_device 校验为 npu:0,输入 past_values [2,512,1] float32 seed 0 显式 .to("npu:0")
  • 计时前后 torch.npu.synchronize(),首轮 177 ms 编译,稳定 4.6 ms

真实结果

以 torch.manual_seed(0) 生成 past_values [2,512,1] float32(past[0,0:5,0]=[-1.1258, -1.1523, -0.2505, -0.4338, 0.8487])在 npu:0 上执行:

[INFO] model_name=ibm-research/test-ttm-v1
[INFO] config context_length=512 prediction_length=96 num_input_channels=1 d_model=192
[INFO] model params=805280 weight_bytes~3.1MB
[INFO] first_param_device=npu:0
[INFO] input past_values shape=(2, 512, 1) dtype=torch.float32 seed=0
[INFO] first_run latency=177.56 ms (includes NPU graph compile)
[INFO] output prediction_outputs shape=(2, 96, 1) dtype=torch.float32 device=npu:0
[INFO] output sample pred[0,0:5,0]=[-0.7851699590682983, -0.503393292427063, -0.43183544278144836, -0.29347968101501465, -0.23588469624519348]
[INFO] output stats mean=-0.094315 std=0.115015 min=-0.785170 max=0.109203
[PERF] warmup=3 stable=10 compile/first=177.56ms avg=4.65ms min=4.62ms max=4.72ms p50=4.63ms p90=4.68ms p95=4.70ms batch=2 dtype=float32 windows/s=429.9
[SUCCESS] NPU inference completed on npu:0

CPU 同输入对照:mean -0.094530 std 0.115139 sample [-0.785889, -0.504125, -0.432579],输出保存于 working/ttm_compare/cpu.npy / npu.npy。

一致性

同一权重、输入、预处理、seed、dtype(float32)下比较 prediction_outputs [2,96,1]:

python .claude/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/ttm_compare/cpu.npy --npu working/ttm_compare/npu.npy --task forecasting --atol 1e-3 --rtol 1e-3

结果:

{
  "task": "forecasting",
  "cpu_shape": [2, 96, 1],
  "npu_shape": [2, 96, 1],
  "finite": true,
  "atol": 0.001,
  "rtol": 0.001,
  "max_abs_error": 0.0007596909999847412,
  "mean_abs_error": 0.0002247318964994823,
  "passed": true
}

阈值说明:FP32 下 atol=1e-3, rtol=1e-3;NPU 与 CPU 均为 float32,max 0.00076 在容差内(属 NPU 融合算子正常抖动)。单样本 smoke consistency,非完整数据集精度评测。

性能

单卡 npu:0,batch 2, context 512, horizon 96, channels 1, float32,torch.npu.synchronize() 包裹:

指标值
compile / first_run177.56 ms
avg (10 runs)4.65 ms
min / max4.62 ms / 4.72 ms
p50 / p90 / p954.63 ms / 4.68 ms / 4.70 ms
warmup3
stable10
batch2 windows
throughput429.9 windows/s
dtypefloat32
峰值显存HBM 3113 MB base
预处理计时不含(随机 Tensor 生成 <1 ms)

证据图

  • assets/agent_workflow.png:工作流(环境检查、获取模型、分析模型、NPU适配、验证、整理、推送)的终端日志
  • assets/npu_device_call.png:torch.npu.is_available()==True、npu:0 调用与 first_param_device=npu:0 证据
  • assets/model_result.png:python inference.py 默认推理的输入摘要、输出摘要、同步耗时与最终 SUCCESS

agent_workflow npu_device_call model_result

三图由 scripts/render_xterm_evidence.mjs 基于真实日志生成,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$(展示标签,非服务器身份证明)。

限制

  • test-ttm-v1 为测试 checkpoint,未经官方大规模 benchmark,零样本效果不代表 ibm-granite/granite-timeseries-ttm-v1;生产建议使用官方正式版。
  • 单变量演示(num_input_channels=1),多变量需 common_channel 模式且输入 channels 匹配权重。
  • 输入需外部标准化(std scaling),本仓随机数据未做逆缩放。
  • 动态 shape 首次编译后已复跑,NPU 侧 CPU fallback 未发生(若发生则判失败)。
  • 显存估算:权重 3 MB + 激活 <100 MB,单卡 64 GB 充足。

许可证

Apache-2.0(权重与代码遵循原仓 ibm-research/test-ttm-v1 与 ibm-granite/granite-tsfm 的 Apache-2.0)。