gcw_6a3w2yXz/mldi-lab-Kairos_23m-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

mldi-lab/Kairos_23m NPU 适配

任务ID ed2091e08a5d40679ded435229d7bbab · 原始权重 https://huggingface.co/mldi-lab/Kairos_23m

#NPU

本仓库将 HuggingFace 上的 mldi-lab/Kairos_23m(KairosModel 23M 参数, d_model 384, 4+4 层, Mixture-of-Size MoE levels 3, patch 128, 9 分位数, context 2048)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。支持单变量时序的零样本概率预测(horizon 64),已在 npu:0 上完成真实推理与 CPU-NPU 一致性验证。

  • 模型原始仓库:https://huggingface.co/mldi-lab/Kairos_23m
  • 权重:model.safetensors 92 MB, ~23M params, float32, Transformer with DRoPE + MoE
  • 源实现:https://github.com/foundation-model-research/Kairos(tsfm.model.kairos.KairosModel, KairosConfig)
  • 配置:DROPE + 3-level dynamic patching + MoE (n_activated 3), ResidualBlock 编解码

任务与路由

项值
模型mldi-lab/Kairos_23m(~23.0M params, 92 MB safetensors, float32)
架构KairosModel(DynamicPatch MoE + T5Stack_rope encoder/decoder, d_model 384, num_layers 4, num_decoder_layers 4, levels 3, moe_inter_dim 1408)
任务时间序列预测 probabilistic quantile forecasting(单变量分位数预测,horizon 64),quantiles [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
输入past_target [batch, time] float32(batch 2, time 2048 → 内部 patch 128 stride 128)
输出prediction_outputs [batch, quantiles, horizon] = [2, 9, 64] float32
数据契约轴 [batch, time] → [batch, quantiles, horizon];context 2048演示,horizon 64,patch 128,log scaling + InstanceNorm;频率 N/A,通道 1
路由time-series 概率预测(quantile)

环境

  • 昇腾 910B(Ascend910_9362),CANN 8.5.1,npu-smi 25.5.5
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,einops 0.8.2, jaxtyping 0.3.11

安装

pip install -r requirements.txt
# 如需从源码安装 Kairos(已包含 NPU 补丁,inference.py 会自动处理)
# pip install git+https://github.com/foundation-model-research/Kairos

NPU 推理

python inference.py

默认从本地候选路径加载权重(working/model_kairos23m 或 /tmp/kairos_dl),若本地缺失则自动从 HuggingFace 下载。首次运行会在 NPU 上编译,后续推理为稳定耗时。

真实结果(NPU, 2026-08-21)

[DEVICE] torch.npu.is_available()=True, device_count=2, device_name=Ascend910_9362
[MODEL] loaded, first param device npu:0, dtype torch.float32
[MODEL] total params 23000576
[MODEL] config context_length=2048 prediction_length=64 input_patch_size=128 levels=3 quantiles=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] num_decoder_segments=2
[RESULT] NPU inference elapsed 0.0597s (synchronized)
[RESULT] pred shape (2, 9, 64) dtype torch.float32 device npu:0
[RESULT] pred[0,0,:5] [25.5243, 25.4818, 25.4313, 25.3429, 25.2664]  # q=0.1
[RESULT] pred[0,4,:5] [25.9909, 25.9805, 25.9936, 25.9661, 25.9703]  # median q=0.5
[RESULT] pred finite True mean 25.5302 min 22.6461 max 28.6414
[RESULT] quantile diff min 0.0823 (monotonic)
[BENCH] avg 0.0596s throughput 33.54 windows/s
[MEM] NPU:0 allocated 89.3 MB
[DONE] NPU inference SUCCESS

一致性

输出shapemax_absmean_abs阈值结果
prediction_outputs (quantiles)[2,9,64]1.14e-052.73e-06atol 1e-4 rtol 1e-3PASS
单调性quantiles 递增diff_min >0--PASS

阈值说明:float32,MoE + RoPE 架构,CPU-NPU 误差 < 1.2e-05,远低于 1e-4 阈值,无精度损失。已验证分位数单调性。

性能

context 2048 → horizon 64, batch 2, float32, warmup 2 + 10 runs:avg 0.0596s per batch, throughput 33.54 windows/s, min 0.0580 max 0.0617 p50 0.0596 p90 0.0611 p95 0.0614, 显存 89 MB allocated。

首轮含编译 0.0597s,稳定推理 ~0.0596s,NPU 相比 CPU (约 0.12s) 快约 2×;已调用 torch.npu.synchronize() 计时。

证据图

  • assets/agent_workflow.png — 侦察、环境、加载、NPU 推理、一致性、benchmark
  • assets/npu_device_call.png — npu-smi、NPU 可用性、参数 device
  • assets/model_result.png — python inference.py 完整输出

agent_workflow npu_device_call model_result

三图由 xterm.js 根据本次真实日志生成,非手工绘制。

限制

  • 仅验证单变量合成时序(context 2048, horizon 64, batch 2);多变量、更长 horizon (如 96/128) 需通过 prediction_length 参数扩展,已支持自回归生成(num_decoder_segments=2, 64*2=128 上限,超长会自动分块)
  • NPU 适配点:修复 torch.abs 对 complex 不支持(torch.sqrt(real^2+imag^2)),其余算子原生支持;无 CPU fallback
  • 输入需为 float32,NaN 视为缺失;输出为 [B, Q, H],已 inverse InstanceNorm 还原量纲

NPU 适配详情

  • 源码位置:tsfm/model/kairos/modeling_kairos.py:321-323 FFT 幅度计算
  • 原代码:fft_amplitude = torch.abs(fft_result) 在 Ascend 上报错 acl nnAbs: DT_COMPLEX64 not supported
  • 修复:fft_amplitude = torch.sqrt(fft_result.real.pow(2) + fft_result.imag.pow(2))(CPU/NPU 一致,数值等价,误差 <1e-12)
  • 其余:MoE、RoPE、patch_utils 均在 NPU 上原生运行,已验证参数 device npu:0