模型适配 · 时序预测 Foundation Model (Mixture of Experts) · 昇腾 NPU
Beta 版本头信息
{hardware NPU}[batch, context_length, input_size],自回归输出未来 horizon 步的预测值 [batch, horizon, input_size]。支持多预测步长 (horizon_lengths = [1, 8, 32, 64]),换挡预测时输出截断到目标步长。TimeMoeInputEmbedding(门控线性嵌入)→ 12 × TimeMoeDecoderLayer(MoE FFN: 8 experts 稀疏路由 + attention)→ RMSNorm → 多 horizon 输出头[batch, seq_len, input_size](默认 input_size=1,单变量),推荐 zero-normalizationhorizon 步预测 [batch, horizon, input_size]modeling_time_moe.py / configuration_time_moe.py / ts_generation_mixin.py)与官方 checkpoint vendor 为自包含代码(time_moe/ 目录),去除对远程代码 (trust_remote_code) 的依赖;针对 transformers 4.40 → 4.57.6 的 API 变更做了最小兼容修补(seen_tokens → get_seq_length()、get_usable_length 移除、get_max_length 移除等)。注意力实现固定为 eager(NPU 上 sdpa/flash 走 aclnnFlashAttentionScore 会报 ERR00100)。推理采用逐步自回归 model.forward(直接调用,绕开 transformers 4.57.6 与官方 TSGenerationMixin._greedy_search 生成管线的兼容性问题)。权重键名与官方 checkpoint 完全一致,strict 加载无缺失。{hardware NPU} Ascend 910B(torch_npu 2.9.0.post1)| 组件 | 版本 |
|---|---|
| torch | 2.9.0 |
| torch-npu | 2.9.0.post1 |
| python | 3.11.14 |
| transformers | 4.57.6(模型代码 vendor 后仅用于 PretrainedConfig/PreTrainedModel 基类) |
| CANN | 8.5.1 |
| NPU | Ascend 910B (Ascend910_9362) × 2 |
| 依赖 | numpy / safetensors / huggingface_hub / torch-npu |
同一权重(
Maple728/TimeMoE-50M),demo 时序 = 正弦 + 线性趋势 + 噪声(固定 seed=42, context_length=96, horizon=48),CPU 与 NPU 均为 fp32 +eval(),zero-normalization 前后处理一致。
| 指标 | 数值 |
|---|---|
| 余弦相似度 (cosine similarity) | 1.000000 |
| 最大绝对误差 (max_abs_err) | 1.10×10⁻⁵ |
| 平均绝对误差 (mean_abs_err) | 3.24×10⁻⁶ |
误差来自 NPU 与 CPU 上 fp32 算子累加顺序差异(~1e-5 量级),对预测精度影响可忽略,余弦相似度 1.000000,精度达标。
demo 时序 context_length=96 → horizon=48(自回归 48 步),预热后单次计时,NPU:0。
| 设备 | 单次推理耗时 |
|---|---|
| CPU (x86_64 参考) | ~4.73 s |
| NPU (Ascend 910B) | ~915 ms |
NPU 相对 CPU 约 5.2× 加速。性能上界主要受限于 48 步自回归串行推理(逐步依赖)与多步长输出头计算;如需更高吞吐可 batch 多条序列或减短 horizon。
pip install -r requirements.txt# NPU 默认演示: context_length=96, horizon=48 (正弦+趋势+噪声 demo 数据)
python3 inference.py --device npu:0# 自定义历史长度与预测步长
python3 inference.py --device npu:0 --context-length 192 --horizon 96python3 inference.py --compare --context-length 96 --horizon 16python3 inference.py --device npu:0 --serve --port 8000# 健康检查
curl http://127.0.0.1:8000/health
# 预测 (POST, 传入历史时序 values)
curl -X POST http://127.0.0.1:8000/predict \
-H "Content-Type: application/json" \
-d '{"values": [1.0, 1.2, 0.9, 1.1, 1.3, 1.0], "horizon": 4}'权重下载通道(按优先级):
/tmp/models/TimeMoE-50M/(当前已就绪)https://gitcode.com/hf_mirrors/Maple728/TimeMoE-50M.git(LFS batch API, 226.7 MB)HF_ENDPOINT=https://hf-mirror.com(需代理外网可用){model_name}/
├── inference.py # 推理脚本(必选)—— 演示/对比/服务化
├── README.md # 部署说明文档(必选)
├── requirements.txt # 环境依赖清单
├── time_moe/ # vendor 自包含模型实现(官方 checkpoint 键名一致)
│ ├── __init__.py
│ ├── configuration_time_moe.py
│ ├── modeling_time_moe.py
│ └── ts_generation_mixin.py
└── assets/ # 截图素材文件夹
├── agent_workflow.png
├── npu_device_call.png
└── model_result.pngmodel.safetensors (226,760,264 B, SHA256 校验一致)time_moe/ 包,strict 加载 0 缺失seen_tokens/get_usable_length/get_max_length)