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

Maple728/TimeMoE-50M on Ascend NPU

模型适配 · 时序预测 Foundation Model (Mixture of Experts) · 昇腾 NPU

Beta 版本头信息

{hardware NPU}

1. 简介

  • 模型来源: Maple728/TimeMoE-50M(AtomGit 镜像: https://ai.gitcode.com/hf_mirrors/Maple728/TimeMoE-50M)
  • 模型类型: Time-MoE —— 论文 Time-MoE: Billion-Scale Time Series Foundation Models with Mixture of Experts 的 50M 稠密预训练版本文档,采用 decoder-only Transformer + Sparse Mixture of Experts (MoE) 架构,面向通用时序预测 (time-series forecasting)。输入一段历史时序 [batch, context_length, input_size],自回归输出未来 horizon 步的预测值 [batch, horizon, input_size]。支持多预测步长 (horizon_lengths = [1, 8, 32, 64]),换挡预测时输出截断到目标步长。
  • 参数量: 113,352,192(113.35M)
  • 架构: 12 层 decoder-only Transformer + 8 个 MoE experts(每 token 激活 2 个)+ 多步长输出头
    • TimeMoeInputEmbedding(门控线性嵌入)→ 12 × TimeMoeDecoderLayer(MoE FFN: 8 experts 稀疏路由 + attention)→ RMSNorm → 多 horizon 输出头
    • MoE 层: 路由负载均衡损失 (load balancing auxiliary loss, λ=0.02)
  • 输入: 浮点时序张量 [batch, seq_len, input_size](默认 input_size=1,单变量),推荐 zero-normalization
  • 输出: 未来 horizon 步预测 [batch, horizon, input_size]
  • 适配状态: SUCCESS
  • 适配说明: TimeMoE-50M 为时序预测模型(非 LLM),不适用 vLLM-Ascend;本项目采用 torch_npu 在昇腾 NPU 上适配(全部算子 Linear/Attention/RMSNorm/softmax 均为 NPU 原生支持算子,零算子改造)。模型实现(modeling_time_moe.py / configuration_time_moe.py / ts_generation_mixin.py)与官方 checkpoint vendor 为自包含代码(time_moe/ 目录),去除对远程代码 (trust_remote_code) 的依赖;针对 transformers 4.40 → 4.57.6 的 API 变更做了最小兼容修补(seen_tokens → get_seq_length()、get_usable_length 移除、get_max_length 移除等)。注意力实现固定为 eager(NPU 上 sdpa/flash 走 aclnnFlashAttentionScore 会报 ERR00100)。推理采用逐步自回归 model.forward(直接调用,绕开 transformers 4.57.6 与官方 TSGenerationMixin._greedy_search 生成管线的兼容性问题)。权重键名与官方 checkpoint 完全一致,strict 加载无缺失。
  • 适配时间: 2026-08-21
  • 硬件: {hardware NPU} Ascend 910B(torch_npu 2.9.0.post1)

2. 验证环境

组件版本
torch2.9.0
torch-npu2.9.0.post1
python3.11.14
transformers4.57.6(模型代码 vendor 后仅用于 PretrainedConfig/PreTrainedModel 基类)
CANN8.5.1
NPUAscend 910B (Ascend910_9362) × 2
依赖numpy / safetensors / huggingface_hub / torch-npu

3. 精度评测 (CPU vs NPU)

同一权重(Maple728/TimeMoE-50M),demo 时序 = 正弦 + 线性趋势 + 噪声(固定 seed=42, context_length=96, horizon=48),CPU 与 NPU 均为 fp32 + eval(),zero-normalization 前后处理一致。

指标数值
余弦相似度 (cosine similarity)1.000000
最大绝对误差 (max_abs_err)1.10×10⁻⁵
平均绝对误差 (mean_abs_err)3.24×10⁻⁶

误差来自 NPU 与 CPU 上 fp32 算子累加顺序差异(~1e-5 量级),对预测精度影响可忽略,余弦相似度 1.000000,精度达标。

4. 性能评测 (NPU)

demo 时序 context_length=96 → horizon=48(自回归 48 步),预热后单次计时,NPU:0。

设备单次推理耗时
CPU (x86_64 参考)~4.73 s
NPU (Ascend 910B)~915 ms

NPU 相对 CPU 约 5.2× 加速。性能上界主要受限于 48 步自回归串行推理(逐步依赖)与多步长输出头计算;如需更高吞吐可 batch 多条序列或减短 horizon。

5. 快速开始

5.1 安装依赖

pip install -r requirements.txt

5.2 推理(默认演示)

# NPU 默认演示: context_length=96, horizon=48 (正弦+趋势+噪声 demo 数据)
python3 inference.py --device npu:0

5.3 自定义参数

# 自定义历史长度与预测步长
python3 inference.py --device npu:0 --context-length 192 --horizon 96

5.4 CPU vs NPU 精度对比

python3 inference.py --compare --context-length 96 --horizon 16

5.5 HTTP 服务化推理

python3 inference.py --device npu:0 --serve --port 8000
# 健康检查
curl http://127.0.0.1:8000/health
# 预测 (POST, 传入历史时序 values)
curl -X POST http://127.0.0.1:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"values": [1.0, 1.2, 0.9, 1.1, 1.3, 1.0], "horizon": 4}'

6. 权重获取

权重下载通道(按优先级):

  1. 本地目录 /tmp/models/TimeMoE-50M/(当前已就绪)
  2. gitcode LFS https://gitcode.com/hf_mirrors/Maple728/TimeMoE-50M.git(LFS batch API, 226.7 MB)
  3. HuggingFace 镜像 HF_ENDPOINT=https://hf-mirror.com(需代理外网可用)

7. 项目结构

{model_name}/
├── inference.py        # 推理脚本(必选)—— 演示/对比/服务化
├── README.md           # 部署说明文档(必选)
├── requirements.txt    # 环境依赖清单
├── time_moe/           # vendor 自包含模型实现(官方 checkpoint 键名一致)
│   ├── __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

8. 适配记录

  • 权重获取: gitcode LFS batch API 下载 model.safetensors (226,760,264 B, SHA256 校验一致)
  • 模型结构分析: 12 层 decoder + 8 MoE experts, horizon_lengths=[1,8,32,64], input_size=1
  • vendor 自包含 time_moe/ 包,strict 加载 0 缺失
  • transformers 4.40 → 4.57.6 API 兼容修补(Cache API: seen_tokens/get_usable_length/get_max_length)
  • NPU eager 注意力(绕开 aclnnFlashAttentionScore ERR00100)
  • CPU vs NPU 精度验证: cosine = 1.000000, max_err = 1.10e-5
  • NPU 推理验证通过, 性能 ~915 ms (horizon=48)