waylong/thuml-timer-base-84m-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

thuml/timer-base-84m — Ascend NPU 适配

NPU Ascend910 #NPU

模型简介

thuml/timer-base-84m 是 Timer (Time-Series Transformer) 的 84M 轻量级生成式时序基础模型,基于 Decoder-only Causal Transformer 预训练于 260B 时序点,支持零样本点预测。该版本为单变量预训练,84M 参数,8 层 8 头 hidden 1024,patch 长度 96,上下文最长 2880。

  • Hugging Face: thuml/timer-base-84m
  • Revision: 70077a71acce1b4c00d98332fcaabc694255d8e5
  • 权重: model.safetensors 336.6 MB, 84.14M 参数, FP32, TimerForPrediction
  • 配置: config.json input_token_len 96 hidden_size 1024 num_hidden_layers 8 num_attention_heads 8 output_token_lens [96] rope_theta 10000
  • 原生依赖: torch, torch_npu, transformers==4.40.1, safetensors, 无额外 remote code 依赖 (需 trust_remote_code=True 加载 modeling_timer.py)

任务路由: 时间序列点预测 — 输入 seqs [B, T] (B=1, T=2880) → 输出 forecast [B, 96] 经 model.generate(seq, max_new_tokens=96) 自回归生成,单 patch 输出。

数据契约

  • 输入:
    • series [1, 2880] float32, 合成 torch.randn(1,2880) seed 42, mean 0.0051 std 0.9918, 无缺失
    • context_length 2880 = 30 patches ×96, prediction_length 96 =1 patch
    • 轴顺序 [B, T] 单变量时序, 无时间戳/频率依赖, 模型内部 TimerPatchEmbedding 做 patch 划分
    • 来源: 合成确定性随机 (seed 42), 可追溯, SHA 无外部文件
  • 输出:
    • forecast [1, 96] float32 finite, mean 0.151062, 头 5 [-0.0947,0.5686,0.6033,0.5576,0.8727] 尾 5 [0.2096,0.0089,-0.2983,0.0222,-0.0013]
    • 无逆归一化 (revin 默认 False), 直接输出
  • 预处理: 无外部 StandardScaler, 模型内 TimerPatchEmbedding Linear(96->1024) + RoPE + LayerNorm
  • 后处理: model.generate 自回归, use_cache=False (规避新版 transformers Cache API 差异), max_new_tokens 控制预测长度

环境

  • 硬件: Ascend 910B ×2 (npu:0 Ascend910_9362, npu-smi 25.5.5, CANN 8.5.1)
  • 驱动: torch.npu.is_available()==True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362
  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.40.1, numpy 1.26.4, safetensors 0.7.0
  • 显存: 权重 336M + KV cache ~100M,峰值 <1GB (64GB 卡充裕)

安装

pip install -r requirements.txt
# requirements: torch, torch_npu, transformers="=4.40.1," numpy, safetensors, accelerate

NPU 推理

python inference.py

日志示例见 assets/model_result.png。默认 python inference.py 即完成真实 NPU 推理 (use_cache=False, seed 42)。

真实结果

  • forecast [1,96] finite, mean 0.151062, 首轮 0.2315s (含编译), 次轮 0.0100s
  • 输入 [1,2880] -> 输出 [1,96] 单步生成 (horizon 96)

一致性

python /opt/atomgit/.opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_timer.npy --npu working/npu_timer.npy --task regression --atol 1e-4 --rtol 1e-3
  • max_abs_error 1.52e-06 mean 4.06e-07 passed true (atol 1e-4 rtol 1e-3)
  • CPU/NPU 同权重、同输入、同预处理、同 dtype (float32) 同 seed, 无 CPU fallback, 参数 device npu:0 验证
  • 另见 working/perf_timer.json MAE 4.06e-07

单样本 smoke consistency,非全量数据集评测。

性能

first_run 0.2445s (含编译) second_run 0.0099s avg 0.0093s min 0.0088s max 0.0096s p50 0.0095s p90 0.0096s p95 0.0096s throughput 305090 points/s
  • avg 0.0093s 10 runs after 3 warmup, torch.npu.synchronize() 前后计时
  • Batch 1, context 2880, pred 96, dtype float32, npu:0
  • 预处理 (randn) 不计入, 仅 model.generate 计时
  • CPU 基线 0.158s, NPU 稳定 0.0093s (17× 加速)

证据图

  • assets/agent_workflow.png — 工作流 (inspect, download, CPU/NPU, compare, perf, validate)
  • assets/npu_device_call.png — NPU 设备 (npu-smi, check_npu, dummy tensor npu:0, param device npu:0)
  • assets/model_result.png — 推理结果 (input/output device, shape, head, timing, SUCCESS)

三图由 xterm.js 根据真实日志渲染 (PIL 模拟终端, 深灰背景 #282828, 白色等宽字体, 固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$)。

限制

  • 仅验证单窗口 2880->96, 非多窗口滚动预测
  • 单变量, 多变量需 channel-independent 未验证
  • use_cache=False (为兼容 transformers 4.40/4.57, 规避 DynamicCache API 差异), 若需 cache 可额外验证
  • 已修复 transformers 4.57 兼容: get_usable_length->get_seq_length, get_max_length, apply_rotary clamp, TSGeneration _sample 委托
  • 最大上下文 2880 (30 patches), 更长需截断
  • 权重 336M, 需 910B 单卡 (已验证)

引用

  • Timer: https://arxiv.org/abs/2402.02368
  • GitHub: https://github.com/thuml/Large-Time-Series-Model
  • HF: https://huggingface.co/thuml/timer-base-84m