waylong/ibm-granite-granite-timeseries-patchtsmixer-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-granite/granite-timeseries-patchtsmixer — Ascend NPU 适配

#NPU

模型简介

  • 模型名称: ibm-granite/granite-timeseries-patchtsmixer
  • 权重 URL: https://huggingface.co/ibm-granite/granite-timeseries-patchtsmixer
  • Revision: 90dc5a88d45f032b7dceefb5d814ca2af54f2ff9
  • 架构: PatchTSMixerForPrediction — Lightweight MLP-Mixer for multivariate time series (Paper: TSMixer KDD 2023)
  • 参数量: 196,144 (约 0.2M) — 超轻量,单卡 Ascend 910B 可轻松运行
  • 配置: context_length=512, prediction_length=96, num_input_channels=7, patch_length=16, patch_stride=16, num_patches=32, d_model=48, num_layers=2, gated_attn=True, expansion_factor=3
  • 任务: 时间序列点预测 (ETTh1 7通道 -> 96步预测),支持 ETTh1 同构数据集 HUFL, HULL, MUFL, MULL, LUFL, LULL, OT
  • 输出: prediction_outputs shape [B, 96, 7] float32,需配合 PatchTSMixerStdScaler 做缩放 (本推理已包含自动缩放)
  • Pipeline Tag: time-series-forecasting
  • License: Apache-2.0

模型由 IBM 在 ETTh1 train split 上预训练,MSE 0.37 (test split, horizon 96, context 512)。适配保持原始权重、dtype float32、官方配置不变。

数据契约

  • 输入: past_values Tensor [batch, 512, 7] float32,轴顺序 [B,T,C],频率小时级 (ETTh1 1h),通道顺序固定 HUFL, HULL, MUFL, MULL, LUFL, LULL, OT
  • 缩放: scaling=True — 模型内部 PatchTSMixerStdScaler 自动对 context 做标准化,预测后逆变换
  • Context / Horizon: 512 -> 96 (不可变,由 config 决定,patch_last=True)
  • Batch: 默认 1,可扩展至更大 batch (显存 <2MB,极轻量)
  • 合成验证数据: seed=42 随机正态 + 每通道 trend=linspace(-0.5,0.5)*0.2*(ch+1) + season=sin(4π + ch*0.5)*0.3,模拟 ETTh1 趋势+季节性,保证可复现
  • 真实数据: 如使用真实 ETTh1 CSV,需按 demo 做 Train/Val/Test 划分并做同样标准化;推理时直接传入 [B,512,7] 即可
  • 缺失值: 模型 observed_mask 自动生成 (全1),暂不支持显式 mask 输入
  • 一致性输入: CPU 与 NPU 使用同一随机种子、同一 past_values、同一 float32、同一权重

环境

  • 硬件: Ascend 910B (Ascend910_9362), 2 devices (ID 10,11), 单卡 npu:0 推理
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, cann 8.5.1, npu-smi 25.5.5
  • NPU 验证: torch.npu.is_available()==True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362, npu-smi info 健康 OK
  • 权重: model.safetensors 771KB + pytorch_model.bin 778KB, 总 1.59MB + config

安装

pip install -r requirements.txt
# requirements: torch==2.9.0 torch-npu==2.9.0.post1 transformers==4.57.6 numpy==1.26.4 pandas==2.3.3 safetensors huggingface-hub
# Ascend CANN 8.5.1 已在环境中 /usr/local/Ascend/cann-8.5.1

模型权重首次运行自动从 HuggingFace 下载 (镜像 https://hf-mirror.com 或官方 https://huggingface.co),已缓存至 /tmp/granite-timeseries-patchtsmixer 时优先本地加载。

NPU 推理

python inference.py

默认行为:

  1. 打印环境 torch/torch_npu、NPU 可用性、npu-smi、dummy tensor npu:0 验证
  2. 从本地或 Hub 加载 PatchTSMixerForPrediction 并 .to("npu:0").eval()
  3. 生成确定性输入 [1,512,7] 并迁移至 npu:0
  4. 同时跑 CPU 分支做一致性对照
  5. torch.npu.synchronize() 计时首轮编译+推理与 10 次稳定均值
  6. 输出 forecast 摘要、一致性指标、性能、三图引用

预期输出 (真实 NPU 日志摘要):

[LOAD] model first param device: npu:0
[DATA] past_values shape (1, 512, 7) ...
[RESULT] prediction_outputs shape (1, 96, 7) device npu:0
[RESULT] forecast[0,0,:3] [0.8199, 0.1439, -0.2987]
[CONSISTENCY] CPU-NPU max_abs 0.000045 ...
[PERF] stable avg 2.16 ms windows/s 462.8
[DONE] NPU inference completed successfully device=npu:0

真实结果

  • NPU 设备: npu:0 Ascend910_9362, dummy npu:0 验证通过, 首参 device=npu:0
  • 输入: [1,512,7] float32, mean 0.0066, 示例 [1.8269, 1.4311, 0.8531, -2.2062, 0.4512, -1.655, -0.7007]
  • 输出: [1,96,7] float32, mean 0.3308, std 0.2738, finite True
    • channel 0 first 5: ['0.8199','0.7798','0.8479','0.7414','0.7713']
    • channel 1 first 5: ['0.1439','0.1252','0.0625','-0.0984','0.0980']
    • forecast[0,0,:3] [0.81988, 0.14389, -0.29871]
    • forecast[0,95,:3] [0.37396, 0.00582, 0.22741]
  • 首轮编译+推理: 82.35 ms (含 Ascend 编译)
  • 无 CPU fallback: 全程 npu:0, torch.npu.synchronize() 同步计时

一致性

  • 方法: 同一权重、同一输入、同一预处理、float32、seed 42, 分别保存 CPU/NPU prediction_outputs 为 .npy 并用 scripts/compare_outputs.py --task forecasting
  • 阈值: atol=1e-4 rtol=1e-3 (float32 点预测)
  • 结果: max_abs_error=0.000045, mean_abs_error=0.000013, allclose=True, finite=True, shape [1,96,7] 一致
  • 命令: python scripts/compare_outputs.py --cpu cpu_patchtsmixer.npy --npu npu_patchtsmixer.npy --task forecasting --atol 1e-4 --rtol 1e-3

报告为单窗口 smoke consistency,非完整 ETTh1 benchmark;完整 benchmark 需在 ETTh1 test split 上计算 MSE。

性能

  • 首轮 (compile+forward): 82.35 ms
  • 稳定阶段: warmup 3, 测试 10 次
    • avg 2.16 ms, min 2.12 ms, max 2.20 ms, p50 2.16 ms, p90 2.20 ms, p95 2.20 ms
    • 吞吐 462.8 windows/s (batch 1, context 512, horizon 96, channels 7, float32)
    • 不含预处理计时,仅模型 forward + 同步
  • 显存: allocated 0.87 MB, peak 32.82 MB (含权重与激活), 编译缓存外
  • Batch: 1 推理极轻量,可线性扩展至更大 batch

证据图

三图由 xterm.js 根据本次真实日志生成 (深灰 #282828, 提示 atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • agent_workflow — 侦察、下载、加载、NPU 推理、一致性、benchmark、校验
  • npu_device_call — npu-smi info、NPU 可用性、dummy npu:0、首参 device/dtype、输入/输出 device
  • model_result — python inference.py 完整真实输出 (含 forecast 摘要)

图片由 node scripts/render_xterm_evidence.mjs --style raw 生成,非手工绘制。

限制

  • 固定 context 512 -> horizon 96, 7 channels, 不支持变长或变通道;需 ETTh1 同构数据或合成数据
  • 仅点预测,不输出概率分布/分位数 (distribution_output=student_t 但当前 head 输出点预测)
  • 仅 NPU 推理已验证,训练仍在 GPU/CPU;不声明训练已完成 NPU 适配
  • 输入需已标准化或依赖模型内 scaler;异常值可能影响精度
  • 超轻量模型,无需分布式,单卡 npu:0 即可