liucu/maple728-timemoe-50m-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Maple728/TimeMoE-50M NPU

#NPU Ascend 910 适配 | 原始模型: Maple728/TimeMoE-50M | 任务: 时序预测 (Time Series Forecasting)

时间序列基础模型 Time-MoE 50M 的昇腾 NPU 单卡推理适配,支持多 horizon 预测(1,8,32,64),已在 npu:0 真实完成推理验证。

模型简介

  • 模型类型: time_moe / TimeMoeForPrediction (Decoder-only Transformer with Mixture-of-Experts)
  • 参数量: 113,352,192 (~113M, 50M 激活)
  • 配置: hidden_size=384, layers=12, heads=12, experts=8, top_k=2, intermediate=1536, max_position=4096, horizon_lengths=[1,8,32,64], input_size=1, dtype=bfloat16 (适配时使用 float32 以获得更高 CPU-NPU 一致性)
  • 权重: model.safetensors 226,760,264 bytes (约 217 MB), BF16
  • Revision: 446753ee48ff3726d0606a81d0092d54acee995e (2024-10-22)
  • Pipeline: time-series-forecasting, Apache-2.0
  • 自定义代码: configuration_time_moe.py, modeling_time_moe.py, ts_generation_mixin.py (已审查,无网络/子进程/文件写入风险;为兼容 transformers 4.57,已对 DynamicCache.get_usable_length / seen_tokens / get_max_length 打补丁)
  • 官方示例: GitHub time-moe/time-moe

数据契约

项值
任务时序预测 (forecasting)
输入 shape[B, T, C] = [2, 512, 1] (batch, time, channel)
输出 shape[B, T, H] = [2, 512, 32] (H = horizon)
轴顺序time-major, channel last, univariate (input_size=1)
频率/上下文上下文 512, 预测 horizon 32 (支持 1/8/32/64 四档)
dtypefloat32 (CPU 与 NPU 同 dtype;BF16 也可用,见精度章节)
预处理无外部归一化;合成输入 = 0.5sin(2π t/50) + 0.5randn, seed=42, 已固定
来源合成可复现数据 (无外部数据集依赖)
Schema输入 float32, 输出 float32 forecast tensor, 有限值校验通过

环境

  • Python 3.11.14
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04
  • transformers 4.57.6, numpy 1.26.4, safetensors
  • Ascend CANN 8.5.1, 驱动 npu-smi 25.5.5
  • NPU: Ascend910_9362, 2 devices, tested on npu:0, HBM 64 GB

安装

pip install -r requirements.txt

模型权重通过 HF_ENDPOINT=https://hf-mirror.com 自动拉取,无需手动设置 http_proxy。

本地已缓存时直接从 working/models/TimeMoE-50M 加载;否则从 Hub 拉取 Maple728/TimeMoE-50M (trust_remote_code=True)。

NPU 推理

python inference.py

默认行为: 加载模型到 npu:0, 构造 [2,512,1] 输入, use_cache=False, max_horizon_length=32, 预热 3 次, 正式 10 次同步计时 (torch.npu.synchronize()), 打印设备/参数/输入输出摘要/耗时/吞吐。

退出码 0 表示成功;非 0 表示异常 (无静默 CPU fallback)。

真实结果

  • 输入: batch 2, seq 512, input_size 1, mean 0.0112 std 0.6020
  • 输出: shape (2, 512, 32), device npu:0, dtype torch.float32
  • 样例 (第一序列前 5 个 horizon 值): [0.8988, 0.8556, 0.8174, 0.7780, 0.7440]
  • 统计: mean -0.006112, std 0.293764, max_abs 1.052641, 有限值校验 PASS
  • 日志: 见 assets/model_result.png 对应 python inference.py 真实 stdout

一致性

  • 策略: 同权重、同输入、同预处理、同 dtype (float32)、同随机种子 (42)、同 use_cache=False、eval 模式,分别保存 CPU 与 NPU 输出为 .npy,运行 scripts/compare_outputs.py
  • 结果 (forecasting, horizon 32):
python scripts/compare_outputs.py --cpu working/cpu_timemoe.npy --npu working/npu_timemoe.npy --task forecasting --atol 1e-4 --rtol 1e-3
=> max_abs 4.25e-06, mean_abs 5.34e-07, passed True
  • 多 horizon: 1/8/32/64 均 PASS,float32 下 max_abs 3e-06~5e-06
  • BF16 备注: 若使用 bfloat16,max_abs ~0.10, mean ~0.003, p99 0.012,需放宽至 atol 0.1 rtol 5e-2 方可 PASS,故本适配默认用 float32 以满足严格阈值;阈值已写入 README
  • 无 fallback: 全程显式 .to("npu:0"),日志打印首个参数 device 为 npu:0,无 CPU fallback

性能

  • 计时方式: 每轮前后 torch.npu.synchronize(),首轮编译与稳定推理分开报告
  • 测试: batch 2, context 512, horizon 32, dtype float32, npu:0
  • 结果 (10 次正式):
first_run 64.45ms (含首次编译后稳定)
avg 64.38ms min 64.28ms max 64.54ms p50 64.34ms p90 64.54ms p95 64.54ms
throughput 31.1 windows/s (15.5 series/s per 512 window)
  • 显存: 权重 0.43 GB (float32) + 激活 ~1.2 GB < 64 GB
  • 是否含预处理: 计时仅含模型 forward,不含数据生成

证据图

三张 PNG 由 xterm.js 根据本次真实日志生成 (scripts/render_xterm_evidence.mjs --style raw):

  • 整体工作流: assets/agent_workflow.png (对应 working/logs/workflow.log)
  • NPU 设备调用: assets/npu_device_call.png (对应 working/logs/device.log)
  • 模型推理结果: assets/model_result.png (对应 working/logs/inference.log + python inference.py)

固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签。

限制

  • 仅验证推理 (fit() 不涉及);NPU 训练未声明
  • 未使用 FlashAttention (NPU 上为 eager);allow_internel_format 警告 benign
  • 输入仅 univariate (input_size=1),multivariate 未测试
  • generate() 自回归多步展开因 transformers 4.57 Cache API 变更暂不稳定,推荐用 forward(max_horizon_length=...) 直接预测
  • 权重未提交,按需从 Hub 拉取

目录结构

.
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png