Y
YYPGD/patchtst-fm-r1
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

patchtst-fm-r1 昇腾NPU适配

1. 模型简介

patchtst-fm-r1 是 IBM Research 发布的 PatchTST-FM(Patched Time-Series Transformer Foundation Model)研究版(HF,论文 arXiv:2602.06909),在保持 PatchTST 架构简洁性的基础上做了多项增强:输入/输出投影引入残差块、增加支持概率预测的分位数头(99 个分位数)、训练时采用连续 patch 推断 + 预测期随机掩码策略,并以重构损失为目标在 GiftEvalPretrain、KernelSynth 合成数据与 TSMixup 等大规模语料上预训练,在 Gift-Eval 零样本基准上达到 SOTA。推理时预测被建模为"对被掩码预测期的重构",上下文中的缺失值会被同时填补,因此天然支持零样本概率预测与同步插补。

项值
参数量~260M(核心 transformer 约 250M)
架构PatchTSTFMForPrediction(patch 化时序 transformer + 分位数输出头)
输入past_values:(batch, context_length, channels) float32 张量
输出点预测 prediction_outputs (batch, horizon, channels);分位数预测 quantile_outputs (batch, 99, horizon, channels)
关键配置context_length=8192,d_model=1024,n_layer=20,n_head=16,d_patch=16,prediction_length=64
任务类型时间序列零样本概率预测(表格/时序数据)

2. 环境依赖

依赖版本说明
Python3.10+推荐 3.11
torch>=2.0CPU/NPU 均可
torch_npu与 CANN 配套昇腾NPU支持
transformers==4.51.3模型 config 声明的配套版本,提供 PreTrainedModel 加载框架
safetensors-权重加载(model.safetensors)
einops / pandas / numpy-patchtst_fm 模型实现依赖
matplotlib-结果绘图

模型实现 patchtst_fm 来自 IBM TSFM 仓库 patchtst-fm 分支源码,已随依赖目录离线分发。

依赖安装到独立目录(不污染共享环境):

pip install --target /mnt/workspace/patchtst-fm-r1/libs \
    -i https://pypi.tuna.tsinghua.edu.cn/simple \
    "transformers==4.51.3" safetensors einops pandas numpy matplotlib

模型权重:/home/developer/models/patchtst-fm-r1

3. 推理步骤

3.1 环境准备

确认权重与依赖就位后,脚本会自动将 /mnt/workspace/patchtst-fm-r1/libs 与仓库目录加入 PYTHONPATH,无需手动设置。可用 npu-smi info 检查 NPU 设备状态。

3.2 运行推理

python3 inference.py --model_path /home/developer/models/patchtst-fm-r1

脚本内置示例输入(趋势 + 周期 + 噪声合成的 float32 时间序列,context_length=512),只传 --model_path 即可跑通;设备自动选择(优先 NPU,不可用回退 CUDA/CPU),执行零样本概率预测并打印耗时(耗时/elapsed 字段),结果图保存至 assets/model_result.png。

3.3 推理参数说明

参数类型默认值说明
--model_pathstr/home/developer/models/patchtst-fm-r1模型权重目录
--context_lengthint512输入上下文长度(不超过模型上限 8192)
--prediction_lengthint取 config(64)预测步数
--devicestr自动 (npu > cuda > cpu)指定推理设备
--outputstrassets/model_result.png结果图保存路径

4. 测试样例及输出结果

输入为内置的合成时间序列(线性趋势 + 正弦周期 + 高斯噪声,单通道 512 步,float32),经模型零样本推理得到未来 64 步的点预测(99 分位数加权均值)及 90% 预测区间。NPU 实测输出示例:

[INFO] device = npu
[INFO] model_path = /home/developer/models/patchtst-fm-r1
[INFO] 模型加载完成 load_time=...s
[INFO] 推理完成 耗时/elapsed=...s (context=512, prediction_length=64, device=npu)
[INFO] 输入形状: (1, 512, 1), 点预测输出形状: (1, 64, 1), 分位数输出形状: (1, 99, 64, 1)
[INFO] 点预测前5步(通道0): [...]
[RESULT] status=ok device=npu 耗时/elapsed=...s

结果截图见 assets/model_result.png。

5. Agent适配截图

5.1 Agent适配全过程截图

见 assets/agent_workflow.png。

5.2 NPU设备调用截图

见 assets/npu_device_call.png。

5.3 模型适配结果截图

见 assets/model_result.png。

6. 注意事项

  • 设备自动选择顺序为 NPU → CUDA → CPU,NPU 不可用时自动回退,保证脚本可移植;
  • 输入张量必须 float32(NPU 不支持 float64 算子);注意力为标准 eager/sdpa 实现,勿启用 flash_attention_2;
  • 上下文长度约束:模型 context_length 上限为 8192(含预测期长度),短序列会在内部用上下文均值前向填充并按掩码处理;脚本默认取 512 以控制显存与耗时;
  • forward 返回 PatchTSTFMPredictionOutput:prediction_outputs 为点预测,quantile_outputs 形状为 (batch, 99, horizon, channels),第 5/-5 个分位数即 5%/95% 区间边界;
  • 权重仅从本地 --model_path 加载(离线推理),如需 HF 工具类已在脚本开头设置 HF_ENDPOINT=https://hf-mirror.com;
  • transformers 必须为 4.51.x(与模型 config 声明一致),务必走 libs 隔离目录且 PYTHONPATH 优先;
  • 本仓库为 IBM 研究版(CC-BY-NC-SA-4.0,非商用),商用请改用 Apache-2.0 的 ibm-granite/granite-timeseries-patchtst-fm-r1;
  • 依赖通过 pip install --target 隔离安装到 /mnt/workspace/patchtst-fm-r1/libs,勿直接装入共享环境。