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

Timer Base 84M NPU #NPU

模型简介

  • 模型名称: thuml/timer-base-84m
  • 权重来源: https://huggingface.co/thuml/timer-base-84m
  • Revision: main (snapshot 2026-08-20, model.safetensors 336 MB)
  • 任务: 时间序列点预测 (time-series forecasting, point prediction)
  • 架构: TimerForPrediction (8 layers, hidden 1024, heads 8, input_token_len 96, output_token_lens [96], max_position 10000, RoPE, 84M params)
  • 数据模态: 单变量时序 [batch, time],本仓验证为 univariate channel=1

Timer 是 THUML 提出的通用时序大模型,基于 Transformer 解码器自回归生成未来序列。本仓库将 TimerForPrediction 迁移至单卡 Ascend 910B npu:0,使用 transformers.AutoModelForCausalLM + torch_npu 真实 NPU 推理,并提供可复现的 inference.py 入口。

数据契约

  • 输入: [batch, time] float32,本次验证 batch=1, context_length=2880 (30 patches ×96),horizon=96
  • 输出: [batch, prediction_length] float32,shape [1,96]
  • 频率/轴: 合成时序模拟 5min 级别周期 (24 与 96 周期 + 趋势 + 高斯噪声),无缩放 (raw float32),inverse transform 无需
  • 预处理: reshape(1, context) -> float32 Tensor -> npu:0,无 tokenizer/normalization,patch unfold 由模型内部 TimerPatchEmbedding 完成 (unfold 96 step 96)
  • 预测长度: 固定 max_new_tokens=96 (等于一个 patch),与 config.output_token_lens 一致
  • 随机性: seed=0, SHA256 prefix 582eac400d36, mean 14.392608 std 8.316635,固定合成数据保证可复现
  • 缺失值: 本次验证无缺失;真实数据缺失需前向填充或插值后同形状输入
  • 来源: 合成随机数据用于验证 NPU 流水;接入真实 CSV/Parquet 时固定列顺序与时间轴,单变量场景每行对应一个时间步

环境

  • NPU: Ascend910_9362, 2 devices, tested on npu:0
  • CANN: 8.5.1
  • Python: 3.11.14
  • Torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • transformers: 4.40.1 (需 trust_remote_code)
  • numpy: 1.26.4
  • 显存: 权重 336 MB, 峰值 NPU 显存 402 MB + 系统 3110 MB,适配单卡 64GB 余量充足
  • 验证 dtype: float32

安装

pip install -r requirements.txt
# 模型权重需预先下载到本地 (正式推理不联网)
huggingface-cli download thuml/timer-base-84m --local-dir /opt/atomgit/adapt-npu-agent/working/models/thuml-timer-base-84m
# 或
python -c "from huggingface_hub import snapshot_download; snapshot_download('thuml/timer-base-84m', local_dir='working/models/thuml-timer-base-84m')"

NPU 推理

python inference.py
# 自定义本地目录
python inference.py --model /path/to/timer-base-84m --device npu:0 --context 2880 --horizon 96

默认优先使用 npu:0,通过 torch_npu.is_available() 探测。模型与输入 Tensor 显式 .to(npu:0),全部 116 参数在 NPU,计时前后 torch.npu.synchronize(),首轮编译时间与稳定推理时间分开报告,发生 CPU fallback 直接退出非零。

真实结果

在 Ascend910 npu:0 上的真实运行输出 (logs/inference.log):

[STEP] Timer-base-84M NPU inference
[INFO] model=thuml/timer-base-84m task=forecasting backend=transformers+torch_npu device=npu:0 dtype=float32
[INFO] torch=2.9.0+cpu torch_npu=2.9.0.post1+gitee7ba04 npu_available=True
[DATA] Forecasting task: context=2880 horizon=96 input_token_len=96
[DATA] Input shape [batch,time]=[1,2880] channel=1 univariate
[DATA] Output shape [batch,prediction]=[1,96]
[DATA] SHA256 prefix 582eac400d36 seed=0 mean=14.392608 std=8.316635
[INFO] Model loaded: TimerForPrediction
[INFO] config: model_type=timer hidden_size=1024 layers=8 input_token_len=96 output_token_lens=[96]
[INFO] Model moved to npu:0
[INFO] First param device: npu:0, dtype: torch.float32
[INFO] Params on NPU: 116/116
[NPU] First run (compile+inference) 0.2027s
[NPU] Output shape (1, 96) device npu:0 dtype torch.float32
[NPU] Forecast preview [0:10]: [29.89783477783203, 29.902790069580078, 29.67729949951172, 29.75293731689453, 29.565731048583984, 29.37419891357422, 29.134037017822266, 29.029586791992188, 28.98033905029297, 29.002613067626953]
[NPU] Stats mean 28.780741 std 0.360835 min 28.107498 max 29.902790
[BENCH] avg 0.0082s min 0.0081s max 0.0083s p50 0.0082s p90 0.0083s p95 0.0083s throughput 122.36 windows/s
[BENCH] peak memory 402.2 MB batch=1 context=2880 horizon=96 dtype=float32
[SUCCESS] Timer NPU inference completed on npu:0 without CPU fallback

CPU 同输入对照: shape [1,96], mean 28.780746, preview [29.897869, 29.902793, ...] 与 NPU 误差 max_abs 3.4e-05。

一致性

同一权重、输入、预处理、seed、float32、eval 模式对比 (scripts/compare_outputs.py):

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu /tmp/timer_work/cpu_output.npy --npu /tmp/timer_work/npu_output.npy --task regression --atol 1e-4 --rtol 1e-3
  • cpu_shape: [1, 96], npu_shape: [1, 96]
  • finite: true
  • max_abs_error: 3.43e-05 (atol 1e-4)
  • mean_abs_error: 7.78e-06
  • passed: true
  • 阈值解释: FP32 误差 + RoPE/Attention 累积,远小于 1e-4 阈值

单窗口 smoke consistency,非完整数据集精度评测。已保存 cpu_output.npy 与 npu_output.npy 可复现。

性能

NPU 同步计时 (torch.npu.synchronize()):

  • 首次编译/运行: 0.2027s (含图编译)
  • 稳定 avg: 0.0082s (10 runs, 预热 3)
  • min: 0.0081s, max: 0.0083s, p50: 0.0082s, p90: 0.0083s, p95: 0.0083s
  • 峰值显存: 402.2 MB (torch.npu.max_memory_allocated)
  • batch: 1, context: 2880, horizon: 96, channel: 1, dtype: float32
  • 吞吐: 122.36 windows/s (1/avg), 约 122 series/s
  • CPU 基线: 0.1404s (慢 ~17x, 同输入)
  • 预处理是否计时: 仅 model.generate,不含数据合成;端到端含同步

动态 shape 首次编译后至少复跑一次,10 次稳定测试,复跑验证编译后稳定。

证据图

三张 PNG 由 xterm.js 根据本次真实日志生成 (scripts/render_xterm_evidence.mjs --style raw):

  • assets/agent_workflow.png - 侦察、环境、下载、加载、NPU 推理、一致性、benchmark、校验全流程 (evidence_logs/workflow.log)
  • assets/npu_device_call.png - npu-smi 与模型参数 device/dtype/input device 证明 (evidence_logs/device.log)
  • assets/model_result.png - 默认 python inference.py 完整输出与同步耗时 (evidence_logs/inference.log)

提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签。

workflow device result

限制

  • 仅验证 NPU 推理,训练未迁移;Timer 无需 fit(),为预训练直接推理
  • 自定义 op 无需改写,标准 PyTorch + torch_npu 已支持,无静默 CPU fallback
  • 多变量/多通道需扩展输入形状并验证显存
  • 权重、数据集、缓存、日志不提交,推理需本地权重
  • 超出单卡时需分片,本配置适配单卡 910B 2880 context

许可

遵循原模型许可 (THUML Timer),权重未包含在本仓库。