g
gcw_coj3XaOd/Kronos-mini
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Kronos-mini - 昇腾 NPU 推理部署

1. 模型简介

模型名称: NeoQuasar/Kronos-mini

模型链接: HuggingFace

模型描述: Kronos 是首个开源的金融 K 线(蜡烛图)基础模型,在来自全球 45+ 交易所的数据上训练。Kronos-mini 是最小变体,具有 4.1M 参数,专为边缘设备和加速器的高效部署而设计。

模型架构: Decoder-only Transformer + Hierarchical BSQ Tokenization

组件值
d_model256
n_heads4
n_layers4
ff_dim512 (SwiGLU)
s1_bits10
s2_bits10
Context Length2048
Parameters4,108,032

输入规格:

  • OHLCV + amount(6 个特征 per timestep)
  • Shape: [T, 6],T 为时间步数
  • 分层离散化: s1 (10-bit coarse) + s2 (10-bit fine) per feature

输出规格:

  • 预测 token logits: [B, T, 6, 1024] (s1) 和 [B, T, 6, 1024] (s2)
  • 自回归生成: [B, n_steps, 6] (s1 + s2 tokens)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
safetensors>= 0.4.0模型权重加载
pandas>= 1.5.0数据处理
numpy>= 1.24.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch-npu safetensors pandas numpy

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型文件

将以下文件放在同一目录下:

  • config.json — 模型配置
  • model.safetensors — 模型权重(15.8 MB)
  • model/kronos.py — 模型定义
  • model/module.py — Transformer 模块
  • model/__init__.py — 包初始化
  • inference.py — 推理脚本

3.3 运行推理

# 基础推理(预测 3 步)
python inference.py --model-path /path/to/kronos-mini

# 自定义预测步数
python inference.py --model-path /path/to/kronos-mini --n-steps 10

# 性能基准测试
python inference.py --model-path /path/to/kronos-mini --benchmark --benchmark-iter 20

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型目录路径
--n-stepsint3预测步数
--temperaturefloat1.0采样温度
--top-pfloat0.9核采样阈值
--benchmarkflagFalse运行基准测试
--benchmark-iterint10基准测试迭代次数

4. 推理成功日志

4.1 单条推理日志

[模型] NeoQuasar/Kronos-mini
[设备] Ascend910_9362 (npu:0)
[输入] 32 步 OHLCV 历史数据 (32, 6)
[输出] 3 步预测 OHLCV (3, 6)
[耗时] Forward: 141.08ms, Generation: 73.95ms
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 10 次推理统计:
  平均: 18.09ms
  最小: 17.80ms
  最大: 18.65ms
  标准差: 0.25ms
  P50: 18.02ms
  P99: 18.63ms
  Throughput: 165.8 steps/s

5. 测试样例及输出结果

样例 1:基础推理(3 步预测)

运行命令:

python inference.py --model-path /tmp/adapt_neoquasar_kronos-mini --n-steps 3

输出:

[INFO] Using NPU device 0: Ascend910_9362
[INFO] NPU device count: 2
[INFO] Model loaded in 0.11s
[INFO] Total parameters: 4,108,032
[INFO] Model device: npu:0

[INFO] Input data shape: (32, 6)
[INFO] Input data columns: ['open', 'high', 'low', 'close', 'volume', 'amount']
[INFO] Date range: 2024-01-02 09:30:00 ~ 2024-01-02 12:05:00

[INFO] Forward pass time: 141.08ms
[INFO] logits_s1 shape: torch.Size([1, 32, 6, 1024])
[INFO] logits_s2 shape: torch.Size([1, 32, 6, 1024])

[INFO] Generation time: 73.95ms
[INFO] Generated s1 shape: torch.Size([1, 3, 6])
[INFO] Generated s2 shape: torch.Size([1, 3, 6])

[INFO] Predicted OHLCV (next 3 steps):
         open         high         low        close     volume      amount
0  620.906250   548.967773   62.717773   698.281250  608.28125  608.717773
1  187.717773   744.386719  744.781250   608.386719  616.28125  616.717773
2  744.281250  1001.717773  746.781250  1000.717773  744.28125  745.717773

[SUMMARY]
  Model:        Kronos-mini (NeoQuasar/Kronos-mini)
  Parameters:   4,108,032
  Device:       npu:0 (Ascend910_9362)
  Load time:    0.11s
  Forward:      141.08ms
  Generate:     73.95ms
  Status:       SUCCESS

样例 2:性能基准测试(10 次迭代)

运行命令:

python inference.py --model-path /tmp/adapt_neoquasar_kronos-mini --benchmark --benchmark-iter 10

输出:

[INFO] Benchmark results (10 runs):
  Mean:   18.09ms
  Std:    0.25ms
  Min:    17.80ms
  Max:    18.65ms
  P50:    18.02ms
  P99:    18.63ms
  Throughput: 165.8 steps/s

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

模型特性: Kronos-mini 是一个生成式时间序列模型,使用分层离散化 (BSQ) 将 OHLCV 数据转换为离散 token。模型预测下一个时间步的 token 分布,而非直接回归价格。

评测指标:

指标结果说明
参数总量4,108,032全精度 float32
State dict 加载strict=True, 0 missing/unexpected keys权重完全匹配
输出形状 (forward)[1, 32, 6, 1024]logits per feature per timestep
输出形状 (generate)[1, 3, 6]生成的 token indices
NPU 推理延迟~18ms/stepAscend910_9362
CPU 推理延迟~63.6ms/step (forward), ~101.7ms (generate)对比基线

评测命令:

python inference.py --model-path /path/to/kronos-mini --benchmark --benchmark-iter 20

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 2(单卡推理,使用 npu:0)
  • 显存占用: ~3.1 GB HBM(模型加载后)
  • CANN 版本: 8.5.1
  • torch_npu 版本: 2.1.0+
  • Tensor Parallel: 单卡推理,无需 TP

9. 已知问题

无。模型已通过完整 NPU 推理验证,所有算子原生兼容。