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)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) 计算torch.npu.is_available()=True, device_count=2/tmp/moment_test (HuggingFace snapshot, commit 5e44b0ea26376a176360f87831124e018f876d96)t=linspace(0,4π,512), x[b,c,t]=amp*sin(freq*t+phase)+0.1*randn, 频率 1.0/1.5/2.0, 归一化前均值 0.0227pip 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# 默认执行 (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 masktorch.npu.synchronize() 计时, 验证首参数 next(model.parameters()).device == npu:0执行命令: 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[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-06embedding (representation learning) 与 reconstruction (imputation) 均为官方预训练任务, forecasting head 未预训练仅演示链路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-3next(model.parameters()).device == npu:0 且 input.device == npu:0条件: 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)| 任务 | 设备 | avg | min | max | p50 | p90 | p95 | throughput |
|---|---|---|---|---|---|---|---|---|
| embedding | NPU Ascend910 | 11.18 ms | 11.06 | 11.28 | 11.16 | 11.28 | 11.28 | 89.45 windows/s (178.9 samples/s) |
| reconstruction | NPU | 11.80 ms | 11.69 | 11.93 | - | - | - | 84.7 windows/s |
| embedding | CPU (EPYC) | 1101 ms | - | - | - | - | - | 0.91 windows/s |
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/FAILforecasting / classification heads 未预训练 (momentfm 警告 "Only reconstruction head is pre-trained"), 本仓演示链路但精度需微调后评估anomaly_detection 通过 reconstruction + mse 计算, 本次未单独评估阈值与 F1model.half() 但需重验一致性Hardware: NPU, NPU, Ascend, Ascend910, Ascend910B, time-series, tabular, forecasting, anomaly-detection, imputation, MOMENT, foundation-model #NPU