g
gcw_coj3XaOd/TimeMoE-50M
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TimeMoE-50M - 昇腾 NPU 推理部署

1. 模型简介

模型名称: Maple728/TimeMoE-50M

模型链接: HuggingFace

AtomGit 镜像: AtomGit

模型描述: TimeMoE-50M 是一个基于 Mixture-of-Experts (MoE) 架构的时间序列预测模型。模型采用 Transformer Decoder 架构,通过稀疏专家混合机制实现高效的时间序列多步预测。

模型架构: MoE Transformer Decoder

  • 12 层 Transformer Decoder
  • 8 个 MoE 专家(top-2 路由)
  • 12 注意力头,hidden_size=384
  • 总参数量:113,352,192 (~113.4M)

输入规格:

  • 类型:一维时间序列 (float32/bfloat16)
  • Shape:[batch_size, seq_len, input_size],其中 input_size=1
  • 推荐 seq_len:96

输出规格:

  • 类型:预测时间序列 (float32/bfloat16)
  • Shape:[batch_size, horizon, 1]
  • 支持的多步预测范围:horizon_lengths = [1, 8, 32, 64]

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.40.0模型配置加载(需兼容补丁)
huggingface_hub>= 0.20.0权重下载
safetensors>= 0.4.0安全张量格式加载
numpy>= 1.24.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch-npu transformers huggingface_hub safetensors numpy

注意事项:

  • 本模型使用自定义代码(modeling_time_moe.py、configuration_time_moe.py),inference.py 脚本通过 importlib 动态加载,无需安装额外包。
  • transformers >= 4.50 移除了 DynamicCache.get_usable_length() 方法,inference.py 已内置运行时兼容补丁(monkey-patch),无需降级 transformers。

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download Maple728/TimeMoE-50M --local-dir ./weights

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

huggingface-cli download --repo-type model https://ai.gitcode.com/hf_mirrors/Maple728/TimeMoE-50M --local-dir ./weights

3.3 运行推理

python3 inference.py --model-path ./weights

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--seq-lenint96输入序列长度
--deviceint0NPU 设备编号

4. 推理成功日志

4.1 单条推理日志

[模型] Maple728/TimeMoE-50M
[设备] Ascend910_9362 (npu:0)
[输入] 正弦波序列, seq_len=96, horizon=8
[输出] shape=(8, 1), 前8个预测值: [0.1206, 0.2734, 0.4141, 0.5469, 0.6680, 0.7656, 0.8594, 0.9375]
[耗时] 248.26ms (首次推理,含 KV Cache 初始化)
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 100次推理统计:
  平均: 64.53ms
  最小: 60.44ms
  最大: 69.31ms
  标准差: 2.03ms
  P50: 64.52ms
  P99: 69.16ms

5. 测试样例及输出结果

样例 1:单条时间序列预测

运行命令:

python3 inference.py --model-path ./weights --seq-len 96

输出:

[输入] 正弦波序列 (seq_len=96)
      前5个值: [0.0, 0.1319, 0.2615, 0.3865, 0.5048]
[输出] 预测 shape: (8, 1)
      预测前8个值 (horizon=1): [0.1206, 0.2734, 0.4141, 0.5469, 0.6680, 0.7656, 0.8594, 0.9375]
[耗时] 248.26ms
[状态] SUCCESS

样例 2:批量时间序列预测 (batch=3)

输出:

[输入] 3条序列, 每条 seq_len=96
[输出] 批量预测 shape: (3, 8, 1)
      序列0 前4个预测值: [0.1240, 0.2793, 0.4141, 0.5547]
      序列1 前4个预测值: [0.9844, 0.9570, 0.9336, 0.8789]
      序列2 前4个预测值: [0.1084, 0.1582, 0.2334, 0.2988]
[耗时] 84.48ms
[状态] SUCCESS

样例 3:性能基准测试 (100次)

输出:

[基准测试] 100次推理统计:
  平均: 64.53ms
  最小: 60.44ms
  最大: 69.31ms
  标准差: 2.03ms
  P50: 64.52ms
  P99: 69.16ms

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 合成正弦波序列 (seq_len=96, horizon=8),模拟典型时间序列预测场景

评测指标:

指标结果说明
参数量113.4MMoE 架构,仅激活部分专家
首次推理延迟248.26ms含 KV Cache 初始化开销
稳态推理延迟 (P50)64.52ms100次推理统计
推理延迟 (P99)69.16ms100次推理统计
硬件Ascend910_9362单卡 NPU,bfloat16 精度

评测命令:

python3 inference.py --model-path ./weights

实际输出:

[基准测试] 100次推理统计:
  平均: 64.53ms
  最小: 60.44ms
  最大: 69.31ms
  标准差: 2.03ms
  P50: 64.52ms
  P99: 69.16ms

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 可用设备(推理使用单卡 npu:0)
  • 精度: bfloat16
  • 显存占用: 模型加载约 ~450MB(bfloat16 权重 + KV Cache)
  • CANN 版本: 8.5.1
  • Tensor Parallel: 无需(模型 113M 参数,单卡即可运行)

9. 已知问题

  • transformers >= 4.50 移除了 DynamicCache.get_usable_length() 方法,inference.py 已通过 monkey-patch 修复,不影响使用。
  • 首次推理包含 KV Cache 初始化开销(~248ms),后续推理稳定在 ~65ms。
  • 模型训练时使用 transformers==4.40.1,运行环境使用 transformers==4.57.6,通过运行时兼容补丁确保功能一致。