w
gcw_uQ09W7jl/ibm-granite-granite-timeseries-patchtsmixer-NPU
模型介绍
文件和版本
Pull Requests
讨论
分析

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

#NPU #Ascend #Ascend910

模型简介

  • 模型名称: ibm-granite/granite-timeseries-patchtsmixer
  • 权重 URL: https://huggingface.co/ibm-granite/granite-timeseries-patchtsmixer
  • 本地路径: working/models/granite-timeseries-patchtsmixer (revision 从下载 snapshot 固定)
  • 模型类型: patchtsmixer (TSMixer: Lightweight MLP-Mixer for Multivariate Time Series Forecasting)
  • 架构: PatchTSMixerForPrediction (transformers 5.9.0)
  • 任务: 时间序列点预测 (multivariate forecasting, point forecast)
  • 预训练数据: ETTh1 7 通道 (ETTh1 7 variates, 512 context -> 96 horizon, MSE 0.37)
  • 参数量: 196,144 (0.196M), float32, 771KB safetensors + 778KB pytorch_model.bin
  • 配置: context_length=512, prediction_length=96, num_input_channels=7, d_model=48, num_layers=2, patch_length=16, patch_stride=16, num_patches=32, expansion_factor=3, gated_attn=True, distribution_output=student_t, scaling=True (StdScaler)

数据契约

  • 输入格式: past_values [batch, context_length, num_input_channels] 即 [B, T, C],本验证 B=2, T=512, C=7, dtype float32, 轴顺序 B-T-C (batch-time-channel)
  • 输出格式: prediction_outputs [B, prediction_length, num_input_channels] 即 [2, 96, 7] float32, 同时 loc/scale [2,1,7] 来自 StdScaler
  • 预处理: PatchTSMixerStdScaler (per-series causal std, patched mixing, inverse scaling included in model), 无频率 token, 无缺失值, observed_mask 自动生成为 ones_like(past_values)
  • 示例数据: 合成数据 torch.randn seed 42, hash 3a1b5e462a04d7c4, 示例 [0,:3,:3] = [[1.9269,1.4872,0.9007],[-1.6046,-0.7521,1.6487],[-0.7688,0.7624,1.6423]], stats mean 0.0083 std 1.00
  • 真实业务: ETTh1 电力 transformer 油温预测, 7 通道多变量, 512 小时历史预测 96 小时未来, 本仓用合成随机数据验证 NPU 数值一致性, 不声称完整 ETTh1 benchmark 排名

环境

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

安装

pip install -r requirements.txt
# requirements: torch==2.9.0 torch_npu==2.9.0.post1 transformers==5.9.0 numpy==1.26.4 huggingface_hub safetensors

NPU 推理

python inference.py
# 默认加载 working/models/granite-timeseries-patchtsmixer (或自动 snapshot_download) 到 npu:0
# 打印模型/revision/route/backend/device/dtype, 输入摘要, CPU vs NPU 预测, 同步耗时, PASS/FAIL
  • 设备证明: 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
  • 加载后端: transformers from_pretrained, 本地路径优先, 无 trust_remote_code

真实结果

  • CPU: prediction_outputs [2,96,7] float32 cpu, sample [0,:3,:3] [[0.7754,-0.0928,-0.7411],[0.7455,-0.0930,-0.4561],[0.8252,-0.1361,-0.3123]], mean -0.0365 std 0.2343
  • NPU: 同形状 [2,96,7] float32 npu:0, sample [0,:3,:3] [[0.77548,-0.09286,-0.74109],[0.74558,-0.09305,-0.45616],[0.82528,-0.13613,-0.31232]], mean -0.03645, finite True, 无 NaN/Inf
  • 首轮编译: 159.33 ms (含图编译, observed_mask ones_like 动态形状首次编译)
  • 稳态: second_run 1.88 ms, benchmark avg 1.73 ms
  • 状态: [RESULT] PASS task=forecasting backend=PatchTSMixer

一致性

  • 方法: 同权重、同输入 (seed 42 合成 [2,512,7])、同预处理、同 dtype float32、同 eval mode, 分别保存 /tmp/cpu_patchtsmixer.npy /tmp/npu_patchtsmixer.npy, 使用 scripts/compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3
  • 结果: max_abs 6.628e-05 mean_abs 1.733e-05, allclose True, shape [2,96,7]==[2,96,7] finite True
  • 阈值: FP32 从 atol 1e-4 rtol 1e-3 起步, 远低于容差, 分类/概率任务不适用 Top-5, 本模型为回归/预测比较 forecast
  • 单样本 smok e: 仅 batch 2 窗口一致性, 不称为完整 ETTh1 数据集精度评测

性能

  • 计时: 每轮前后 torch.npu.synchronize(), warmup 3, 测试 10
  • 结果: compile/first_run 159.33 ms | avg 1.73 ms min 1.69 max 1.83 p50 1.73 p90 1.77 p95 1.80
  • 吞吐: 1152.79 windows/s (2 series/s * 64?) 实际 batch 2 windows/s, 报告 windows/s, 预处理 excluded (synthetic), inverse scaling included
  • 显存: allocated 1.1 MB reserved 38 MB peak 33 MB, batch 2 context 512 horizon 96 channels 7 dtype float32
  • 对比: CPU 10 runs avg 4.647 ms, NPU 约 2.7x 加速 (CPU 无 synchronize)

证据图

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

  • agent workflow
  • npu device call
  • model result

限制

  • 单卡 npu:0 即可, 资源 <10MB, 超出单卡 MoE 等模型不适用
  • 合成随机输入验证数值一致性, 非 ETTh1 完整 test 划分, 不报告 MSE/SMAPE 排名
  • 无 tokenizer, 无 remote code, 无训练, 仅推理迁移到 NPU
  • 动态 shape 首次编译后复跑一次, 自定义 op 不支持时可改写为标准 PyTorch (本模型无需改写)
  • 模型许可证 Apache-2.0, 权重遵循原 ibm-granite

仓库结构

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

许可证 Apache-2.0