Maple728/TimeMoE-200M(时间序列预测模型,pipeline_tag=time-series-forecasting,架构
TimeMoeForPrediction,MoE 大模型)在 昇腾 Ascend910 NPU 上基于 torch-npu 的适配与推理实现。
本仓库仅含适配代码与说明,权重不入库;推理时从镜像自动下载到本地缓存
./model_weights/Maple728__TimeMoE-200M(已加入 .gitignore)。
本适配遵循统一强制模板:全域无 CUDA,模型与输入 Tensor 全部迁移至
npu:0, 入口try-except捕获 NPU 初始化异常并优雅退出;trust_remote_code=True必需。
| 项 | 值 |
|---|---|
| 硬件 | Ascend910 × 2(本实现固定使用 npu:0) |
| 框架 | torch 2.9.0 + torch_npu 2.9.0.post1 |
| 模型 | Maple728/TimeMoE-200M(TimeMoeForPrediction,200M,8 experts) |
| 输入 | 数值时间序列(Numeric,非文本/图像),形状 [batch, context_len, input_size=1] |
| 输出 | 未来 horizon 步的预测数值序列 |
| 权重源 | 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.txtpython inference.py --text "1.23,4.56,7.89,..."--text:逗号分隔(或 JSON 数值数组,如 "[1.23,4.56,7.89]")的数值时间序列(必填)。--horizon:预测步长(可选,默认 32)。输出示例(真实运行,120 点输入):
============================================================
模型 : Maple728/TimeMoE-200M
设备 : npu:0
[NPU] device=npu:0 available=True count=2 name=Ascend910_9362 torch=2.9.0+cpu
输入序列 : 120 个数值,前8: [50.0, 52.4977, 54.9816, 57.4383, 59.8551, 62.2205, 64.5244, 66.7583]
输入形状 : [1, 120, 1] (batch, context_len, input_size)
预测步长 : 32
预测结果 : 前8: [289.75, 291.367188, 293.523438, 294.601562, 296.21875, 297.835938, 298.375, 299.992188] 后4: [294.601562, 294.601562, 292.984375, 292.445312]
耗时 : 0.377s
============================================================uvicorn inference:app --host 0.0.0.0 --port 8003请求:
curl -s -X POST http://127.0.0.1:8003/predict \
-H "Content-Type: application/json" \
-d '{"text": [1.23, 4.56, 7.89, ...], "horizon": 32}'响应 JSON(真实 HTTP 200):
{
"model": "Maple728/TimeMoE-200M",
"device": "npu:0",
"input_length": 120,
"horizon": 32,
"forecast": [289.75, 291.367188, 293.523438, 294.601562, ...],
"head8": [289.75, 291.367188, 293.523438, 294.601562, 296.21875, 297.835938, 298.375, 299.992188],
"tail4": [294.601562, 294.601562, 292.984375, 292.445312],
"latency_s": 0.3736
}健康检查:GET /health → {"status": "ok", "device": "npu:0"}
AutoModelForCausalLM.from_pretrained(..., trust_remote_code=True),
torch_dtype=bfloat16,eval(),全量 .to("npu:0")。horizon_lengths=[1,8,32,64]
分块自回归预测(forward(..., max_horizon_length, use_cache=False) 取末位输出)→
反归一化得到未来 horizon 步预测。model_weights/Maple728__TimeMoE-200M,命中跳过下载;
未命中则通过 HF_ENDPOINT=https://hf-mirror.com 镜像自动下载;若 hf-mirror 失败
自动尝试 ModelScope。.to("npu:0"),输出 cpu() 后转列表返回。generate 路径存在 DynamicCache API 差异,故本脚本直接调用 forward(
use_cache=False)实现预测,规避该差异。TimeMoE-200M-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 真实响应。