#NPU
本仓库将 HuggingFace 上的 ibm-granite/granite-timeseries-patchtst-fm-r1(PatchTST-FM 时间序列基础预测模型,~260M 参数,context 8192, patch 16, 20 层 Transformer,1024 d_model,99 分位数概率预测)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。支持单变量/多变量时序的零样本概率预测,输出 99 个分位数与积分得到的点预测(中位/均值)。
model.safetensors 约 984 MB,config.json context_length 8192, prediction_length 64, d_patch 16, d_model 1024, n_layer 20, n_head 16, num_quantile 99tsfm_public.models.patchtst_fm.PatchTSTFMForPrediction, 论文 https://arxiv.org/abs/2602.06909)| 项 | 值 |
|---|---|
| 模型 | ibm-granite/granite-timeseries-patchtst-fm-r1(revision main, ~257.9M Total, 251.9M Transformer) |
| 架构 | PatchTSTFMModel (ResidualBlock in/out + LearnedPositionalEmbedding + 20×TransformerBlock + RevIN) + Quantile head |
| 任务 | 时间序列预测 probabilistic forecasting(99 分位数 + trapz 积分点预测) |
| 输入 | past_values shape [batch, time, channels](本仓 batch 2, time 512, channels 1, float32),prediction_length=64,past_observed_mask 自动由 NaN 推断 |
| 输出 | prediction_outputs shape [batch, horizon, channels](本例 [2,64,1])点预测;quantile_outputs shape [batch, num_quantile, horizon, channels](本例 [2,99,64,1])分位数 |
| 数据契约 | 轴顺序明确为 [batch, time, channel] 输入 -> [batch, quantile, horizon, channel] 输出;context 512(max 8192, 超长将 pruning 左截断),horizon 64(可变,需满足 ctx+horizon≤8192),patch_len 16(n_patch 512),频率隐式(RevIN 归一),缺失值以 NaN 填充后 nan_to_num,均值填充左 pad,缩放经 RevIN 可逆 |
| 路由 | time-series 概率预测(参考 time-series-models.md),验收 quantile monotonic 与 point forecast 一致性 |
表格数据类型关联:本模型为表格型时序概率预测基础模型,试用于表格数据(CSV/Parquet)中多列数值时序的零样本预测,任务映射为 forecasting,验收指标为 quantile monotonic、forecast 数值一致性与性能,而非表格分类 logits。
torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,einops 0.8.2,safetensors,datasets,huggingface_hubpip install -r requirements.txt
# 昇腾 NPU 需确保 torch 与 torch_npu 版本匹配,并已加载 Ascend 驱动/固件
# 权重已缓存在 /opt/atomgit/adapt-npu-agent/models/patchtst-fm-r1,无需重复下载;缺失时自动从 HuggingFace 拉取(约 984 MB)
# 需克隆 granite-tsfm 以提供 tsfm_public:git clone https://github.com/ibm-granite/granite-tsfm.git /tmp/granite/granite-tsfm
# inference.py 已自动处理 sys.path 注入,无需额外配置inference.py 为自包含脚本,默认使用合成可复现时序(seed 0)在 npu:0 上完成 CPU 基线、NPU 推理、一致性与性能基准。核心 forward 的所有参数与参与计算的 Tensor 均显式位于 npu:0(见 npu_device_call.png 与 model_result.png)。计时前后均调用 torch.npu.synchronize()。通过 copy.deepcopy 保持 CPU/NPU 权重一致,避免随机头差异;使用 use_pruning=True 自动裁剪左 pad 以减少计算。
# 方式一:权重已缓存在本地(离线,推荐)
python inference.py
# 方式二:自动从 HuggingFace 下载(需联网,首次约 984 MB)
python inference.py
# 方式三:指定本地权重目录
MODEL_DIR=/path/to/patchtst-fm-r1 python inference.py设备选择固定为 npu:0(可用时),输入输出均校验 device 均为 npu:0,无 CPU fallback。
以合成时序 ctx=512, batch=2, n_channels=1, horizon=64, 99 quantiles 为例(seed 0,torch.randn):
past_values shape (2, 512, 1),均值 0.0322,标准差 1.0280,范围 [-3.1537, 4.1015]prediction_outputs shape (2,64,1) 均值 -0.058892,标准差 0.015847,最值 [-0.092544, -0.024626],样例 batch0 前5 horizon [-0.03454151004552841, -0.04810812324285507, -0.04698144644498825, -0.050454989075660706, -0.026575498282909393];quantile_outputs shape (2,99,64,1) 均值 -0.060398,horizon0 前5 quantiles [-2.321880340576172, -2.226245641708374, -2.2262234687805176, -1.8621621131896973, -1.86213219165802],单调性 Truenpu:0 上 prediction_outputs shape (2,64,1) 均值 -0.058893,标准差 0.015847,最值 [-0.092544, -0.024626],样例 batch0 前5 [-0.03454124554991722, -0.04811039939522743, -0.04698120057582855, -0.05045577883720398, -0.026575423777103424];quantile_outputs shape (2,99,64,1) 均值相同,horizon0 10%..90% quantiles [-1.33348, -0.91591, -0.54415, -0.31300, -0.01478, 0.26524, 0.52249, 0.86105, 1.27484],单调性 True[STATUS] SUCCESS - NPU inference completed without CPU fallback完整输出见 model_result.png。
使用相同权重(通过 deepcopy)、相同数据切片(seed 0)、相同预处理、相同随机种子与 float32 精度,分别在 CPU 与 npu:0 上运行并保存原始输出,用 scripts/compare_outputs.py 比较:
python scripts/compare_outputs.py --cpu /tmp/patchtst_cpu_pred.npy --npu /tmp/patchtst_npu_pred.npy --task forecasting --atol 1e-4 --rtol 1e-3
python scripts/compare_outputs.py --cpu /tmp/patchtst_cpu_quant.npy --npu /tmp/patchtst_npu_quant.npy --task forecasting --atol 1e-4 --rtol 1e-3max_abs 2.28e-06,mean_abs 6.28e-07,atol=1e-4 rtol=1e-3 PASS,有限值 Truemax_abs 6.68e-06,mean_abs 7.42e-07,PASS,有限值 True,单调性 CPU True / NPU Trueatol=1e-4 rtol=1e-3(FP32 跨硬件正常波动,Transformer 含 LayerNorm/GELU/RevIN,误差远低于阈值)结果:PASS。数值差异在 fp32 跨硬件正常波动范围内。
在 npu:0、float32 下,预热 3 次后正式计时 10 次(batch=2, ctx=512, horizon=64, n_channels=1, 99 quantiles):
| 指标 | 值 |
|---|---|
| compile/first-run | 260.75 ms(首次 NPU 编译含图编译) / 11.75 ms(首轮计时 after warmup) |
| avg / min / max | 11.66 / 11.59 / 11.75 ms |
| p50 / p90 / p95 | 11.65 / 11.73 / 11.74 ms |
| throughput | 171.47 windows/s(= batch / avg_time) |
| batch / ctx / horizon / channels / quantiles / dtype | 2 / 512 / 64 / 1 / 99 / float32 |
| 权重显存 | 约 983.8 MB(model.safetensors 984 MB, 257.9M params) |
| 峰值 NPU 显存 | via npu-smi ~ 3131 MB(空载)-> 推理期间见 device log,无 OOM |
计时均含 torch.npu.synchronize() 前后同步,不含 CSV 读取/预处理(合成数据生成 <1ms)。
三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成,深灰背景、等宽字体,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$:
assets/agent_workflow.png:侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark 和提交校验assets/npu_device_call.png:npu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入与输出 Tensor deviceassets/model_result.png:默认 python inference.py 的真实完整输出,包括 schema/窗口摘要、真实预测、同步耗时和状态README 仅引用图片,不重复渲染工具细节;图片由 xterm.js 根据真实日志生成,非手工模拟。
past_observed_mask 由 NaN 自动推断,需自行处理分类/文本列npu:0),训练仍需 GPU/CPU;未测试多卡/量化/编译缓存持久化cc-by-nc-sa-4.0(研究版),商用请使用 ibm-granite/granite-timeseries-patchtst-fm-r1 Apache-2.0 许可版本inference.py/README),上游 granite-tsfm 按原仓许可