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 激活 Experts | 2 |
| 输入维度 | 1 (单变量) |
| 支持 Horizon 长度 | 1, 8, 32, 64 |
| 权重精度 | bfloat16 |
(batch_size, input_len) 的时间序列(浮点数)(batch_size, input_len + horizon) 的完整序列(包含输入 + 预测)| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| 操作系统 | CentOS 7.6+ / Ubuntu 18.04+ | 支持主流 Linux 发行版 |
| CANN | 5.0.RC2+ | 华为昇腾计算架构 |
| Python | 3.10 / 3.12 | 推荐 3.10+ |
| PyTorch | 2.0.0+ | 推荐 2.10 |
| torch_npu | 与 PyTorch 版本匹配 | 昇腾 PyTorch 扩展 |
| transformers | 4.40.1 | 模型加载(必须此版本) |
| accelerate | 0.20+ | device_map 需要 |
# 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())
"# 进入适配目录
cd /workspace/agent/TimeMoE-50M-ascend
# 确认上游权重存在
ls -la /workspace/agent/TimeMoE-50M/
# 期望输出:config.json model.safetensors (~217MB) modeling_time_moe.py ...# 检查 NPU 设备状态
npu-smi info
# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"# 基本用法:默认参数 (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 cpuconda 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)解决方案:
source /usr/local/Ascend/ascend-toolkit/latest/set_env.sh)npu-smi info 检查设备状态解决方案:TimeMoE 模型依赖 ts_generation_mixin.py 中的 EosTokenCriteria,该 API 在 transformers 4.40.1 中引入。更高版本可能存在兼容性问题。
解释:TimeMoE 的 generate 方法返回完整序列(输入 + 预测)。用户需要从完整输出中提取预测部分:forecast = output[:, -horizon:]。
解释:TimeMoE-50M 是更轻量的版本,hidden_size 从 768 降至 384,intermediate_size 从 3072 降至 1536。总参数量从 453M 降至 113M。架构相同,精度略有下降但推理更快。
解释:当前实现的 input_size=1,主要针对单变量时间序列预测。
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 # 原始文档# 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