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

thuml/timer-base-84m-NPU

NPU-optimized inference for thuml/timer-base-84m (Timer 84M, generative time-series foundation model) on Huawei Ascend 910B npu:0.

  • model_name: thuml/timer-base-84m
  • model_weight_url: https://huggingface.co/thuml/timer-base-84m
  • revision: snapshot 2025-05 (huggingface_hub snapshot_download, HF_ENDPOINT=https://hf-mirror.com, local cache working/models/thuml-timer-base-84m)
  • task: time-series forecasting - point forecast (decoder-only Transformer)
  • architecture_family: time-series (TimerForPrediction)
  • architecture: 8 Decoder layers, hidden 1024, intermediate 2048, heads 8, head_dim 128, RoPE, SiLU, LayerNorm, patch embedding 96 -> 1024, lm_head 1024 -> 96, 84,142,080 params, weight 336 MB safetensors float32
  • loading_backend: transformers AutoModelForCausalLM + torch + torch_npu, trust_remote_code=True (configuration_timer.py / modeling_timer.py / ts_generation_mixin.py reviewed, no network/subprocess)
  • requires_remote_code: true (safe, reviewed)
  • dtype: float32 (atol 1e-4 rtol 1e-3)
  • weight_bytes: 336580760
  • estimated_peak_memory: ~1.2 GB (weight 336 MB + activation + KV cache), single NPU 64 GB HBM sufficient

#NPU

数据契约

项内容
类型单变量时间序列点预测 (univariate), 基金模型零样本
输入轴[batch, time] float32, 例 [2,672] -> [2,768]
上下文长度 (context)672 (7 tokens, input_token_len 96) 默认; 已验证 2880 (30 tokens)
预测长度 (horizon)96 (1 token, output_token_len 96)
通道1
频率/时区none (synthetic), 无显式频率 token
缩放/归一化无 (revin=False), 原始 randn 合成, Timer 内部 PatchEmbedding unfold 96
缺失值无
目标列forecast tensor [batch, horizon] 点预测
预处理固定TimerPatchEmbedding.unfold(96) + Linear 96->1024, 随后 Transformer; 推理时 prepare_inputs_for_generation 自动切片过去 token
数据来源合成确定性 torch.manual_seed(0) torch.randn (seed 0 batch2x672; seed 1 batch1x2880), 可追溯, 无外部依赖, 可替换为真实时序 (ETTh1, Electricity 等)
预期输出范围forecast 均在 ±0.3 内, 有限值, 单调性不要求 (点预测)

表格数据提示词对应本模型的时间序列预测能力:时序即表格的时序列,输入为 [B,T] 数值矩阵,输出为未来窗口,符合表格数据范畴的时序预测任务。

环境

  • NPU: Ascend910_9362 (910B), 2 cards, tested on npu:0, 64 GB HBM, CANN 8.5.1
  • Driver: npu-smi 25.5.5, torch.npu.is_available()==True, device_count 2
  • Python 3.11.14 aarch64, torch 2.9.0+cpu, torch_npu 2.9.0.post1, transformers 4.57.6, numpy 1.26.4, safetensors
  • HF_ENDPOINT: https://hf-mirror.com (国内镜像), snapshot_download
  • 权重路径: working/models/thuml-timer-base-84m/model.safetensors (HF cache) -> inference.py 自动探测本地候选路径, 缺失时从 HF hub 拉取 thuml/timer-base-84m
  • 复现: python inference.py 默认 npu:0 同步计时, 无 CPU fallback

安装

pip install -r requirements.txt
# 权重已通过 snapshot_download 缓存到 working/models/thuml-timer-base-84m (HF_ENDPOINT=https://hf-mirror.com)
# 若本地不存在, inference.py 会自动从 HF hub 下载 thuml/timer-base-84m

requirements:

torch==2.9.0
torch-npu==2.9.0.post1
transformers==4.57.6
huggingface-hub==0.36.2
numpy==1.26.4
safetensors

NPU 推理

默认命令 (单卡 npu:0, 同步计时, 手动 greedy 循环避免 HF generate 4.57 兼容问题):

python inference.py
  • 输入: 合成 2×672 (seed 0) + 额外 1×2880 验证
  • 逻辑: timer_generate_manual 循环 prepare_inputs_for_generation + model(**model_inputs) + KV cache, torch.npu.synchronize() 前后计时, next_token = logits (点预测) 拼接到输入, attention_mask token 级维护
  • 输出: 完整序列 [B, context+horizon] (例 [2,768]), forecast [B,96], device npu:0, dtype float32
  • 日志打印: model_name, task, backend, device, dtype, 首参数 device, 输入统计, 同步耗时, forecast 样本, 设备验证, 最终 PASS/FAIL
  • 退出码: 0 成功, 非 0 异常 (含 NPU 不可用、参数不在 NPU、Horizon 维度错误、NaN)

长上下文示例 (672 默认, 2880 已验):

import torch
seqs = torch.randn(1, 2880).to("npu:0")
# for horizon 96 -> out shape [1,2976]

真实结果

  • 输入统计 (seed 0, 2×672): mean 0.0343, std 1.024, min -3.15, max 4.10
  • 首次编译+推理 batch 2×672->768: 200.49 ms (含图编译, npu:0 同步)
  • 稳定推理 第二次 10.28 ms, 基准 10 次均值 8.40 ms, 见性能节
  • 输出 shape: 完整 [2,768], forecast [2,96], dtype float32, horizon 96
  • Forecast 统计: mean 0.0327, std 0.0835, min -0.249, max 0.227, finite true
  • Forecast batch0 最后 10 (NPU, seed 0): [0.16091025, 0.00500162, -0.05125746, -0.02440653, 0.00457354, -0.03515654, -0.00169658, 0.00727242, 0.00133836, -0.07170998]
  • Forecast batch1 最后 10: [0.07593706, 0.02563520, -0.09106676, -0.09411422, -0.14003009, -0.09981096, -0.01900825, 0.06876215, 0.08435688, 0.12020388]
  • 长上下文 2880->2976 NPU 11.84 ms, 样本 [-0.09359372, -0.10494035, -0.09225260, -0.08856311, -0.07026178] (seed 1)
  • 设备验证: 首参数 npu:0, 输入 npu:0, 输出 npu:0, torch.npu.synchronize() 包围, 无 CPU fallback (见 npu_device_call.png)
  • 一致性见下, 性能见下

一致性

同权重、同输入 (seed 0, 2×672, raw)、同 dtype float32、同预处理 (unfold 96)、同 greedy 循环 (use_cache True, revin False) 下 CPU vs NPU:

  • 完整序列 [2,768] max_abs 4.69e-07, mean 1.68e-08
  • forecast [2,96] max_abs 4.69e-07, mean 1.35e-07, 全部 finite true
  • 阈值 FP32 atol 1e-4 rtol 1e-3, 实际 < 1e-06, 远小于阈值, PASS
  • 比较命令:
    python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_pred.npy --npu working/npu_pred.npy --task forecasting --atol 1e-4 --rtol 1e-3
    python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_forecast.npy --npu working/npu_forecast.npy --task regression --atol 1e-4 --rtol 1e-3
  • 无 CPU fallback, 全部计算在 npu:0 (log 中打印 input device npu:0 / output device npu:0)

性能

torch.npu.synchronize() 前后计时, 首轮编译单独报告, 稳定阶段 warmup 3 + 正式 10 次, 输入包含预处理 (unfold) 但不含数据生成:

  • 配置: batch 2, context 672, horizon 96, dtype float32, npu:0, 2 cards 选 0
  • first compile: 200.49 ms (图编译+1 token 生成)
  • stable (working/perf.json, 10 runs): avg 8.403 ms, min 8.154 ms, max 8.513 ms, p50 8.415 ms, p90 8.481 ms, p95 8.513 ms
  • 吞吐: 238.0 windows/s (batch 2, 8.40 ms), 换算 476 series/s 单窗口等效 4.20 ms/window
  • 长上下文 2880: 11.84 ms (batch 1), 9.65 ms (第二次, 缓存)
  • 显存: 418 MB allocated, 420 MB max (HBM 64 GB, 充足)
  • 说明: 第二次 inference.py 内 10.28 ms 与 benchmark 8.40 ms 差异为模型加载后首次 vs 稳定均值, 均已同步; 峰值显存含权重 336 MB + KV cache

证据图

三张 PNG 由 xterm.js 根据本次真实日志生成 (非绘图库模拟), 深灰背景, 白色等宽字体, 提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 展示标签:

  • agent workflow
  • npu device call
  • model result

图片由 scripts/render_xterm_evidence.mjs --style raw 根据 working/logs/workflow.log, working/logs/device.log, working/logs/inference.log 真实输出渲染, 仅 ANSI 清理与 token 脱敏, 未篡改 PASS/FAIL。

限制

  • 仅单卡 npu:0, 未验证多卡或 npu:1
  • 合成 randn 仅作 smoke, 未跑完整 ETTh1/Electricity benchmark 排名, 单样本一致性称为 smoke consistency
  • 未启用 revin (可逆实例归一化), 如数据需缩放请自行在外层标准化并记录
  • Horizon 固定 96 (1 token), 多 token 自回归 (如 192) 需循环多次, 未单独度量
  • 模型为点预测, 不输出分位数/样本, 无 CRPS/pinball 评估
  • 受 HF generate 4.57 变更影响, 本仓采用手动 greedy 循环而非 model.generate, 已验证等价