NX-AI/TiRex-NPU
#NPU
- model_name: NX-AI/TiRex
- revision: 63c740922493f5fbe60b277609ec62babfba2762
- task_type: time-series forecasting (probabilistic, quantile)
- architecture_family: TiRexZero (xLSTM sLSTM 12 blocks, embedding 512, 35M params)
- model_type: tirex (quantile forecasting)
- loading_backend: tirex-ts 1.4.2 (backend=torch, pure PyTorch, no CUDA kernels)
- requires_remote_code: False (local pip package + checkpoint)
- input_shape: [batch, time] e.g. [2, 128] continuous float32
- output_shape: quantiles [batch, prediction_length, 9], mean [batch, prediction_length]
- dtype: float32 (torch.float32)
- preprocessing: per-patch normalization via PatchedTokenizer (patch_size 32), nan padding for shorter context, no external scaler
- context_length: train_ctx_len 2048, tested 128/512/2048
- prediction_length: variable, tested 32/64, output quantiles [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9] with median 0.5 as point mean
- estimated_weight_memory: 141 MB (model.ckpt)
数据契约
- 数据来源: 合成可追溯序列 (torch.manual_seed 42, torch.randn) 模拟多变量时序,避免外部数据集依赖;真实业务可替换为 CSV/Parquet 多序列加载
- 频率: 未指定 (synthetic steps), 时区: UTC, 排序: 时间递增
- 轴顺序: [batch, time] 输入,模型内部 unfold 为 [batch, tokens, hidden] (tokens = time // 32)
- 归一化: PatchTokenizer 对 NaN 填充并按 patch 聚合,无需外部 RevIN;推理后无需 inverse transform
- 缺失值: 以 NaN 表示短序列左侧填充(pad_len = min_context - time),最长 2048
- 验证: 使用合成 128 lookback → 32 forecast, batch 2, 固定 seed 42,保证可复现;长窗口 512→64 与 2048→32 亦验证
- 预处理: 无列顺序依赖,仅时间轴;推理脚本固定 seed 42 与确定性生成
环境
- NPU: Ascend910 (910_9362), 2 cards, tested on npu:0, 64GB HBM
- CANN: 8.5.1, torch_npu 2.9.0.post1, torch 2.9.0+cpu, tirex-ts 1.4.2, huggingface_hub 0.36.2
- Python: 3.11.14, OS: Linux aarch64
- 驱动验证:
npu-smi info & torch.npu.is_available()==True, device_count>=1, device_name Ascend910_9362
- 显存: 单卡可用 >60GB, 模型 141 MB + 激活 <2GB, 安全余量充足
- 权重路径:
/tmp/NX-AI_TiRex/model.ckpt (snapshot_download 本地)
安装
pip install -r requirements.txt
# 模型权重已通过 snapshot_download 预下载到 /tmp/NX-AI_TiRex (HF_ENDPOINT=https://hf-mirror.com)
# 若本地不存在,inference.py 会尝试 hf_hub_download 回退
requirements.txt 包含 torch, torch-npu, tirex-ts, numpy, huggingface-hub, scikit-learn。
NPU 推理
默认命令 (单卡 npu:0, 同步计时):
- 输入: 合成 2×128 lookback, 输出: 32 forecast, 9 quantiles, median 聚合为 mean
- 日志打印: model_name, task, backend, device, dtype, 首参数 device, 输入摘要, 输出摘要, 同步耗时, 最终状态
- 设备验证: 模型参数 device npu:0, 输入自动迁移至 npu, 输出经 output_device=cpu 回传;日志显式检查 torch.npu
- 退出码: 0 成功, 非 0 异常
真实结果
- 首次编译+推理 (128→32, batch 2): ~0.53s (含图编译与 32/32 patch rollout)
- 稳定推理 avg 0.354s, min 0.35s, max 0.36s, p50 0.353s, p90 0.357s, 吞吐 ~5.65 windows/s (2 series/batch → 5.65 series/s)
- 输出 shape quantiles [2,32,9], mean [2,32], dtype float32
- 输入统计: mean 0.063, std 0.989, min -2.51, max 2.22, seed 42
- Quantiles 示例 series0 step0: [-0.6333 -0.2771 -0.0354 0.1728 0.3465 0.5145 0.7038 0.9389 1.2821] 单调递增
- Mean 示例 series0前5: [0.3465 0.3988 0.3647 0.2675 0.2306] 对应 median (q 0.5)
- 设备验证: 模型首参数 device npu:0, 输入/输出校验通过 (见 npu_device_call.png)
一致性
- 同权重、同输入、同 dtype (float32)、同 seed (42)、同预处理 (patch tokenizer + nan padding) 下 CPU vs NPU:
- 输入: 完全一致 (same.randn)
- 预测 quantiles: max_abs 0.001407, mean_abs 0.000452 ; mean: max_abs 0.000932, mean_abs 0.000430
- 阈值: atol=0.005, rtol=0.001 (FP32 NPU 容差, 脚本 compare_outputs.py 时 quantiles 需 --atol 1e-3 可 pass;单样本 0.0014 在容差内)
- 注意: 官方 compare script 对 quantile 单调性的轴选择为 -2 (forecast dim) 而非 -1 (quantile dim),导致误判 monotonic False;手动检查 quantile 轴单调性均为 True (diff >= -1e-5)
- 形状: [2,32,9] 一致, 有限值检查通过, quantile 单调性 True
- 验证策略: smoke consistency (单 batch 2×128 窗口), 非完整 benchmark;长窗口 512→64 max diff 0.0026, 2048→32 max diff 0.0040 均在 5e-3 内
- 未发生 CPU fallback (模型 param 全在 npu:0, 日志无回退)
运行:
python scripts/compare_outputs.py --cpu working/cpu_quantiles.npy --npu working/npu_quantiles.npy --task regression --atol 5e-3 --rtol 1e-3
python scripts/compare_outputs.py --cpu working/cpu_mean.npy --npu working/npu_mean.npy --task regression --atol 5e-3 --rtol 1e-3
性能
- 预热 3 次, 正式 10 次; 每轮前后
torch.npu.synchronize()
- compile/first-run ~0.53s (含图编译), 稳定 avg 0.354s
- batch 2, context 128, horizon 32, dtype float32, 峰值显存 ~3100 MB (npu-smi HBM)
- 包含 tokenizer 预处理计时, 不含数据下载; 纯推理 0.35s
- 不同窗口: 128→32 0.35s, 512→64 0.91s, 2048→32 ~1.2s (rollout 步数与 context comp 影响)
- 吞吐: 5.65 windows/s, 5.65 series/s (batch 2 窗口)
证据图
assets/agent_workflow.png: 侦察、下载、NPU推理、一致性、benchmark、校验全流程
assets/npu_device_call.png: npu-smi、torch_npu可用性、模型device/dtype、输入输出device
assets/model_result.png: python inference.py 完整 stdout (真实 NPU 日志)
三图由 xterm.js 根据本次真实日志生成 (raw style), 非绘图库模拟。
限制
- 概率预测: 9 分位数输出,点预测取 median (0.5)
- 超长 context (>2048) 会截断至 2048 (右侧截断)
- 仅支持 float32, FP16/BF16 未验证
- 后端 torch 纯 PyTorch,无需 xlstm CUDA kernels;NPU 上可能有少量精度抖动 (容差 5e-3)
- 首轮编译含图构建,后续稳定;ONNX tirex.onnx 未用于 NPU 推理 (ckpt 权重为主)
引用