liucu/AutonLab-MOMENT-1-base-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

AutonLab/MOMENT-1-base NPU on Ascend 910 #NPU

模型简介

  • 模型名称: AutonLab/MOMENT-1-base
  • 权重地址: https://huggingface.co/AutonLab/MOMENT-1-base
  • 任务类型: 时间序列重构 (reconstruction) / 通用时序基础模型,支持 forecasting / classification / anomaly detection / imputation
  • 架构: MOMENT encoder_only,Transformer backbone google/flan-t5-base (T5, d_model=768, d_ff=2048, 12 encoder layers + 12 decoder layers, 12 heads, vocab 32128), patch_len=8, patch_stride=8, seq_len=512
  • 权重大小: 433 MB safetensors (pytorch_model.bin 433 MB, 总 907 MB), 预计峰值显存 ~435 MB (实测 434.5 MB allocated, 452.8 MB max)
  • 许可证: MIT
  • 来源: AutonLab Timeseries-PILE 预训练
  • 推理入口: python inference.py 默认在 npu:0 完成真实 NPU 推理

本仓验证该权重的 exact checkpoint 在 Ascend 910 (npu:0) 上完成重构推理,CPU-NPU 误差在 FP32 阈值内。

数据契约

  • 输入契约: x_enc: [batch, n_channels, seq_len] float32, input_mask: [batch, seq_len] float32 (全1表示无缺失), 轴顺序明确为 [B, C, T]
  • 预处理: 无归一化缩放 (原始合成数据直接输入), dtype float32, 随机种子 42 固定生成
  • 样例数据: 合成时序 batch=2, n_channels=3, seq_len=512
    • 通道0: sin(5 periods) + 0.5 cos(2 periods) + N(0,0.05)
    • 通道1: 0.01*t trend + 0.8 sin(3 periods) + N(0,0.03)
    • 通道2: 0.6 sin(8 periods) + 0.4 sin(12 periods)
  • 输出契约: reconstruction: [batch, n_channels, seq_len] float32, 与输入同形状, 逆变换已完成 (直接重构值)
  • 预处理是否计时: 性能计时仅包含模型推理,不含数据生成
  • 轴顺序/频率/缩放: seq_len=512 patch_len=8 stride=8 共64 patches, frequency 未显式建模, scale 无

环境

  • NPU: Ascend910_9362, 2 devices, 选中 npu:0
  • CANN: 8.5.1 (ASCEND_TOOLKIT_HOME=/usr/local/Ascend/cann-8.5.1)
  • Python: 3.11.14
  • torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • transformers: 4.57.6
  • numpy: 1.26.4
  • momentfm: 0.1.5 (git+https://github.com/moment-timeseries-foundation-model/moment.git)
  • huggingface_hub + HF_ENDPOINT=https://hf-mirror.com
  • npu-smi: 25.5.5, Health OK, HBM 3112/65536 MB (device 0)

安装

pip install -r requirements.txt
# 包含 torch, torch_npu, transformers, huggingface_hub, numpy, momentfm (git)

NPU 推理

python inference.py

行为:

  • 优先从本地 working/models/MOMENT-1-base 加载 (含 model.safetensors 432.9 MB),否则从 HF Hub AutonLab/MOMENT-1-base 下载到 ./.cache/MOMENT-1-base
  • MOMENTPipeline(task_name="reconstruction").to("npu:0"),输入/掩码显式 .to("npu:0")
  • 前后 torch.npu.synchronize() 计时,首轮含编译,2 次 warmup + 10 次正式
  • 退出码 0 表示成功,非零表示失败 (NPU 不可用/fallback)

真实结果 (NPU, npu:0, batch=2, n_channels=3, seq_len=512, float32, seed 42)

  • 首参数 device: npu:0, dtype float32, 权重 432.9 MB
  • 输入: x_enc [2,3,512] 均值 0.8528 std 1.5611, input_mask [2,512] 全1
  • 输入 preview 通道0前5: [0.5248357, 0.5542569, 0.6541928, 0.7578365, 0.7288649]
  • 输出 device: npu:0, shape (2,3,512), dtype float32, finite True
  • 重构 preview 通道0前5: [-0.4207615, -0.07447138, 0.12163288, -0.12251939, 0.14481770]
  • 重构统计: mean 0.725750 std 1.195563 min -0.652373 max 3.718409
  • 单次同步耗时: first run 11.43 ms (含编译), second 12.85 ms
  • 10次均值: 12.31 ms, 吞吐 162.4 windows/s (249465.7 points/s)
  • 显存: allocated 434.5 MB, max 452.8 MB
  • 状态: [SUCCESS] MOMENT-1-base NPU reconstruction completed, exit 0, 无 CPU fallback

真实日志见 logs/inference.log 及证据图 assets/model_result.png。

一致性 (CPU vs NPU, 同权重同输入同预处理, seed 42, float32)

  • 工具: .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3
  • 输入: torch.manual_seed(42) 合成序列 (2,3,512)
  • CPU 输出: (2,3,512) finite True, preview [-0.4207619, -0.07447129, 0.12163303, -0.12251929, 0.14481776]
  • NPU 输出: (2,3,512) npu:0, preview [-0.4207615, -0.07447138, 0.12163288, -0.12251939, 0.14481770]
  • 结果:
{"task":"regression","cpu_shape":[2,3,512],"npu_shape":[2,3,512],"finite":true,"atol":0.0001,"rtol":0.001,"max_abs_error":1.9073486328125e-06,"mean_abs_error":1.9285979154422725e-07,"passed":true}
  • 阈值说明: FP32 默认 atol 1e-4 rtol 1e-3,有 2 个数量级余量,证明无 fallback 且数值一致
  • 发生 CPU fallback: 否 (参数与输出均在 npu:0,fallback_detected: False)

性能 (npu:0, float32, torch.npu.synchronize, 排除数据生成)

  • 配置: batch 2, seq_len 512, n_channels 3, patch 8 -> 64 tokens, dtype float32
  • 预热: 2 次
  • 正式: 10 次
  • 结果:
    • first run (compile): 11.43 ms
    • second run: 12.85 ms
    • avg: 12.31 ms, min ~11.4 ms, max ~12.8 ms, p50 ~12.3 ms
    • throughput: 162.4 windows/s, 249465 points/s
    • peak memory: 452.8 MB
  • 计时是否含预处理: 否,仅模型 forward;含 synchronize
  • 编译 vs 稳定: 首轮包含 NPU 图编译,后续稳定

证据图

三图由 xterm.js 根据本次真实日志渲染生成 (raw 风格, 深灰背景 #282828, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • 工作流: assets/agent_workflow.png — 侦察、下载、NPU 推理、一致性、提交校验
  • 设备调用: assets/npu_device_call.png — npu-smi info + check_npu.py + 参数/张量 device
  • 模型结果: assets/model_result.png — python inference.py 真实完整输出 (schema、预测、耗时、状态)

agent workflow npu device call model result

限制

  • 仅验证 reconstruction 任务 (seq_len=512, n_channels=3, patch 8) 的单窗口 smoke 场景,非完整 Timeseries-PILE benchmark
  • ArgSort 算子在 NPU 上回退到 AI CPU (warning: kernel [ArgSort] can not support dtype int32 or int64 on AiCore) 但不影响功能与精度,未来可 cast 到 float32 优化
  • 未验证 forecasting/classification/anomaly detection heads,需对应 head 权重与数据
  • 权重需本地存在或联网下载 (HF_ENDPOINT 镜像),离线环境需预先放置到 working/models/MOMENT-1-base