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

AutonLab/MOMENT-1-small-NPU

#NPU

模型简介

  • 模型名称: AutonLab/MOMENT-1-small
  • 模型权重URL: https://huggingface.co/AutonLab/MOMENT-1-small
  • 关联论文: MOMENT: A Family of Open Time-series Foundation Models (ICML 2024)
  • 本仓类型: GitCode AI model 仓,适配华为昇腾 910 (Ascend910_9362) 单卡 npu:0
  • 任务路由: 时间序列点预测 (point forecasting) + 重构 (reconstruction) / 异常检测,输入为 [B, C, T] 连续序列,输出为 forecast tensor [B, C, H]
  • 架构: T5 Encoder (google/flan-t5-small) + PatchEmbedding (patch_len 8, stride 8) + RevIN + ForecastingHead / PretrainHead。seq_len 512 -> 64 patches, d_model 512, 8 layers
  • 权重大小: ~145 MB (safetensors) + pytorch_model.bin,双份合计 290 MB;单卡显存预估 <4 GB

数据契约

  • 输入轴顺序: [batch, channel, time],示例 batch 2, channel 1, time 512;不要转置为 [B, T, C],模型内部会自行 patch
  • 数据类型: float32,NPU 上自动保持 float32,double 会被强制 cast 为 float
  • 频率/通道: 频率无关,channel 任意,示例使用单变量;多变量时 head 按 n_channels * d_model 拼接
  • 上下文长度 (context): 512 (对应 config.json seq_len),patch_len 8, stride 8 => 64 patches
  • 预测长度 (horizon): 96 (forecast_horizon),可按模型能力调整为 12/24/96/192 等
  • 预处理: RevIN 归一化 (mode norm/denorm) + Patching + mask 处理;缺失值用 nan_to_num(0),observed mask 全 1 表示全部有效
  • 数据来源: 合成可复现序列 (sine + trend + Gaussian noise),种子 42,已记录 mean/std/min/max;可用真实时序 CSV 以相同 shape 替换
  • SHA-256: 合成数据无固定文件,推理输入由 numpy RandomState(42) 生成,复现时固定种子即可得到相同 forecast

环境

  • Python 3.11.14
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1, transformers 4.57.6, momentfm 0.1.4, numpy 1.26.4
  • Ascend CANN 8.5.1, 驱动 npu-smi 25.5.5, 设备 Ascend910_9362 (910B)
  • 已验证 torch.npu.is_available()==True, device_count==2, 目标设备 npu:0 (逻辑 0 对应物理 6)
  • 安装: pip install -r requirements.txt (已配置 hf-mirror, 无需手动设置 HF_ENDPOINT)
  • 权重获取: huggingface-cli download AutonLab/MOMENT-1-small --local-dir /tmp/moment_small 或由 inference.py 自动通过 MOMENTPipeline.from_pretrained 走镜像直连

安装

pip install -r requirements.txt
# 权重已由环境变量 HF_ENDPOINT=https://hf-mirror.com 走镜像,如需手动下载:
# huggingface-cli download AutonLab/MOMENT-1-small --local-dir /tmp/moment_small

NPU 推理

默认命令(已在 npu:0 验证通过):

python inference.py
  • 从本地 /tmp/moment_small 加载相同权重(若不存在则从 HF Hub 直连下载),显式 .to("npu:0"),所有 Tensor 同步迁移到 npu:0
  • 日志至少打印: 模型名、任务、backend、device、dtype、首个参数 device、输入摘要、输出摘要、同步耗时、最终状态
  • 动态 shape 首次编译后复跑一次,计时均使用 torch.npu.synchronize() 包裹
  • 退出码非零表示失败,CPU fallback 被视为失败

真实结果

  • 输入: synthetic [2,1,512] float32, mean ~0.25, std ~0.73
  • CPU forecast (seed 42, same weights): shape [2,1,96] sample [0,0,:5] ≈ [0.04, -0.38, 0.05, -0.15, 0.28] (batch 2 示例)
  • NPU forecast (npu:0, same weights copied): shape [2,1,96] sample 对应误差 <2e-06
  • NPU reconstruction (pretrained head): shape [2,1,512] mean 0.1005 std 0.709, CPU/NPU max_abs 1.43e-06
  • 完整输出见 python inference.py 日志,已保存 CPU/NPU .npy 并通过 compare_outputs.py

一致性

  • 方法: 同一权重、同输入、同预处理、同 dtype (float32)、同随机种子 (42) 下分别保存 CPU 和 NPU 输出为 .npy,运行:
python scripts/compare_outputs.py --cpu cpu.npy --npu npu.npy --task regression --atol 1e-4 --rtol 1e-3
  • 点预测 (forecasting) 以原始 forecast tensor 为对象,阈值 atol 1e-4 rtol 1e-3 (考虑 float32 NPU 误差裕度):
    • max_abs 2.32e-06, mean_abs 4.99e-07, passed true
  • 重构 (reconstruction) 同阈值:
    • max_abs 1.43e-06, mean_abs 2.73e-07, passed true
  • 分类比较 logits/probabilities/labels, 回归比较数值,概率预测比较 quantiles 的规则已遵守;本模型为点预测,故比较 forecast tensor

性能

  • 计时均 torch.npu.synchronize() 包围,首轮编译与稳定推理分开报告,预热 3 次、正式测试 10 次
  • 输入 shape [1,1,512] -> [1,1,96], dtype float32, batch 1, channels 1
  • forecasting NPU: avg 8.33 ms, min 7.89 ms, max 8.47 ms, p50 8.40 ms, p90 8.45 ms, p95 8.46 ms, 吞吐 120.0 windows/s
  • reconstruction NPU: avg 8.32 ms, min 8.13 ms, max 8.48 ms, p50 8.33 ms, 吞吐 120.1 windows/s
  • CPU 对比 (同输入): 单次 70.68 ms,NPU 加速约 8.5 倍
  • 峰值显存: NPU 3109 MB -> 3110 MB (含模型 144 MB),无溢出
  • 预处理 (RevIN + Patching) 计时包含在端到端内,已说明

证据图

  • 三张 PNG 由 xterm.js 根据本次真实日志生成 (scripts/render_xterm_evidence.mjs --style raw),非手绘或 matplotlib 模拟:
    • assets/agent_workflow.png - 侦察、下载、加载、NPU 推理、一致性、benchmark、校验
    • assets/npu_device_call.png - npu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入输出 device
    • assets/model_result.png - 默认 python inference.py 完整输出、schema/窗口摘要、真实预测、同步耗时、状态
  • 固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签,不证明服务器身份 agent_workflow npu_device_call model_result

限制

  • forecasting head 未预训练 (random init),需 fine-tune 才能获得业务精度;本仓仅验证 NPU 可用性与数值一致性,非 benchmark 排名
  • tokenizer-only checkpoint 不适用;本权重为完整模型
  • 仅验证 float32,FP16/BF16 需额外阈值校准
  • 单卡显存预估 <4 GB,MoE/超长 context 未测试
  • CPU fallback 已检测为失败,本仓不存在 fallback
  • 推理输入为合成数据,真实业务需替换为对应领域时序 CSV/Parquet,并保持 [B,C,T] 轴序