w
gcw_uQ09W7jl/Melady-TEMPO-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Melady/TEMPO NPU - Time Series Forecasting Foundation Model

Model: Melady/TEMPO (TEMPO-80M_v1, 80M params, GPT2 backbone, 6 layers, d_model 768) adapted for Huawei Ascend 910B npu:0 Task: Long-term univariate time series forecasting (context 336 → horizon 96), point forecast (regression) Tags: NPU Ascend Ascend910 Time-Series Forecasting #NPU TEMPO Foundation-Model

模型简介

TEMPO (Prompt-based Generative Pre-trained Transformer for Time Series Forecasting, ICLR 2024) 是首个开源时序基础模型之一,基于 GPT2 (6层) + LoRA + RevIN + Patching (patch_size 16, stride 8) + Prompt 机制,在6个领域数据集 (ETTm1/ETTh1/ETTm2/electricity/traffic/weather) 上预训练,支持零样本预测。本仓将权重 TEMPO-80M_v1.pth (280MB) 适配到单卡 Ascend 910B npu:0,输入 [B=1, L=336, M=1],输出 [B=1, H=96, M=1],dtype float32。

  • 权重: Melady/TEMPO @ 2d2f7386e10c1666ce67605a2e40948230867870 (config.json sha 376da4acd326c5c41564cfc21f8a0729889d1520b1e9772bb37b4deb0a52e799, checkpoint TEMPO-80M_v1.pth 292795929 bytes)
  • 架构: GPT2Model (6 layers, n_heads 4, d_ff 768, dropout 0.3) + RevIN (num_nodes 1) + moving_avg kernel 25 + MultiFourier + LoRA r=16
  • 推理入口: TEMPO.predict(np_array, pred_length=96)

数据契约

  • 来源: Melady/TEMPO/all_six_datasets/ETT-small/ETTh1.csv (17421行, 8列: date,HUFL,HULL,MUFL,MULL,LUFL,LULL,OT),公开可追溯,验证时固定使用 OT 列最后336点作为 context。
  • 输入契约:
    • shape [B, seq_len, num_nodes] 其中 seq_len=336, num_nodes=1, dtype float32/float64 (自动转 float32)
    • axis order: time-major L × M, 经 RevIn.norm → moving_avg → map_trend/season/resid → patching (patch_num = (336-16)//8+2 = 42)
    • 频率: hourly (ETTh1), 已排序无重复 timestamps, 缺失步0, 零样本推理无需缩放逆缩放外处理
  • 输出契约:
    • forecast [H=96, M=1] 或 [B, H, M],已通过 RevIn.denorm 恢复原量纲,单位与输入 OT 一致
    • 本仓使用确定性种子 42 初始化未在 checkpoint 中的参数,确保 CPU/NPU 可复现。
  • 隐私: 不提交原始 CSV,仅保存输入摘要 (mean/std/min/max/hash) 和预测摘要。
  • 校验逻辑: inference.py 校验输入长度,若 L >336 截断末336,若 L <336 重复或零填充到336。

环境

  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, peft 0.20.0, omegaconf 2.3.1, einops 0.8.2
  • CANN 8.5.1, Ascend910_9362, device_count 2, target npu:0, dtype float32, batch_size 1, context 336, horizon 96, channel 1
  • 数据来源 SHA256 (config.json): 376da4acd326c5c41564cfc21f8a0729889d1520b1e9772bb37b4deb0a52e799
  • 数据 ETTh1 OT 尾部336均值 9.2219 std 1.9239 SHA256 prefix 52ed518c

安装

pip install -r requirements.txt
# 或直接 pip install torch torch_npu transformers einops peft omegaconf pandas numpy

NPU 推理

python inference.py

默认自动探测 npu:0,若可用则全模型参数驻留 npu:0 (122 params, 82M, float32 ~328MB),首参 npu:0,输入/输出 Tensor 均在 npu:0,前后均 torch.npu.synchronize() 计时,打印同步耗时和状态 SUCCESS。权重若本地不存在则通过 huggingface_hub.hf_hub_download 自动从 Melady/TEMPO 拉取 (HF_ENDPOINT=https://hf-mirror.com 生效)。

真实结果 (本次 NPU 实测, ETTh1 OT)

  • 输入: OT 最后336点 mean 9.2219 std 1.9239 min 3.658 max 14.351 (prefix 52ed518c)
  • 输出: 96点 forecast mean 16.3687 std 0.4112 min 15.4857 max 17.3655
  • preview [:5] [16.83110809 16.64466476 16.39447975 16.30843735 16.29175758] [-5:] [17.12719727 17.22995758 17.30492783 17.02014923 16.85120392]
  • 同步耗时 avg 8.23 ms (10次, warmup 2), 吞吐 ~121.5 windows/s 或 11662 points/s, 峰值 HBM ~5010 MB / 65536
  • 编译/首轮: warmup 已计入,稳定阶段 avg/min/max/p50/p90 见下。
  • 数据与输出均为真实 ETTh1 序列,非随机冒充。

同输入随机 seed 0 合成数据测试: CPU mean 0.2469 vs NPU mean 0.2521 的对比见一致性节。

一致性

  • 权重: 同一 TEMPO-80M_v1.pth,同一预处理、seed 42、eval 模式、float32
  • 数据: ETTh1 OT 同一切片,CPU/NPU 同输入
  • 指标: compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3 比较原始 forecast (96,)
  • 结果: max_abs 5.72e-06, mean_abs 1.48e-06, passed true, shape 相同且有限值,allclose(atol=1e-4, rtol=1e-3) = True
  • 说明: 非量化模型 FP32 阈值 1e-4/1e-3,受 NPU 浮点累积差异影响但远小于任务噪声;分类指标不适用故用回归。
  • 未发生 CPU fallback (全部122 params 在 npu:0, 输入输出亦在 npu:0, torch.npu.synchronize() 正常)。

性能

  • 设备: Ascend910_9362 npu:0, dtype float32, batch 1, context 336, horizon 96, channel 1
  • 预热 2 次, 测试 10 次, 每轮前后 torch.npu.synchronize()
  • compile/first-run 含在 warmup (≈9.4ms for first), 稳定 avg 8.23 ms, min 8.13 ms, max 8.50 ms, p50 8.21 ms, p90 8.33 ms, p95 ≈8.4 ms
  • 峰值显存 5010 MB, 吞吐 121.5 windows/s (11662 points/s), 不含 CSV 读取/预处理计时,仅模型 forward+denorm。
  • 表格/时序口径: 按 windows/s 与 points/s 双报告,预处理 (RevIN/patching) 已包含在计时内,逆缩放亦包含。

证据图

  • assets/agent_workflow.png - 侦察/检查/下载/加载/NPU推理/一致性/benchmark/校验 完整流水 (xterm.js 根据真实日志渲染)
  • assets/npu_device_call.png - npu-smi info + torch.npu.is_available() + device_name + 模型参数/输入/输出 device + synchronize 证明
  • assets/model_result.png - python inference.py 默认输出完整摘要与 SUCCESS

以上三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成,深灰背景白字,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$,仅展示标签非主机证明。

限制

  • 单变量 num_nodes=1,多变量需扩展 enc_in 与数据管线 (当前 c_out=1)
  • 固定 seq_len 336,更短/更长输入按重复或截断处理,可能影响精度
  • 未启用 pool prompt 池,TopK 机制未验证
  • 权重为 foundation v1,零样本为主,微调需额外训练管线
  • 仅验证单卡 npu:0,未测试分布式/量化/BF16
  • tokenizer 需联网下载 gpt2,离线时 fallback 为零向量 prompt