本交付将 thuml/timer-base-84m 在 华为昇腾 Ascend NPU(
npu:2,Ascend 910B4-1)上使用 torch_npu 完成端到端生成式时间序列预测(zero-shot point forecasting)。 全链路输入张量、模型参数与输出张量均位于npu:2;若 NPU 初始化、放置或执行失败, 脚本以非零退出码失败,绝不静默回退到 CPU。
| 图片 | 说明 |
|---|---|
![]() | ModelAgent 从 gate 读取到 NPU 推理的完整工作流(1440×1800) |
![]() | ModelAgent 实时会话截图(由编排器从会话转录生成,1440×900) |
![]() | 推理期间并发 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 点预测。
TimerForPrediction)hidden_size=1024input_token_len=96,输入必须是 96 的倍数)output_token_lens=[96],支持多输出头)torch.float32)time-series-forecasting本交付使用模型仓库自带的定制实现 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 |
| 权重 SHA256 | 9c3d18f12ffe1ea7d4fa70eb3304b26e3841164a6a265fbae4f7a05cd213aa3d |
| 上游许可证 | 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) | 容器 |
| Python | 3.11.14 | 容器 |
| CANN | 8.5.1 | /usr/local/Ascend/ascend-toolkit/latest |
| npu-smi | 25.5.1 | 驱动工具 |
| NPU 硬件 | Ascend 910B4-1 ×8(本任务使用 npu:2) | 容器 |
| torch | 2.9.0+cpu | 容器提供,禁止替换 |
| torch_npu | 2.9.0 | 容器提供,禁止替换 |
| transformers | 4.40.1 | aliyun 镜像(模型 README 指定稳定版本) |
| safetensors | 0.8.0 | aliyun 镜像 |
| tokenizers | 0.19.1 | aliyun 镜像 |
| numpy | 1.26.4 | aliyun 镜像 |
| Pillow | 12.3.0 | aliyun 镜像(仅用于渲染交付截图) |
# 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"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'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 结构化结果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命令行参数:
| 参数 | 默认值 | 说明 |
|---|---|---|
--device | npu:2 | 目标 NPU 设备,必须为 npu:* |
--lookback | 2880 | 输入上下文长度(必须是 96 的倍数) |
--forecast-length | 96 | 预测步长(horizon) |
--seed | 1234 | 随机种子(确定性输入) |
--out-json | logs/npu_result.json | 结构化结果输出路径 |
--out-input-csv | test_input.csv | 测试输入 CSV 输出路径 |
/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 记录)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 关键字段:
| 字段 | 值 |
|---|---|
engine | torch_npu |
assigned_device | npu:2 |
exit_code | 0 |
npu_available | true |
npu_device_name | Ascend910B4-1 |
npu_device_count | 8 |
source_revision | 70077a71acce1b4c00d98332fcaabc694255d8e5 |
input_device / model_device / output_device | npu:2 / npu:2 / npu:2 |
process.pid | 2170910(与 npu-smi 日志进程行一致) |
load_evidence.loaded | true(84,142,080 参数全部置于 npu:2) |
environment.hardware_name | Ascend910B4-1 |
environment.npu_count | 8 |
environment.cann_version | 8.5.1 |
environment.torch_version | 2.9.0+cpu |
environment.torch_npu_version | 2.9.0 |
model.architecture | TimerForPrediction |
model.num_parameters | 84,142,080 |
output.shape | [1, 96](batch, forecast_length) |
output.finite | true |
output.min / max / mean / std | 0.460651 / 1.011088 / 0.631851 / 0.128949 |
latency_s | 0.3237 |
peak_memory_mb | 402.18 |
预测值全部有限。输入序列在尾部处于约 0.88–1.01 的高位区间,模型输出的前 8 点
从 1.007 平滑下降至 0.887,与输入上下文尾部趋势自然衔接,说明模型基于
2880 点上下文产生了有意义的未来序列(确定性贪心生成,无采样噪声)。
70077a71acce1b4c00d98332fcaabc694255d8e51ff8d1afc073182e6d46022069ff32470ab47945logs/npu_inference.loglogs/npu_device_call.loglogs/npu_result.jsonassets/model_result.pngassets/npu_device_call.pngassets/agent_workflow.pngassets/modelagent_live_session.pngmodel.safetensors,336580760 bytes,
SHA256 9c3d18f12ffe1ea7d4fa70eb3304b26e3841164a6a265fbae4f7a05cd213aa3d| 现象 | 可能原因 | 处理 |
|---|---|---|
torch.npu.is_available() == False | CANN 环境未加载 | source /usr/local/Ascend/ascend-toolkit/set_env.sh 后重试 |
assigned device npu:2 is not visible | ASCEND_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 NULL | torch_npu 初始化提示(无害) | 不影响结果,可忽略 |
| 安装拉取了 CUDA wheel | 未锁定镜像源 | 使用 PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/ |
lookback=2880, forecast-length=96 配置;更长 horizon 需自行评估显存与耗时。npu-smi 进程表仅在采样瞬间可见短生命周期进程,本交付已用 0.2s 高频采样捕获到
NPU 2 上的活动进程;若无法复现,请以 logs/npu_result.json 中的 device 字段与
HBM 用量为准。_greedy_search 直接取回归输出),因此同一输入可复现;
设置 --seed 主要保证测试输入与随机源固定。inference.py、脚本与文档以 Apache-2.0 提供。