atlasleong/timer-base-84m-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Timer base-84m — 昇腾 NPU 时间序列预测推理

本交付将 thuml/timer-base-84m 在 华为昇腾 Ascend NPU(npu:2,Ascend 910B4-1)上使用 torch_npu 完成端到端生成式时间序列预测(zero-shot point forecasting)。 全链路输入张量、模型参数与输出张量均位于 npu:2;若 NPU 初始化、放置或执行失败, 脚本以非零退出码失败,绝不静默回退到 CPU。

证据截图

图片说明
Agent 工作流ModelAgent 从 gate 读取到 NPU 推理的完整工作流(1440×1800)
ModelAgent 实时会话ModelAgent 实时会话截图(由编排器从会话转录生成,1440×900)
NPU 设备监控推理期间并发 npu-smi info 监控,捕获 NPU 2 上的活动进程(1440×900)
推理结果真实推理日志渲染的输入/模型/输出放置与预测结果(1440×900)

模型简介

Timer(Time-Series Transformer) 是清华大学 THUML 团队提出的大型时间序列模型, 见论文 Timer: Generative Pre-trained Transformers Are Large Time Series Models (ICML 2024,arXiv:2402.02368)。本模型 timer-base-84m 为单变量版本,在约 260B 时间点上预训练,参数量约 84M(实际 num_parameters = 84,142,080), 可进行 zero-shot 点预测。

  • 架构:Decoder-only Causal Transformer(TimerForPrediction)
  • 层数 / 头数:8 层 / 8 注意力头,hidden_size=1024
  • Patch 长度:96(input_token_len=96,输入必须是 96 的倍数)
  • 预测长度:96(output_token_lens=[96],支持多输出头)
  • 上下文长度:最长 2880
  • 精度:FP32(模型权重 torch.float32)
  • 任务:time-series-forecasting
  • 官方论文:Timer: Generative Pre-trained Transformers Are Large Time Series Models(ICML 2024)

本交付使用模型仓库自带的定制实现 modeling_timer.py(TimerForPrediction)与 ts_generation_mixin.py(时间序列专用生成逻辑),不依赖任何 CUDA 专属算子, 通过 torch_npu 可直接在昇腾 NPU 上运行。

源仓库与版本

项目值
模型仓库https://huggingface.co/thuml/timer-base-84m
镜像仓库https://ai.gitcode.com/hf_mirrors/thuml/timer-base-84m
Git 远程https://atomgit.com/hf_mirrors/thuml/timer-base-84m.git
固定模型修订70077a71acce1b4c00d98332fcaabc694255d8e5
权重文件model.safetensors
权重字节数336,580,760 bytes
权重 SHA2569c3d18f12ffe1ea7d4fa70eb3304b26e3841164a6a265fbae4f7a05cd213aa3d
上游许可证Apache-2.0(模型卡);官方实现仓库为 MIT
官方实现仓库https://github.com/thuml/Large-Time-Series-Model
实现仓库修订1ff8d1afc073182e6d46022069ff32470ab47945
实现来源模型仓库 custom code:modeling_timer.py / configuration_timer.py / ts_generation_mixin.py(与固定模型修订一致)

环境依赖清单

运行环境由容器提供 匹配的 torch==2.9.0+cpu 与 torch_npu==2.9.0, 两者不允许被重新安装或替换。其余 Python 依赖仅从配置镜像 https://mirrors.aliyun.com/pypi/simple/ 安装,禁止安装 CUDA wheel、Triton、 flash-attn、xformers、bitsandbytes 或另一套 torch。

组件版本来源 / 说明
操作系统Linux 5.10.0(openEuler 22.03 SP4, aarch64)容器
Python3.11.14容器
CANN8.5.1/usr/local/Ascend/ascend-toolkit/latest
npu-smi25.5.1驱动工具
NPU 硬件Ascend 910B4-1 ×8(本任务使用 npu:2)容器
torch2.9.0+cpu容器提供,禁止替换
torch_npu2.9.0容器提供,禁止替换
transformers4.40.1aliyun 镜像(模型 README 指定稳定版本)
safetensors0.8.0aliyun 镜像
tokenizers0.19.1aliyun 镜像
numpy1.26.4aliyun 镜像
Pillow12.3.0aliyun 镜像(仅用于渲染交付截图)

环境准备命令

# 1. 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh

# 2. 保持全部 8 张物理 NPU 可见,使 npu:2 是字面设备索引
export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7

# 3. 创建模型专用虚拟环境(继承容器 torch/torch_npu)
/usr/local/python3.11.14/bin/python3 -m venv --system-site-packages \
  /work/pipeline/runtime/venvs-tabular16/timer-base-84m-npu

# 4. 安装非 torch 依赖(仅 aliyun 镜像)
export PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
export PIP_TRUSTED_HOST=mirrors.aliyun.com
/work/pipeline/runtime/venvs-tabular16/timer-base-84m-npu/bin/pip install \
  "transformers==4.40.1" "safetensors==0.8.0" "tokenizers==0.19.1" "Pillow>=10.0,<13"

分步推理操作流程

1) 校验 NPU 与 torch_npu

source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
/work/pipeline/runtime/venvs-tabular16/timer-base-84m-npu/bin/python -c \
  "import torch, torch_npu; torch.npu.set_device('npu:2'); a=torch.randn(3,4).npu(); print(a.device, (a+a).sum().item())"
# 预期输出包含 device='npu:2'

2) 运行端到端推理(推荐脚本,含并发 npu-smi 监控)

cd /work/pipeline/jobs/tabular16/timer-base-84m/timer-base-84m-npu
bash scripts/run_inference_npu.sh --device npu:2 --lookback 2880 --forecast-length 96
# 退出码 0 表示成功;产物:
#   logs/npu_inference.log    推理原始日志
#   logs/npu_device_call.log  并发 npu-smi 监控日志
#   logs/npu_result.json      结构化结果

3) 直接运行推理脚本

cd /work/pipeline/jobs/tabular16/timer-base-84m/timer-base-84m-npu
/work/pipeline/runtime/venvs-tabular16/timer-base-84m-npu/bin/python inference.py \
  --device npu:2 --lookback 2880 --forecast-length 96

命令行参数:

参数默认值说明
--devicenpu:2目标 NPU 设备,必须为 npu:*
--lookback2880输入上下文长度(必须是 96 的倍数)
--forecast-length96预测步长(horizon)
--seed1234随机种子(确定性输入)
--out-jsonlogs/npu_result.json结构化结果输出路径
--out-input-csvtest_input.csv测试输入 CSV 输出路径

4) 渲染交付截图(可选,需真实日志)

/work/pipeline/runtime/venvs-tabular16/timer-base-84m-npu/bin/python scripts/render_assets.py

完整测试用例

测试输入(自创作,CC0-1.0,确定性,不依赖任何网络数据集):

由固定 numpy RandomState(seed=1234) 生成的 2880 点单变量序列:

x[t] = 0.9*sin(2*pi*t/288.0) + 0.5*sin(2*pi*t/48.0 + 1.3) + 0.0002*t + 0.01*N(0,1)
  • 输入来源:self-authored deterministic synthetic series(模块 docstring 记录)
  • 输入许可证:CC0-1.0
  • 输入文件:test_input.csv(t,x 两列,共 2880 行,文件 SHA256 c5b4923e0abce92ab2392470336e016d2fc2706b04ccc7b304f5e2bf4ad3cbd0)

测试命令:

cd /work/pipeline/jobs/tabular16/timer-base-84m/timer-base-84m-npu
bash scripts/run_inference_npu.sh --device npu:2 --lookback 2880 --forecast-length 96

真实输出摘要(来自 logs/npu_inference.log 与 logs/npu_result.json):

== start 2026-08-22T16:35:58Z ==
device=0,1,2,3,4,5,6,7
inference_pid=2170910
[ENV] hardware_name=Ascend910B4-1 cann=8.5.1 torch=2.9.0+cpu torch_npu=2.9.0 npu_count=8
[PROC] pid=2170910 name=inference.py device=npu:2
[WEIGHT] hashing model.safetensors (336580760 bytes) ...
[WEIGHT] sha256=9c3d18f12ffe1ea7d4fa70eb3304b26e3841164a6a265fbae4f7a05cd213aa3d
[MODEL] loading from /work/pipeline/models/tabular16/timer-base-84m (trust_remote_code) ...
[MODEL] loaded+placed in 2.01s
[MODEL] architecture=TimerForPrediction params=84142080 param_devices=['npu'] ids=[2]
[INPUT] device=npu:2 shape=(1, 2880) dtype=torch.float32
[RUN] generate(lookback=2880, max_new_tokens=96, revin=True) ...
[OUT] shape=(1, 96) device=npu:2 finite=True
[OUT] min=0.460651 max=1.011088 mean=0.631851 std=0.128949
[OUT] first8=[1.0071359872817993, 0.9940869808197021, 1.0110880136489868, 1.0025629997253418,
              0.9718809723854065, 0.9294610023498535, 0.9135439991950989, 0.8871639966964722]
[PERF] latency=0.324s peak_memory=402.2MB
[DONE] wrote logs/npu_result.json exit_code=0
exit_code=0
npu_smi_samples=13

并发 npu-smi info 监控在 NPU 2 上捕获到活动推理进程(真实日志行,PID 2170910, 与 inference_pid=2170910 及 npu_result.json 的 process.pid 完全一致):

| 2       0                 | 2170910       | python                   | 114                     |
| 2       0                 | 2170910       | python                   | 114                     |
| 2       0                 | 2170910       | python                   | 200                     |
| 2       0                 | 2170910       | python                   | 200                     |
| 2       0                 | 2170910       | python                   | 200                     |

NPU 2 的 HBM 用量从基线 ~3398MB 上升到 ~3544MB,且 npu-smi 进程表捕获到 PID 2170910 的 python 推理进程(进程内存 114→200MB),证明推理确实发生在昇腾 NPU 上。

输出结果

结构化结果文件 logs/npu_result.json 关键字段:

字段值
enginetorch_npu
assigned_devicenpu:2
exit_code0
npu_availabletrue
npu_device_nameAscend910B4-1
npu_device_count8
source_revision70077a71acce1b4c00d98332fcaabc694255d8e5
input_device / model_device / output_devicenpu:2 / npu:2 / npu:2
process.pid2170910(与 npu-smi 日志进程行一致)
load_evidence.loadedtrue(84,142,080 参数全部置于 npu:2)
environment.hardware_nameAscend910B4-1
environment.npu_count8
environment.cann_version8.5.1
environment.torch_version2.9.0+cpu
environment.torch_npu_version2.9.0
model.architectureTimerForPrediction
model.num_parameters84,142,080
output.shape[1, 96](batch, forecast_length)
output.finitetrue
output.min / max / mean / std0.460651 / 1.011088 / 0.631851 / 0.128949
latency_s0.3237
peak_memory_mb402.18

预测值全部有限。输入序列在尾部处于约 0.88–1.01 的高位区间,模型输出的前 8 点 从 1.007 平滑下降至 0.887,与输入上下文尾部趋势自然衔接,说明模型基于 2880 点上下文产生了有意义的未来序列(确定性贪心生成,无采样噪声)。

证据链接

  • 模型主页:https://huggingface.co/thuml/timer-base-84m
  • 固定模型修订:70077a71acce1b4c00d98332fcaabc694255d8e5
  • 官方实现仓库:https://github.com/thuml/Large-Time-Series-Model
  • 实现仓库修订:1ff8d1afc073182e6d46022069ff32470ab47945
  • 推理原始日志:logs/npu_inference.log
  • npu-smi 监控日志:logs/npu_device_call.log
  • 结构化结果:logs/npu_result.json
  • 推理截图:assets/model_result.png
  • NPU 设备监控截图:assets/npu_device_call.png
  • Agent 工作流截图:assets/agent_workflow.png
  • ModelAgent 实时会话截图:assets/modelagent_live_session.png
  • 权重校验:model.safetensors,336580760 bytes, SHA256 9c3d18f12ffe1ea7d4fa70eb3304b26e3841164a6a265fbae4f7a05cd213aa3d

故障排查

现象可能原因处理
torch.npu.is_available() == FalseCANN 环境未加载source /usr/local/Ascend/ascend-toolkit/set_env.sh 后重试
assigned device npu:2 is not visibleASCEND_RT_VISIBLE_DEVICES 把设备重映射设置 export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7,保持 8 卡可见
设备为 cpu 或报“Fallback”未正确使用 NPU确认 import torch_npu 且脚本 --device npu:2
No module named 'decorator'torch_npu 运行时依赖缺失从 aliyun 镜像安装 decorator
transformers 版本不匹配使用了非 4.40.1 版本按 requirements.txt 安装 transformers==4.40.1
输入长度不是 96 的倍数Timer 按 96 分 patch将 --lookback 设为 96 的倍数(如 2880)
输出为非有限值(NaN/Inf)输入含异常或精度问题检查输入是否有限,保持 revin=True(默认)
导入 torch_npu 时 path string is NULLtorch_npu 初始化提示(无害)不影响结果,可忽略
安装拉取了 CUDA wheel未锁定镜像源使用 PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/

限制

  • 本交付只覆盖 单卡 NPU 推理;训练、微调与多卡分布式(HCCL)不在范围内。
  • 当前仅验证 lookback=2880, forecast-length=96 配置;更长 horizon 需自行评估显存与耗时。
  • 模型上下文长度上限为 2880 时间点;超长输入需自行切片。
  • 模型为单变量预训练模型,多变量输入需逐通道分别预测或另行微调。
  • npu-smi 进程表仅在采样瞬间可见短生命周期进程,本交付已用 0.2s 高频采样捕获到 NPU 2 上的活动进程;若无法复现,请以 logs/npu_result.json 中的 device 字段与 HBM 用量为准。
  • 生成逻辑为确定性贪心(_greedy_search 直接取回归输出),因此同一输入可复现; 设置 --seed 主要保证测试输入与随机源固定。

许可证与使用限制

  • 模型:Apache-2.0(上游模型卡声明;引用自 模型主页)。
  • 官方实现仓库(thuml/Large-Time-Series-Model):MIT。
  • 测试输入:CC0-1.0(本交付自创作的确定性合成序列)。
  • 本交付的 inference.py、脚本与文档以 Apache-2.0 提供。
  • 请遵守 Apache-2.0 关于署名与免责声明的条款;模型训练数据来自多个公开数据集 (UTSD、lotsa_data 等),使用模型时请同时遵守其数据来源的许可约束。
  • 本交付不包含任何凭据、密钥或非公开路径。