Y
YYPGD/test-patchtsmixer
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

test-patchtsmixer 昇腾NPU适配

1. 模型简介

test-patchtsmixer 是 IBM Research 发布的 PatchTSMixer 时间序列预测模型(HF),架构源自论文 TSMixer: An All-MLP Architecture for Time Series Forecasting。它将输入时间序列切分为 patch,通过全 MLP 的 mixer 结构(patch-mixer / channel-mixer)建模时序依赖,并在预测头上输出未来多步预测值。本权重为在 ETTh1 数据集上预训练的轻量版本(7 通道输入,上下文长度 512,预测长度 96)。

项值
参数量~0.2M(轻量级)
架构PatchTSMixerForPrediction(全 MLP patch mixer)
上下文长度512(patch_length=16, stride=16, 32 patches)
预测长度96
输入多变量时间序列 (batch, context, channels),float32
输出未来 96 步预测值(student-t 分布头)
任务类型时间序列零样本预测(表格数据)

2. 环境依赖

依赖版本说明
Python3.10+推荐 3.11
torch>=2.0CPU/NPU 均可
torch_npu与 CANN 配套昇腾NPU支持
transformers==4.49.0含 PatchTSMixer 原生实现
numpy / matplotlib / safetensors-数据处理与绘图

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

pip install --target /mnt/workspace/test-patchtsmixer/libs \
    -i https://pypi.tuna.tsinghua.edu.cn/simple \
    "transformers==4.49.0" safetensors

模型权重:/home/developer/models/test-patchtsmixer

3. 推理步骤

3.1 环境准备

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

3.2 运行推理

python3 inference.py --model_path /home/developer/models/test-patchtsmixer

脚本内置合成多变量时间序列示例输入(趋势+周期+噪声,7 通道 × 512 步),只传 --model_path 即可跑通;设备自动选择(优先 NPU,不可用回退 CUDA/CPU),输出预测结果并打印耗时(耗时/elapsed 字段),结果图保存至 assets/model_result.png。

3.3 推理参数说明

参数类型默认值说明
--model_pathstr/home/developer/models/test-patchtsmixer模型权重目录
--context_lengthint512输入历史长度(不超过模型上限 512)
--devicestr自动 (npu > cuda > cpu)指定推理设备
--outputstrassets/model_result.png结果图保存路径

4. 测试样例及输出结果

输入为内置合成的 7 通道、512 步时间序列,预测未来 96 步。NPU 实测输出示例:

[INFO] device = npu
[INFO] model_path = /home/developer/models/test-patchtsmixer
[INFO] 模型加载完成 load_time=1.02s
[INFO] 推理完成 耗时/elapsed=0.15s (context=512, channels=7, pred_len=96, device=npu)
[INFO] 输入形状: (1, 512, 7), 输出形状: (1, 96, 7)
[INFO] 预测值前5步(通道0): [...]
[RESULT] status=ok device=npu 耗时/elapsed=0.15s

结果截图见 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 算子),脚本已强制转换;
  • PatchTSMixer 为纯 MLP 结构,无注意力算子,均为标准 PyTorch 算子,NPU 原生支持;
  • context_length 上限为 512,超出会自动截断;
  • 依赖通过 pip install --target 隔离安装到 /mnt/workspace/test-patchtsmixer/libs,勿直接装入共享环境。