liuhongwei-2026/TimeMoE-200M
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TimeMoE-200M on Ascend NPU

模型适配 · 表格/时序数据预测 (Time Series Foundation Model with MoE) · 昇腾 NPU

Beta 版本头信息

{hardware NPU}

1. 简介

  • 模型来源: Maple728/TimeMoE-200M(AtomGit 镜像: https://ai.gitcode.com/hf_mirrors/Maple728/TimeMoE-200M,HuggingFace: hf-mirror.com)
  • 模型类型: TimeMoE(Time Series Foundation Model with Mixture of Experts, arxiv 2409.16040)—— 基于 MoE(Mixture of Experts)架构的时序基础模型,8 专家,12 层 decoder-only Transformer,适用于表格化/单变量时序数据的零样本预测
  • 参数量: 200,737,028(200M)
  • 权重格式: bf16 · model.safetensors(≈ 865 MB)
  • 输入/输出: (batch, seq_len, input_size=1) → (batch, seq_len, horizon_length) —— 支持多 horizon 预测(1/8/32/64)
  • 适配状态: SUCCESS
  • 适配说明: TimeMoE 为自定义 MoE 时序架构(自定义 TimeMoeConfig、TimeMoeForPrediction、无 tokenizer、非语言模型),不适用 vLLM-Ascend;本项目采用 torch_npu 直接加载 safetensors 权重在昇腾 NPU 上完成适配,并以 FastAPI 提供 HTTP 服务化推理。
  • 适配时间: 2026-08-18
  • 硬件: {hardware NPU} Ascend 910B(torch_npu 2.9.0.post1)

2. 验证环境

组件版本
torch2.9.0
torch-npu2.9.0.post1
CANN8.5.1
NPUAscend 910B (2 × 64GB HBM)
Python3.11.14
transformers4.57.6

3. 精度评测

指标数值
Cosine Similarity (NPU vs CPU)0.999983
最大绝对误差 (max_abs_err)0.15625
L2 相对误差 (rel_err)0.64%
是否满足要求满足(< 1%)✅

说明: bf16 精度下,NPU 与 CPU 的 cosine similarity 约 0.999983,L2 相对误差 0.64%(使用平滑正弦信号对比),在 bf16 数值精度预期范围内。

4. 性能

指标数值
平均延迟 (seq_len=512, horizon=1)120.3 ms
平均延迟 (seq_len=512, batch=4, horizon=1)206.4 ms
平均延迟 (seq_len=512, horizon=64)117.8 ms
峰值 HBM0.89 GB
参数量200,737,028(200M)
单卡Ascend 910B · npu:0

5. 复现步骤

5.1 安装依赖

pip install -r requirements.txt

5.2 获取模型权重

# 方式一:ModelScope
python -c "from modelscope import snapshot_download; snapshot_download('Maple728/TimeMoE-200M', local_dir='/models/TimeMoE-200M')"

# 方式二:HuggingFace (hf-mirror)
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download Maple728/TimeMoE-200M --local-dir /models/TimeMoE-200M

# 方式三:AtomGit 镜像仓
git clone https://ai.gitcode.com/hf_mirrors/Maple728/TimeMoE-200M.git /models/TimeMoE-200M

5.3 单次推理(NPU)

cd TimeMoE-200M
python3 inference.py --model-path /models/TimeMoE-200M --device npu:0 --seq-len 512 --horizon 64 --n-runs 3

5.4 精度对比(CPU vs NPU)

python3 inference.py --model-path /models/TimeMoE-200M --device npu:0 --compare

5.5 服务化推理(HTTP Service)

# 启动服务(默认 8000 端口)
python3 inference.py --model-path /models/TimeMoE-200M --device npu:0 --serve --port 8000

# 健康检查
curl -s http://127.0.0.1:8000/health
# -> {"status":"ok","device":"npu:0","model":"/models/TimeMoE-200M"}

# 时间序列预测
curl -s http://127.0.0.1:8000/v1/forecast \
  -H 'Content-Type: application/json' \
  -d '{"values": [1.0, 1.1, 1.2, 1.05, ...], "horizon": 64}'
# -> {"horizon": 64, "shape": [1, 512, 64], "values": [[...]]}

6. 项目结构

TimeMoE-200M/
├── inference.py             # 推理脚本(必选)
├── README.md                # 部署说明文档(必选)
├── requirements.txt         # 环境依赖清单(推荐)
├── time_moe/                # vendored 模型代码(必选)
│   ├── __init__.py
│   ├── configuration_time_moe.py
│   ├── modeling_time_moe.py
│   └── ts_generation_mixin.py
└── assets/                  # 截图素材文件夹(必选,用户自行截图放置)
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png

7. 适配说明

TimeMoE-200M 使用自定义代码(trust_remote_code=True),需要通过 vendored 方式将模型代码嵌入项目。适配要点:

  1. import 路径修正: 将 modeling_time_moe.py 中的相对导入(.configuration_time_moe、.ts_generation_mixin)改为基于包名的绝对导入(time_moe.configuration_time_moe、time_moe.ts_generation_mixin)
  2. 权重加载: 使用 safetensors.torch.load_file 直接加载 model.safetensors,避免 transformers from_pretrained 的 config 参数冲突
  3. NPU 兼容性: 模型代码中仅使用标准 PyTorch 算子(torch.nn.functional.linear、torch.nn.functional.softmax、torch.topk 等),与昇腾 NPU 完全兼容,无需算子替换
  4. 注意力机制: 模型使用 eager 注意力路径(_supports_sdpa=False,_supports_flash_attn_2=True 但未启用),NPU 上自动走标准实现