g
gcw_coj3XaOd/thuml_timer-base-84m
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Timer (Base 84M) - 昇腾 NPU 推理部署

1. 模型简介

模型名称: thuml/timer-base-84m

模型链接: HuggingFace

模型描述: Timer 是清华大学THUML实验室发布的时间序列预测基础模型。基于Decoder-only Transformer架构,使用Patch Embedding将时间序列切分为固定长度的token,通过自回归方式预测未来时间步。适用于单变量和多变量时间序列长期预测任务。

模型架构: Transformer(Decoder-only,8层,RoPE位置编码,SiLU激活,Patch Embedding)

参数规模: ~84M

输入规格:

  • 输入类型:浮点时间序列张量
  • 输入形状:[batch_size, seq_len],其中 seq_len 必须为 input_token_len=96 的整数倍
  • 输入特征数:每个patch包含 input_token_len=96 个连续时间步

输出规格:

  • 输出形状:[batch_size, output_token_len=96]
  • 输出类型:浮点张量,每个输出 token 预测 input_token_len=96 个时间步的未来值

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.40.0模型加载(需 trust_remote_code=True)
numpy>= 1.20.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch_npu transformers numpy

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

模型权重已包含在交付目录中,或从 HuggingFace 下载:

huggingface-cli download thuml/timer-base-84m --local-dir ./weights

3.3 运行推理

python3 inference.py --model-path ./weights

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--seq-lenint96输入序列长度
--pred-lenint96预测长度
--batch-sizeint1批大小
--num-runsint10推理次数(用于性能统计)

3.5 启动服务

Timer 模型为纯 PyTorch 推理脚本,无需启动独立服务进程。直接运行 inference.py 即可完成推理:

# 基础推理
python3 inference.py --model-path ./weights

# 指定批大小和序列长度
python3 inference.py --model-path ./weights --batch-size 4 --seq-len 96 --pred-len 96

# 性能基准测试(100次推理取统计)
python3 inference.py --model-path ./weights --num-runs 100

3.6 推理调用

单条时间序列推理:

python3 inference.py \
  --model-path ./weights \
  --seq-len 96 \
  --pred-len 96 \
  --batch-size 1

批量时间序列推理:

python3 inference.py \
  --model-path ./weights \
  --seq-len 96 \
  --pred-len 96 \
  --batch-size 16

自定义输入数据(修改 inference.py 中的 dummy_input):

# 替换为真实时间序列数据
import torch
real_data = torch.randn(1, 96)  # [batch, seq_len]
output = model(input_ids=real_data.to('npu:0'))

4. 推理成功日志

4.1 单条推理日志

[模型] thuml/timer-base-84m
[设备] Ascend 910B (npu:0)
[输入] batch=1, seq_len=96, dtype=float32
[输出] pred_len=96
[输出] shape: torch.Size([1, 96])
[首次推理] 15.23 ms
[基准测试] 10次推理统计:
  平均: 14.87 ms
  最小: 14.52 ms
  最大: 15.23 ms
  P50:  14.95 ms
  P99:  15.23 ms

[状态] SUCCESS

5. 测试样例及输出结果

样例 1:默认参数推理

运行命令:

python3 inference.py --model-path ./weights --batch-size 1 --num-runs 10

输出:

============================================================
Timer 时间序列预测模型 — 昇腾 NPU 推理
============================================================

[1/3] 加载模型...
      模型加载完成,参数量: 84,142,080

[2/3] 执行推理...
[模型] thuml/timer-base-84m
[设备] Ascend 910B (npu:0)
[输入] batch=1, seq_len=96, dtype=float32
[输出] pred_len=96

[输出] shape: torch.Size([1, 96])
[首次推理] 15.23 ms

[基准测试] 10次推理统计:
  平均: 14.87 ms
  最小: 14.52 ms
  最大: 15.23 ms
  P50:  14.95 ms
  P99:  15.23 ms

[3/3] 推理完成

============================================================
[状态] SUCCESS
============================================================

样例 2:批量推理

运行命令:

python3 inference.py --model-path ./weights --batch-size 4 --num-runs 10

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 随机生成的标准化时间序列(与训练分布一致)

评测指标:

指标结果说明
输出Shape[1, 96]与期望一致
推理延迟 (avg)~15msAscend 910B, batch=1
参数量84,142,080与官方一致

评测命令:

python3 inference.py --model-path ./weights --batch-size 1 --num-runs 10

8. NPU 配置说明

  • NPU 型号: Ascend 910B
  • NPU 卡数: 1
  • 显存占用: ~340MB(模型权重 + 中间激活)
  • CANN 版本: 8.5.1
  • Tensor Parallel: 单卡无需 TP

9. 已知问题

  1. transformers 版本兼容性: Timer 模型代码基于 transformers 4.40.1 编写,在 transformers >= 4.50 版本中 DynamicCache.get_usable_length() API 发生变更。已在 modeling_timer.py 中 patch 为 get_seq_length() 以兼容当前环境(transformers 4.57.2)。