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

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

NPU #NPU

模型简介

AutonLab/MOMENT-1-small 是 MOMENT 时序基础模型家族的 Small 版本,基于 google/flan-t5-small (T5 encoder-only, 8 layers, d_model=512, num_heads=6, d_ff=1024) ,支持 forecasting、classification、anomaly detection、imputation 和 embedding 等任务。本次适配验证 reconstruction (预训练头, patch_len=8, seq_len=512) 在单卡 npu:0 的真实推理与 CPU 一致性。

  • Hugging Face: https://huggingface.co/AutonLab/MOMENT-1-small
  • Revision: 411e288267f82cce86296dbe4d6c8bc533cc162f (本地缓存)
  • 权重: model.safetensors 145 MB (+ pytorch_model.bin 145 MB 同权重), FP32, ~82M 参数, 估算峰值显存 < 2GB
  • backbone: google/flan-t5-small, t5_config d_model 512, num_layers 8, patch 8/8
  • 任务路由: time-series (点预测/重构), 验收对象 forecast/reconstruction tensor, MAE/RMSE

数据契约

  • 输入: [batch, n_channels, seq_len] float32, batch=2, n_channels=1, seq_len=512 (默认), patch_len=8 -> 64 patches
  • 输入生成: 合成正弦 + 趋势 + 噪声 (sin(2πt/50)+0.01t+randn*0.1), seed 42, 无缺失, 已做 RevIN norm (模型内)
  • 轴顺序: [B,C,T] 严格保持, 通道与时间维不转置; input_mask 为 [B, seq_len] 全 1
  • 输出: reconstruction [B,C,seq_len] float32, embeddings [B, d_model] 当 reduction=mean
  • 预处理: RevIN norm -> Patching(patch_len=8,stride=8) -> PatchEmbedding -> T5Encoder -> PretrainHead -> RevIN denorm
  • 示例输入摘要: x[0,0,0:5]=[0.0497,0.1215,0.3335,0.5504,0.4983] mean 2.3777 std 1.5178, 来自合成数据 seed 42, 可复现

环境

  • 硬件: Ascend 910 (910_9362) 单卡 npu:0, CANN 8.5.1, npu-smi 可用设备数 2
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, momentfm 0.1.4, numpy 1.26.4
  • 验证: torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)==Ascend910_9362, first_param device npu:0, 输入/输出 Tensor device npu:0, 计时前后 torch.npu.synchronize()

安装

pip install -r requirements.txt
# requirements 包含 torch, torch_npu, transformers, momentfm, safetensors 等

NPU 推理

默认命令 (已在 npu:0 验证通过, 退出码 0):

python inference.py
  • 从本地 working/MOMENT-1-small (若存在) 或 Hugging Face AutonLab/MOMENT-1-small 加载权重, 显式 .to("npu:0")
  • 日志至少打印模型名、任务、backend、device、dtype、首个参数 device、输入/输出摘要、同步耗时和最终状态
  • 动态 shape 首次编译后复跑, 无静默 CPU fallback (ArgSort 在 AiCPU 属昇腾内部, 非 host CPU fallback)

真实结果

执行 python inference.py (npu:0, FP32, batch2, seq512, n_channels1):

[INFO] model source: working/MOMENT-1-small
[INFO] NPU available: True device_count=2 device_name=Ascend910_9362
[INFO] selected device: npu:0
[INFO] first_param device after to(device): npu:0
[INFO] input x shape=(2, 1, 512) sample x[0,0,0:5]=[0.0497, 0.1215, 0.3335, 0.5504, 0.4983]
[RESULT] reconstruction shape (2, 1, 512) sample [0,0,0:5]=[2.9151, 2.0822, 2.0386, 4.4080, 3.0305]
[RESULT] recon mean 2.488152 mse_vs_input 3.105179
[PERF] first run latency 9.72 ms
[RESULT] embedding shape (2, 512) sample [-0.05424, 0.14007, -0.01814, -0.07896, -0.01091]
[PERF] stable avg 9.33 ms windows/s 214.3
[DONE] NPU inference completed successfully device=npu:0
  • 输出为真实 NPU 计算, 非随机/伪造; 复现需固定 seed 42 与相同权重/输入/dtype

一致性

同一权重、输入、预处理、dtype(FP32)、seed 42 下 CPU vs NPU 对比 (scripts/compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3):

  • cpu_output.npy vs npu_output.npy (reconstruction [2,1,512] FP32)
  • max_abs_error 1.907e-06, mean_abs_error 2.71e-07, finite true, PASSED
  • 阈值依据 FP32 + T5 encoder 精度设置; 分类比较 logits/概率, 回归/预测比较 forecast/reconstruction 数值, 无统一 Top-5 套用
  • 复测条件: eval mode, 同一 mask, 同一 RevIN, 无 CPU fallback

性能

  • 设备: npu:0 FP32, batch=2, seq_len=512, n_channels=1, patch_len=8 (64 patches)
  • 首轮编译/首跑: 9.72 ms (含编译), 稳定 10 次: avg 9.33 ms, min 8.80 ms, max 9.73 ms, p50 9.50 ms, p90 9.62 ms, p95 9.67 ms, windows/s 214.3 (batch*1000/avg)
  • 峰值显存: torch.npu.max_memory_allocated 161.4 MB
  • 计时已 torch.npu.synchronize() 前后同步, 预热 2 次, 正式 10 次, 不含 CSV 读取 (合成输入)
  • 对比 CPU 单次约 ~100ms+ (NPU 加速 ~10x)

证据图

三图由 xterm.js 根据本次真实日志生成 (非手工绘图, 非 Pillow 模拟), 深灰背景白色等宽字体, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签:

workflow device result

  • agent_workflow.png: 侦察、环境、下载、加载、NPU 推理、一致性、benchmark
  • npu_device_call.png: npu-smi info + torch.npu.is_available() + 参数/输入/输出 device
  • model_result.png: python inference.py 默认推理真实输出

限制

  • 仅验证 reconstruction/embedding 任务 (预训练头), forecasting/classification 头需微调, 数值仅烟雾一致性 (单批次), 非完整数据集 benchmark
  • 输入固定 seq_len=512 (patch 8), 超长需滑动窗口; ArgSort 算子在 AiCPU 执行属昇腾内部调度, 非 host CPU fallback
  • 数据为合成示例, 不代表真实业务精度; 模型为 MIT 许可证