冬
gcw_IDzXRVNw/TimeMoE-50M-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TimeMoE-50M 昇腾 NPU 适配部署文档

1. 模型简介

TimeMoE 是来自论文 Time-MoE: Billion-Scale Time Series Foundation Models with Mixture of Experts 的时间序列基础模型。

本模型是 50M 参数版本(实际 113.35M),采用 Mixture of Experts (MoE) 架构,在大规模时间序列数据上预训练。

模型架构

属性值
模型类型TimeMoeForPrediction (Causal LM + MoE)
参数量113.35M (total)
隐藏层维度384
Transformer 层数12
Attention 头数12
Experts 数量8
每 Token 激活 Experts2
输入维度1 (单变量)
支持 Horizon 长度1, 8, 32, 64
权重精度bfloat16

模型特点

  • Mixture of Experts: 稀疏激活机制,只激活 8 个专家中的 2 个
  • 多 Horizon 预测: 支持同时预测多个时间跨度
  • 生成式: 基于 Causal LM 的时间序列生成
  • 无 Tokenizer: 直接操作原始浮点时间序列
  • 轻量化: 相比 200M 版本,hidden_size 从 768 降至 384

输入输出格式

  • 输入: 形状 (batch_size, input_len) 的时间序列(浮点数)
  • 输出: 形状 (batch_size, input_len + horizon) 的完整序列(包含输入 + 预测)

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 1 GB NPU 显存(TimeMoE-50M 推理峰值约 267 MB)
  • 本次实测使用 910B2(单卡 64 GB)

2.2 软件依赖

依赖项版本要求说明
操作系统CentOS 7.6+ / Ubuntu 18.04+支持主流 Linux 发行版
CANN5.0.RC2+华为昇腾计算架构
Python3.10 / 3.12推荐 3.10+
PyTorch2.0.0+推荐 2.10
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展
transformers4.40.1模型加载(必须此版本)
accelerate0.20+device_map 需要

2.3 安装方式

# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100

# 2) 安装模型依赖(使用华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple \
    transformers==4.40.1 accelerate

# 3) 验证环境
python -c "
import torch, torch_npu
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"

3. 分步推理操作流程

步骤 1:准备工作目录

# 进入适配目录
cd /workspace/agent/TimeMoE-50M-ascend

# 确认上游权重存在
ls -la /workspace/agent/TimeMoE-50M/

# 期望输出:config.json model.safetensors (~217MB) modeling_time_moe.py ...

步骤 2:检查 NPU 环境

# 检查 NPU 设备状态
npu-smi info

# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"

步骤 3:运行推理脚本

# 基本用法:默认参数 (input_len=512, horizon=64)
python inference.py --device npu:0

# 查看 NPU 显存使用
python inference.py --device npu:0 --show_npu_memory

# 自定义参数:input_len=1024, horizon=96
python inference.py --device npu:0 --input_len 1024 --horizon 96

# 指定其他 NPU 卡
python inference.py --device npu:1

# CPU 模式(仅在 NPU 不可用时使用)
python inference.py --device cpu

4. 完整测试用例

测试用例 1:标准预测(NPU:0, horizon=64)

conda activate pt2100
cd /workspace/agent/TimeMoE-50M-ascend
python inference.py --device npu:0 --show_npu_memory --input_len 512 --horizon 64

实际输出:

[W819 01:33:25.615892850 FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
[W819 01:33:32.684626380 NPUCachingAllocator.cpp:198] Warning: The current CANN and Soc Version require processing for 32 padding size...
[设备] npu:0
NPU设备数量: 1
  设备 0: Ascend910B2
[权重信息] 文件: /workspace/agent/TimeMoE-50M/model.safetensors
[配置] 模型类型: time_moe
[配置] hidden_size: 384
[配置] num_hidden_layers: 12
[配置] num_experts: 8
[配置] num_experts_per_tok: 2
[配置] horizon_lengths: [1, 8, 32, 64]
[配置] torch_dtype: bfloat16
[参数量] 113.35M

加载模型 (使用 trust_remote_code)...

模型: TimeMoE (时间序列预测, MoE架构)
  - 任务: 时间序列预测 (Mixture of Experts)
  - 参数量: 113.35M
  - Experts: 8 (每token激活 2)
  - Horizon长度: [1, 8, 32, 64]
[NPU显存 · 模型加载后] 总显存=61.0GB | 已分配=224.2MB | 缓存=252.00MB | 峰值=224.2MB

构建测试输入: 随机时间序列 (input_len=512)
输入形状: (1, 512)

运行推理...
  - Horizon: 64
  - max_length: 576
[NPU显存 · 推理完成后] 总显存=61.0GB | 已分配=224.2MB | 缓存=306.00MB | 峰值=267.0MB

==============================================================================
输出摘要
==============================================================================
  模型: TimeMoE (时间序列预测, MoE架构)
  输入形状: (1, 512)
  输出形状: (1, 576)
  Horizon: 64
  总耗时: 0.46s
  输出 finite: True
  输出均值: 0.0248
  输出标准差: 0.9161
  输出范围: [-2.5850, 2.8340]

✅ 验证要点:

  • 输出形状 (1, 576): 完整输出序列 = input_len(512) + horizon(64)
  • NPU 推理稳定: 单次推理 0.46 秒
  • NPU 显存峰值: 约 267 MB,远低于 61GB 总显存
  • 输出有限值: 所有输出均为有限值,无 NaN/Inf
  • 输出分布: 均值接近 0,标准差约 0.92(符合标准化后的时间序列特征)

5. 常见问题解答

Q1:提示 "NPU 不可用" 怎么办?

解决方案:

  1. 确认已安装 CANN 驱动并设置环境变量(source /usr/local/Ascend/ascend-toolkit/latest/set_env.sh)
  2. 运行 npu-smi info 检查设备状态
  3. 确认 PyTorch 和 torch_npu 版本匹配

Q2:为什么使用 transformers 4.40.1?

解决方案:TimeMoE 模型依赖 ts_generation_mixin.py 中的 EosTokenCriteria,该 API 在 transformers 4.40.1 中引入。更高版本可能存在兼容性问题。

Q3:输出形状为什么是 (1, 576) 而不是 (1, 64)?

解释:TimeMoE 的 generate 方法返回完整序列(输入 + 预测)。用户需要从完整输出中提取预测部分:forecast = output[:, -horizon:]。

Q4:TimeMoE-50M 和 TimeMoE-200M 有什么区别?

解释:TimeMoE-50M 是更轻量的版本,hidden_size 从 768 降至 384,intermediate_size 从 3072 降至 1536。总参数量从 453M 降至 113M。架构相同,精度略有下降但推理更快。

Q5:支持多变量时间序列吗?

解释:当前实现的 input_size=1,主要针对单变量时间序列预测。

6. 文件结构

TimeMoE-50M-ascend/
├── inference.py              # 推理脚本(torch_npu 推理)
├── README.md                 # 本文档
├── requirements.txt          # 依赖列表
└── assets/                   # 资源占位目录

上游权重目录(只读,未被修改):

/workspace/agent/TimeMoE-50M/
├── config.json               # TimeMoeConfig
├── model.safetensors         # 权重(113.35M, ~217MB)
├── modeling_time_moe.py      # 模型实现
├── configuration_time_moe.py # 配置类
├── ts_generation_mixin.py   # 生成 Mixin
├── generation_config.json    # 生成配置
└── README.md                 # 原始文档

7. requirements.txt - 依赖列表

# TimeMoE-50M 昇腾 NPU 推理环境依赖
# 注意: 需要 transformers 4.40.1 版本(与模型代码兼容)

# 核心依赖(必须)
torch>=2.0.0
torch_npu>=2.0.0
transformers==4.40.1
accelerate>=0.20.0
safetensors>=0.4.0
numpy>=1.21.0

使用方式:

# 激活环境
conda activate pt2100

# 安装依赖(华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple -r requirements.txt

8. 参考资源

  • TimeMoE 论文 (arXiv:2409.16040)
  • TimeMoE GitHub 仓库
  • HuggingFace 模型页 (time-moE/TimeMoE-50M)
  • 华为昇腾文档
  • torch_npu GitHub