Maple728/TimeMoE-50M(Time-MoE 时间序列基础模型,Mixture of Experts,输入模态为数值时间序列)
在 昇腾 Ascend910 NPU 上基于 torch-npu 的适配与推理实现。
本仓库仅含适配代码与说明,权重不入库;推理时从镜像自动下载到本地缓存
./model_weights/Maple728__TimeMoE-50M(已加入 .gitignore)。
本适配遵循统一强制模板:全域无 CUDA,模型与输入 Tensor 全部迁移至
npu:0, 入口try-except捕获 NPU 初始化异常并优雅退出。 该模型使用自定义代码(TimeMoeForPrediction),加载时必须trust_remote_code=True。
| 项 | 值 |
|---|---|
| 硬件 | Ascend910 × 2(本实现固定使用 npu:0) |
| 框架 | torch 2.9.0 + torch_npu 2.9.0.post1 |
| 模型 | Maple728/TimeMoE-50M(Time-MoE,MoE,~50M 参数,数值时间序列预测) |
| 权重源 | hf-mirror.com 镜像(HF_ENDPOINT),ModelScope 兜底 |
验证 NPU 可用性:
python3 -c "import torch, torch_npu; print(torch.npu.device_count(), torch.npu.is_available())"pip install -r requirements.txt输入为数值时间序列(非图像/文本)。CLI 使用 --text 传入逗号分隔的浮点序列:
"1.23,4.56,7.89,3.21,6.54,..."FastAPI POST /predict 的 JSON body 使用 text 数组字段:
{"text": [1.23, 4.56, 7.89, 3.21, 6.54], "horizon": 8}模型将历史序列编码后,输出未来 horizon 步的预测值。horizon 可选值:1 / 8 / 32 / 64(对应 config.horizon_lengths),默认 8。
python inference.py --text "1.23,4.56,7.89,3.21,6.54,1.11,2.22,3.33,4.44,5.55,6.66,7.77,8.88,9.99,10.01,1.23,4.56,7.89,3.21,6.54,1.11,2.22,3.33,4.44,5.55,6.66,7.77,8.88,9.99,10.01" --horizon 8输出示例(真实运行,Ascend910 NPU):
============================================================
模型 : Maple728/TimeMoE-50M
设备 : npu:0
[NPU] device=npu:0 available=True count=2 name=Ascend910_9362 torch=2.9.0+cpu
输入形状 : [1, 30, 1] (B, seq_len, input_size=1)
最近观测 : 10.01
预测步数 : 8
预测结果 : [8.793159, 7.683948, 6.507532, 5.354751, 4.429321, 3.68064, 3.108883, 2.660882]
耗时 : 279.724ms
============================================================uvicorn inference:app --host 0.0.0.0 --port 8004请求:
curl -s -X POST http://127.0.0.1:8004/predict \
-H "Content-Type: application/json" \
-d '{"text":[1.23,4.56,7.89,3.21,6.54,1.11,2.22,3.33,4.44,5.55,6.66,7.77,8.88,9.99,10.01,1.23,4.56,7.89,3.21,6.54,1.11,2.22,3.33,4.44,5.55,6.66,7.77,8.88,9.99,10.01],"horizon":8}'响应 JSON(真实运行):
{
"model": "Maple728/TimeMoE-50M",
"device": "npu:0",
"input_shape": [1, 30, 1],
"horizon": 8,
"last_observed": 10.01,
"forecast": [8.793159, 7.683948, 6.507532, 5.354751, 4.429321, 3.68064, 3.108883, 2.660882],
"forecast_raw": [8.793158531188965, 7.683948040008545, 6.507532119750977, 5.354751110076904, 4.429320812225342, 3.68064022064209, 3.1088831424713135, 2.660881519317627],
"latency_ms": 287.627
}健康检查:GET /health → {"status": "ok", "device": "npu:0"}
AutoConfig + AutoModelForCausalLM,trust_remote_code=True(自定义 TimeMoeForPrediction)。attn_implementation="eager"(NPU 上不启用 flash-attn);权重按 float32 加载,保证算子兼容。[1, seq_len, 1](input_size=1),单次前向 model(input_ids=x, max_horizon_length=horizon),
取最后一个位置输出 logits[0, -1, :] 作为未来 horizon 步预测。model_weights/Maple728__TimeMoE-50M,命中跳过下载;
未命中则通过 HF_ENDPOINT=https://hf-mirror.com 镜像自动下载,失败时 ModelScope 兜底。.to("npu:0"),输出 cpu() 后转列表返回。TimeMoE-50M-npu/
├── inference.py # 单文件双模式推理脚本(CLI + FastAPI)
├── requirements.txt
├── README.md
├── .gitignore # 忽略 model_weights/ 权重缓存
└── assets/ # 运行截图
├── agent_workflow.png # NPU 环境部署与任务启动
├── npu_device_call.png # CLI 推理真实输出
└── model_result.png # FastAPI 返回真实 JSON三张截图均由真实命令输出经 render_terminal.py 渲染:
assets/agent_workflow.png:NPU 环境检测、依赖、权重下载、任务启动日志。assets/npu_device_call.png:python inference.py --text "..." 的 CLI 推理输出。assets/model_result.png:curl POST /predict 的 FastAPI 真实响应。