任务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 通道预测。
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_channeltransformers PatchTSMixerForPrediction(论文 https://arxiv.org/pdf/2306.09364.pdf, KDD 2023)| 项 | 值 |
|---|---|
| 模型 | 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 个电力负荷通道。
torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,numpy 1.26.4,huggingface_hub 0.36.2,safetensorspip install -r requirements.txt
# 昇腾 NPU 需确保 torch 与 torch_npu 版本匹配,并已加载 Ascend 驱动/固件
# 权重已缓存在 working/model_patchtsmixer 或 models/ibm-granite-patchtsmixer,无需重复下载;缺失时自动从 HuggingFace 拉取(约 1.5 MB)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 的格式提示,已同步,不影响正确性。
以合成时序 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(无缺失)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: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。
使用相同权重(通过 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-3max_abs 6.63e-05,mean_abs 1.73e-05,atol=1e-4 rtol=1e-3 PASS,有限值 Truecompare_outputs.py 返回 0 即 PASS,已保存 working/patchtsmixer_cpu.npy 与 working/patchtsmixer_npu.npytorch.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三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成(非手工绘制,Pillow 仅用于校验 PNG),固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$,深灰背景白色等宽字体,展示真实设备与结果,说明图片由 xterm.js 渲染(非原始桌面截图):

num_input_channels/prediction_channel_indices;缺失值依赖 observed_mask,未验证大规模缺失场景。