r
redannancy/TiRex-20260820
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TiRex NPU 适配

TiRex 是基于 xLSTM 的零样本时间序列预测模型(35M 参数),支持任意长度的自回归预测。

本仓库为 NPU (Ascend 910) 适配版本,使用 torch_npu + tirex-ts 的 torch 后端(纯 PyTorch sLSTM 实现,无需自定义 CUDA 内核)。

性能

配置延迟
batch=1, pred_len=32, context=128~348ms
batch=8, pred_len=32, context=128~347ms
batch=1, pred_len=100, context=20~1.4s

NPU 占用 (Ascend 910, npu-smi 实测)

指标值
AICore 利用率~6-7%
HBM 占用3249 MB / 65536 MB
进程内存434 MB
持续推理吞吐~2.9 iters/s (batch=8)
  • 模型尺寸: 35M 参数, 141MB (FP32)
  • 输出: 9 个分位数 ([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) + 中位数均值
  • 最大上下文长度: 2016 个时间步
  • 补丁大小: 32 步

使用方式

安装依赖

pip install tirex-ts torch_npu fastapi uvicorn pydantic

启动服务

# 默认使用 npu:1
python3 app.py

# 或指定设备和端口
TIREX_NPU_DEVICE=npu:0 TIREX_MODEL_PATH=/path/to/model.ckpt python3 app.py

API 接口

POST /forecast

{
  "context": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
  "prediction_length": 32,
  "output_type": "numpy"
}

响应:

{
  "quantiles": [[[0.1_quantile, ..., 0.9_quantile], ...]],
  "mean": [[0.5_quantile, ...]],
  "quantile_labels": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
  "prediction_length": 32,
  "inference_ms": 412.5,
  "context_length": 10
}

GET /health

{
  "status": "ok",
  "device": "npu:1",
  "model_loaded": true,
  "max_context_length": 2016
}

Python 直接调用

import torch
from tirex import load_model

model = load_model("NX-AI/TiRex", device="cpu", backend="torch")
data = torch.rand((2, 128))  # 2 条时间序列, 每条 128 步
quantiles, mean = model.forecast(context=data, prediction_length=32)

适配说明

  • 使用 tirex-ts 库的 torch 后端(sLSTMCellTorch),无需自定义 CUDA 内核
  • 全部算子均为标准 PyTorch 算子,与 Ascend NPU 完全兼容
  • 初次加载时 torch.minimum(torch.exp(...), torch.ones_like(...)) 会触发 allow_internel_format=False 告警,不影响正确性
  • 已通过 torch.npu 后端全流程测试,输出与 CPU 参考结果相关性 > 0.999999

许可证

NX-AI Community License