atlasleong/timemoe-200m-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TimeMoE-200M 昇腾 NPU 时序预测推理

本项目将 Maple728/TimeMoE-200M(Time-MoE 200M,基于混合专家 MoE 的 decoder-only 时序基础模型)在华为昇腾 NPU 上完成部署与推理验证。

  • 任务类型(pipeline_tag):time-series-forecasting
  • 推理框架:PyTorch + torch_npu
  • 推理设备:npu:1(Ascend 910B4-1,CANN 8.5.1)
  • 模型架构:TimeMoeForPrediction(MoE 解码器,8 experts / top-2)
  • 引擎:torch_npu

模型简介

Time-MoE 是首个将时序基础模型扩展到十亿级参数的工作(论文 Time-MoE: Billion-Scale Time Series Foundation Models with Mixture of Experts, ICLR 2025 Spotlight)。TimeMoE-200M 是其中的 large 版本,采用 decoder-only 自回归架构,通过混合专家(MoE)提升容量,支持任意预测长度与最长 4096 的 上下文。

本交付使用官方实现源码(固定 revision 915bfda4c78a544d62a2bec6ab22948423059236,文件 time_moe/models/modeling_time_moe.py 中的真实 TimeMoeForPrediction), 加载本地固定模型权重 model.safetensors(HF 仓库 revision 794591bfeb1225fdf742cec0f4c71f20c3f3b87e,字节数 906450104), 在昇腾 NPU npu:1 上完成端到端时序预测。推理过程中不做 CPU 回退, NPU 初始化、张量放置或执行失败时以非零退出码失败。

模型关键配置(config.json):

配置项值
hidden_size768
intermediate_size3072
num_hidden_layers12
num_attention_heads / kv_heads12 / 12
num_experts / num_experts_per_tok8 / 2
horizon_lengths[1, 8, 32, 64]
max_position_embeddings4096
torch_dtypebfloat16

环境依赖清单

容器已提供匹配的 torch==2.9.0+cpu 与 torch_npu==2.9.0,严禁重新安装 或用普通 PyPI wheel 替换。其余依赖仅从 https://mirrors.aliyun.com/pypi/simple/ 安装。

组件版本说明
操作系统openEuler (aarch64)容器环境
NPU 硬件Ascend 910B4-1 × 8分配设备 npu:1
CANN8.5.1(cann-8.5.1)/usr/local/Ascend
npu-smi25.5.1设备监控
Python3.11.14容器自带
torch2.9.0+cpu容器提供,勿替换
torch_npu2.9.0容器提供,勿替换
transformers4.57.6与官方实现兼容(>=4.57.0)
safetensors0.8.0权重加载
numpy1.26.4数值处理
Pillow12.3.0截图渲染(交付工具)

requirements.txt 仅固定顶层非 torch 依赖;torch/torch_npu 由容器提供。

分步推理操作流程

1. 加载 CANN 环境

source /usr/local/Ascend/ascend-toolkit/set_env.sh

2. 创建虚拟环境并安装依赖

python3 -m venv --system-site-packages /work/pipeline/runtime/venvs-tabular16/timemoe-200m-npu
export PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
/work/pipeline/runtime/venvs-tabular16/timemoe-200m-npu/bin/pip install -r requirements.txt

使用 --system-site-packages 以复用容器匹配的 torch/torch_npu,仅在 venv 内安装其余依赖。

3. 运行端到端推理(含并发 npu-smi 监控)

cd /work/pipeline/jobs/tabular16/timemoe-200m/timemoe-200m-npu
bash scripts/run_inference.sh

该脚本会:

  • source CANN 环境;
  • 启动一个并发的 npu-smi info + npu-smi info -t proc-mem 监控进程, 把每个采样写入 logs/npu_device_call.log;
  • 在 npu:1 上运行 inference.py(context=32,prediction=8,batch=2), 原始输出写入 logs/npu_inference.log;
  • 生成结构化结果 logs/npu_result.json(pid 为 npu-smi 可见的 host PID, 另记录 container_pid);
  • 生成脱敏日志 logs/npu_inference.sanitized.log 与 logs/npu_device_call.sanitized.log。

也可以直接运行:

/work/pipeline/runtime/venvs-tabular16/timemoe-200m-npu/bin/python inference.py \
  --device npu:1 --context-length 32 --prediction-length 8 --batch-size 2 \
  --output logs/npu_result.json

4. 重新渲染交付截图(可选)

/work/pipeline/runtime/venvs-tabular16/timemoe-200m-npu/bin/python scripts/render_assets.py

截图全部从真实脱敏日志渲染,不写入任何手工构造的数值。

完整测试用例

测试输入为自生成的确定性 CC0 时序数据(无外部数据集、不使用随机数): y(t) = 0.2·t + amp·sin(2π·t/12 + φ) + 1.5·sin(2π·t/7 + 0.5b), 其中 batch=2,context_length=32,prediction_length=8。

运行命令(真实执行):

bash scripts/run_inference.sh

关键日志(logs/npu_inference.log,真实输出;pid/start_time/load_s/latency_s 随运行略有变化,数值字段确定不变):

[corr] baseline_pids=[]
[env] pid=121454
[env] start_time=2026-08-22T16:25:57.520001Z
[env] torch=2.9.0+cpu
[env] torch_npu=2.9.0
[env] transformers=4.57.6
[env] npu_available=True npu_count=8 npu_device_name=Ascend910B4-1
[env] cann=cann-8.5.1
[env] assigned_device=npu:1
[weights] source_revision=794591bfeb1225fdf742cec0f4c71f20c3f3b87e
[weights] impl_revision=915bfda4c78a544d62a2bec6ab22948423059236
[weights] weight=model.safetensors bytes=906450104 sha256=28d1cbf480c1f63d4ad56d8b7649d9860b9afa9f31abeedff9decea48d9aee57 sha_time=0.67s
[input] source=self-authored-CC0-deterministic-series license=CC0 shape=(2,32) raw_mean=3.697878
[input] input_device=npu:1 input_dtype=torch.bfloat16
[model] load_s=3.87 strict_local_safetensors=True
[model] npu_resident_hold_s=2.0
[corr] container_pid=121454 host_pid=2075412 samples=5 new_pids={"2075412": [1050.0, 1050.0, 1050.0, 1050.0, 1050.0]}
[model] num_params=453196800 model_device=npu:1 dtype=torch.bfloat16
[inference] output_device=npu:1 output_shape=[2, 40] latency_s=0.5688
[forecast] shape=[2, 8] stats={"min": -1.933887243270874, "max": 6.564408779144287, "mean": 2.034532070159912, "std": 2.768984079360962}
[forecast] sample=[1.790663, 0.5791, 0.395065, 1.238559, 2.549808, 4.029756, 5.202978, 6.268847]
[forecast] finite=True num_values=16
[router] num_experts=8 topk=2 total_tokens=64
[router] aggregate_expert_selection_counts=[200, 192, 245, 158, 202, 150, 121, 268]
[mem] peak_npu_memory_mb=930.421875
[result] wrote .../logs/npu_result.json
[exit] code=0 pid=2075412

输出结果

  • NPU 设备名:Ascend910B4-1(npu_device_name)
  • 模型 revision(固定):794591bfeb1225fdf742cec0f4c71f20c3f3b87e (source_revision)
  • 推理进程:pid=2075412(npu-smi 可见的 host PID)、container_pid=121454
  • 输入张量设备:npu:1
  • 模型参数设备:npu:1(453,196,800 参数,bfloat16)
  • 输出张量设备:npu:1
  • 输出形状:[2, 40](32 上下文 + 8 预测),预测切片 [2, 8]
  • 推理时延:0.5688 s
  • 峰值 NPU 内存:930.42 MB
  • 预测统计:min=-1.9339, max=6.5644, mean=2.0345, std=2.7690, 样本(第 1 条序列)[1.7907, 0.5791, 0.3951, 1.2386, 2.5498, 4.0298, 5.2030, 6.2688]
  • MoE 路由(专家)统计:8 experts / top-2 / 64 tokens,聚合专家选择计数 [200, 192, 245, 158, 202, 150, 121, 268]

并发 npu-smi info / npu-smi info -t proc-mem 监控 (logs/npu_device_call.log)在推理期间捕获到推理进程(host PID 2075412, container PID 121454)附着于 NPU 1,进程内存最高 1100 MB;同时 NPU 1 HBM 从基线约 3400 MB 升至峰值 4396 MB(+996 MB)。 npu_result.json 中的 pid 与 npu-smi 进程表行一一对应 (PID_CORRELATION ... MATCH),证明推理进程确实运行于分配的 NPU。

结构化结果:logs/npu_result.json(含 engine、exit code、NPU 可用性/名称/数量、 分配设备、输入/模型/输出设备、输出形状、时延、峰值内存、预测统计、路由统计、 权重 SHA256、pid/container_pid/start_time/end_time、strict_load、 checkpoint 与顶层 output 对象等)。

证据文件

文件说明
inference.py推理主脚本(torch_npu,npu:1)
requirements.txt顶层依赖固定(Aliyun 镜像)
scripts/run_inference.sh可复现运行脚本(含并发 npu-smi 监控与 PID 关联)
scripts/render_assets.py截图渲染脚本(从真实日志)
scripts/validate_delivery.py交付校验脚本
logs/npu_inference.log原始推理日志
logs/npu_inference.sanitized.log脱敏推理日志
logs/npu_device_call.log原始 npu-smi 监控日志(含 proc-mem)
logs/npu_device_call.sanitized.log脱敏 npu-smi 监控日志
logs/npu_result.json结构化结果
assets/agent_workflow.png工作流截图(真实日志渲染)
assets/modelagent_live_session.pngModelAgent 现场会话截图
assets/npu_device_call.pngnpu-smi 监控截图(真实日志渲染)
assets/model_result.png推理结果截图(真实日志渲染)

版本与修订

项目值
HF 模型仓库Maple728/TimeMoE-200M
模型 revision(固定)794591bfeb1225fdf742cec0f4c71f20c3f3b87e
官方实现仓库 revision(固定)915bfda4c78a544d62a2bec6ab22948423059236
权重文件model.safetensors
权重字节数906450104
权重 SHA25628d1cbf480c1f63d4ad56d8b7649d9860b9afa9f31abeedff9decea48d9aee57

权重 SHA256 由本交付在推理前实时计算并与清单字节数校验,未改动任何权重。

故障排查(Troubleshooting)

现象可能原因处理
torch.npu.is_available() is FalseCANN 环境未加载source /usr/local/Ascend/ascend-toolkit/set_env.sh
No module named 'torch_npu'torch_npu 未安装或被覆盖使用容器自带 Python/venv,勿重装 torch 对
设备越界--device 超出 npu_count使用 npu:1(本机 8 卡)
算子报 not supported on NPU依赖了 CUDA 专属算子本模型官方源码已使用标准 PyTorch/torch_npu 算子
日志尾部出现 path string is NULLtorch_npu 运行时的良性提示已在下游脚本中从脱敏日志剥离,不影响结果
输出含非有限值输入含 NaN/Inf检查输入序列;本交付输入为确定性有限序列

局限性(Limitations)

  • 本交付聚焦单卡 NPU 推理,未覆盖训练/微调与多卡分布式。
  • 上下文长度 + 预测长度之和不应超过 max_position_embeddings(4096), 超出需按官方说明微调。
  • 输入归一化(mean/std)在 CPU 完成,逆归一化与结果格式化也在 CPU 完成 (均已记录);神经 forward/generation 路径全程在 npu:1。
  • 未安装 flash-attn,推理使用标准 attention,性能较 flash-attn 版本有差异。
  • ASCEND_GLOBAL_LOG_LEVEL=3 仅保留 ERROR 级 CANN 日志以保持日志脱敏简洁。

许可证与使用限制(License & Use Restrictions)

  • 模型权重仓库 Maple728/TimeMoE-200M 声明许可证:Apache-2.0。
  • 官方实现仓库(Time-MoE)许可证:Apache-2.0。
  • 测试输入为本交付自生成的确定性 CC0 数值序列,可再分发,无外部数据依赖。
  • 请遵守上游 Apache-2.0 条款;不得将本交付中的凭证、密钥或本地权重外泄。
  • 本仓库为模型迁移交付物,不含模型权重文件(见 .gitignore)。

昇腾 NPU 适配证据截图

Agent 完整适配工作流

Agent 完整适配工作流

ModelAgent 现场会话

ModelAgent 现场会话

NPU 硬件设备调用日志

NPU 硬件设备调用日志

模型最终适配验收结果

模型最终适配验收结果