sheepss/Maple728-TimeMoE-200M-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Maple728/TimeMoE-200M-NPU

#NPU 时间序列预测模型 TimeMoE-200M 昇腾 NPU 适配仓

TimeMoE-200M 是基于 Mixture-of-Experts 的时间序列基础模型(Time-MoE: Billion-Scale Time Series Foundation Models with Mixture of Experts, arXiv:2409.16040)。本仓将该模型真实运行在单卡昇腾 Ascend910(npu:0)上,完成单变量多步时间序列点预测,并提供 CPU/NPU 一致性验证与性能基准。

  • 模型:Maple728/TimeMoE-200M(time_moe 架构,TimeMoeForPrediction,MoE:8 experts / top-2,200M active / 453M total 参数)
  • Revision:794591bfeb1225fdf742cec0f4c71f20c3f3b87e
  • 任务:单变量时间序列多步点预测(forecasting)
  • 后端:PyTorch + torch_npu,transformers(trust_remote_code=True 加载模型自带 modeling_time_moe.py)
  • 设备:npu:0(Ascend910,Ascend910_9362)

数据契约

使用公开可追溯的经典月度国际航空旅客数据集(Airline Passengers, Box & Jenkins, 1949-1960,公有领域),共 144 个等间隔月度观测,单变量。

  • 轴顺序:一维序列 [T]
  • context(预测起点前窗口):最后 96 个观测
  • horizon(预测长度):32 步
  • 缩放:输入除以序列绝对最大值(scale=622.0),输出乘以 scale 逆缩放回原始量纲
  • 频率:月度;数据严格按时间升序,无缺失、无重复时间戳
  • 预测窗口不越过数据边界

环境

  • 操作系统:Ubuntu 22.04(aarch64)
  • Python 3.11.14
  • PyTorch 2.9.0 + torch_npu 2.9.0(CANN 8.5.1)
  • Transformers 4.40.1(匹配模型自定义代码所需版本)
  • 单卡 Ascend910(HBM 64GB),本任务峰值显存约 1.88 GB

安装

pip install torch torch_npu transformers==4.40.1 numpy
python -c "import torch, torch_npu; print(torch.npu.is_available())"   # 应输出 True

模型权重需先下载到本地(本仓不提交权重):

from huggingface_hub import snapshot_download
snapshot_download("Maple728/TimeMoE-200M", revision="794591bfeb1225fdf742cec0f4c71f20c3f3b87e",
                  local_dir="models/TimeMoE-200M")

NPU 推理

默认运行真实 npu:0 推理,context=96、horizon=32:

python inference.py
# 或指定模型目录
TIMEMOE_MODEL_DIR=/path/to/TimeMoE-200M python inference.py

inference.py 内部检查 torch.npu.is_available(),将模型与输入 Tensor 显式迁移到 npu:0,使用 model.generate(..., max_new_tokens=32, do_sample=False) 做确定性点预测,并在计时前后调用 torch.npu.synchronize()。

真实结果(npu:0,float32)

  • 数据摘要:n=144, context=96, horizon=32, scale=622.0
  • 最后 8 个 context 值:[472.0, 535.0, 622.0, 606.0, 508.0, 461.0, 390.0, 432.0]
  • 预测前 8 步:[444.43, 435.91, 483.26, 478.44, 502.00, 554.37, 610.67, 602.27]
  • 预测后 8 步:[399.70, 387.22, 437.01, 426.71, 443.06, 490.24, 539.92, 524.30]
  • 首轮(含编译)约 0.37 s,输出与上下文衔接平滑,符合月度季节性

CPU-NPU 一致性

相同权重、相同输入、相同 dtype(float32)、相同 seed、eval 模式下分别运行 CPU 与 NPU 推理,保存原始输出数组并对比:

python scripts/compare_outputs.py --cpu cpu_forecast.npy --npu npu_forecast.npy \
  --task forecasting --atol 1e-2 --rtol 1e-2

结果:passed=true, max_abs_error=1.50e-3, mean_abs_error=6.29e-4(阈值按 float32 与输出尺度确定,见 references/validation.md)。

性能

NPU 上预热 3 次、正式测试 10 次(每次前后 torch.npu.synchronize()):

  • 平均 123.97 ms / 次,min 119.92 ms,max 130.24 ms,p50 123.36 ms,p90 126.67 ms,p95 128.46 ms
  • 吞吐约 8.07 series/s(context=96, horizon=32, batch=1)
  • 峰值显存 1876 MB;dtype=float32
  • 计时不含 CSV/DataFrame 预处理与逆缩放(本次输入为内存中的 numpy 序列)

证据图

三张证据图由 xterm.js 根据本次真实日志渲染生成(非桌面截图):

  • assets/agent_workflow.png:侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark 与提交校验全流程
  • assets/npu_device_call.png:npu-smi、torch.npu.is_available()、设备名、模型参数 device/dtype、输入输出 Tensor device
  • assets/model_result.png:默认 python inference.py 的真实完整输出(数据摘要、真实预测、同步耗时、状态)

限制

  • 权重约 906 MB(453M 参数 bf16),本仓按规范不提交权重,使用前需按上文下载。
  • 模型自定义代码(modeling_time_moe.py 等)要求 trust_remote_code=True,已在本地审查,无网络/子进程行为。
  • 数值一致性为固定窗口(96→32)的 smoke/示例级验证,非完整 benchmark 排名。
  • 目标设备为单卡 npu:0;多卡并行未验证。

License: Apache-2.0