liucu/ibm-research-test-patchtsmixer-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

#NPU

ibm-research/test-patchtsmixer — Ascend NPU 适配

模型简介

  • 模型名称: ibm-research/test-patchtsmixer (PatchTSMixerForPrediction, 2层, d_model=48)
  • 权重URL: https://huggingface.co/ibm-research/test-patchtsmixer
  • Revision: 929deb5554d0dc787505243f5372463b241f0c68 (blobs 789KB model.safetensors, 196K params)
  • 架构: PatchTSMixer — patchify (patch_length=16, stride=16, num_patches=32) -> StdScaler (scaling=True) -> MLP-Mixer encoder 2层 (patch_mixer + feature_mixer with gated_attn=True, expansion_factor=3, d_model=48, norm LayerNorm) -> head max_pool -> Linear 1536->96 -> point forecast
  • 任务: 时间序列点预测 (direct multi-step forecast, 非自回归), context 512 -> horizon 96
  • 通道: 7 输入通道 (num_input_channels=7, num_targets=3 名义但实际输出 7 通道全部预测 [B,96,7]), mode=common_channel
  • 权重: 789KB float32 safetensors (35 tensors)
  • 框架: HuggingFace transformers PatchTSMixerForPrediction.from_pretrained + torch_npu, 无 remote code
  • 许可证: apache-2.0, pipeline time-series-forecasting

注意: 本仓库为 test-patchtsmixer 官方测试 checkpoint (README 说明正式模型为 https://huggingface.co/ibm-granite/granite-timeseries-patchtsmixer),权重与 granite-timeseries-patchtsmixer 结构一致 (d_model 48, 2 layers),已在单卡 npu:0 验证真实权重推理。

数据契约

项值
任务forecasting (点预测)
架构族time-series PatchTSMixer
加载后端transformers PatchTSMixerForPrediction
dtypefloat32
context_length512
prediction_length96
num_input_channels7
num_targets3 (配置名义,实际输出 7 通道)
patch_length / patch_stride16 / 16
num_patches32
scalingtrue (PatchTSMixerStdScaler per-channel, 模型内部)
modecommon_channel
head_aggregationmax_pool
input_shape[B, T=512, C=7] float32 past_values + [B, T, C] observed_mask
output_shape[B, H=96, C=7] forecast tensor prediction_outputs + loc [B,1,7]
频率无显式 freq token; 依赖内部 patch 位置
缺失observed_mask 全 True, 无插值
验证策略CPU vs NPU forecast tensor 对比 (forecasting)
输入示例确定性合成 (seed 42): t=0..511, freq=0.02*(c+1), value=sin(2π freq t + b*0.5) + 0.1 N(0,1) + c*0.1, batch=2, SHA256 2ce714834dabe91c (前16)
预处理无外部归一化; 缩放在模型内部; 输入为 raw float32
轴顺序严格 [B,T,C] (禁止转置); 内部 patchify 为 [B,C,num_patches,patch_length]

环境

  • 设备: Ascend910_9362 (npu:0), driver 25.5.5, 2 NPUs (0000:0B:00.0, 0000:0A:00.0)
  • CANN: /usr/local/Ascend/cann-8.5.1
  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, huggingface_hub 0.36.2, numpy 1.26.4, safetensors
  • torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)==Ascend910_9362
  • HF_ENDPOINT=https://hf-mirror.com (国内镜像, 无手动 http_proxy 覆盖)
  • 加载: snapshot_download 或本地 working/models/test-patchtsmixer -> PatchTSMixerForPrediction.from_pretrained -> model.to("npu:0"), 所有 Tensor 显式 to(npu:0), 校验首参数 device

安装

pip install -r requirements.txt
# 首次运行自动从 HF 镜像下载权重 (若本地不存在), 缓存至 HF hub 或 /tmp/test-patchtsmixer_download

依赖见 requirements.txt: torch, torch_npu, transformers, safetensors, huggingface_hub, numpy.

NPU 推理

默认命令完成真实 NPU 推理、同步计时与校验:

python inference.py

流程:

  1. 校验 torch.npu.is_available() 并锁定 npu:0.
  2. 从本地 working/models/test-patchtsmixer 或 HF 缓存加载同权重 PatchTSMixerForPrediction.
  3. 移至 npu:0 并断言首参数 device==npu:0 (无 CPU fallback).
  4. 生成确定性 past_values [2,512,7] + observed_mask 并移至 npu:0.
  5. 首轮 warmup (compile) 用 torch.npu.synchronize() 计时.
  6. 3 次预热 + 10 次同步测量, 报告 compile、avg/min/max/p50/p90/p95 与吞吐.
  7. 打印输入摘要、输出 prediction_outputs [2,96,7] 预览、统计、SHA256、device 校验与 SUCCESS.

无 CPU fallback: 输出 tensor device 校验为 npu:0, 无固定 device="cpu".

真实结果

在 npu:0 (batch=2, context 512, horizon 96, 7ch, seed 42) 执行 python inference.py:

[INFO] model_name=ibm-research/test-patchtsmixer revision=929deb5554d0dc787505243f5372463b241f0c68
[INFO] config context_length=512 prediction_length=96 num_input_channels=7 d_model=48 num_layers=2 patch_length=16 num_patches=32 num_targets=3
[INFO] past_values shape (2, 512, 7) device cpu
[INFO] input preview ch0 first5 [0.049671, 0.111506, 0.313458, 0.520427, 0.458338]
[INFO] input stats mean 0.306118 std 0.739892 min -1.193994 max 1.843679
[INFO] input SHA256 2ce714834dabe91c
[INFO] input device npu:0
[INFO] warmup done compile/first-run 169.36 ms
[CHECK] first param device npu:0 output device npu:0 fallback_detected False
[RESULT] output prediction_outputs shape (2, 96, 7) device npu:0 dtype torch.float32
[RESULT] loc shape (2, 1, 7)
[RESULT] output preview first window first3 steps first3 channels:
[[ 0.62826025  0.24495828 -0.47008938]
 [ 0.5110709   0.15090051 -0.40747142]
 [ 0.39897928  0.06587921 -0.2994752 ]]
[RESULT] output stats mean 0.296376 std 0.270607 min -0.470089 max 1.293187
[RESULT] output SHA256 e6a295a4d5c32bdb
[PERF] compile/first-run 169.36 ms
[PERF] stable 10 runs avg 1.64 ms min 1.57 max 1.78 p50 1.64 p90 1.68 p95 1.73
[PERF] throughput 1219.89 windows/s
[SUCCESS] NPU inference completed on npu:0

loc 同步返回 [2,1,7].

一致性

同权重、同输入、预处理、dtype、seed 下 CPU vs NPU forecast 对比:

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_test_patchtsmixer.npy --npu working/npu_test_patchtsmixer.npy --task forecasting --atol 1e-4 --rtol 1e-3

输出:

{
  "task": "forecasting",
  "cpu_shape": [2, 96, 7],
  "npu_shape": [2, 96, 7],
  "finite": true,
  "atol": 1e-04, "rtol": 1e-03,
  "max_abs_error": 5.03e-05,
  "mean_abs_error": 1.46e-05,
  "passed": true
}

阈值 atol=1e-4, rtol=1e-3 适用于 FP32 PatchTSMixer (LayerNorm + MLP + Gated Attention + Linear head), 实测 max_abs 5.03e-05 < 阈值, 无 fallback, shape 一致.

性能

  • compile/first-run: ~167-169 ms (首次图编译, 含 HBM 分配)
  • stable: 预热 3, 测量 10, 每轮前后 torch.npu.synchronize()
  • avg 1.60-1.64 ms, min 1.55, max 1.78, p50 1.57-1.64, p90 1.68, p95 1.73 (batch 2, float32)
  • 吞吐 ~1219-1253 windows/s (约 609-626 series/s)
  • 峰值 NPU 显存 1043456 bytes (权重 789KB + 激活)
  • batch=2, context=512, horizon=96, channels=7, dtype=float32
  • 预处理不计入 (仅模型前向计时, scaler 在模型内部)

证据图

三张 PNG 由 xterm.js 基于本次真实日志生成 (raw 风格, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$), 仅展示, 非手绘:

agent workflow npu device call model result

Manifest working/evidence_test_patchtsmixer.json 关联每张图与其源日志、命令、exit_code 与 UTC 时间.

限制

  • 仅点预测; distribution_output=student_t 的概率采样 (num_parallel_samples=100) 未在本次验证采样路径.
  • 仅合成数据 (seed 42) 验证; 未捆绑 ETTh1/ETTm1 真实数据集.
  • 仅 FP32; FP16/BF16 未测.
  • 仅 batch 2; 更大 batch 未做 OOM 测试.
  • 单卡 npu:0; 多卡未用.
  • 测试 checkpoint (test-patchtsmixer) 非正式基线; 正式大规模权重请见 granite-timeseries-patchtsmixer.