模型名称: thuml/timer-base-84M
模型链接: https://huggingface.co/thuml/timer-base-84m
模型描述: 清华 THUML 团队发布的时序基础模型(Timer),面向表格/时序数据的零样本(zero-shot)预测:输入历史时序窗口,自回归输出未来多步预测值。适用于负载预测、销量预测、传感器趋势、金融行情等场景。
模型架构: Timer(decoder-only 时序 Transformer,96 点 patch 嵌入 + RoPE 位置编码,input_token_len=96,每次前向预测一个 96 点 patch)
参数规模: ~84M(bf16,单分片 model.safetensors 336,580,760 bytes,sha256 校验与 LFS oid 一致)
适配引擎: torch_npu(昇腾 NPU 推理引擎)
| 依赖项 | 版本 | 说明 |
|---|---|---|
| torch | 2.9.0 | |
| torch_npu | 2.9.0.post1 | 昇腾 NPU 推理引擎 |
| transformers | 4.46.3 | 官方配套版本(custom code 基于 4.40 编写) |
| accelerate | >= 0.33 | 模型加载 |
| fastapi / uvicorn | - | 服务化推理(server.py) |
注:本模型走 transformers 加载路径(仓内自带 custom code:
modeling_timer.py/configuration_timer.py/ts_generation_mixin.py,需trust_remote_code=True)。custom code 的TSGenerationMixin._greedy_search在 transformers 4.46 起不再被generate()分发(GREEDY_SEARCH 统一并入_sample),且_extract_past_from_model_output签名变更,timer_compat.py已做兼容补丁。
# 内置演示序列(8 个正弦周期+微趋势)预测未来 30 步
python inference.py --prediction-length 30 --device npu:0
# 从 CSV 表格数据读取时序列:上下文 40 点(自动补齐到 96 patch),预测 30 步
python inference.py --csv examples/demo_series.csv --context-length 40 --prediction-length 30 --device npu:0
# 更长上下文 + 96 步预测
python inference.py --csv examples/demo_series.csv --context-length 960 --prediction-length 96 --device npu:0
# 纯 CPU 推理(对比验证)
python inference.py --device cpu推理流程:AutoModelForCausalLM.from_pretrained(trust_remote_code=True) → timer_compat.load_timer() 加载至 npu:0 → 输入经 align_context() 补齐/对齐到 96 的整数倍 → model.generate(inputs, max_new_tokens=N) 官方 _greedy_search 自回归循环 → 截取预测段。
Timer 按 96 点一个 patch 切分时序(
input_token_len=96),要求输入长度 ≥96 且为 96 的整数倍;align_context()对短序列按周期重复补齐、长序列截取末尾对齐。
以下输出为在昇腾 Ascend 910B4(CANN 8.5.1 + torch_npu 2.9.0.post1 + transformers 4.46.3)上实测获得。
命令:
python inference.py --prediction-length 30 --device npu:0输出:
[输入] 内置演示序列(正弦+趋势)长度=960
[加载] 完成,耗时 4.05s
[结果] 预测未来 30 步:
[0.4375, 0.4824, 0.5195, 0.5391, 0.5859, ..., 1.0391, 1.0625]
[耗时] 0.33s(预测 30 步,上下文 192 点)正确性验证:同一输入在 CPU 与 NPU 上推理,输出最大偏差 0.00293(bf16 数值正常波动),NPU 推理链路数值正确;预测曲线延续输入序列的上升相位与周期形态。
命令:
python inference.py --csv examples/demo_series.csv --context-length 40 --prediction-length 30 --device npu:0输出:
[输入] CSV=examples/demo_series.csv 列=value 长度=500
[输入] 补齐/对齐: 40 -> 96(patch=96)
[结果] 预测未来 30 步:
[9.625, 9.6875, 9.625, 9.5625, 9.4375, ..., 9.0625]
[耗时] 0.35s(预测 30 步,上下文 96 点)结果说明:纯时序与 CSV 表格数据两种输入均正常输出,验证了 timer-base-84m 在昇腾 NPU 上的零样本时序预测链路通畅。

python server.py --port 8000 --device npu:0接口:
GET /health → {"status":"ok","model":"thuml/timer-base-84m","device":"npu:0","loaded":true}POST /predict(JSON:{"values": [...], "prediction_length": 12})→ 时序预测实测(服务化):
/health → {"status":"ok","model":"thuml/timer-base-84m","device":"npu:0","loaded":true}/predict {"values":[正弦波 20 点],"prediction_length":12} → {"forecast":[-0.410156,-0.3125,...],"elapsed_s":0.357}/tmp/timer-base-84m/(336,580,760 bytes,sha256 校验通过),不占用仓库空间,不纳入本仓库timer_compat.py 在加载时自动打两处补丁:(1) 把 _sample 委托回官方 _greedy_search;(2) 兼容 _extract_past_from_model_output 新签名(不再接受 standardize_cache_format)config.input_token_len=96,输入长度必须 ≥96 且为 96 的整数倍;align_context() 自动按周期重复补齐短输入(如任务要求的 40 点上下文),长输入截取末尾对齐output_token_lens=[96]),max_new_tokens 小于 96 时 generate 返回值会自动截断到目标步数