本模型仓由 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 未内置该架构) |
data/nn5_daily_dataset_with_missing_values.zip)。T1(起始 1996-03-18,日频,原始 791 点;16 个缺失值做线性插值,不改变量纲)。float32,shape [512]。[1, 512, 1](batch=1, context=512, channel=1),与 config.context_length=512 严格一致。[batch, time, channel];scaling="std" 实例归一化由模型内部完成,脚本不再额外缩放。e8650888526d51e292a0fcb239c83ca07c1de2dc5dc3e804e72da67be519af08(512×float32 原始字节)。float32 常量内嵌于 inference.py(含上述来源说明)。| 组件 | 版本 |
|---|---|
| OS | Linux(aarch64) |
| Python | 3.11.14 |
| PyTorch | 2.9.0+cpu(torch_npu 2.9.0.post1) |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| CANN | 8.5.1 |
| NPU | Ascend910(Ascend910_9362,单卡) |
| transformers | 4.40.1 |
| granite-tsfm | 0.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: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]
同一权重、同一输入(512 步 NN5 T1)、同一 dtype(float32)、eval 模式、固定 seed 42:
| 指标 | 值 |
|---|---|
| 任务 | forecasting |
| atol / rtol | 1e-4 / 1e-3 |
| 最大绝对误差 | 0.0021 |
| 平均绝对误差 | 0.0011 |
| 判定 | PASS |
说明:CPU 与 NPU 输出逐元素
np.allclose(atol=1e-4, rtol=1e-3)通过;微小误差来自 NPU 算子浮点运算差异, 属正常数值误差,不影响预测语义。比较使用原始.npy输出(非终端四舍五入值)。
torch.npu.synchronize()):| 指标 | 值 |
|---|---|
| avg | 4.362 ms |
| min / max | 4.348 / 4.380 ms |
| p50 / p90 / p95 | 4.362 / 4.375 / 4.378 ms |
| 峰值显存 | 3.207 MB |
| batch / context / horizon / channel | 1 / 512 / 96 / 1 |
| dtype | float32 |
| 吞吐 | 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 的真实完整输出(真实预测值、同步耗时与状态)。config.num_input_channels 扩展输入。num_parallel_samples),本仓未覆盖采样输出。