m
mengsihan/AutonLab-MOMENT-1-large-NPU-Model
模型介绍
文件和版本
Pull Requests
讨论
分析

AutonLab/MOMENT-1-large Ascend NPU Adaptation #NPU

模型简介

  • 模型: AutonLab/MOMENT-1-large
  • Revision: main (config.json: 2026-08-22, model.safetensors 1.385 GB)
  • 任务: 时间序列预测 / 重构 / 表示学习 (Time-Series Foundation Model)
  • 架构: MOMENT + T5 Encoder (google/flan-t5-large, d_model=1024, 24 layers, 16 heads, seq_len=512, patch_len=8, stride=8, n_patches=64)
  • 权重: model.safetensors 1.385 GB, 无需 trust_remote_code, 使用 momentfm==0.1.5 + transformers.T5EncoderModel
  • 许可证: MIT

MOMENT 是 AutonLab 提出的时间序列基础模型,支持 forecasting (点预测)、classification、anomaly detection、imputation 和 embedding。MOMENT-1-large 以 google/flan-t5-large 为骨干,Encoder-only,输入为 [batch, channels, seq_len],输出根据任务为 forecast [B,C,H]、reconstruction [B,C,T] 或 embeddings [B,D]。

数据契约

  • 输入: x_enc: torch.Tensor [batch, n_channels, seq_len] + input_mask: [batch, seq_len] (1=有效, 0=mask)
  • 轴顺序: [B, C, T],禁止转置,通道维独立归一化 (RevIN, affine=False)
  • 固定参数: seq_len=512, patch_len=8, stride=8, n_patches=64, d_model=1024
  • forecasting: forecast_horizon=96 (可配置 1~512),输出 [B,C,96]
  • reconstruction: 输出 [B,C,512],与输入同形,用于重构/异常检测
  • 频率/缺失: 合成数据无真实时间戳,模拟连续采样;缺失步用 mask 0 表示,NaN 会被置 0
  • 示例数据: 合成 batch=2, channels=3, seq_len=512, seed=0 正态分布,SHA256 可追溯 (working/cpu_forecasting.npy)
  • 预处理: RevIN norm → Patching (unfold) → PatchEmbedding (Linear 8→1024 + sin/cos positional) → T5Encoder → Head (ForecastingHead: Linear 65536→96, PretrainHead: Linear 1024→8)
  • 数据隐私: 未提交原始数据,日志仅展示统计量 (mean/std) 和 shape

环境

  • NPU: Ascend 910B (Ascend910_9362), torch_npu 2.9.0.post1, CANN 8.5.1, npu-smi 25.5.5
  • 计算: torch 2.9.0+cpu, transformers 4.57.6, numpy 1.26.4, huggingface_hub (hf-mirror)
  • 镜像: SOC_VERSION=ascend910_9391, TASK_QUEUE_ENABLE=1
  • 设备验证: torch.npu.is_available()==True, device_count=2, 目标 npu:0 (Bus 0000:0A:00.0)
  • 显存: 峰值 HBM ~3.2 GB (模型 1.3 GB + 激活 + KV), 单卡 64 GB 余量充足 (>60 GB)

安装

pip install -r requirements.txt
# 或手动安装核心依赖
pip install torch torch_npu transformers safetensors huggingface_hub
pip install git+https://github.com/moment-timeseries-foundation-model/moment.git

权重下载 (本地优先,失败则联网):

HF_ENDPOINT=https://hf-mirror.com huggingface-cli download AutonLab/MOMENT-1-large --local-dir ./models/MOMENT-1-large --local-dir-use-symlinks False
# 或让 inference.py 自动处理

NPU 推理

默认命令 (真实 NPU 推理,含 warmup 编译):

python inference.py
# 等价指定:
python inference.py --task forecasting --forecast-horizon 96 --batch 2 --channels 3 --seq-len 512
# 其他任务:
python inference.py --task reconstruction
python inference.py --task embedding

关键日志 (stdout 截选):

[INFO] model: AutonLab/MOMENT-1-large task: forecasting seq_len: 512 n_patches: 64 horizon: 96
[INFO] npu device count: 2 name: Ascend910_9362
[INFO] selected device: npu:0
[INFO] using local model dir: ../models/MOMENT-1-large
[INFO] first param device: npu:0 dtype: torch.float32
[INFO] input shape: torch.Size([2, 3, 512]) mean -0.0017 std 1.00
[INFO] warmup 1: 262 ms (compile) | 2: 21 ms | 3: 21 ms
[INFO] sync inference time: 20.4 ms
[INFO] output forecast shape: torch.Size([2, 3, 96]) device: npu:0
[INFO] forecast sample [0,0,:5]: [0.11, 0.04, 0.03, -0.01, -0.07]
[INFO] NPU inference completed successfully PASS

真实结果

  • 输入: batch=2, channels=3, seq_len=512 合成正态 (seed 0)
  • forecasting (horizon 96): 输出 [2,3,96], 均值 ~0.005, std ~0.09, 示例 [0.11,0.04,0.03,-0.01,-0.07] (NPU)
  • reconstruction: 输出 [2,3,512], 均值 ~0.041, 示例 [0.258,0.315,0.588,0.216,0.221] (与 CPU 对齐,见一致性)
  • embedding: 输出 [2,1024], 均值 ~0.0026
  • 说明: forecasting head 为随机初始化 (官方提示 "Only reconstruction head is pre-trained"),因此数值仅验证 NPU 流程,不代表微调后精度;reconstruction head 为预训练,直接可用。

一致性

同一权重、输入、预处理、seed、eval、dtype (float32) 下比较原始张量:

python scripts/compare_outputs.py --cpu working/cpu_forecasting.npy --npu working/npu_forecasting.npy --task forecasting --atol 1e-4 --rtol 1e-3
任务CPU shapeNPU shapemax_absmean_abspassed (atol 1e-4)
forecasting[2,3,96][2,3,96]1.64e-062.64e-07✅
reconstruction[2,3,512][2,3,512]1.73e-062.77e-07✅
embedding[2,1024][2,1024]1.79e-072.26e-08✅

torch.npu.synchronize() 前后计时,无 CPU fallback (首参 device=npu:0, 输出 device=npu:0)。阈值按 FP32 严格 1e-4,实际误差 <2e-06,满足。分类场景会额外检查 argmax 一致率,此模型为回归/预测故以数值为主。

性能

设备 npu:0, dtype float32, batch=2, channels=3, seq_len=512, horizon=96, 计时含 torch.npu.synchronize(),不含数据生成,预处理包含在模型内。

阶段值
compile / first-run262 ms (含图编译+缓存)
稳定 avg (10 runs)20.41 ms
min / max20.07 / 21.44 ms
p50 / p90 / p9520.28 / 20.91 / 21.17 ms
reconstruction avg20.71 ms
吞吐~98 windows/s (2 windows / 0.0204 s) / ~49 series/s
峰值 HBM~3.9 GB (npu-smi) / 64 GB 总量
电源170 W (npu-smi)

表格模型 rows/s 口径对应时间序列 windows/s。

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据真实日志生成 (深灰 #282828, 白色等宽, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$),README 仅引用图片。

  • assets/agent_workflow.png — 侦察、环境、下载、加载、NPU推理、一致性、benchmark、提交校验
  • assets/npu_device_call.png — npu-smi info, torch.npu.is_available(), 设备名、参数 device/dtype、输入/输出 Tensor device
  • assets/model_result.png — python inference.py 完整输出 (输入摘要、输出摘要、同步耗时、PASS)

agent_workflow npu_device_call model_result

限制

  • forecasting head 未预训练,需下游微调;当前仅验证 NPU 链路
  • 不支持动态 seq_len !=512 未测试,转置输入会报错
  • 仅验证 npu:0 单卡,npu:1 未测
  • 半精度 (FP16/BF16) 未验证,保持 FP32
  • 数据为合成,真实业务需替换为带时间戳的真实序列并固定频率
  • 内核 ArgSort 在 AiCPU 回退 (warning),不影响精度但可优化为 float32

Topics

NPU, Ascend, Ascend910, time-series, forecasting, moment, t5, foundation-model