#NPU Ascend 910 适配 | 原始模型: Maple728/TimeMoE-50M | 任务: 时序预测 (Time Series Forecasting)
时间序列基础模型 Time-MoE 50M 的昇腾 NPU 单卡推理适配,支持多 horizon 预测(1,8,32,64),已在 npu:0 真实完成推理验证。
time_moe / TimeMoeForPrediction (Decoder-only Transformer with Mixture-of-Experts)model.safetensors 226,760,264 bytes (约 217 MB), BF16446753ee48ff3726d0606a81d0092d54acee995e (2024-10-22)time-series-forecasting, Apache-2.0configuration_time_moe.py, modeling_time_moe.py, ts_generation_mixin.py (已审查,无网络/子进程/文件写入风险;为兼容 transformers 4.57,已对 DynamicCache.get_usable_length / seen_tokens / get_max_length 打补丁)| 项 | 值 |
|---|---|
| 任务 | 时序预测 (forecasting) |
| 输入 shape | [B, T, C] = [2, 512, 1] (batch, time, channel) |
| 输出 shape | [B, T, H] = [2, 512, 32] (H = horizon) |
| 轴顺序 | time-major, channel last, univariate (input_size=1) |
| 频率/上下文 | 上下文 512, 预测 horizon 32 (支持 1/8/32/64 四档) |
| dtype | float32 (CPU 与 NPU 同 dtype;BF16 也可用,见精度章节) |
| 预处理 | 无外部归一化;合成输入 = 0.5sin(2π t/50) + 0.5randn, seed=42, 已固定 |
| 来源 | 合成可复现数据 (无外部数据集依赖) |
| Schema | 输入 float32, 输出 float32 forecast tensor, 有限值校验通过 |
npu:0, HBM 64 GBpip install -r requirements.txt模型权重通过 HF_ENDPOINT=https://hf-mirror.com 自动拉取,无需手动设置 http_proxy。
本地已缓存时直接从 working/models/TimeMoE-50M 加载;否则从 Hub 拉取 Maple728/TimeMoE-50M (trust_remote_code=True)。
python inference.py默认行为: 加载模型到 npu:0, 构造 [2,512,1] 输入, use_cache=False, max_horizon_length=32, 预热 3 次, 正式 10 次同步计时 (torch.npu.synchronize()), 打印设备/参数/输入输出摘要/耗时/吞吐。
退出码 0 表示成功;非 0 表示异常 (无静默 CPU fallback)。
batch 2, seq 512, input_size 1, mean 0.0112 std 0.6020(2, 512, 32), device npu:0, dtype torch.float32[0.8988, 0.8556, 0.8174, 0.7780, 0.7440]assets/model_result.png 对应 python inference.py 真实 stdoutuse_cache=False、eval 模式,分别保存 CPU 与 NPU 输出为 .npy,运行 scripts/compare_outputs.pypython scripts/compare_outputs.py --cpu working/cpu_timemoe.npy --npu working/npu_timemoe.npy --task forecasting --atol 1e-4 --rtol 1e-3
=> max_abs 4.25e-06, mean_abs 5.34e-07, passed Trueatol 0.1 rtol 5e-2 方可 PASS,故本适配默认用 float32 以满足严格阈值;阈值已写入 README.to("npu:0"),日志打印首个参数 device 为 npu:0,无 CPU fallbacktorch.npu.synchronize(),首轮编译与稳定推理分开报告first_run 64.45ms (含首次编译后稳定)
avg 64.38ms min 64.28ms max 64.54ms p50 64.34ms p90 64.54ms p95 64.54ms
throughput 31.1 windows/s (15.5 series/s per 512 window)三张 PNG 由 xterm.js 根据本次真实日志生成 (scripts/render_xterm_evidence.mjs --style raw):
assets/agent_workflow.png (对应 working/logs/workflow.log)assets/npu_device_call.png (对应 working/logs/device.log)assets/model_result.png (对应 working/logs/inference.log + python inference.py)固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签。
fit() 不涉及);NPU 训练未声明allow_internel_format 警告 benigngenerate() 自回归多步展开因 transformers 4.57 Cache API 变更暂不稳定,推荐用 forward(max_horizon_length=...) 直接预测.
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
├── agent_workflow.png
├── npu_device_call.png
└── model_result.png