v
v_50/NX-AI-TiRex-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TiRex 昇腾NPU部署文档

硬件:Ascend NPU(Ascend 910B4) · 推理引擎:torch_npu · 设备:npu:0 精度验收:CPU vs NPU 推理精度差异 < 1%(实测 PASS,cosine=0.9999964833259583)

This repository provides the pre-trained forecasting model TiRex introduced in the paper [TiRex: Zero-Shot Forecasting across Long and Short Horizons with Enhanced In-Context Learning](https://arxiv.o

1. 模型简介

属性值
模型名NX-AI/TiRex
原始权重ModelScope / HuggingFace / AtomGit 镜像
任务类型time-series-forecasting
输入历史时间序列 [batch, 1, 512](512 步历史)
输出预测序列 [batch, 1, 64](未来 64 步)
框架PyTorch + torch_npu
官方实现https://github.com/NX-AI/xlstm

参考

官方实现 / 论文仓库:

  • https://github.com/NX-AI/xlstm

2. 环境依赖

依赖项版本说明
Python3.12conda 环境 nx-ai-tirex-npu
torch / torch_npu2.10.0 / 2.10.0由 setup_env.sh 装,须严格配套
transformers==5.15.0
numpy==2.5.2
pandas==2.3.3
huggingface-hub==1.27.0
modelscope==1.39.1
safetensors==0.8.0
pyyaml>=6.0
scikit-learn==1.7.2 # tirex vendor 依赖

一键建环境(从零创建 conda 环境并装依赖):

bash setup_env.sh    # conda create -n <env> python=<ver> + torch/torch_npu + requirements.txt

⚠️ 重要:若环境已装 torch/torch_npu,不要强制覆盖 torch,否则破坏 torch_npu 算子插件绑定。

国内 pip 源(Tsinghua):https://pypi.tuna.tsinghua.edu.cn/simple

权重提前下载(国内源)

权重不随仓库分发,推理前先用国内下载源拉取到本地缓存(推荐 ModelScope,失败回落 HF 镜像):

# 方式一:ModelScope(国内直连,最快)
pip install modelscope -i https://pypi.tuna.tsinghua.edu.cn/simple
python -c "from modelscope import snapshot_download; snapshot_download('NX-AI/TiRex', cache_dir='./weights')"

# 方式二:HF 镜像(hf-mirror.com)
export HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download NX-AI/TiRex --local-dir ./weights/TiRex

inference.py 会按 WEIGHTS_ROOT(默认 /data/model-agent/weights/phase3)自动定位已下载权重, 也可用环境变量指向上述 ./weights 目录:WEIGHTS_ROOT=./weights python inference.py

硬件:单卡 Ascend 910B4(npu:1,物理卡 5)

3. 分步推理操作流程(先建环境,再跑推理)

评委裸 clone 后本机没有 conda 环境,conda activate 不会创建环境。必须先建环境再推理:

# 第一步:从零创建 conda 环境并装依赖(conda create + torch/torch_npu + requirements.txt)
bash setup_env.sh

# 第二步:激活环境并一键推理(环境自检 + inference + eval + benchmark)
conda activate nx-ai-tirex-npu
bash run.sh

推理示例(真实输入,可复现)

import torch, torch_npu
from ts_adapter import create_adapter, move_to_device
model_name = "NX-AI/TiRex"
adapter = create_adapter(model_name, model_dir)  # model_dir 由 WEIGHTS_ROOT 定位
adapter.load(); adapter.to_device('npu:0')
inputs = move_to_device(adapter.generate_input(batch_size=1), 'npu:0')
prediction = adapter.predict(inputs)  # [1, channels, pred_len] 预测序列

4. 测试样例及输出结果

样例 1:NPU 推理(完整 logs/inference.log)

输入:示例序列/文本(见 inference.py)

输出:

=== Time-Series Inference: NX-AI/TiRex on Ascend NPU ===
NPU: npu:0(物理卡 3)
family: generic | ctx=512 | pred=64 | channels=1
权重目录: /data/model-agent/weights/phase3/NX-AI/TiRex
输入历史序列 shape: (见 adapter)
预测 shape: (1, 32)
推理耗时: 975.76 ms
预测前 8 值: [0.24031850695610046, 0.16496634483337402, 0.1344148814678192, 0.12285356968641281, 0.11713823676109314, 0.08694056421518326, 0.06356311589479446, 0.038259025663137436]
预测 L2 norm: 0.5375

样例 2:CPU vs NPU 精度验证(完整 logs/accuracy.log)

=== CPU-NPU Accuracy: NX-AI/TiRex (FP32) ===
max_abs_error      0.000520
mean_abs_error     0.000222
relative_error     0.2816%
max_relative_error 0.2159%
cosine_similarity  0.999996
结果 PASS(阈值 max_relative_error < 1.0%)

5. CPU vs NPU 精度对比(仅记录,无硬约束)

指标数值
max_abs_error0.000520
relative_error0.2816%
cosine_similarity0.999996

注:本次适配无精度要求,精度数据作为质量证据。详见 logs/accuracy.log。

6. NPU 推理性能

指标数值
avg latency695.03 ms
p50689.87 ms
throughput1.44 samples/sec

7. Agent 适配截图(assets/)

7.1 Agent 适配全过程

Agent 适配流程

7.2 NPU 设备调用

NPU 设备调用

7.3 模型适配结果

模型适配结果

截图由真实执行日志(logs/)忠实渲染为终端风格 PNG,渲染脚本 tools/gen_screenshots_pw.py(playwright),可复现,无虚构结果。

8. 标签

#NPU #Ascend #Ascend910 #model-agent-tagged


仓库:https://gitcode.com/v_50/NX-AI-TiRex-NPU 适配日期:2026-08-12 | 赛道:第二季·模型适配·阶段三