liuhongwei-2026/timer-base-84m
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

timer-base-84m on Ascend NPU

模型适配 · 表格/时序数据零样本点预测 (Time-Series Forecasting) · 昇腾 NPU

Beta 版本头信息

{hardware NPU}

1. 简介

  • 模型来源: thuml/timer-base-84m(AtomGit 镜像: https://ai.gitcode.com/hf_mirrors/thuml/timer-base-84m)
  • 模型类型: Timer —— thuml(清华机器学习组)发布的时序基础模型(decoder-only Causal Transformer,patch length=96),适用于表格化/单变量时序数据的零样本点预测
  • 参数量: 84M
  • 上下文长度: 最长 2880
  • 权重格式: fp32 · model.safetensors(≈ 337 MB)
  • 输出: 点预测序列,形状 [batch, horizon](horizon 为预测长度)
  • 适配状态: SUCCESS
  • 适配说明: Timer 官方用 AutoModelForCausalLM + trust_remote_code + model.generate(...) 调用,但模型实为时序预测模型(无 tokenizer、输入是原始时序数值),不适用 vLLM-Ascend 的 OpenAI 兼容 chat 服务;本项目采用 torch_npu + transformers 在昇腾 NPU 上完成适配,并以 FastAPI 提供 HTTP 服务化推理。
  • NPU 适配改动: 官方自定义远程代码已 vendor 到本仓库 timer/ 包(configuration_timer.py / modeling_timer.py / ts_generation_mixin.py),与官方 state_dict 键名逐一对齐。模型为 fp32、确定性点预测(_greedy_search 直接取回归输出),全程零算子替换,昇腾 NPU 原生可跑。

2. 验证环境

组件版本
torch2.9.0
torch-npu2.9.0.post1
transformers4.57.6
CANN8.5.1
NPUAscend 910B (2 × 64GB HBM)
Python3.11.14

3. 精度评测

输入为平滑时序信号(正弦 + 线性趋势 + 小噪声),对比 CPU 与 NPU 的预测序列(lookback=1440, horizon=96)。

指标数值
Cosine Similarity (NPU vs CPU)1.0
最大绝对误差2e-06
相对误差1e-06(满足 < 1%)

全程 fp32、零算子替换,NPU 与 CPU 结果几乎逐位一致。

4. 性能

指标数值
平均延迟 (lookback=2880, horizon=96)7.86 ms
峰值 HBM0.391 GB
CPU 参考延迟 (同配置)139.3 ms
参数量84M

5. 复现步骤

5.1 安装依赖

pip install -r requirements.txt

注意: 本机已验证 transformers==4.57.6(官方要求 ~=4.40.1)。Timer 使用自定义远程代码 + Cache API,其他 transformers 大版本可能不兼容。

5.2 获取模型权重

# 方式一:HuggingFace (hf-mirror,推荐)
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download thuml/timer-base-84m --local-dir /models/timer-base-84m

# 方式二:ModelScope(如可用)
python -m modelscope download --model thuml/timer-base-84m --local_dir /models/timer-base-84m

# 方式三:AtomGit 镜像仓
git clone https://gitcode.com/hf_mirrors/thuml/timer-base-84m.git

5.3 单次推理(NPU)

python3 inference.py --model-path /models/timer-base-84m --device npu:0 --lookback 2880 --horizon 96 --n-runs 3

说明: inference.py 直接使用本仓库 timer/ 下的 vendored 模型代码(与官方 state_dict 键名一致),无需 trust_remote_code。horizon 最大支持 2880(须小于输入长度)。

5.4 精度对比(CPU vs NPU)

python3 inference.py --model-path /models/timer-base-84m --device npu:0 --lookback 1440 --horizon 96 --compare

5.5 服务化推理(HTTP Service)

# 启动服务(默认 8000 端口)
python3 inference.py --model-path /models/timer-base-84m --device npu:0 --serve --port 8000

# 健康检查
curl -s http://127.0.0.1:8000/health

# 表格/时序数据零样本点预测(values 为一段时序数值)
curl -s http://127.0.0.1:8000/v1/forecast \
  -H 'Content-Type: application/json' \
  -d '{"values": [1.0, 1.1, 1.2, 1.5, 1.8, 2.0, 2.1, ...], "horizon": 96, "revin": true}'

6. 截图素材

见 assets/ 目录(agent_workflow.png / npu_device_call.png / model_result.png)。


贡献者: ohhoohho | 赛道: 模型适配赛道