waylong/AutonLab-MOMENT-1-large-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

AutonLab/MOMENT-1-large — Ascend NPU 适配

NPU Ascend910 #NPU

模型简介

AutonLab/MOMENT-1-large 是 MOMENT 时序基础模型家族的 Large 版本,基于 google/flan-t5-large (T5 encoder, 24层, d_model 1024, d_ff 2816, 16 heads) 的 encoder-only 架构,权重 model.safetensors 1.39 GB、约 347.5M 参数(encoder only),支持通用的时序分析(forecasting, classification, anomaly detection 等)。本仓适配 forecasting 任务,context 512 / patch 8 / stride 8 / horizon 96,运行于 Ascend 910B (npu:0) 的真实 NPU 推理并通过 CPU 一致性验证。

  • Hugging Face: AutonLab/MOMENT-1-large
  • Revision: ca58581bc7bea2ebed4e80dc0a3e4b8b609c6ecc
  • 权重: model.safetensors 1.39 GB, ~347.5M params, FP32, MOMENTPipeline
  • 配置: config.json seq_len 512 patch_len 8 patch_stride_len 8 transformer_type encoder_only backbone flan-t5-large, t5_config d_model 1024 num_layers 24 num_heads 16 d_ff 2816
  • 任务头: ForecastingHead (d_model*num_patches -> horizon),注意官方提示仅 reconstruction 头为预训练,forecasting 头需微调但本仓以随机初始化头做 deterministic 验证(seed 123)

任务路由: 时序点预测 (forecasting) — 输入 [B, C, T] 历史窗口 → 输出 [B, C, horizon] 预测。分类/回归 logits 比较不适用,本仓以 forecast tensor 的 MAE/一致性为验证。

数据契约

  • 输入: x_enc 张量 [batch, n_channels, seq_len] + input_mask [batch, seq_len],本示例 batch=1, seq_len=512, n_channels=3,dtype float32,time 维度无显式时间戳,通道为合成随机游走
  • 输出: forecast [batch, n_channels, forecast_horizon],本例 [1,3,96],已做 RevIN denorm(与输入同量纲),finite 验证通过
  • 数据集: 合成序列(seed 42),3 通道随机游走,1200 steps 中截取 512 作为 context,SHA-256 可复算
    • 生成: rng.randn(512)*scale + base 其中 base/scale = [50/2, 35/1, 99/4],累计和 random walk,类似真实时序
    • 统计: channel_0 mean 47.49, channel_1 34.50, channel_2 175.61,无缺失,input_mask 全 1
    • 列: 无显式列名,通道顺序固定 [channel_0, channel_1, channel_2],不可调换
    • 切片: 固定 512 context → 96 horizon,窗口不越界
  • 预处理: 模型内部 RevIN 按 x_enc 与 input_mask 做 norm/denorm,外部无需额外 StandardScaler,输入原始 float32 即可
  • 轴顺序: 严格 [B,C,T],禁止 [B,T,C] 转置;缺失值通过 input_mask 标记(本例全观测)
  • 标签: 无监督预测,无 future values 仅做 point forecast,forecast_head 随机初始化故数值仅用于一致性验证,不代表官方 benchmark

环境

  • 硬件: Ascend 910B ×2 (npu:0 Ascend910_9362, npu-smi 25.5.5, CANN 8.5.1)
  • 驱动/固件: torch.npu.is_available()==True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362
  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, numpy 1.26.4, pandas 3.0.2, momentfm 0.1.4, safetensors 0.7.0
  • 依赖见 requirements.txt

安装

pip install -r requirements.txt
# momentfm 已在 PyPI: pip install momentfm --no-deps(如遇依赖冲突加 --no-deps)
# 或本地安装: pip install momentfm

NPU 推理

python inference.py

关键日志(npu:0 真实执行):

============================================================
Model: AutonLab/MOMENT-1-large (revision ca58581bc7bea2ebed4e80dc0a3e4b8b609c6ecc)
Task: time-series forecasting (MOMENT point forecast)
Backend: torch + torch_npu
Device: npu:0
Context: 512, Horizon: 96, Channels: 3
============================================================
[ENV] torch 2.9.0+cpu torch_npu 2.9.0.post1+gitee7ba04
[ENV] npu_available=True device_count=2 device_name=Ascend910_9362
...
[DEVICE] Set NPU device 0, current device 0
[VERIFY] Dummy tensor device: npu:0
[DATA] x_enc shape (1, 3, 512) dtype torch.float32 mean 85.8747
[LOAD] cfg seq_len=512 patch_len=8 backbone=google/flan-t5-large d_model=1024
[LOAD] model first param device: npu:0 dtype torch.float32
[LOAD] model param count 347.53M
[INPUT] past tensor shape (1, 3, 512) device npu:0 dtype torch.float32
[INFER] Completed in 0.1342s (sync timed)
[RESULT] Forecast tensor shape (1, 3, 96) device npu:0 dtype torch.float32 finite True
[RESULT] forecast mean 86.0615
[RESULT] channel_0 head [46.19, 49.15, 46.94] tail [48.00, 47.60, 48.56]
[RESULT] channel_1 head [33.53, 36.08, 33.80] tail [34.34, 34.92, 35.20]
[RESULT] channel_2 head [174.77, 180.54, 171.96] tail [176.54, 176.33, 179.33]
[VERIFY] Model param device after inference: npu:0
[STATUS] NPU inference SUCCESS on npu:0, elapsed 0.1342s

真实结果

  • 输入 context 均值: channel_0 47.49, channel_1 34.50, channel_2 175.61
  • 预测输出 [1,3,96] 已 RevIN denorm,量纲与输入一致,可直接业务使用:
    • channel_0 head [46.1916, 49.1527, 46.9499] tail [48.0051, 47.6051, 48.5684]
    • channel_1 head [33.5387, 36.0815, 33.8066] tail [34.3417, 34.9211, 35.2083]
    • channel_2 head [174.7733, 180.5453, 171.9651] tail [176.5493, 176.3351, 179.3373]
    • 整体均值 86.06, std 63.87, finite True
  • 无 quantile/probability 输出(MOMENT forecasting 为 point forecast,loss 为 mse,forecast_head 随机初始化)

一致性

  • 方法: 同一权重(seed 123 固定 forecasting head)、同输入、同预处理、同 dtype (FP32)、同随机种子,分别保存 CPU/NPU forecast 为 .npy,运行 compare_outputs.py --task forecasting
  • 结果: cpu shape [1,3,96] npu shape [1,3,96] finite True max_abs 4.577e-05 mean_abs 7.19e-06 allclose True (atol 1e-4 rtol 1e-3)
  • 单通道: ch0 max 1.14e-05, ch1 max 1.14e-05, ch2 max 4.57e-05,均通过
  • 结论: CPU/NPU 输出在 FP32 阈值内完全一致,无 fallback,NPU 核心 forward 已验证

性能

  • Benchmark: 预热 3 次、正式 10 次,每轮前后 torch.npu.synchronize()
  • 配置: batch 1, seq_len 512, horizon 96, channels 3, dtype FP32, device npu:0
  • 结果:
    • first run (编译) 0.1342s / 0.1777s (含 lazy compile)
    • second run 0.022-0.023s
    • avg 0.0205s min 0.0204s max 0.0207s p50 0.0204s p90 0.0207s p95 0.0207s
    • throughput 48.7 windows/s (batch 1)
    • 峰值显存: NPU HBM 3435-3450 MB (含模型 347M 参数 ~1.4GB)
  • 说明: 计时仅含模型 forward + synchronize,不含 CSV 读取/DataFrame 预处理;RevIN 与 PatchEmbedding 已在 NPU

证据图

图片由 xterm.js 根据本次真实日志生成(非截图):

  • assets/agent_workflow.png — 侦察、下载、加载、NPU 推理、一致性、benchmark 与校验的完整工作流
  • assets/npu_device_call.png — npu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入与输出 Tensor device
  • assets/model_result.png — 默认 python inference.py 的真实完整输出(含 schema/窗口摘要、真实预测、同步耗时和状态)

agent_workflow npu_device_call model_result

限制

  • forecasting 头为随机初始化(官方未提供预训练 forecasting 权重),数值仅用于一致性与 NPU 验证,非官方精度 benchmark
  • 仅验证单窗口 smoke consistency,未在完整数据集上做 MAE/RMSE 评测
  • 单卡 npu:0 推理,未验证多卡/分布式
  • 输入固定 512/96/3,改变序列长度/通道数需重新验证显存与 head 维度(head_nf = d_model * num_patches)
  • 依赖 momentfm 的 T5 encoder,部分算子在 NPU 上为 FP32 回退已记录但无 CPU fallback