#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 步点预测。
pytorch_model.bin 约 1.32 GB(本地已转换为 model.safetensors 1.38 GB 用于 from_pretrained 本地加载),config.json seq_len 512main| 项 | 值 |
|---|---|
| 模型 | 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) |
torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,momentfm 0.1.4pip 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_pretrainedinference.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 关注数值一致性而非预训练精度。
以合成时序 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]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: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。
使用相同权重(通过 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-3max_abs 1.83e-06,mean_abs 2.81e-07,atol=1e-4 rtol=1e-3 PASSatol=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-run | 253.23 ms(首次编译含图编译) / 21.88 ms(首轮计时) |
| avg / min / max | 20.78 / 20.38 / 21.88 ms |
| p50 / p90 / p95 | 20.68 / 21.12 / 21.50 ms |
| throughput | 96.27 windows/s (batch 2) |
| dtype | float32,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 deviceassets/model_result.png:默认 python inference.py 的完整输出,包括 schema/窗口摘要、真实预测、同步耗时和状态

图片由 scripts/render_xterm_evidence.mjs --style raw 渲染,源日志经 ANSI 清理并脱敏后生成。
npu:0,未验证多卡或 910A/C。fit(),本仓仅适配推理,不声称训练已完成 NPU 适配。见 inference.py 与本 README。权重从 pytorch_model.bin 加载时若缺 model.safetensors 会自动本地转换,无需联网。