z
zhangyuge147/NeoQuasar-Kronos-mini-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

NeoQuasar/Kronos-mini on Ascend NPU

1. 模型简介

Kronos 是首个面向金融 K 线(OHLCV)的开源基础模型,采用两阶段架构:专用 Tokenizer 将连续多维 K 线量化为层次化离散 token,去噪自回归 Transformer 在 45+ 全球交易所、120 亿条 K 线上预训练。Kronos-mini 为轻量版(4.1M 参数,d_model 256, n_layers 4, n_heads 4, s1_bits 10 s2_bits 10, max_context 2048),配套 Kronos-Tokenizer-2k(group_size 5, d_model 256, 16MB)。任务为时序预测(time-series-forecasting),输入 6 通道 OHLCV+amount 与 5 维时间特征,输出未来 OHLCV 预测。本仓在单卡 Ascend 910B npu:0 完成真实推理与 CPU-NPU 一致性验证,pipeline 为 Tokenizer.encode → Kronos Transformer 自回归 → Tokenizer.decode(含归一化/反归一化)。

  • 模型:NeoQuasar/Kronos-mini (f4e68697d9d5aed55cef5c96aabc3376bcad9f81, 16MB) + NeoQuasar/Kronos-Tokenizer-2k (26966d0035065a0cae0ebad7af8ece35bc1fb51c, 16MB)
  • 权重格式:safetensors,FP32 推理
  • 官方用法:KronosPredictor(model, tokenizer, device, max_context=2048).predict(df, x_timestamp, y_timestamp, pred_len)
  • 数据:示例为 /tmp/Kronos/tests/data/regression_input.csv 真实 K 线(2500 行,5min 频),或合成 random walk 备用

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910B), 64GB HBM, 2x NPU 逻辑设备
  • CANN: 8.5.1, npu-smi 25.5.5
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, pandas, numpy, einops, tqdm, safetensors
  • torch.npu.is_available()=True, device_count=2, npu:0 可用
  • 显存 free ~60GB, total ~61GB

3. 安装依赖

pip install -r requirements.txt
# requirements.txt 包含 torch, torch_npu, transformers, huggingface_hub, pandas, numpy, einops, tqdm, safetensors

4. NPU 推理

# 默认:lookback 120, pred_len 8, npu:0, FP32, 确定性采样 top_k=1 top_p=1.0
python inference.py
python inference.py --device npu:0 --lookback 120 --pred-len 8

# 自定义
python inference.py --device npu:0 --lookback 512 --pred-len 24

5. 真实推理结果

以下为本次在 npu:0 的真实执行(CPU 与 NPU 同权重、同输入、同随机种子 123,FP32):

输入摘要

  • x_df shape (120,6) columns [open, high, low, close, volume, amount], 来自真实 CSV /tmp/Kronos/tests/data/regression_input.csv
  • x_timestamp 2024-06-18 11:15:00 -> 2024-06-20 14:40:00 (120 rows)
  • y_timestamp 2024-06-20 14:45:00 -> 2024-06-21 09:50:00 (8 rows)
  • x stats open mean 11.1802 std 0.1113 volume mean 1119.3

CPU 基准

  • tokenizer device cpu, model device cpu, d_model 256 n_layers 4
  • latency 243.6 ms, output (8,6)
  • pred head (close/volume):
2024-06-20 14:45:00  close 11.00247  volume 1424.27  amount 1354019.75
2024-06-20 14:50:00  close 11.00558  volume 1357.79  amount 1290006.62
2024-06-20 14:55:00  close 11.00593  volume 1654.34  amount 1597993.12
  • stats mean 253801.5625 min 10.9915 max 2100676 finite True

NPU 结果 (npu:0)

  • tokenizer device npu:0, model device npu:0, 首个参数 device npu:0
  • first run 382.8 ms (compile+infer), second run 63.1 ms
  • latency post-compile ~60.9 ms
  • pred head:
2024-06-20 14:45:00  close 11.00247  volume 1424.270386  amount 1354019.5
2024-06-20 14:50:00  close 11.00558  volume 1357.790161  amount 1290006.375
2024-06-20 14:55:00  close 11.00593  volume 1654.339111  amount 1597993.375
  • stats mean 253801.5781 min 10.9915 max 2100676.25 finite True
  • shape [8,6], 通道 6 (OHLCV+amount), 有限值校验通过

6. CPU-NPU 一致性验证

同权重、同输入、同预处理、同 dtype FP32、同 seed 123、eval 模式。 使用 np.allclose(atol=1e-3, rtol=1e-3) 比较 forecast 张量,保存 cpu_pred.npy / npu_pred.npy。

  • task forecasting (point), metric: forecast value consistency
  • max_abs 2.5e-01, mean_abs 2.87e-02, rmse 8.27e-02, passed True
  • 分通道:
    • open max_abs 0, high 0, low 9.5e-07, close 0, volume 4.8e-04, amount 2.5e-01
    • amount 的 0.25 绝对误差在 FP32 1e6 量级下相对误差 1.9e-07,符合 FP32 阈值
  • 阈值说明:FP32 起步 atol 1e-4 rtol 1e-3;对大尺度 amount 通道放宽至 atol 1e-3,仍通过。单窗口 smoke consistency,非全量数据集评测。
python scripts/compare_outputs.py --cpu cpu_pred.npy --npu npu_pred.npy --task regression --atol 1e-3 --rtol 1e-3
# 结果通过,已在日志中打印

7. 性能测试

torch.npu.synchronize() 包裹,预热 3 次,正式 10 次,lookback 120 horizon 8 batch 1。

  • compile/first-run 382.8 ms
  • avg 60.9 ms, min 60.5, max 61.3, p50 60.8, p90 61.2, p95 61.3
  • throughput 16.4 windows/s (series/s)
  • batch 1, context 120, horizon 8, channels 6, dtype FP32, input [120,6] -> [8,6]
  • 峰值显存估算 权重复位 32MB + 激活/编译缓存 <200MB,远低于 64GB
  • 不含 CSV 读取/预处理计时,仅模型 encode+自回归+decode

8. 自验证截图

三张真实日志渲染图(xterm.js raw 模式,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • assets/agent_workflow.png:下载、侦察、环境、CPU 基准、NPU 推理、一致性、性能全流程
  • assets/npu_device_call.png:npu-smi info, torch.npu.is_available(), 设备名与参数 device
  • assets/model_result.png:python inference.py 默认输出,含任务结果、精度、性能与 PASS

workflow device result

9. 已知限制

  • 仅适配推理(KronosPredictor),不含训练/finetune;训练仍为 CPU
  • Tokenizer + Model 联合推理,必须配套使用,不能单独用 tokenizer 交付
  • 最大 context 2048,超长输入会截断
  • 采样温度 T=1.0 top_p=0.99 确定性模式 top_k=1,用于一致性验证;随机采样会增大方差
  • 仅验证单窗口 smoke,不能代表全量回测排名;量纲大的 amount 通道绝对误差阈值需按相对误差判断
  • 量化/动态 shape 首次编译额外耗時,已单独报告

10. 标签

  • #NPU
  • Hardware: NPU, Ascend, Ascend910, Kronos, time-series-forecasting, financial, tokenizer