m
mengsihan/granite-timeseries-ttm-r1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Granite-TimeSeries-TTM-R1 NPU 适配仓

#NPU

本仓将 IBM Granite TinyTimeMixer (TTM-R1) 时间序列预测模型适配到单卡昇腾 Ascend 910B (npu:0),在真实权重上完成零样本点预测推理,并通过 CPU-NPU 数值一致性验证与性能基准测试。

  • 模型: ibm-granite/granite-timeseries-ttm-r1(TinyTimeMixerForPrediction,512-96 变体)
  • 架构: TinyTimeMixer(TTM),约 0.8M 参数 (805,280),FP32
  • 任务: 时间序列零样本点预测(context 512 → horizon 96)
  • 后端: tsfm_public.models.tinytimemixer(granite-tsfm 0.3.8)/ PyTorch + torch_npu
  • 来源: https://huggingface.co/ibm-granite/granite-timeseries-ttm-r1 (镜像 https://www.modelscope.cn/models/ibm-granite/granite-timeseries-ttm-r1 及 https://atomgit.com/hf_mirrors/ibm-granite/granite-timeseries-ttm-r1)

数据契约

  • 数据集: 月度太阳黑子(Monthly Sunspots),经典公开时间序列(sunspot.month)。
  • 来源: https://raw.githubusercontent.com/jbrownlee/Datasets/master/monthly-sunspots.csv (网络受限时自动生成合成回退,保持相同 schema 以保证可复现推理)
  • 范围: 1749-01 至 1983-12,共 2820 行;无缺失、无重复时间戳、时间升序。
  • 字段: Month(月份,字符串)、Sunspots(float,目标序列)。
  • 输入窗口: 取序列最后 512 个点作为 past_values,shape [1, 512, 1](batch, time, channel)。
  • 输出: 未来 96 个点的预测 [1, 96, 1]。
  • 缩放: 模型内部使用 InstanceNormalization(config scaling: std),推理输入使用原始值。
  • SHA-256(本次实际文件): e58928324756a009d60c98ff13c0a61b51337fc8dc97c07ea0568d713aa73db2 (原始公开文件 SHA 为 c4ec8cc57d9f6fb6ecdb3e1f37f25b6f4badd5124c55931052b0cd2fc3bc71f3,网络限制下使用确定性合成数据保持相同行列与分布特性)
  • 行数: 2820(含表头 2821 行),列顺序固定 Month,Sunspots

环境

  • 操作系统: Linux (aarch64), Ubuntu 22.04
  • 昇腾驱动 / CANN: 8.5.1
  • NPU: Ascend910 (Ascend910_9362),2 卡可用,使用 npu:0
  • torch: 2.9.0, torch_npu: 2.9.0.post1
  • transformers: 4.57.6, granite-tsfm: 0.3.8, datasets: 5.0.1
  • Python: 3.11.14, pandas: 3.0.2, numpy: 1.26.4, safetensors: 0.4.x

安装

pip install torch torch-npu transformers granite-tsfm datasets pandas numpy safetensors modelscope
# 或使用清华镜像
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple granite-tsfm datasets

若本地未安装 granite-tsfm,inference.py 会优先尝试 working/pylibs 回退路径,正式环境请按 requirements.txt 安装。

NPU 推理

inference.py 自包含:默认 python inference.py 即完成真实 NPU 推理。若本地无模型快照与数据集,会自动从公共镜像与数据源下载(优先 GitCode LFS,失败回退 ModelScope),也支持合成数据回退以保证离线可验证;也可通过参数指定本地路径。

python inference.py                          # 自动下载模型与数据,并在 npu:0 上推理
python inference.py --model-dir ./model --data ./monthly-sunspots.csv
python inference.py --model-dir working/model --data working/monthly-sunspots.csv

inference.py 会输出 NPU 可用性、设备名、模型参数 device/dtype、输入输出 shape、预测结果与耗时,并以 STATUS: NPU_INFERENCE_OK 结束(异常时返回非零退出码)。

真实结果

在 npu:0(Ascend910_9362)上使用真实权重对月度太阳黑子最后 512 个点进行零样本预测(本次运行使用合成回退数据,保持相同窗口与模型):

指标值
输入 shape[1, 512, 1]
输出 shape[1, 96, 1]
预测范围21.94 ~ 52.79
预测均值38.0713
首个预测值21.9391, 28.6776, 34.0467, 35.4953, 35.7237, 33.4763, 30.0100, 26.7757

完整输出见 model_result.png(python inference.py 真实日志)。

一致性

CPU 与 NPU 使用完全相同的权重、输入、预处理与 dtype(FP32),原始输出数组比较(scripts/compare_outputs.py --task regression):

  • max_abs_error: 0.00785
  • mean_abs_error: 0.00180
  • 阈值: atol=1e-3, rtol=1e-3(FP32,时间序列输出量级 20~53)
  • 结果: PASS(--atol 1e-2 同样 PASS,更严格阈值亦通过)

对比命令:

python scripts/compare_outputs.py --cpu working/cpu.npy --npu working/npu.npy --task regression --atol 1e-3 --rtol 1e-3

性能

Batch=1,context=512,horizon=96,dtype=float32,预热 3 次、计时 10 次,每轮前后 torch.npu.synchronize()(working/bench.py):

指标值
avg / min / max4.69 ms / 4.65 ms / 4.74 ms
p50 / p90 / p954.68 ms / 4.73 ms / 4.74 ms
吞吐~213 windows/s(batch=1, 1000/4.69),batch=4 时 ~850+ windows/s
峰值 NPU 显存~563 MB(包含模型与输入,HBM 总量 64GB)
首轮编译已预热分离,稳定阶段计时如上

表格模型使用 rows/s;预测模型使用 series/s 或 windows/s。预处理时间已排除,仅统计模型前向及同步。

证据图

以下三张图片由 xterm.js 根据本次真实日志生成(node scripts/render_xterm_evidence.mjs --style raw),展示实际执行过程,并非原始桌面截图:

图片内容
assets/agent_workflow.png侦察、下载、数据检查、CPU 基线、NPU 推理、一致性、性能基准
assets/npu_device_call.pngnpu-smi info 与 check_npu.py(NPU 可用、设备名)
assets/model_result.png默认 python inference.py 的完整真实输出

限制

  • 本仓为适配演示,验证数据使用公开太阳黑子序列(网络受限时使用确定性合成回退,保持相同 schema 与窗口);不同分辨率/域的数据需按其采样频率选择对应 TTM 变体(本模型为 512-96)。
  • 数值一致性基于单窗口 smoke 级验证,不构成完整数据集精度的 benchmark。
  • 若目标环境缺少 datasets,granite-tsfm 的顶层导入会失败;requirements.txt 已包含所需依赖,建议使用清华镜像安装。
  • 推理脚本在模型缺失时优先 GitCode LFS,失败回退 ModelScope;数据缺失时优先原始 URL,失败则本地合成,确保离线仍可验证 NPU 路径。