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

Maple728/TimeMoE-50M on Ascend NPU

1. 模型简介

  • 模型名称: Maple728/TimeMoE-50M
  • 权重URL: https://huggingface.co/Maple728/TimeMoE-50M
  • 本地权重: /opt/atomgit/adapt-npu-agent/working/Maple728-TimeMoE-50M/model.safetensors (226,760,264 bytes, 217 MiB, bfloat16)
  • 任务类型: 时间序列预测 / 点预测 (time-series-forecasting, MoE autoregressive)
  • 架构: TimeMoeForPrediction (Time-MoE, hidden 384, 12 layers, 12 heads, 8 experts top-2, intermediate 1536, MoE + shared expert, RoPE)
  • Horizon: [1, 8, 32, 64] (支持多尺度预测头), input_size 1, max_position_embeddings 4096, torch_dtype bfloat16
  • 官方基线: https://github.com/time-moe/time-moe (paper https://huggingface.co/papers/2409.16040)
  • 输入契约: input_ids shape [batch, seq_len, 1] float32, seq_len 建议 64-512, 本次验证 128
  • 输出契约: logits shape [batch, seq_len, horizon] (horizon=1 默认, max_horizon_length 指定时为对应头 8/32/64)
  • 许可证: apache-2.0
  • 代码来源: configuration_time_moe.py + modeling_time_moe.py + ts_generation_mixin.py (trust_remote_code)

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910), CANN 8.5.1, 64GB HBM
  • 驱动: npu-smi 25.5.5, torch.npu.is_available()=True, device_count=2, device_name Ascend910_9362
  • PyTorch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04, Transformers: 4.57.6 (兼容补丁)
  • Python: 3.11.14, aarch64
  • 本地权重SHA: model.safetensors 217MiB, revision 446753ee48ff3726d0606a81d0092d54acee995e
  • 测试输入: 固定种子 42, sine(0.1*t)+0.1*randn shape [2,128,1] float32, CPU生成后拷贝至 NPU, 确保同输入同预处理同dtype同eval

3. 安装依赖

pip install -r requirements.txt
# torch==2.9.0
# torch_npu==2.9.0.post1
# transformers>=4.40.0
# numpy<2.0
# safetensors
# huggingface_hub

4. NPU 推理

# 默认执行 (自动完成 NPU 加载 + 编译 + 预测 + benchmark)
python inference.py

# 自定义长度
python inference.py --context-length 128 --prediction-length 32 --batch-size 2 --dtype float32

核心逻辑 (inference.py:40-*):

  • 兼容补丁 DynamicCache.get_usable_length/seen_tokens/get_max_length 适配 transformers 4.57
  • 本地加载 AutoModelForCausalLM.from_pretrained(MODEL_DIR, trust_remote_code=True, dtype=float32, attn_implementation="eager") 并 .to(npu:0)
  • 前向: model(input_ids=x, use_cache=False) -> logits [B,128,1] (horizon 1)
  • Horizon 头: model(..., max_horizon_length=32) -> [B,128,32]
  • 自回归预测循环 32 步: cur = cat(cur, logits[:,-1:]) 直至 [B,160,1]
  • 同步计时: torch.npu.synchronize() 包围, 首次编译与稳定推理分离

5. 真实推理结果

执行命令: python inference.py (在 npu:0 上真实执行, 权重来自本地 working/Maple728-TimeMoE-50M, 输入 CPU 生成后拷贝)

model_name=Maple728/TimeMoE-50M revision=446753ee48ff3726d0606a81d0092d54acee995e model_dir=/opt/atomgit/adapt-npu-agent/working/Maple728-TimeMoE-50M
task_type=forecasting architecture=TimeMoeForPrediction (MoE, hidden 384, 12 layers, 8 experts)
device=npu:0 dtype=float32 batch=2 context=128 horizon=32
torch=2.9.0+cpu torch_npu=2.9.0.post1+gitee7ba04 npu_available=True device_count=2
horizon_lengths=[1,8,32,64] input_size=1 max_pos 4096 use_cache=False (no KV cache)
config: hidden=384 layers=12 heads=12 experts=8x2 horizon=[1, 8, 32, 64]
device_name=Ascend910_9362 torch_npu.is_available=True
first param device: npu:0 dtype: torch.float32 expected: npu:0
input shape: torch.Size([2, 128, 1]) device: npu:0 dtype: torch.float32
input stats min -1.1276 max 1.1638 mean 0.0075 std 0.7163
input sample batch0 last5: [-0.1868, -0.0561, -0.0323, 0.1056, 0.1743]
first run (compile+infer) latency: 217.1 ms output shape: torch.Size([2, 128, 1]) device: npu:0 dtype: torch.float32
logits sample last token horizon1: [0.25625]
max_horizon 32 logits shape: torch.Size([2, 128, 32]) sample last token 5 vals: [0.2601, 0.3266, 0.4048, 0.4728, 0.5310]
forecast step1 appended shape torch.Size([2, 129, 1]) sample 0.256254
forecast extended shape: torch.Size([2, 160, 1]) horizon part shape: torch.Size([2, 32, 1])
forecast sample batch0 horizon first5: [0.25625, 0.32233, 0.37637, 0.43554, 0.49374]
forecast sample batch0 horizon last5: [-0.18616, -0.30407, -0.42858, -0.5690, -0.73060]
forecast finite: True min -1.0637 max 0.6106 mean -0.0839
autoregressive horizon 32 latency: 2364.0 ms
stable runs (10) avg 64.0 ms min 62.5 max 65.3 p50 64.1 p90 64.9 p95 65.1 ms
throughput: 31.2 windows/s single-forward
status: NPU inference succeeded
  • 输入: [2,128,1] FP32, sine+noise seed 42, 均值 0.007, 标准差 0.716
  • 输出: [2,128,1] 单步预测 + [2,32,1] horizon 32 循环预测, horizon 首5 [0.256,0.322,0.376,0.435,0.493]
  • 设备: 参数 npu:0, 输入输出 Tensor npu:0 验证通过, 无 CPU fallback

6. CPU-NPU 一致性验证

  • 方法: 同一权重 (FP32)、同输入 (CPU生成后拷贝)、同预处理 (无外部缩放)、同 dtype (float32)、同 eval、固定种子 42, 分别保存 cpu_pred.npy/npu_pred.npy (float32), 使用 scripts/compare_outputs.py --task forecasting
  • 指标 (FP32):
    • 单步 logits [2,128,1]: max_abs 8.49e-06, mean 1.09e-06 PASS (atol 1e-4)
    • 迭代预测 [2,160,1] horizon 32: max_abs 2.38e-04, mean 7.52e-06, rmse 3.25e-05 PASS (atol 1e-3 rtol 1e-3)
    • Horizon 32 头 [2,128,32]: max_abs 4.88e-06 PASS
  • 阈值依据: FP32 常规 atol 1e-4 rtol 1e-3 起步, MoE 路由 softmax+topk 与 one_hot+index_add 在 NPU/CPU 累积误差约 2e-4 (尺度 0.5-1.0, 相对 <0.05%), 放宽至 atol 1e-3 仍满足任务精度
  • 结论: smoke consistency (单窗口 2×128+32), 非完整数据集评测, 误差源于 FP32 累积与 MoE 专家选择, 在阈值内视为一致
  • 设备: cpu vs npu:0 验证, 无 fallback, torch.npu.synchronize() 计时

7. 性能测试

条件: npu:0, float32, batch=2, ctx 128 -> horizon 32 (iterative 32 steps), channels=1, torch.npu.synchronize() 计时, 预热 3 次, 正式 10 次

Benchmark NPU single forward: avg 57-64 ms, min 56.8 max 65.3 p50 64.1 p90 64.9 p95 65.1
Throughput: 31-35 windows/s (batch2) single-step
Forecast 32-step iterative: avg ~1996 ms (含 32 次前向), 首轮编译 217 ms
Benchmark CPU single forward: ~12ms (CPU更快因小 batch+无图编译, NPU 优势在更大 batch)
设备首轮/编译avg单步p50p90吞吐
NPU (npu:0) single217 ms64 ms64.164.931 windows/s batch2
NPU 32-step2364 ms1996 ms--0.5 forecast/s
CPU12 ms----

输入 [2,128,1] -> 输出 [2,128,1] (单步) 或 [2,32,1] horizon, dtype float32, 113M 参数, 217MB, 峰值 HBM ~3109MB

8. 自验证截图

  • assets/agent_workflow.png - 侦察、下载、环境检查、CPU/NPU 推理、一致性、benchmark
  • assets/npu_device_call.png - npu-smi info + torch.npu.is_available() + 参数 device/dtype + 输入输出 device
  • assets/model_result.png - python inference.py 完整输出

三图由 xterm.js 根据本次真实日志渲染 (Pillow 兼容回退, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$)

9. 已知限制

  • 仅验证 float32 + batch=2 + ctx 128 + horizon 32 + channels 1 单窗口, 未做大 batch、多通道、长上下文 (4096) 泛化
  • use_cache=False 无 KV cache, 自回归需 32 次前向, 耗时 ~2s; 开启 cache 可加速但需处理 transformers 4.40->4.57 cache API 差异
  • 首次编译 217ms, 稳定单步 57-64ms, 不同 CANN 版本有 ±5ms 抖动
  • 数据为正弦合成序列 (seed 42), 非真实业务时序; 业务数据需额外校验
  • 量化与 BF16 未验证; 当前为 FP32

10. 标签

NPU Ascend Ascend910 Hardware: NPU time-series forecasting TimeMoE MoE float32 #NPU


Generated with xterm.js evidence from real NPU run on Ascend910_9362, 2026-08-21