模型名称: 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 的整数倍input_token_len=96 个连续时间步输出规格:
[batch_size, output_token_len=96]input_token_len=96 个时间步的未来值| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| 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# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"模型权重已包含在交付目录中,或从 HuggingFace 下载:
huggingface-cli download thuml/timer-base-84m --local-dir ./weightspython3 inference.py --model-path ./weights| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 路径 |
| --seq-len | int | 96 | 输入序列长度 |
| --pred-len | int | 96 | 预测长度 |
| --batch-size | int | 1 | 批大小 |
| --num-runs | int | 10 | 推理次数(用于性能统计) |
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单条时间序列推理:
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'))[模型] 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运行命令:
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
============================================================运行命令:
python3 inference.py --model-path ./weights --batch-size 4 --num-runs 10


测试数据: 随机生成的标准化时间序列(与训练分布一致)
评测指标:
| 指标 | 结果 | 说明 |
|---|---|---|
| 输出Shape | [1, 96] | 与期望一致 |
| 推理延迟 (avg) | ~15ms | Ascend 910B, batch=1 |
| 参数量 | 84,142,080 | 与官方一致 |
评测命令:
python3 inference.py --model-path ./weights --batch-size 1 --num-runs 10DynamicCache.get_usable_length() API 发生变更。已在 modeling_timer.py 中 patch 为 get_seq_length() 以兼容当前环境(transformers 4.57.2)。