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]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 一致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')"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 fallbackCPU 同输入对照: 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单窗口 smoke consistency,非完整数据集精度评测。已保存 cpu_output.npy 与 npu_output.npy 可复现。
NPU 同步计时 (torch.npu.synchronize()):
动态 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:~$ 仅为展示标签。

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