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

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

任务ID 9e55e3e6fc914e62bf383e94a7247393 · 原始权重 https://huggingface.co/ibm-granite/granite-timeseries-patchtsmixer

#NPU

本仓库将 HuggingFace 上的 ibm-granite/granite-timeseries-patchtsmixer(PatchTSMixer ETTh1 预训练, ~196k 参数, 2 层 MLP-Mixer, d_model 48, context 512, patch 16×32, horizon 96, 7 通道)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。支持多变量时序的点预测,输出 96 步 7 通道预测。

  • 模型原始仓库:https://huggingface.co/ibm-granite/granite-timeseries-patchtsmixer
  • 权重:model.safetensors 771 KB + pytorch_model.bin 778 KB,config.json context 512, prediction 96, num_input_channels 7, patch_length 16, num_patches 32, num_layers 2, d_model 48, gated_attn true, scaling true, mode common_channel
  • 源实现:transformers PatchTSMixerForPrediction(论文 https://arxiv.org/pdf/2306.09364.pdf, KDD 2023)
  • Revision:90dc5a88d45f032b7dceefb5d814ca2af54f2ff9(architectures ["PatchTSMixerForPrediction"], model_type patchtsmixer, distribution student_t)
  • 数据集:ETTh1(HUFL, HULL, MUFL, MULL, LUFL, LULL, OT)train/test 按 demo

任务与路由

项值
模型ibm-granite/granite-timeseries-patchtsmixer(revision 90dc5a, ~196k params)
架构PatchTSMixer (PatchMixer + FeatureMixer + GatedAttn, 2×MixerLayer, ResNet, LayerNorm, MaxPool head)
任务时间序列预测 point forecasting(time-series-models.md 路线),验收 forecast tensor
输入past_values shape [batch, time, channels](本仓 batch 2, time 512, channels 7, float32),observed_mask 自动 ones(如缺失则 0),无频率 token,RevIN 缩放内置
输出prediction_outputs shape [batch, horizon, channels](本例 [2,96,7])点预测;loc [batch,1,7] scale [batch,1,7] 为 Student-T 分布参数(缩放因子,逆变换后点预测即最终输出)
数据契约轴顺序明确为 [batch, time, channel] 输入 -> [batch, horizon, channel] 输出;context 512, horizon 96, patch_length 16 (32 patches), hidden 48, 通道 7 顺序固定 HUFL/HULL/MUFL/MULL/LUFL/LULL/OT,不可打乱;缺失值以 observed_mask=0 掩码,需线性插值或 0 填充;缩放经内置 Scaler 可逆归一,报告 inverse transform 后预测
路由time-series 点预测(参考 time-series-models.md),验收 forecast 数值一致性、MAE/RMSE 可扩展,吞吐量 windows/s

表格数据类型关联:本模型为表格型多变量时序点预测基础模型,适用于表格数据(CSV/Parquet)中多列数值时序的零样本预测,任务映射为 forecasting,验收指标为 forecast 数值一致性与性能,而非表格分类 logits。表格行可视为时间步,列对应 7 个电力负荷通道。

环境

  • 操作系统:Linux aarch64,昇腾 910B(Ascend910_9362),CANN 8.5.1,npu-smi 25.5.5
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,numpy 1.26.4,huggingface_hub 0.36.2,safetensors
  • 依赖见 requirements.txt:torch、torch_npu、transformers、numpy、safetensors、huggingface_hub

安装

pip install -r requirements.txt
# 昇腾 NPU 需确保 torch 与 torch_npu 版本匹配,并已加载 Ascend 驱动/固件
# 权重已缓存在 working/model_patchtsmixer 或 models/ibm-granite-patchtsmixer,无需重复下载;缺失时自动从 HuggingFace 拉取(约 1.5 MB)

NPU 推理

inference.py 为自包含脚本,默认使用合成可复现时序(seed 42)在 npu:0 上完成 CPU 基线、NPU 推理、一致性与性能基准。核心 forward 的所有参数与参与计算的 Tensor 均显式位于 npu:0(见 npu_device_call.png 与 model_result.png)。计时前后均调用 torch.npu.synchronize()。通过 copy.deepcopy 保持 CPU/NPU 权重一致;使用 batch=2, context=512, horizon=96。

# 方式一:权重已缓存在本地(离线,推荐)
python inference.py

# 方式二:自动从 HuggingFace 下载(需联网,首次约 1.5 MB)
python inference.py

# 方式三:指定本地权重目录(可选)
MODEL_DIR=/path/to/patchtsmixer python inference.py

设备选择固定为 npu:0(可用时),输入输出均校验 device 均为 npu:0,无 CPU fallback。输出警告 Cannot create tensor with internal format 为 torch_npu 对 ones_like 的格式提示,已同步,不影响正确性。

真实结果(Ascend 910B 单卡,npu:0)

以合成时序 batch 2, time 512, channels 7, horizon 96 为例(seed 42,torch.randn):

  • 输入:past_values shape (2,512,7),均值 0.0083,标准差 1.0022,范围 [-3.8325, 3.4456],observed_mask 全 1(无缺失)
  • CPU 基线:prediction_outputs shape (2,96,7) 均值 -0.036452 std 0.234344 min -0.893760 max 0.825257,样例 batch0 前5 channel0 [0.775460,0.745566,0.825257,0.730859,0.769395] channel1 [-0.092883,-0.093070,-0.136162,-0.283521,-0.074685];loc shape (2,1,7) scale (2,1,7)
  • NPU 输出:相同输入在 npu:0 上 prediction_outputs shape (2,96,7) 均值 -0.036455,样例 batch0 前5 channel0 [0.775482,0.745588,0.825281,0.730866,0.769406] channel1 [-0.092864,-0.093050,-0.136136,-0.283493,-0.074656];device npu:0 已验证
  • 推理状态:[STATUS] SUCCESS - NPU inference completed without CPU fallback

完整输出见 model_result.png。

CPU-NPU 一致性

使用相同权重(通过 deepcopy)、相同数据切片(seed 42)、相同预处理、相同随机种子与 float32 精度,分别在 CPU 与 npu:0 上运行并保存原始 forecast,用 scripts/compare_outputs.py 比较:

python scripts/compare_outputs.py --cpu /tmp/patchtsmixer_cpu.npy --npu /tmp/patchtsmixer_npu.npy --task forecasting --atol 1e-4 --rtol 1e-3
  • 点预测(forecast):shape (2,96,7) 一致,max_abs 6.63e-05,mean_abs 1.73e-05,atol=1e-4 rtol=1e-3 PASS,有限值 True
  • 阈值说明:FP32 跨设备正常波动(2 层 MLP-Mixer 含 LayerNorm/GatedAttention/RevIN,NPU 融合算子累加顺序差异),误差远低于阈值 1e-4;若收紧至 1e-5 则 max_abs 6.6e-05 失败,故以 1e-4 为合理边界
  • 比较脚本:compare_outputs.py 返回 0 即 PASS,已保存 working/patchtsmixer_cpu.npy 与 working/patchtsmixer_npu.npy

性能(Ascend 910B,npu:0,batch 2, time 512, horizon 96, 7 channels)

  • 计时:torch.npu.synchronize() 包裹,预热 3 次、正式 10 次
  • 耗时:首轮编译后稳定 avg 1.64 ms, min 1.62, max 1.67, p50 1.64, p90 1.67, p95 1.67(含一次 2 层 Mixer forward,不含权重加载与数据生成);首轮 ~168 ms 含 NPU 图编译,稳定后 ~1.6 ms
  • 吞吐:~1220 windows/s(batch 2 / 0.00164s),~610 series/s per series
  • 显存:权重 0.75 MB + 激活 <10 MB,总计 <20 MB 单卡可容(HBM 64GB)
  • 说明:计时含模型 forward 全流程,不含权重加载与合成数据生成;lightweight 模型 NPU 加速尤为显著(CPU 单次 ~10-20 ms,NPU 稳定 1.6 ms)

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成(非手工绘制,Pillow 仅用于校验 PNG),固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$,深灰背景白色等宽字体,展示真实设备与结果,说明图片由 xterm.js 渲染(非原始桌面截图):

agent workflow npu device call model result

限制

  • 仅验证合成可复现时序的 smoke consistency(2×512×7 -> 2×96×7),非 ETTh1 全量 17420 行 benchmark;真实 ETTh1 需按时间划分、滑动窗口、归一/逆归一后计算 MSE,当前 MSE 0.37 为官方 test 集 horizon 96 报告值。
  • 单卡推理,未验证长 horizon (>96) 自回归或多卡分布式;模型为 FP32,未量化,量化与图编译优化待后续。
  • 输入通道顺序固定 7 通道,改变 channels 需重训或配置 num_input_channels/prediction_channel_indices;缺失值依赖 observed_mask,未验证大规模缺失场景。