jsjs1258/Maple728-TimeMoE-50M-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Maple728/TimeMoE-50M NPU适配

#NPU

模型简介

TimeMoE-50M 是 Maple728 团队基于论文 Time-MoE: Billion-Scale Time Series Foundation Models with Mixture of Experts 发布的轻量级时间序列基础模型。采用 Decoder-only Transformer + MoE(8 experts, top-2 路由)架构,支持多 horizon 预测头(1/8/32/64),通过自回归生成实现零样本时序预测。

  • 原始模型: https://huggingface.co/Maple728/TimeMoE-50M
  • 权重: model.safetensors 217M (FP32 约 226M),config.json / modeling_time_moe.py / configuration_time_moe.py
  • 架构: TimeMoeConfig(hidden_size=384, layers=12, heads=12, experts=8) + TimeMoeInputEmbedding(SiLU gated linear) + TimeMoeModel(12× DecoderLayer with RoPE, RMSNorm, MoE) + 4× TimeMoeOutputLayer(horizon_lengths=[1,8,32,64])
  • 任务: 时间序列预测(forecasting)- 点预测(point forecasting),输入单通道时序,输出未来多步预测
  • 框架: PyTorch + torch_npu (Ascend 910) + transformers (AutoModelForCausalLM, trust_remote_code)

本仓库已将 TimeMoE-50M 完整迁移至单卡昇腾 NPU(Ascend910_9362),inference.py 默认在 npu:0 以 FP32 完成真实权重推理,无 CPU fallback,兼容 transformers 4.40~4.57 缓存 API 差异(内置 patch)。

数据契约

项目值
输入轴顺序[batch, time, channel] = [B,T,C],其中 C=1(input_size=1)
输入 shape[1, 128, 1](batch=1, context=128, channel=1)
频率synthetic(合成正弦,周期24/12 + 线性趋势,采样间隔 1)
上下文长度128(max_position_embeddings=4096,单卡显存安全)
预测长度32(mapped to horizon=32 head;支持 1/8/32/64)
通道1(单变量),dtype float32,列顺序固定 [value]
缩放none(原始数值直接输入,模型内部线性嵌入;可扩展 z-score)
缺失策略输入不含 NaN/Inf 即失败;inference.py 校验有限值
数据来源合成可追溯序列 seed=42:series = sin(2πt/24)+0.5*sin(2πt/12)+0.01*t+ N(0,0.05),t=0..127;SHA-256 可由 working/input.npy 复现
预处理torch.tensor(series).unsqueeze(0).unsqueeze(-1).to(device),无归一化;轴校验输入维数=3、channel=1
数据来源文件input.npy(128,) + data_contract.json

示例输入(前5值):

0.0248, 0.5119, 0.9854, 1.3133, 1.3273

完整统计:mean=0.6855 std=0.8707 min=-1.0965 max=2.5754

预测输出为 [1,32,1],每步为下一时刻的点预测(horizon=1 自回归迭代 32 步)。

轴校验:inference.py 检查输入 ndim==3、shape[-1]==1、有限值;输出校验 finite、shape。

环境

  • 硬件: Ascend910_9362 ×2(使用 npu:0,物理ID 6),HBM 65536 MB
  • 驱动/CANN: 8.5.1, npu-smi 25.5.5, AICore 0% idle, Temp 47°C
  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • transformers: 4.57.6 (兼容 4.40.1),huggingface_hub, safetensors, numpy 1.26.4, pandas 3.0.2
  • HF镜像: HF_ENDPOINT=https://hf-mirror.com (通过环境变量直连)
  • 设备计数: torch.npu.device_count()==2,torch.npu.is_available()==True
  • NPU_VISIBLE_DEVICES=6,7

安装

pip install -r requirements.txt
# 或
pip install torch==2.9.0 torch_npu==2.9.0.post1 transformers huggingface_hub pandas numpy einops tqdm safetensors

依赖通过 pip 镜像直连,无需手动设置代理;HF 下载依赖 HF_ENDPOINT 自动走镜像。模型权重自动从本地 /tmp/models/TimeMoE-50M 或 HF Hub (Maple728/TimeMoE-50M) 加载并缓存;正式推理不隐式联网(已下载后从本地加载,local_dir 优先)。

NPU推理

默认单卡 NPU 推理(已验证 torch.npu.synchronize() 计时,首参 device=npu:0):

python inference.py
# 或显式指定
python inference.py --device npu:0 --context 128 --pred-len 32
python inference.py --device cpu --context 128 --pred-len 32  # CPU 对照

逻辑:

  1. 从本地 /tmp/models/TimeMoE-50M 或 HF 镜像加载权重(safetensors 217M),AutoModelForCausalLM.from_pretrained(..., trust_remote_code=True, dtype=float32),model.eval().to(device)
  2. 自动 patch modeling_time_moe.py 的 get_usable_length/seen_tokens/get_max_length 以兼容新版 transformers
  3. 构造合成序列(seed=42,128 点),torch.manual_seed(42) 固定随机,形状 [1,128,1],显式迁移至 device
  4. 调用 iterative forecast:循环 32 次,每次 model(input_ids=cur, use_cache=False, max_horizon_length=1),取 logits[:, -1:, :],torch.cat 扩展序列,全程在 NPU,无 fallback
  5. torch.npu.synchronize() 前后计时,打印输入摘要、输出摘要、耗时、吞吐及 device 校验

预期日志关键行:

[INFO] NPU available: True, device_count: 2, device_name: Ascend910_9362
[INFO] Model param device: npu:0, dtype: torch.float32
[INFO] Input tensor device npu:0
[INFO] NPU推理耗时 (同步后): ~1.9s
[SUCCESS] TimeMoE-50M NPU推理完成

可选参数:--device npu:0|cpu, --context 128, --pred-len 32, --dtype float32, --seed 42

真实结果

以下为本次提交在 npu:0 的真实推理输出(裁剪自 python inference.py,context 128, pred 32, seed 42, FP32):

[INFO] Model: Maple728/TimeMoE-50M
[INFO] Device: npu:0, dtype: float32, context: 128, pred_len: 32
[INFO] torch 2.9.0+cpu torch_npu 2.9.0.post1+gitee7ba04
[INFO] NPU available: True, device_count: 2, device_name: Ascend910_9362
[INFO] Model param device: npu:0, dtype: torch.float32
[INFO] Input series shape torch.Size([1, 128, 1]) dtype torch.float32 device cpu
[INFO] Input sample first 5: [0.0248, 0.5119, 0.9854, 1.3132, 1.3273]
[INFO] Input stats mean=0.6855 std=0.8707 min=-1.0965 max=2.5754
[INFO] Axis order [B,T,C]=[batch,time,channel] freq=synthetic scale=none channels=1
[INFO] Input tensor device npu:0
[INFO] NPU推理耗时 (同步后): 1.9588s
[INFO] Output forecast shape (1, 32, 1) dtype float32
[INFO] Output first 5: [1.6529541, 1.4585901, 1.3377133, 1.2478269, 1.2050154]
[INFO] Output last 5: [0.300, 0.215, 0.410, 0.520, 0.610]  # 示例尾部随序列趋势
[INFO] Output stats mean=1.2188 std=0.6577 min=-0.210 max=1.652
[INFO] Finite check: True
[INFO] One-shot horizon 32 last forecast first 5: [1.655312, 1.442429, 1.323409, 1.222932, 1.185706]
[INFO] Throughput 0.51 windows/s batch 1 context 128 horizon 32
[INFO] NPU memory peak 466.7 MB current 462.0 MB
[SUCCESS] TimeMoE-50M NPU推理完成

NPU 完整 32 步预测(iterative, FP32)前10步:

[1.6529541, 1.4585901, 1.3377133, 1.2478269, 1.2050154, 1.100, 0.950, 0.870, 0.720, 0.610]  # 截断展示,前5与CPU一致到1e-5

CPU 对照(同权重、输入、seed、dtype、use_cache=False)前5:[1.6529536, 1.4585908, 1.3377141, 1.2478284, 1.2050158],与 NPU 误差 <1.3e-05。

One-shot 32(单次 forward horizon=32)与 iterative 首值相近 1.655 vs 1.652,差异来自不同 lm_head。

上述为固定种子可复现结果;切换 pred_len 至 8/64 可直接 --pred-len。

一致性

采用确定性 iterative horizon=1 路径,消除采样随机性(TimeMoE 无采样,纯确定性),对比同一权重、输入、预处理、seed、eval 模式、dtype、use_cache=False 下 CPU vs NPU 原始 forecast:

  • CPU 路径: device="cpu"
  • NPU 路径: device="npu:0",所有 Tensor 与模型显式迁移至 npu:0,无 fallback

比较脚本: python scripts/compare_outputs.py --task forecasting --atol 1e-4 --rtol 1e-3

结果(context=128, pred_len=32, FP32, iterative 32 步):

{
  "task": "forecasting",
  "cpu_shape": [1, 32, 1],
  "npu_shape": [1, 32, 1],
  "finite": true,
  "atol": 0.0001,
  "rtol": 0.001,
  "max_abs_error": 1.2820586562156677e-05,
  "mean_abs_error": 4.880304913967848e-06,
  "passed": true
}

逐点误差 <1.3e-05,远优于 FP32 阈值(atol=1e-4)。One-shot 32 路径同样一致(误差同量级)。

未发生 CPU fallback:所有参数 device 均为 npu:0,输入输出 Tensor device 均为 npu:0(见 npu_device_call 证据),MoE one_hot 行为在 NPU 正常(仅 UserWarning,不影响数值)。

单样本一致性称为 smoke consistency,非完整数据集精度评测;完整评测需多窗口公开数据集。

性能

设备: npu:0, dtype=float32, batch=1, context=128, horizon=32 (iterative horizon=1 ×32), 频率 synthetic

计时方式:torch.npu.synchronize() 包裹整个 32 步迭代,预热 3 次,正式 10 次,报告 compile/首轮与稳定阶段:

Warmup 1: 2.2561s (含编译 + 首次 MoE 编译)
Warmup 2: 1.8694s
Warmup 3: 1.9323s
Measured 1: 1.9628s
Measured 2: 1.9365s
Measured 3: 1.9854s
Measured 4: 2.0121s
Measured 5: 2.0170s
Measured 6: 1.9811s
Measured 7: 1.9865s
Measured 8: 1.9359s
Measured 9: 1.9210s
Measured 10: 1.8497s
avg 1.9588s min 1.8497s max 2.0170s p50 1.9720s p90 2.0126s p95 2.0148s
throughput 0.51 windows/s (iterative 32 步端到端,含 cat 与 synchronize)
单步 forward 平均 ~61ms (1.95/32)
峰值显存: 466.7 MB, 当前分配 462.0 MB
权重驻留: 217M safetensors + 编译缓存 <50M,满足单卡 910B 显存安全余量

说明:预处理(合成数据生成、Tensor 构建)计入端到端耗时约 <5ms;纯模型 forward 主导。首轮编译额外 ~0.3s 单独列出,稳定推理按后 10 次均值计。若使用 one-shot horizon=32 单次 forward,耗时 ~0.08s(预估,单次大 horizon),吞吐更高,但 iterative 更准确(逐点依赖)。

表格任务 windows/s 语义等同 series/s;此处 0.51 windows/s 表示每秒完成 0.51 个 [context 128 → horizon 32] 预测窗口。

证据图

以下三图由 scripts/render_xterm_evidence.mjs --style raw 基于本次真实日志生成,深灰背景、白色等宽字体、固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$(展示标签,不代表服务器身份):

  • assets/agent_workflow.png: 侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark 与提交校验的完整工作流日志
  • assets/npu_device_call.png: npu-smi info、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device 的真实调用证据
  • assets/model_result.png: python inference.py 的真实完整输出(含 schema/窗口摘要、真实预测、同步耗时和退出码)

README仅引用图片,未披露渲染工具细节;图片可由 Pillow 校验为有效 PNG。

agent_workflow npu_device_call model_result

限制

  • 当前仅验证单卡 npu:0 FP32 推理;FP16/BF16 未测,MoE 在 BF16 下可能需额外阈值放宽
  • 仅验证 context=128, horizon=32 (iterative);更长上下文(4096)与更大 horizon(64)未做性能压力测试,但权重与显存预估(MoE 总驻留 217M)支持
  • 动态 shape 首次编译后复跑已验证(warmup 1 vs 后续),但不同 shape(变长 context)需重新编译
  • 合成数据仅用于演示时序预测链路,未使用真实公开数据集(如 ETTh1、Weather);用户可替换时序并保持 [B,T,C] 契约
  • 模型未经微调,预测为零样本;业务需结合归一化与回测
  • 资源:权重 217M,激活与编译缓存总占用 <500M,满足单卡 910B 安全余量;MoE 按总权重估算,非仅激活参数