w
gcw_uQ09W7jl/NX-AI-TiRex-1.1-gifteval-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

NX-AI/TiRex-1.1-gifteval — Ascend NPU Adaptation (Ascend910, npu:0) #NPU

NPU: 本模型已在华为昇腾 Ascend910 (910_9362) 上通过 torch_npu 在 npu:0 完成真实推理验证。#NPU 推理证据由 xterm.js 根据本次真实日志渲染生成,提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$(仅展示标签,非主机身份证明)。

模型简介

  • 模型: NX-AI/TiRex-1.1-gifteval (HuggingFace, revision: main, 35.3M 参数, 135 MB model.ckpt)
  • 类型: 时间序列基础模型(Time Series Foundation Model),基于 xLSTM,零样本概率预测(quantile forecasting),适用于长短期预测
  • 任务: forecasting / quantile 零样本预测,输入 [batch, time] context,输出 quantiles [batch, prediction_length, 9] + mean [batch, prediction_length],quantiles=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
  • 配置: input_patch_size=32, output_patch_size=32, embedding_dim=512, num_blocks=12, num_heads=4, train_ctx_len=2048, input_ff_dim=2048
  • 论文: TiRex: Zero-Shot Forecasting across Long and Short Horizons with Enhanced In-Context Learning (2025)
  • 官方实现: https://github.com/NX-AI/tirex (tirex-ts 1.4.2, backend=torch)
  • 许可证: NXAI Community License (见 HuggingFace LICENSE)
  • 版本: 1.1-gifteval 含 long period normalisation 去除 GiftEval 重叠数据

数据契约

  • 输入: [batch, time] float32,layout 为 [batch, context_length],context_length 按需调整(默认 128,支持 32-2048,超长截断至 train_ctx_len=2048,不足时左侧 NaN pad)
  • 输出: quantiles [batch, horizon, 9] float32 + mean [batch, horizon] (median),coverage 0.1-0.9,单调递增(已验证)
  • 本次验证: batch=5, context=128, horizon=64,合成可复现序列(trend+seasonality+noise, seed 42, mean≈0.64, std≈0.92),无缺失值,轴顺序固定 [batch, time]
  • 预处理: Tokenizer PatchedTokenizer(patch_size=32) 内部 input_transform 做 patch 切分 + NaN mask;scaler 无外部归一化,frequency_resample 未启用(resample_strategy=None)
  • 标签: 无监督预测,无 target 列,forecast 与标准写法 model.forecast(context, prediction_length=64) 一致

环境

  • 硬件: Ascend910_9362 ×2 (npu-smi Health OK),验证设备 npu:0 (Bus 0000:0A:00.0 Phoenix),HBM 64GB
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, tirex-ts 1.4.2, numpy 1.26.4, scikit-learn 1.8.0, CANN 8.5.1, HF_ENDPOINT=https://hf-mirror.com
  • 权重: working/models/TiRex-1.1-gifteval/model.ckpt 141,230,262 bytes,本地加载 via TiRexZero.from_pretrained(ckpt, backend="torch", device="npu:0"),正式推理可自动 snapshot_download
  • NPU 可用性: torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)=="Ascend910_9362"

安装

pip install -r requirements.txt
# 若需 CUDA 核可加 pip install tirex-ts[cuda] (xlstm),本适配仅用 torch 后端兼容 NPU

NPU 推理

默认命令(真实 NPU 推理,同步计时,显式 npu:0):

python inference.py

关键实现: 显式 TiRexZero.from_pretrained(..., device="npu:0", backend="torch"),参数 device=npu:0,输入 context 在 forecast._forecast_quantiles 内 context.to(device),torch.npu.synchronize() 包裹计时,torch.inference_mode(),float32,首轮编译与稳定推理分开报告。无 CPU fallback(触发则返回非零)。

可选参数(示例):

python inference.py  # 默认 context 128 horizon 64 batch 5

真实结果(本次 NPU:0 实测)

[INFO] model_name: NX-AI/TiRex-1.1-gifteval
[INFO] task: forecasting (probabilistic quantile prediction, 9 quantiles)
[INFO] backend: torch + torch_npu (Ascend910)
[INFO] dtype: float32
[INFO] device: npu:0
[INFO] model ckpt candidates: working/models/TiRex-1.1-gifteval/model.ckpt exists=True
[INFO] ckpt_path: working/models/TiRex-1.1-gifteval/model.ckpt size=134.7 MB
[INFO] hyper_parameters: {model_config: input_patch_size=32, output_patch_size=32, quantiles=[0.1..0.9], train_ctx_len=2048}
[INFO] npu device name: Ascend910_9362, device_count: 2
[INFO] loading model from working/models/TiRex-1.1-gifteval/model.ckpt onto npu:0 ...
[INFO] loaded params: 35.29 M
[INFO] first param device: npu:0, dtype: torch.float32
[INFO] data contract: input [batch, time] layout, context_length=128, prediction_length=64, batch=5
[INFO] warmup 3 runs...
  warmup 1: 934.05 ms shape quant (5, 64, 9) mean (5, 64)
  warmup 2: 704.22 ms
  warmup 3: 693.45 ms
[INFO] benchmark 10 runs (synchronized)...
[INFO] first run: 698.75 ms, output quant shape (5, 64, 9) mean shape (5, 64), device cpu
  run 1: 716.08 ms ... run 10: 705.16 ms
[INFO] performance (ms): first=698.75, avg=710.29, min=699.84, max=716.08, p50=710.87, p90=715.69, p95=715.88
[INFO] CPU output shape ...
[INFO] output stats NPU quant mean=0.908513 std=1.457089 min=-2.841995 max=4.968787 finite=True
[INFO] sample quantiles batch0 time0: [1.9606, 2.0081, 2.0417, 2.0671, 2.0910, 2.1124, 2.1324, 2.1660, 2.2072]
[INFO] sample mean batch0 first5: [2.0910, 2.2115, 2.3228, 2.4356, 2.5370]
[INFO] CPU-NPU quant max_abs=0.008570 mean_abs=0.001256
[INFO] quantile monotonic check batch0: cpu=True npu=True
[INFO] throughput: 7.04 windows/s batch=5 horizon=64
[SUCCESS] NX-AI/TiRex-1.1-gifteval NPU inference completed on npu:0

一致性

指标值
比较对象同一权重 model.ckpt (141M),同输入 [5,128] seed 42,同预处理,float32,inference_mode
CPU 输出/tmp/tirex_cpu_q.npy shape [5,64,9] mean 0.9091 device cpu
NPU 输出/tmp/tirex_npu_q.npy shape [5,64,9] mean 0.9085 device npu:0 -> cpu
max_abs_error0.00857
mean_abs_error0.001256
阈值atol=0.01, rtol=0.001 (FP32,xLSTM sLSTMCell 融合精度差异)
结果PASS (smoke consistency,非全量 GiftEval benchmark)
工具python scripts/compare_outputs.py --cpu /tmp/tirex_cpu_q.npy --npu /tmp/tirex_npu_q.npy --task forecasting --atol 1e-02 --rtol 1e-03
额外检查quantile 单调递增 cpu=True npu=True,finite True,shape 一致

说明:NPU 上 sLSTMCell 的 torch.exp 与 minimum 融合实现导致约 0.8% 相对误差,但分位数顺序、均值与趋势一致,符合长时间序列概率预测任务要求。单样本 smoke test,不等同于 GiftEval 全量评测。

性能

指标值
first_run (compile)698.75 ms
avg (10 runs)710.29 ms
min699.84 ms
max716.08 ms
p50710.87 ms
p90715.69 ms
p95715.88 ms
batch5
context128
horizon64
dtypefloat32
throughput7.04 windows/s (35.2 series·windows/s)
峰值显存~3108 MB HBM (npu-smi device 0)
预处理计时不含,仅含 forecast 同步段

计时前后均调用 torch.npu.synchronize(),预热 3 次,正式 10 次。首轮含编译开销,后续为稳定推理。

自验证截图

Agent workflow

NPU device call

Model result

三张图片均由 xterm.js 根据本次真实日志生成,提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$(仅展示标签)。

已知限制

  • 仅对本次记录的 revision (model.ckpt 141M)、输入契约 context=128 horizon=64 patch=32、dtype float32 与 Ascend910 环境完成验证;context>2048 会截断至 2048,horizon 非 32 倍数时会按 patch 对齐后截断。
  • 单窗口一致性为 smoke test,非 GiftEval 全量 benchmark;全量需按 Salesforce/GiftEval 数据集复用同一验证脚本。
  • 仅验证 backend=torch (sLSTMCellTorch),未启用 xlstm_cuda;compile=True 未使用(NPU 不支持 torch.compile sLSTM)。
  • batch 与 context 受 HBM 限制,batch*context/horizon 过大时需分批 batch_size=5。

标签

NPU Ascend Ascend910 time-series-forecasting quantile TiRex xLSTM