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

ibm-granite/granite-timeseries-ttm-r2 — 昇腾 NPU 时序预测适配

本模型仓由 NPU 适配 Agent 将 HuggingFace 模型 ibm-granite/granite-timeseries-ttm-r2 (IBM Granite 系列,TinyTimeMixer 架构)适配到单卡昇腾 Ascend910 NPU,完成真实 NPU 推理、 CPU-NPU 一致性验证与性能基准,并交付可复现的 inference.py。 技术路线:granite-tsfm(tsfm_public.models.tinytimemixer.TinyTimeMixerForPrediction)+ torch_npu, 设备固定 npu:0,NPU 不可用时显式抛错,禁止静默回退 CPU。

#NPU — 本模型仓为昇腾 Ascend NPU 适配仓(单卡 Ascend910,hardware: NPU)。 topics:NPU、Ascend、Ascend910。

模型与任务

项目内容
模型ibm-granite/granite-timeseries-ttm-r2(revision main)
架构TinyTimeMixerForPrediction(TTM R2 时序基础模型)
任务时间序列点预测(forecasting)
参数量0.80 M
上下文512 步
预测长度96 步
通道数1(单变量)
缩放std(实例归一化,模型内置)
加载后端granite-tsfm TinyTimeMixerForPrediction(transformers 未内置该架构)

数据契约

  • 来源:Monash 时间序列预测仓库(Monash Time Series Forecasting Repository)NN5 数据集, 真实 UK ATM 每日现金取款量 序列。
  • HF 数据集仓:https://huggingface.co/datasets/monash_tsf(文件 data/nn5_daily_dataset_with_missing_values.zip)。
  • 序列:series T1(起始 1996-03-18,日频,原始 791 点;16 个缺失值做线性插值,不改变量纲)。
  • 输入窗口:最近 512 个真实观测,float32,shape [512]。
  • 模型输入张量:[1, 512, 1](batch=1, context=512, channel=1),与 config.context_length=512 严格一致。
  • 轴顺序:[batch, time, channel];scaling="std" 实例归一化由模型内部完成,脚本不再额外缩放。
  • 输入 SHA-256:e8650888526d51e292a0fcb239c83ca07c1de2dc5dc3e804e72da67be519af08(512×float32 原始字节)。
  • 本仓不提交任何权重或数据集文件;真实序列以 float32 常量内嵌于 inference.py(含上述来源说明)。

环境

组件版本
OSLinux(aarch64)
Python3.11.14
PyTorch2.9.0+cpu(torch_npu 2.9.0.post1)
torch_npu2.9.0.post1+gitee7ba04
CANN8.5.1
NPUAscend910(Ascend910_9362,单卡)
transformers4.40.1
granite-tsfm0.2.23

NPU 可用性:torch.npu.is_available() == True,device_count == 2(本验证使用 npu:0)。

依赖与安装

pip install -r requirements.txt
# granite-tsfm 提供 tsfm_public.models.tinytimemixer.TinyTimeMixerForPrediction
# torch/torch-npu 需与 CANN 配套(昇腾环境通常预置)

默认命令

python inference.py
# 等价:python inference.py --model-dir <本地模型目录>(离线加载)

默认从 HuggingFace 拉取模型权重(ibm-granite/granite-timeseries-ttm-r2,revision main); 使用本地已下载权重时可用 --model-dir 指定离线目录。脚本对 NPU 可用性做硬校验,失败时以非零码退出。

真实结果(NPU 实测)

项目值
设备npu:0(Ascend910_9362)
输入NN5 T1 真实日序列,最近 512 步
输出未来 96 步预测,(1, 96, 1)
历史末值32.667
预测首值34.626
预测末值32.621
预测均值 / 标准差34.282 / 1.075
预测最小值 / 最大值32.333 / 36.548
推理耗时(首跑/编译)170.5 ms
推理耗时(稳态 avg)4.36 ms
峰值 NPU 显存3.21 MB
吞吐229.2 windows/s

prediction_outputs[0,:5,0] = [34.626, 35.278, 35.894, 36.089, 36.548] prediction_outputs[0,-5:,0] = [32.964, 32.501, 33.307, 33.645, 32.622]

CPU-NPU 一致性

同一权重、同一输入(512 步 NN5 T1)、同一 dtype(float32)、eval 模式、固定 seed 42:

指标值
任务forecasting
atol / rtol1e-4 / 1e-3
最大绝对误差0.0021
平均绝对误差0.0011
判定PASS

说明:CPU 与 NPU 输出逐元素 np.allclose(atol=1e-4, rtol=1e-3) 通过;微小误差来自 NPU 算子浮点运算差异, 属正常数值误差,不影响预测语义。比较使用原始 .npy 输出(非终端四舍五入值)。

性能

  • 首轮编译单独报告:170.5 ms(含 ACL 图编译)。
  • 预热 3 次后正式测试 10 次(每次前后 torch.npu.synchronize()):
指标值
avg4.362 ms
min / max4.348 / 4.380 ms
p50 / p90 / p954.362 / 4.375 / 4.378 ms
峰值显存3.207 MB
batch / context / horizon / channel1 / 512 / 96 / 1
dtypefloat32
吞吐229.2 windows/s

吞吐不含 CSV/DataFrame 读取与逆缩放(数据以常量内嵌,模型内置 inverse scaling 逻辑)。

证据图

三张证据图由 xterm.js 根据本次适配的真实日志(stdout/stderr、UTC 时间、退出码)渲染生成, 非手工绘制或绘图库模拟:

  • assets/agent_workflow.png — 适配工作流日志(侦察、数据准备、下载、NPU 推理、一致性、性能、校验)。
  • assets/npu_device_call.png — NPU 设备证据(npu-smi、check_npu.py、模型参数/输入/输出 Tensor 均位于 NPU)。
  • assets/model_result.png — 默认 python inference.py 的真实完整输出(真实预测值、同步耗时与状态)。

限制

  • 本仓验证基于单变量(1 通道)真实日频序列;多变量/其他频率需按 config.num_input_channels 扩展输入。
  • 点预测输出为模型确定性均值预测;TTM 亦支持概率预测(num_parallel_samples),本仓未覆盖采样输出。
  • NPU 算子差异引入约 1e-3 量级数值误差,属正常范围。
  • 首次运行包含图编译耗时(~170 ms),后续稳定约 4.4 ms。