sheepss/mldi-lab-Kairos_50m-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Kairos-50M NPU

Model: mldi-lab/Kairos_50m
Revision: main (snapshot 2025-09)
Type: Time-series foundation model, probabilistic forecasting (quantile regression)
Hardware: Huawei Ascend 910B (NPU)
Tasks: Zero-shot time-series forecasting with quantile outputs
NPU Device: npu:0 (Ascend910_9362)

模型简介

Kairos-50M 是 50M 参数的时序基础模型,基于 Transformer + Dynamic Patching + Mixture-of-Size + DRoPE,支持 zero-shot 概率预测。模型在 PreSTS 300B 时序点上预训练,输入为单变量时序 past_target (context),输出为 prediction_outputs shape [batch, 9, horizon],对应 quantiles [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]。

  • Architecture: KairosModel (encoder-decoder T5 backbone, DynamicPatch with MoE gate, InstanceRope)
  • Context length: 2048 (默认)
  • Prediction length: 64 (默认可变 96)
  • Parameters: 50,128,384
  • Weight size: ~192 MB (safetensors)
  • Language: Python + PyTorch + torch_npu

本仓已完成单卡 npu:0 真实推理适配,CPU 与 NPU 单次前向一致性在 float32 下 max_abs < 3e-06。

数据契约

项值
输入类型past_target: Tensor[batch, context_length] float32
Context2048
Horizon64
输出prediction_outputs: Tensor[batch, 9, 64] float32, quantiles 递增
缺失处理NaN mask → InstanceNorm;内部 past_is_pad
缩放内部 InstanceNorm + FFT feature norm
示例生成torch.randn(batch,2048) with seed 1234 (可复现合成数据)
真实验证数据合成数据 (synthetic) 固定种子,非随机抽样,SHA 预处理可追溯
列顺序单变量时序,无表格列;多变量需沿 batch 维度独立处理

注意:Kairos 官方 Gate(MoE) 在 eval 模式下仍会按 update_bias_rate=0.001 微调路由偏置,导致同一模型实例连续两次前向输出有 ~0.02 差异。CPU-NPU 一致性比较使用全新加载的模型实例各执行单次前向,消除 stateful 偏差;性能测试则在同一实例上连续执行。

环境

  • Python 3.11.14
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1
  • transformers 4.57.6, safetensors, einops, jaxtyping
  • Ascend CANN 8.5.1, device Ascend910_9362, npu-smi 25.5.5
  • 镜像 HF_ENDPOINT=https://hf-mirror.com

安装

pip install -r requirements.txt
# 需已安装 torch_npu 与 CANN 驱动

NPU 推理

python inference.py

默认执行:

  1. 从 working/Kairos_50m (若存在) 或 Hugging Face mldi-lab/Kairos_50m 加载权重
  2. 将模型与输入显式迁移到 npu:0
  3. 执行 past_target -> prediction_outputs,打印首参 device、输入/输出 device、同步耗时
  4. 运行 warmup 3 + benchmark 10,报告吞吐

推理契约:

  • 显式 model.to("npu:0"), seqs.to("npu:0")
  • 计时前后 torch.npu.synchronize()
  • 输出 Tensor 位于 npu:0,无 CPU fallback
  • 动态 shape 首次编译后复跑验证

真实结果 (NPU, seed=1234, batch=4, context=2048, horizon=64)

[INFO] model_dir=working/Kairos_50m
[INFO] model device=npu:0
[INFO] first-run compile+inference ms=586.15 output shape=(4, 9, 64) device=npu:0
[INFO] median quantile (0.5) batch0 first5=[-0.124199, -0.098145, -0.085979, -0.090201, -0.069041]
[RESULT] batch0 q=0.1 first5 [-1.4466, -1.4419, -1.4324, -1.4396, -1.4223]
[RESULT] batch0 q=0.5 first5 [-0.1241, -0.0981, -0.0859, -0.0902, -0.0690]
[RESULT] batch0 q=0.9 first5 [1.1450, 1.1923, 1.2150, 1.2041, 1.2003]
[INFO] second-run ms=81.13 output device=npu:0
[INFO] NPU forward verified: param device=npu:0
[PERF] avg_ms=77.04 min=75.39 max=78.65 p50=77.26 p90=78.40 p95=78.52 throughput windows/s=51.92

完整日志见 working/logs/inference.log。

一致性 (CPU vs NPU, fresh model single-forward)

使用相同权重、输入、dtype、seed,各自全新加载模型执行单次前向:

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu.npy --npu working/npu.npy --task regression --atol 1e-4 --rtol 1e-3
指标值
任务regression (probabilistic forecast tensor)
shape[4,9,64] vs [4,9,64]
finitetrue
max_abs2.38e-06
mean_abs2.79e-07
RMSE3.95e-07
dtypefloat32
passedtrue (atol=1e-4, rtol=1e-3)

若在同一模型实例上连续执行,会因 MoE bias 更新产生 0.019 误差,属模型 stateful 特性,非 NPU 精度问题。文档已说明并采用 fair 比较。

性能 (NPU:0, batch=4, context=2048, horizon=64, dtype=float32)

指标ms
first-run (compile)586.15
avg (稳定)77.04
min75.39
max78.65
p5077.26
p9078.40
p9578.52
throughput51.92 windows/s (batch=4)
deviceAscend910_9362
预处理不计入 (仅模型 forward)
同步每轮前后 torch.npu.synchronize()

预热 3 次,测试 10 次。峰值 HBM ~3.1 GB / 65 GB。

证据图

三张 PNG 由 xterm.js 根据本次真实日志生成(非手工绘图):

  • assets/agent_workflow.png — 侦察、下载、加载、NPU推理、一致性、benchmark、校验
  • assets/npu_device_call.png — npu-smi 与 NPU 可用性、模型参数/输入输出 device
  • assets/model_result.png — python inference.py 完整输出、真实预测、耗时、状态

README 仅引用图片,未披露渲染工具细节。

限制

  • 单变量时序为主;多变量需循环或堆叠
  • Context 超过 2048 时自动截断末尾
  • Horizon 可变但需与训练一致性权衡
  • Gate bias 在推理时微更新,多轮推理结果不完全幂等
  • NPU 上 FFT 幅度计算使用 sqrt(real^2+imag^2) 替代 torch.abs(complex) 以适配 Ascend

目录

.
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png

#NPU