gcw_6a3w2yXz/AutonLab-MOMENT-1-large-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

AutonLab/MOMENT-1-large NPU 适配

#NPU

本仓库将 HuggingFace 上的 AutonLab/MOMENT-1-large(MOMENT 时间序列基础模型,基于 google/flan-t5-large,seq_len 512,patch 8,点预测 horizon 96,权重 1.3GB)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。支持单变量/多变量时序的零样本预测,输出未来 96 步点预测。

  • 模型原始仓库:https://huggingface.co/AutonLab/MOMENT-1-large
  • 权重:pytorch_model.bin 约 1.32 GB(本地已转换为 model.safetensors 1.38 GB 用于 from_pretrained 本地加载),config.json seq_len 512
  • Revision:main
  • 源实现:https://github.com/moment-timeseries-foundation-model/moment(momentfm 0.1.4)

任务与路由

项值
模型AutonLab/MOMENT-1-large(revision main,backbone google/flan-t5-large d_model 1024, 24 encoder + 24 decoder layers)
架构MOMENT encoder_only (T5EncoderModel) + PretrainHead/ForecastingHead
任务时间序列预测 point forecasting(通过 forecast 分支,horizon 96)
输入x_enc shape [batch, n_channels, seq_len](本仓 batch 2, n_channels 1, seq_len 512),input_mask [batch, seq_len] 全1,预处理含 RevIN 归一 + patching
输出forecast shape [batch, n_channels, horizon](本例 [2,1,96]),float32
数据契约轴顺序明确为 [batch, channel, time] 输入 -> [batch, channel, horizon] 输出;context 512,horizon 96;patch_len 8 / stride 8;频率隐式,缩放经 RevIN;缺失值以 NaN 填充后 nan_to_num
路由time-series 点预测(非概率预测,输出 deterministic forecast)

环境

  • 操作系统:Linux aarch64,昇腾 910B(Ascend910_9362),CANN 8.5.1
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,momentfm 0.1.4
  • 依赖见 requirements.txt:torch、torch_npu、transformers、momentfm、safetensors、numpy、huggingface_hub

安装

pip install -r requirements.txt
# 昇腾 NPU 需确保 torch 与 torch_npu 版本匹配,并已加载 Ascend 驱动/固件
# 权重已缓存在 /opt/atomgit/adapt-npu-agent/models/MOMENT-1-large,无需重复下载;缺失时自动从 HuggingFace 拉取
# 本地若仅有 pytorch_model.bin,inference.py 会自动转换为 model.safetensors 以兼容 from_pretrained

NPU 推理

inference.py 为自包含脚本,默认使用合成可复现时序(seed 0)在 npu:0 上完成 CPU 基线、NPU 推理、一致性与性能基准。核心 forward 的所有参数与参与计算的 Tensor 均显式位于 npu:0(见 npu_device_call.png 与 model_result.png)。计时前后均调用 torch.npu.synchronize()。

# 方式一:权重已缓存在本地(离线)
python inference.py

# 方式二:自动从 HuggingFace 下载(需联网,首次约 1.3 GB)
python inference.py

# 方式三:指定本地权重目录
MODEL_DIR=/path/to/MOMENT-1-large python inference.py

设备选择固定为 npu:0(可用时),通过 copy.deepcopy 保持 CPU/NPU 预测头权重一致,避免随机头差异。权重为纯 reconstruction 预训练,forecasting head 为随机初始化但参数在 CPU/NPU 间以 deepcopy 保持一致,故 comparison 关注数值一致性而非预训练精度。

真实结果(Ascend 910B 单卡,npu:0)

以合成时序 seq_len=512, batch=2, n_channels=1, horizon=96 为例(seed 0,torch.randn):

  • 输入:x_enc shape (2,1,512),均值 0.0322,标准差 1.0280,范围 [-3.1537, 4.1015]
  • CPU 基线:forecast shape (2,1,96) 均值 0.030594,标准差 0.085816,最值 [-0.233041, 0.315949],样例 batch0前5 horizon [0.04463139, 0.16241003, 0.10950955, 0.11929408, 0.05555399]
  • NPU 输出:相同输入在 npu:0 上 forecast shape (2,1,96) 均值 0.030594,标准差 0.085592,最值 [-0.233041, 0.315948],样例 [0.04463124, 0.16240964, 0.10950952, 0.11929430, 0.05555389]
  • 推理状态:[STATUS] SUCCESS - NPU inference completed without CPU fallback

完整输出见 model_result.png。

CPU-NPU 一致性

使用相同权重(通过 deepcopy 保持随机预测头一致)、相同数据切片(seed 0)、相同预处理、相同随机种子与 float32 精度,分别在 CPU 与 npu:0 上运行并保存原始输出,用 scripts/compare_outputs.py 比较:

python scripts/compare_outputs.py --cpu /tmp/moment_large_cpu.npy --npu /tmp/moment_large_npu.npy --task forecasting --atol 1e-4 --rtol 1e-3
  • 点预测(forecast):shape (2,1,96) 一致,max_abs 1.83e-06,mean_abs 2.81e-07,atol=1e-4 rtol=1e-3 PASS
  • 有限值:CPU 与 NPU 均 all_finite True
  • 阈值:atol=1e-4 rtol=1e-3(FP32 跨硬件正常波动,T5 编码器含 LayerNorm/GELU,误差远低于阈值,large 模型 d_model 1024 仍保持 1e-6 级)

结果:PASS。数值差异在 fp32 跨硬件正常波动范围内。

性能基准

在 npu:0、float32 下,预热 3 次后正式计时 10 次(batch=2, seq_len=512, horizon=96, n_channels=1):

指标值
compile/first-run253.23 ms(首次编译含图编译) / 21.88 ms(首轮计时)
avg / min / max20.78 / 20.38 / 21.88 ms
p50 / p90 / p9520.68 / 21.12 / 21.50 ms
throughput96.27 windows/s (batch 2)
dtypefloat32,batch 2,seq_len 512,horizon 96
权重显存~1325 MB (float32, 347M params),NPU HBM 64 GB 未超限
预处理计时不含 CSV 读取,仅模型 forward;RevIN+patching 已计入

首轮编译时间与稳定推理时间分开报告,计时前后均调用 torch.npu.synchronize()。

证据图

三张 PNG 由 xterm.js 根据本次真实日志生成(非手工绘图),深灰背景、白色等宽字体,固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$(仅展示标签,不证明服务器身份):

  • assets/agent_workflow.png:侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark 和提交校验的流水日志
  • assets/npu_device_call.png:npu-smi info、NPU 可用性、设备名、模型参数 device/dtype、输入与输出 Tensor device
  • assets/model_result.png:默认 python inference.py 的完整输出,包括 schema/窗口摘要、真实预测、同步耗时和状态

agent workflow npu device model result

图片由 scripts/render_xterm_evidence.mjs --style raw 渲染,源日志经 ANSI 清理并脱敏后生成。

限制

  • 预测头为随机初始化(未 fine-tune),本仓验证关注硬件一致性与部署能力,不提供金融/医疗等关键任务精度承诺。
  • 仅验证 seq_len 512 + horizon 96 + patch 8 的点预测路径;分类/异常检测/重构路径未在本仓 benchmark。
  • 单样本 smoke consistency,不能称为完整数据集精度评测;完整评测需在业务数据集上以 MAE/RMSE 评估。
  • NPU 仅验证 Ascend 910B 单卡 npu:0,未验证多卡或 910A/C。
  • 若模型须对长时间序列执行 fit(),本仓仅适配推理,不声称训练已完成 NPU 适配。

复现

见 inference.py 与本 README。权重从 pytorch_model.bin 加载时若缺 model.safetensors 会自动本地转换,无需联网。