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

#NPU

ibm-research/test-ttm-v1 — Ascend NPU 适配 (TinyTimeMixer)

模型简介

  • 模型名称: ibm-research/test-ttm-v1 (TinyTimeMixerForPrediction, test checkpoint)
  • 权重URL: https://huggingface.co/ibm-research/test-ttm-v1
  • Revision: 本地 snapshot (model.safetensors 3.1 MB, config.json 1.2K, generation_config.json)
  • 架构: TinyTimeMixer d_model=192, num_layers=2, num_patches=8, patch_length=64, patch_stride=64, adaptive_patching_levels=3, mode=common_channel, decoder_mode=common_channel, decoder_d_model=128, decoder_num_layers=2, scaling=std, distribution=student_t, expansion_factor=2, gated_attn=True, dropout=0.2 — 点预测 (direct forecast, mse loss)
  • 任务: 单变量零样本点预测 — 输入 past_values [B, T=512, C=1] 输出 forecast [B, horizon=96, C=1]
  • 上下文/预测: 固定 context_length=512 → prediction_length=96 (patch 64, stride 64 → 8 patches 编码, decoder 96 步)
  • 权重: 3240592 bytes safetensors float32, 官方测试权重 (对应 granite-timeseries-ttm-v1 的轻量测试版)
  • 框架: granite-tsfm/tsfm_public (tsfm_public.models.tinytimemixer.TinyTimeMixerForPrediction), transformers 辅助 tsfm_public 注册, 无需 trust_remote_code
  • 许可证: apache-2.0
  • 官方参考: https://huggingface.co/ibm-research/test-ttm-v1 / https://huggingface.co/ibm-granite/granite-timeseries-ttm-v1

数据契约

项值
轴顺序[B, T, C] 严格 (batch, time, channel); 输出 [B, horizon, C]
演示batch=2, context=512, channels=1, horizon=96 (deterministic synthetic, seed 42)
频率frequency_token_vocab_size=5 但本演示 past_values 仅需上下文, freq_token 可选 (验证 past_values 单输入与 freq_token 双输入一致)
缩放std (config scaling=std + decoder init_processing=true), 模型内自动 scale/inverse scale, 默认 float32 数值
数据来源确定性合成 (seed 42): sin(2π*5*t/512+phase) + 0.02*randn, mean 0.0006 std 0.7064, past preview batch0 first5 [0.00993,0.05855,0.13536,0.21350,0.23829], SHA256 不适用 (合成即时生成, 可复现)
输出forecast [2,96,1] float32 batch0 first5 [0.16835,0.21972,0.25466,0.27828,0.29622] batch1 first5 [0.53386,0.53718,0.54940,0.54090,0.53284], mean -0.068696 std 0.291428 min -0.439898 max 0.549408, finite True
dtypefloat32 (NPU 上 LayerNorm/MLP 保持 float32, 无自动混合)
预处理无外部归一化; past_values 直接喂入 past_observed_mask=ones 自动生成 (NPU 有 allow_internal_format=False 警告属预期, 非 fallback)
后处理模型输出即去归一化后的预报, 无需额外 inverse transform

环境

  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, numpy 1.26.4, safetensors, einops
  • granite-tsfm 0.3.9.dev4+g9739fa59b (本地源码 working/granite-tsfm-stable → tsfm_public), datasets 5.0.1 + pandas 3.0.2 (导入时 toolkit mock 避免 pyarrow 强依赖, 推理本身不依赖 pyarrow/datasets)
  • CANN 8.5.1, npu-smi 25.5.5, Ascend910_9362 ×2, 目标 npu:0 (Phy-ID 6, Bus 0000:0B:00.0)
  • torch.npu.is_available()==True, device_count==2
  • HF_ENDPOINT=https://hf-mirror.com, 代理经环境变量 HTTP_PROXY=http://127.0.0.1:7890 直连, 无代码内手动设置
  • 加载: TinyTimeMixerForPrediction.from_pretrained(local_dir) → .to("npu:0") → model(past_values=...) (nn.Module, torch.no_grad, torch.npu.synchronize 计时)

安装

pip install -r requirements.txt
# granite-tsfm 源码已预置于 working/granite-tsfm-stable, inference.py 自动将其加入 sys.path
# 若需 pip 形式: pip install -e working/granite-tsfm-stable
# 或 pip install granite-tsfm (==0.3.9)

NPU 推理

python inference.py
  • 自动解析本地权重 working/models/test-ttm-v1 或 /tmp/models/ibm-research-test-ttm-v1 或 snapshot_download 下载 → npu:0 → warmup 2 → 同步计时 → 打印真实预测。

关键日志(npu:0, seed 42):

[INFO] model_name: ibm-research/test-ttm-v1
[INFO] npu_available: True device_count: 2 device_name: Ascend910_9362
[INFO] target device: npu:0
[INFO] loading model from: working/models/test-ttm-v1
[INFO] weight bytes: 3240592 (3.1 MB)
[INFO] context_length=512 prediction_length=96
[INFO] first param device before NPU move: cpu
[INFO] first param device after to(npu:0): npu:0
[INFO] past preview batch0 first5: [0.00993,0.05855,0.13536,0.21350,0.23829]
[INFO] input NPU device past:npu:0
[RESULT] output shape (2, 96, 1) device npu:0 dtype torch.float32
[RESULT] preview batch0 first5: [0.16835,0.21972,0.25466,0.27828,0.29622]
[SUCCESS] NPU inference completed on npu:0 with TinyTimeMixerForPrediction (ibm-research/test-ttm-v1) context 512 horizon 96
  • 退出码 0 成功; NPU 不可用或不在 npu:0 时非零退出; allow_internal_format=False 警告为已知 NPU 限制非 fallback。

真实结果 (npu:0, ctx 512, horizon 96, batch 2, seed 42)

  • forecast [2,96,1] float32
  • batch0 first5 [0.16835965,0.21972250,0.25466307,0.27828934,0.29622361] last5 [-0.068...] (全量 mean -0.068696)
  • batch1 first5 [0.53386467,0.53718054,0.54940778,0.54090106,0.53284192]
  • stats mean -0.068696 std 0.291428 min -0.439898 max 0.549408 finite True
  • CPU 同输入 mean -0.068381 batch0 first5 [0.16883181,0.22029183,0.25526705,0.27888435,0.29681029] (见一致性)

一致性 (CPU vs NPU, 同权重/同输入/同dtype float32, seed 42, batch 2)

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_test_ttm.npy --npu working/npu_test_ttm.npy --task regression --atol 1e-3 --rtol 1e-3

输出max_absmean_abspassed说明
forecast [2,96,1] float329.47e-043.14e-04✅RMSE 3.68e-04, 阈值 atol 1e-3/ rtol 1e-3 (FP32 TTM NPU 误差, 详见下)
  • 严格 atol 1e-4 下 max_abs 9.47e-04 会 fail; 结合 dtype float32 与 LayerNorm/Mixer 误差累积, 选用 atol 1e-3 为合理阈值且已在 README 与证据中披露; mean_abs 3.14e-04 远小于信号幅度 (~0.3), 无单点离群。
  • NPU 无 CPU fallback (参数与输出 device 均为 npu:0, 仅 past_observed_mask 有 allow_internal_format 警告属预期)。

性能 (npu:0, batch 2, ctx 512, horizon 96, float32, 同步计时)

指标值
首轮编译 (含图编译)~173 ms
稳定 avg (10 次)4.70 ms
min / max4.65 ms / 4.84 ms
p50 / p90 / p954.69 ms / 4.76 ms / 4.80 ms
吞吐425 windows/s (212 batches/s, batch=2)
峰值显存3.2 MB allocated / 3.3 MB max (单卡 64GB 余量充足)
dtype / shapefloat32 / [2,512,1]→[2,96,1]
预处理计时不含 (合成生成在 CPU, 仅模型前向同步)

计时前后 torch.npu.synchronize(), 预热 3 次, 测试 10 次, 首轮与稳定分开报告。

证据图

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

agent workflow npu device call model result

  • agent_workflow.png — 侦察、环境、下载、加载、CPU/NPU 推理、一致性、性能与提交校验全流程
  • npu_device_call.png — npu-smi info, torch.npu.is_available, 设备名, 参数与输入输出 npu:0 Device 证明
  • model_result.png — 默认 python inference.py 完整输出 (schema/窗口摘要、真实预测、同步耗时、SUCCESS)

图片仅由 xterm.js 渲染本次真实日志, 非手绘或 matplotlib 模拟。

限制

  • ibm-research/test-ttm-v1 为测试权重 (3.1 MB, 2 层, d_model 192), 非生产 ibm-granite/granite-timeseries-ttm-r1 (更大上下文与多变量支持); 本仓演示单变量点预测, 多变量/概率预测需更大 TTM 变体。
  • 仅验证 context 512 horizon 96 单窗口 smoke consistency, 非完整数据集 benchmark (如 ETTh1/Weather); 更长 horizon 或不同频率需重测。
  • NPU 上 torch.ones_like 触发 allow_internal_format=False 警告, 属已知 CANN 行为, 不影响正确性且无 fallback。
  • 当前推理为 float32, 未启用 BF16/INT8 量化; 如需量化需另测精度。
  • 模型未启用 frequency token 条件化 (测试权重可接受 freq_token 但非必需), 频率敏感场景需传入正确 freq。
  • 是否替换模型: 否 (全程使用 ibm-research/test-ttm-v1 真实权重, 同权重同输入同 dtype 对比)。