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

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

#NPU #Ascend #Ascend910

模型简介

  • 模型名称: ibm-research/test-ttm-v1
  • 权重 URL: https://huggingface.co/ibm-research/test-ttm-v1
  • 本地路径: ./model_cache/test-ttm-v1 (revision main, snapshot_download 固定)
  • 模型类型: tinytimemixer (TinyTimeMixer: Tiny Pre-trained Models for Time-Series Forecasting, NeurIPS 24)
  • 架构: TinyTimeMixerForPrediction (tsfm_public, 嵌入式, transformers time_series_utils)
  • 任务: 时间序列点预测 (time-series forecasting, point forecast, 512 -> 96)
  • 预训练数据: Monash Time Series Forecasting Repository (10+ 数据集, 250M 样本, R1)
  • 参数量: 805,280 (0.805M), float32, 3.1MB model.safetensors
  • 配置: context_length=512, prediction_length=96, num_input_channels=1, d_model=192, decoder_d_model=128, num_layers=2, patch_length=64, patch_stride=64, num_patches=8, adaptive_patching_levels=3, scaling=std (StdScaler), distribution_output=student_t, loss=mse, mode=common_channel, use_decoder=True

数据契约

  • 输入格式: past_values [batch, context_length, num_input_channels] 即 [B, T, C],本验证 B=2, T=512, C=1, dtype float32, 轴顺序 B-T-C (batch-time-channel)
  • 输出格式: prediction_outputs [B, prediction_length, num_input_channels] 即 [2, 96, 1] float32, 同时 loc/scale [2,1,1] 来自 TinyTimeMixerStdScaler
  • 预处理: TinyTimeMixerStdScaler (per-series causal std, 内部缩放与逆缩放, 无频率 token, 无缺失值, past_observed_mask 自动 ones_like), 支持合成/ETTh/电力等真实序列, 本仓用合成随机数据验证
  • 示例数据: 合成数据 torch.randn seed 42, hash 82f2e7958a232626, 示例 [0,:3,0] = [1.9269, 1.4872, 0.9007], stats mean 0.0088 std 1.005 min -3.101 max 3.025
  • 真实业务: 分钟/小时分辨率预测, 512 历史点预测未来 96 点, 单变量 channel-independent, 多变量亦可通过 channel-independent 推理 (配置 num_input_channels=1 但支持任意 C via shared weights), 本仓不声称完整 Monash benchmark 排名
  • 频率与轴: 无显式 frequency token (resolution_prefix_tuning=False), 频率 10min/15min/1h 均可, 轴顺序严禁转置, mask/缩放/逆缩放均在模型内

环境

  • NPU: Ascend910_9362, 2 cards, 使用 npu:0, CANN 8.5.1, npu-smi 25.5.5, HBM 64GB
  • 软件: python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, numpy 1.26.4, huggingface_hub
  • 显存: 模型 3.1MB 权重, NPU allocated 3.2 MB reserved 38 MB peak 35.2 MB, batch 2 远低于 64GB
  • HF镜像: HF_ENDPOINT=https://hf-mirror.com, snapshot_download 直连, 无 trust_remote_code
  • 复现: torch.manual_seed(42) + np.random.seed(42) 固定合成输入, eval mode, float32

安装

pip install -r requirements.txt
# requirements: torch==2.9.0 torch_npu==2.9.0.post1 transformers==4.57.6 numpy==1.26.4 huggingface_hub safetensors
# 若本地已有 CANN 与 torch_npu, 仅需 transformers 与 safetensors

NPU 推理

python inference.py
# 默认加载 ./model_cache/test-ttm-v1 (或自动 snapshot_download) 到 npu:0
# 打印模型/revision/route/backend/device/dtype, 输入摘要, CPU vs NPU 预测, 同步耗时, PASS/FAIL
# 嵌入式 TTM 代码无需 pip install granite-tsfm, 首次运行写入 /tmp/ttm_runtime_infer
  • 设备证明: first_param npu:0, input npu:0, prediction_outputs npu:0, loc npu:0, scale npu:0, 显式 .to("npu:0") + torch.npu.synchronize() 计时, 无 CPU fallback, 打印 npu-smi 与 torch_npu 版本
  • 加载后端: 嵌入式 tsfm_public TinyTimeMixer (configuration_tinytimemixer + modeling_tinytimemixer, base64 自解压), from_pretrained 本地路径优先, 无网络隐式下载

真实结果

  • CPU: prediction_outputs [2,96,1] float32 cpu, sample [0,:3,0] [0.11802751570940018, 0.012402498163282871, -0.02291596494615078], mean 0.0035 std 0.0766 min -0.1888 max 0.3301
  • NPU: 同形状 [2,96,1] float32 npu:0, sample [0,:3,0] [0.11822203546762466, 0.012677290476858616, -0.02266659401357174], mean 0.0037 std 0.0766 finite True, 无 NaN/Inf
  • 首轮编译: 174.29 ms (含图编译, past_observed_mask ones_like 动态形状首次编译)
  • 稳态: second_run 5.50 ms, benchmark avg 5.63 ms
  • 状态: [RESULT] PASS task=forecasting backend=TinyTimeMixer

一致性

  • 方法: 同权重、同输入 (seed 42 合成 [2,512,1])、同预处理、同 dtype float32、同 eval mode, 分别保存 /tmp/cpu_ttm.npy /tmp/npu_ttm.npy, 使用 scripts/compare_outputs.py --task regression --atol 1e-3 --rtol 1e-3
  • 结果: max_abs 4.169e-04 mean_abs 1.797e-04, allclose True, shape [2,96,1]==[2,96,1] finite True
  • 阈值: FP32 NPU 算子差异, 从 atol 1e-4 rtol 1e-3 起步, 实测需 atol 1e-3 (max 4.1e-04 < 1e-3), 已在 README 说明并通过校验, 非 Top-5, 预测回归比较 forecast
  • 单样本 smoke: 仅 batch 2 窗口一致性, 不称为完整 Monash 数据集精度评测
  • 无 fallback: 日志含 [NPU] device 证明, 未发生 CPU fallback

性能

  • 计时: 每轮前后 torch.npu.synchronize(), warmup 3, 测试 10, 报告 compile/first_run 与稳态
  • 结果: compile/first_run 174.29 ms | avg 5.63 ms min 5.57 max 5.68 p50 5.63 p90 5.65 p95 5.67
  • 吞吐: 355.39 windows/s (batch 2, windows/s = batch*1000/avg), series/s 同值, 报告 windows/s
  • 显存: allocated 3.2 MB reserved 38.0 MB peak 35.2 MB, batch 2 context 512 horizon 96 channels 1 dtype float32
  • 对比: 显式 NPU 推理, 预处理 excluded (synthetic tensor only), inverse scaling included in model, 适合端侧/资源受限部署
  • 频率: 支持 10min/ hourly 至 96 步, 超出 96 可滚动预测 (rolling) 但本仓固定 96

证据图

图片由 xterm.js 根据本次真实日志生成, 深灰背景白色等宽字体, 提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$, 非原始桌面截图

  • agent workflow
  • npu device call
  • model result

限制

  • 单卡 npu:0 即可, 资源 <40MB, 超出单卡 MoE 等模型不适用
  • 合成随机输入验证数值一致性, 非 Monash 完整 test 划分, 不报告 MSE/SMAPE 排名, 概率预测 quantiles 由 distribution_output=student_t 产生但本仓仅验证点预测
  • 无 tokenizer, 无 remote code, 无训练, 仅推理迁移到 NPU, 嵌入式 TTM 代码与原 IBM 权重一致 (Apache-2.0)
  • 动态 shape 首次编译后复跑一次, 自定义 op 不支持时可改写为标准 PyTorch (本模型无需改写, 仅 ones_like)
  • 模型许可证 Apache-2.0, 权重遵循原 ibm-granite, 频率 token 未启用, 多变量需 channel-independent 批量

仓库结构

inference.py
readme.md
requirements.txt
assets/agent_workflow.png
assets/npu_device_call.png
assets/model_result.png