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

AutonLab/MOMENT-1-base on Ascend NPU

1. 模型简介

  • 模型名称: AutonLab/MOMENT-1-base
  • 权重URL: https://huggingface.co/AutonLab/MOMENT-1-base
  • 任务类型: 时间序列基础模型 (Time-Series Foundation Model) - 表格数据 / 时间序列分析 (forecasting, classification, anomaly detection, imputation, representation learning)
  • 架构: MOMENT (encoder-only T5 backbone, google/flan-t5-base, d_model=768, 12 layers, 12 heads, d_ff=2048, gated-gelu, patch_len=8, patch_stride=8, seq_len=512)
  • 权重格式: model.safetensors (433M) + pytorch_model.bin (433M) 冗余, 实际使用 model.safetensors (113,481,608 参数, FP32 0.42 GiB / FP16 0.21 GiB)
  • 加载后端: momentfm MOMENTPipeline (PyTorchModelHubMixin, T5EncoderModel)
  • 许可证: MIT
  • 输入契约: x_enc: [B, n_channels, seq_len] float32, input_mask: [B, seq_len] (1=valid, 0=masked/padded), 轴顺序固定 [B,C,T], 频率无关, scale via RevIN (Reversible Instance Normalization)
  • 输出契约:
    • embedding: [B, d_model=768] (reduction=mean) 或 [B, n_patches=64, d_model] (reduction=none)
    • reconstruction: [B, n_channels, seq_len] (imputation / pre-training)
    • forecast: [B, n_channels, forecast_horizon] (forecasting head, 需微调)
    • anomaly_scores: 通过 detect_anomalies (mse criterion) 计算

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910B), CANN 8.5.1, 64GB HBM, 2 devices (使用 npu:0)
  • 驱动: npu-smi 25.5.5, torch.npu.is_available()=True, device_count=2
  • PyTorch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04
  • Transformers: 4.57.6, momentfm: 0.1.4
  • Model cache: /tmp/moment_test (HuggingFace snapshot, commit 5e44b0ea26376a176360f87831124e018f876d96)
  • Python: 3.11.14
  • 测试输入: 合成正弦时间序列, seed=0, B=2, C=3, T=512, t=linspace(0,4π,512), x[b,c,t]=amp*sin(freq*t+phase)+0.1*randn, 频率 1.0/1.5/2.0, 归一化前均值 0.0227

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers>=4.39.1
# momentfm==0.1.4
# numpy==1.26.4
# safetensors
# huggingface_hub

4. NPU 推理

# 默认执行 (CPU 基准 + NPU 推理 + 一致性验证 + 性能测试)
python inference.py

# 指定设备
python inference.py --device npu:0 --task embedding
python inference.py --device cpu --task reconstruction

# 仅性能测试
python inference.py --mode benchmark --task embedding

# 预测任务 (forecast horizon 96, head 需微调, 仅演示链路)
python inference.py --task forecasting --device npu:0

核心逻辑 (inference.py:1):

  • 本地加载 MOMENTPipeline.from_pretrained(model_dir, model_kwargs={"task_name": "embedding"}) -> model.init() -> model.to("npu:0") -> model.eval()
  • 数据: x_enc [2,3,512] + input_mask [2,512] (synthetic sine waves, deterministic seed=0)
  • 预处理: RevIN norm -> Patching(patch_len=8) -> PatchEmbedding(d_model=768, positional) -> reshape [B*C, n_patches=64, 768] -> T5Encoder with patch mask
  • 首次编译后同步 torch.npu.synchronize() 计时, 验证首参数 next(model.parameters()).device == npu:0

5. 真实推理结果

执行命令: python inference.py (在 npu:0 上真实执行, 权重来自本地 /tmp/moment_test/model.safetensors)

[INFO] Model: AutonLab/MOMENT-1-base
[INFO] Weight URL: https://huggingface.co/AutonLab/MOMENT-1-base
[INFO] Loading backend: momentfm MOMENTPipeline (T5Encoder, d_model=768, 12 layers)
[INFO] NPU available: True, device_name: Ascend910_9362, free 60.91 GiB total 61.27 GiB
[INFO] Synthetic input x_enc shape [2, 3, 512] mean 0.0227 std 0.8610
[INFO] Input sample (batch0, channel0, first 8): [0.1764, 0.0646, 0.1470, 0.2977, 0.2849, 0.0249, 0.2420, 0.1561]
[CPU] embedding shape [2, 768] mean 0.000354 std 0.261954 latency 1116.41 ms
[CPU] embedding first 5 (batch0): [-0.00274, -0.01214, -0.07041, 0.03595, -0.03032]
[CPU] reconstruction shape [2, 3, 512] mean -0.092594 std 0.317333 latency 1097.83 ms
[NPU] embedding shape [2, 768] mean 0.000354 std 0.261954 latency 204.81 ms (first run includes compilation)
[NPU] embedding first 5 (batch0): [-0.00274, -0.01214, -0.07041, 0.03595, -0.03032]
[NPU] reconstruction shape [2, 3, 512] mean -0.092594 std 0.317333 latency 26.57 ms
[NPU] device npu:0, dtype torch.float32, seq_len 512, n_channels 3
[CONSISTENCY] embedding: MAE 1.08e-08, MaxAbs 4.77e-07, RMSE 2.45e-08, mean_cosine 1.000000
[CONSISTENCY] reconstruction: MAE 1.81e-07, MaxAbs 1.31e-06, RMSE 2.38e-07, mean_cosine 1.000000
[RESULT] PASS - NPU inference completed and CPU-NPU consistency within threshold
  • 输入: 合成正弦 B=2, C=3, T=512 -> patch 64 tokens
  • 嵌入: [2,768], 均值 0.000354, CPU-NPU MAE 1.08e-08, Max 4.77e-07
  • 重构: [2,3,512], 均值 -0.09259, CPU-NPU MAE 1.81e-07, Max 1.31e-06
  • NPU 首次推理 204 ms (含图编译), 重构稳定 26 ms, 嵌入稳定见第7节
  • 任务: embedding (representation learning) 与 reconstruction (imputation) 均为官方预训练任务, forecasting head 未预训练仅演示链路

6. CPU-NPU 一致性验证

  • 方法: 同一权重 (model.safetensors, 113M 参数)、同一合成输入、同一 RevIN+Patch 预处理、同一 dtype float32、同一 seed=0, 分别在 cpu 与 npu:0 执行, 保存原始输出 .npy 后用 scripts/compare_outputs.py 比较
  • 命令:
python scripts/compare_outputs.py --cpu /tmp/moment_cpu_embedding.npy --npu /tmp/moment_npu_embedding.npy --task regression --atol 1e-4 --rtol 1e-3
python scripts/compare_outputs.py --cpu /tmp/moment_cpu_reconstruction.npy --npu /tmp/moment_npu_reconstruction.npy --task regression --atol 1e-4 --rtol 1e-3
  • 指标:
    • embedding: MAE 1.08e-08 (<5e-5 PASS), MaxAbs 4.77e-07 (<1e-3 PASS), RMSE 2.45e-08, cosine 1.000000
    • reconstruction: MAE 1.81e-07 (<1e-4 PASS), MaxAbs 1.31e-06 (<1e-3 PASS), RMSE 2.38e-07, cosine 1.000000
    • 阈值基于 FP32 smoke consistency, 非完整数据集精度评测
  • 结论: 单样本 smoke consistency 通过, 误差仅来自 NPU/CPU 浮点累积, 远低于阈值视为一致
  • 设备: 验证 next(model.parameters()).device == npu:0 且 input.device == npu:0

7. 性能测试

条件: npu:0, float32, B=2, C=3, T=512, n_patches=64, d_model=768, 调用 torch.npu.synchronize() 计时, 预热 3 次, 正式 10 次 (embedding) / 10 次 (reconstruction)

[PERF] embedding NPU avg 11.18 ms, min 11.06, max 11.28, p50 11.16, p90 11.28, p95 11.28
[PERF] throughput 178.90 samples/s, 89.45 windows/s (B=2 per window)
[PERF] reconstruction NPU avg 11.80 ms, min 11.69, max 11.93
[PERF] embedding CPU avg 1101.00 ms (EPYC single core)
任务设备avgminmaxp50p90p95throughput
embeddingNPU Ascend91011.18 ms11.0611.2811.1611.2811.2889.45 windows/s (178.9 samples/s)
reconstructionNPU11.80 ms11.6911.93---84.7 windows/s
embeddingCPU (EPYC)1101 ms-----0.91 windows/s
  • 首次编译额外开销 ~190 ms (embedding first run 204 ms vs stable 11 ms, 已单独报告不计入稳定推理)
  • NPU 加速比: 98.5× (embedding, 1101/11.18)
  • 峰值显存: 基线 3107 MB -> 推理后 free 60.82 GiB / total 61.27 GiB (HBM 增长约 800 MB, 权重 433 MB + 激活/编译缓存)
  • dtype: 默认 FP32, 未尝试 FP16/BF16 (显存充足, 精度优先)
  • 批大小: 2, 通道 3, 序列 512 为官方默认, 更大 B/C/T 可线性扩展

8. 自验证截图

  • assets/agent_workflow.png - 阶段日志 (环境检查、获取模型、分析模型、NPU适配、NPU验证)
  • assets/npu_device_call.png - npu-smi info、torch.npu.is_available()、device_name 与模型参数 npu:0 证据
  • assets/model_result.png - python inference.py 真实输出、MAE/RMSE 与 PASS/FAIL

9. 已知限制

  • 仅验证单样本合成正弦数据 smoke consistency, 非 Timeseries-PILE 完整 benchmark 评测
  • forecasting / classification heads 未预训练 (momentfm 警告 "Only reconstruction head is pre-trained"), 本仓演示链路但精度需微调后评估
  • anomaly_detection 通过 reconstruction + mse 计算, 本次未单独评估阈值与 F1
  • 输入固定 seq_len=512, patch_len=8; 更短序列经 RevIN pad, 更长需滑动窗口, 未验证动态长度
  • 权重文件冗余 model.safetensors / pytorch_model.bin, 实际仅加载前者
  • 未启用 FP16/BF16 量化, 如需更高吞吐可尝试 model.half() 但需重验一致性
  • 自定义 T5Encoder 在 NPU 上有 ArgSort AiCpu fallback 警告, 不影响正确性但可关注后续算子优化

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, Ascend910B, time-series, tabular, forecasting, anomaly-detection, imputation, MOMENT, foundation-model #NPU