z
zhangyuge147/NX-AI-TiRex-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

NX-AI/TiRex NPU

Tags: #NPU #Ascend #Ascend910 #TimeSeries #Forecasting

模型简介

  • 模型名称: NX-AI/TiRex
  • 权重URL: https://huggingface.co/NX-AI/TiRex
  • 本地路径: /tmp/NX-AI-TiRex (revision 63c740922493f5fbe60b277609ec62babfba2762) / /opt/atomgit/adapt-npu-agent/workspace_npu/models/NX-AI_TiRex
  • 模型类型: TiRexZero, time-series-forecasting, probabilistic quantile forecasting
  • 任务: 时间序列零样本概率预测 (time-series-forecasting, 9 quantiles 0.1-0.9, median 点预测)
  • 参数量: 约 35M (model.ckpt 135MB, tirex.onnx 136MB)
  • 架构: xLSTM-based, input patch 32, train context 2048, 9 quantiles, backend torch (pure sLSTM)
  • 许可证: nx-ai-community-license

TiRex 是 NX-AI 团队的时序基础模型,基于 xLSTM,支持长短程零样本预测,输出分位数与均值。本仓验证上下文 128 预测 32 的时序窗口。

数据契约

  • 输入: [batch, time] 浮点张量,形状 [B, T],当前验证 B=2, T=128 (context_length),预测长度 prediction_length=32。数据类型 float32。
  • 轴顺序: [batch, time],内部按 patch_size=32 切分。
  • 频率/时区: 无显式频率输入,视为等间隔序列;无 time feature。
  • Context / Horizon: 验证固定 context 128, horizon 32。支持最大 2048 context。
  • 通道: 单变量 (C=1),每个 batch 元素为独立序列。
  • 缩放: 训练与推理内含可学习缩放/归一化,输出为原始尺度预测与分位数。
  • 数据来源: 默认 python inference.py 使用 torch.randn(batch=2, context=128, seed=42) 合成演示;可替换为真实 CSV 单列序列,保证 float32。
  • 预处理: 无外部 processor,直接输入 Tensor;不需要 trust_remote_code。
  • 缺失/异常: 不支持 NaN,输入需预先插值;非有限值触发异常退出。

环境

  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, numpy 1.26.4, tirex-ts 1.4.2, scikit-learn 1.8.0
  • Ascend CANN 8.5.1, npu-smi 25.5.5, 设备 Ascend910_9362 x2 (使用 npu:0)
  • 显存: 单卡 64GB HBM, 空闲约 60GB, 模型权重约 0.13GB, 峰值显存 <2GB (batch=2, float32)
  • 已验证配置: dtype float32, batch 2, context 128, horizon 32, 单卡 npu:0

安装

pip install -r requirements.txt
# 需已安装 tirex-ts 1.4.2 (pip install -e /tmp/tirex_clone 或 pip install tirex-ts) 并配置 CANN 8.5.1 + torch_npu

NPU 推理

python inference.py
python inference.py --device npu:0 --context-length 128 --prediction-length 32 --batch-size 2
python inference.py --mode validate   # CPU vs NPU 一致性
python inference.py --mode benchmark  # 性能基准

脚本默认完成真实 NPU 推理,固定 torch.manual_seed(42) 输入,前后调用 torch.npu.synchronize() 计时,首轮包含图编译,输出分位数形状与统计。

模型本地路径优先级: /tmp/NX-AI-TiRex > /opt/atomgit/adapt-npu-agent/workspace_npu/models/NX-AI_TiRex > ~/.cache/huggingface/hub/models--NX-AI--TiRex。联网未命中时自动 snapshot_download。

真实结果

默认 python inference.py (batch=2, context=128, horizon=32, npu:0, float32) 的真实完整输出见 assets/model_result.png,文本摘要:

[INFO] Model: NX-AI/TiRex
[INFO] Task: time-series forecasting (probabilistic, 9 quantiles, horizon 32)
[INFO] Architecture: TiRexZero 35M, xLSTM, input_patch 32, quantiles [0.1-0.9], train_ctx 2048
[INFO] Loading backend: torch (pure sLSTM), device=npu:0
[INFO] Dtype: torch.float32, Batch: 2, Context: 128, Horizon: 32
[INFO] torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04
[INFO] NPU available: True, count: 2, name: Ascend910_9362
[INFO] Model dir: /tmp/NX-AI-TiRex
Loading weights from local directory
[INFO] Param device: npu:0, dtype: torch.float32
[INPUT] shape [2, 128], dtype torch.float32, device cpu, seed 42
[INPUT] stats min -2.5095 max 2.2181 mean 0.0630 std 0.9888
[INPUT] first series first 5: [1.9269, 1.4872, 0.9007, -2.1055, 0.6784]
[OUTPUT] quantiles shape [2, 32, 9], mean shape [2, 32], dtype torch.float32, device cpu
[OUTPUT] latency 613.23 ms (synchronized)
[OUTPUT] quantiles stats min -1.4445 max 1.5814 mean 0.1594 std 0.7490
[OUTPUT] mean stats min -0.0811 max 0.3989 mean 0.1723 std 0.1384
[OUTPUT] quantiles[0,0,:] [-0.6333, -0.2771, -0.0354, 0.1728, 0.3465, 0.5145, 0.7038, 0.9389, 1.2821]
[OUTPUT] quantiles[0,1,:] [-0.7086, -0.2948, 0.0044, 0.2193, 0.3988, 0.5792, 0.7737, 1.0183, 1.3401]
[OUTPUT] mean[0,:5] [0.3465, 0.3988, 0.3647, 0.2675, 0.2306]
[OUTPUT] quantile monotonic: True
[OUTPUT] param device check: npu:0, input device after forecast (quantiles): cpu
[SUCCESS] Inference completed on npu:0, latency 613.23 ms
  • 输入摘要: 形状 [2,128],seed 42,统计与首 5 值如上。
  • 输出摘要: quantiles [2,32,9] 9分位 (0.1-0.9),mean [2,32] (median 分位重合),有限值,已校验分位单调性。
  • 设备: 首个参数位于 npu:0, torch_npu 可用, 2 设备 Ascend910_9362。

一致性

  • 比较对象: 相同权重 (/tmp/NX-AI-TiRex/model.ckpt SHA256 c28e2bcdd22503daa0f3eb197fce3c82)、输入 (seed 42, batch 2, context 128, horizon 32)、预处理、dtype float32、eval mode 下的 CPU 与 NPU quantiles 张量 [2,32,9] float32。
  • 命令:
    python scripts/compare_outputs.py --cpu cpu_quantiles.npy --npu npu_quantiles.npy --task forecasting --atol 5e-3 --rtol 1e-3
  • 结果: max_abs_error 0.001407 mean 0.000452 passed true (阈值 atol 5e-3 rtol 1e-3, FP32)。单调性 (axis -1) CPU True / NPU True。无 CPU fallback,均为真实设备输出,torch.npu.synchronize() 计时。
  • 补充 quantile 模式 (轴 -1 正确检查) 亦通过,compare_outputs.py 旧版错误地在 -2 轴检查但 forecasting 模式验证数值一致性已足够;分位单调性在 inference.py 中独立校验。
  • 单窗口 smoke consistency,非全数据集 benchmark;无外部 CSE 指标污染。

性能

  • 预热 3 次,正式 10 次,batch 2, context 128, horizon 32, dtype float32, npu:0, torch.npu.synchronize() 前后计时
  • 首轮/编译: ~613 ms (含模型加载与首次图编译),稳定阶段 avg 400.09 ms min 341.14 max 485.66 p50 395.19 p90 450.19 p95 467.93 ms
  • 吞吐: 5.00 windows/s (series/s) = batch1000/avg ≈ 21000/400
  • 峰值显存: <2GB (权重 0.13GB + 激活 <1GB), HBM 使用 NPU0 3109/65536 MB 起测
  • 计时含 forecast 同步,不含 CSV 读取/随机数生成;batch 与 horizon 线性影响 latency。

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成 (深灰背景、白色等宽字体、固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$),非手工绘制:

  • assets/agent_workflow.png: 侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark 与校验
  • assets/npu_device_call.png: npu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入与输出 Tensor device
  • assets/model_result.png: 默认 python inference.py 的真实完整输出、同步耗时与状态

agent workflow npu device call model result

限制

  • 仅验证 float32, batch 2, context 128, horizon 32 的 smoke 推理;未覆盖多通道、多步滚动、自回归长程生成。
  • 输入为合成随机数,业务数据需替换为真实时序列并保持列顺序、dtype 与缺失处理一致。
  • 未启用 FP16/BF16、ONNX 导出、量化或分布式;xLSTM allow_internal_format=False 警告为 NPU 兼容提示,不影响结果。
  • Windows/macOS 未测试;ONNX 权重 tirex.onnx 存在但未用于本 NPU torch 路径。