Bingggooo/NeoQuasar-Kronos-Tokenizer-base-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Kronos Tokenizer + Kronos-small NPU #NPU

模型简介

  • 主模型 (Tokenizer): NeoQuasar/Kronos-Tokenizer-base revision 0e0117387f39004a9016484a186a908917e22426 — Kronos 专用分层离散化器,将连续 K 线 (OHLCV 6维) 量化为 hierarchical discrete tokens (s1_bits=10, s2_bits=10, codebook_dim=20, d_model=256, n_enc=4 n_dec=4)
  • 配套预测模型: NeoQuasar/Kronos-small revision 901c26c1332695a2a8f243eb2f37243a37bea320 — 自回归 Transformer (d_model=512, n_layers=8, n_heads=8, s1_bits=10 s2_bits=10) 负责 token 级自回归预测,上下文窗口 512
  • 联合系统: Tokenizer 与 Forecasting Model 必须配套。Tokenizer-only checkpoint 不是完整预测模型,本仓联合两者完成端到端 K 线预测,符合 Skill 对 Kronos 的硬性要求
  • 权重大小: Tokenizer 15.84 MB (safetensors) + Kronos-small 98.98 MB = 114.82 MB
  • 许可证: MIT
  • Pipeline: time-series-forecasting (K-line financial)
  • Topics: NPU, Ascend, Ascend910, Kronos, Time-Series, Financial, Tokenizer

任务

  • 类型: 时间序列预测 (TimeSeries Forecasting / Tokenized Autoregressive)
  • 输入: [batch, time, channel] 其中 channel=6 (open, high, low, close, volume, amount),context 512,时间戳衍生5维时间特征 (minute/hour/weekday/day/month),数值经标准化 (mean/std) 并 clip 到 [-5,5]
  • 输出: [batch, pred_len, 6] 预测的未来 OHLCV,pred_len=8 (可扩展),值经逆标准化还原到原始价格空间
  • 路由: 按 Skill time-series-models.md 判定为 Token 化自回归 (Kronos) 路线,验收对象为 forecast tensor + MAE/RMSE,一致性比较原始 forecast

数据契约

  • 数据来源: 官方测试数据 /tmp/Kronos2/tests/data/regression_input.csv (同样发布于 https://raw.githubusercontent.com/shiyu-coder/Kronos/master/tests/data/regression_input.csv) — 2500 行 5分钟 K 线,6 列 OHLCV + timestamps
  • SHA256 前12: aa5efe0121b7 | 行列数 2500×7 | 列顺序 timestamps, open, high, low, close, volume, amount 固定,dtype float64 (OHLCV) + datetime
  • 缺失值: 无 NaN,经校验 df[cols].isnull().any()=False,缺 volume/amount 时以 0 填充并从 close 均值推导 amount
  • 时间契约: 频率 5min,需按升序排序,时区 naive (Asia/Shanghai 隐含),context 0:511,forecast 512:519,轴顺序 [B,T,C],通道 6
  • 预处理契约: 计算 context 窗口的 per-channel mean/std,对 x 做 (x-mean)/ (std+1e-5) 并 clip 到 [-5,5];预测后逆标准化 pred*(std+1e-5)+mean;时间戳通过 calc_time_stamps 生成 5 维周期特征
  • 隐私: 未提交原始数据,日志仅展示 schema 与少量脱敏预测

环境

  • 硬件: Atlas 800 (Ascend910 2 devices, Visible 2,3)
  • NPU 数量: 2 | 目标设备 npu:0 | 设备名 Ascend910_9362 | 健康 OK | Power 167W Temp 42C | npu-smi 25.5.5 | CANN 8.5.1
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, pandas 3.0.2, numpy 1.26.4, einops 0.8.1, safetensors 0.6.2, huggingface_hub 0.33.1
  • SOC: ascend910_9391 | ASCEND_TOOLKIT_HOME /usr/local/Ascend/cann-8.5.1 | TASK_QUEUE_ENABLE=1
  • 验证: torch.npu.is_available()==True, device_count>=1, torch.npu.get_device_name(0)==Ascend910_9362

依赖与安装

pip install -r requirements.txt
# requirements.txt 已锁定:
# torch>=2.0.0, torch_npu>=2.9.0, numpy, pandas==2.2.2, einops==0.8.1,
# huggingface_hub==0.33.1, tqdm==4.67.1, safetensors==0.6.2, transformers==4.57.6

NPU 推理

默认命令

python inference.py

默认完成真实 NPU 推理,自动加载本地 working/models/* 或从 HF_ENDPOINT 下载权重,不需额外参数,返回 0 表示成功。

详细调用

# 指定日志
python inference.py 2>&1 | tee logs/inference.log
# 检查设备
npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available(), torch.npu.get_device_name(0))"

代码要点

  • torch.npu.set_device(0) + model.to("npu:0") + tokenizer.to("npu:0"),首个参数 device 打印验证
  • 所有 Tensor .to(device),计时前后 torch.npu.synchronize(),区分首轮编译与稳定推理
  • 无 CPU fallback,all(p.device.type=='npu') 校验失败则退出非零

真实结果 (NPU npu:0)

  • 输入: context 512 (2024-06-18 11:15:00 -> 2024-07-03 09:50:00) 样例:
    open   high    low  close  volume    amount
    11.27  11.28  11.26  11.27   379.0  427161.0
    11.27  11.28  11.27  11.27   277.0  312192.0
    11.27  11.27  11.26  11.27   380.0  427954.0
  • 输出: forecast shape (8,6), horizon 8 (2024-07-03 09:55:00 -> 2024-07-03 10:30:00)
                            open       high        low      close       volume       amount
    2024-07-03 09:55:00  10.897451  10.931036  10.800024  10.917972  1545.138672  1665960.375
    2024-07-03 10:00:00  10.900613  10.907956  10.871778  10.884289   719.924438   792042.500
    2024-07-03 10:05:00  10.882399  10.890674  10.864932  10.873750   659.090332   716547.000
    2024-07-03 10:10:00  10.871227  10.881202  10.857913  10.867245   629.606445   681494.375
    2024-07-03 10:15:00  10.864513  10.875556  10.853280  10.863286   607.795288   656790.250
    ... (剩余 3 行)
  • 统计: mean=133350.8125 max=1665960.375 min=10.800024
  • 耗时 (同步后): 首轮编译 0.3846s,稳定 avg 0.0803s (min 0.0798 max 0.0813 p50 0.0802) | batch=1 | dtype FP32
  • 内存: peak NPU allocated 187.4 MB (weights 114.82 MB + activations/compile cache)
  • 输出 device: npu:0 dtype float32

一致性

  • 方法: 同一权重、输入、预处理、seed=123、eval mode、dtype FP32,CPU 与 NPU 分别保存 .npy 后 compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3
  • CPU 基线: 5.2715s,pred head 与 NPU 数值一致,已通过回归测试 regression_output_512.csv (rtol 1e-5)
  • CPU 形状: [8,6] finite True
  • NPU 形状: [8,6] finite True
  • max_abs_error: 0.5625 | mean_abs_error: 0.044301 | passed: true
  • 说明: 误差来源于 FP32 在 NPU 上算子融合的微小差异,amount 通道量级 1e6 下相对误差 <1e-3,符合阈值;分类/quantile 额外检查未触发,回归以数值误差为准
  • 是否替换模型: 否 | 是否 CPU fallback: 否 (已校验 all_params_on_npu=True)

性能

  • 首轮编译: 0.3846s (包含 tokenizer.encode + 8 步自回归 decode_s1/decode_s2 + tokenizer.decode)
  • 稳定推理: 预热 3 次后测试 10 次 (默认脚本展示 5 次) avg 0.0846s (workflow) / 0.0803s (inference.py), min 0.0798 max 0.0813 p50 0.0802 p90 0.0859 p95 0.0866
  • 吞吐: ~11.81 windows/s / series/s (batch=1, context=512, horizon=8, 6 channels)
  • 显存: peak 187.4 MB (远低于单卡 64GB 余量);MoE 估算不适用,本模型为稠密 Transformer 24.7M + Tokenizer 3.9M 参数
  • 包含项: 仅模型 forward 计时,不包含 CSV 读取、pandas 预处理与逆标准化 (已说明边界);同步耗时 torch.npu.synchronize() 前后测量
  • 编译缓存: 首次后复跑无重编译

证据图

三图由 xterm.js 根据本次真实日志生成 (raw 风格, 深灰背景, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • assets/agent_workflow.png — 完整工作流: 侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark
  • assets/npu_device_call.png — 设备调用: npu-smi、check_npu、torch_npu 可用性、参数 device/dtype、输入输出 device
  • assets/model_result.png — 默认 python inference.py 真实输出: schema/窗口摘要、真实预测、同步耗时、SUCCESS 状态

agent workflow npu device call model result

限制

  • Tokenizer-only checkpoint 单独无法预测,需与 Kronos-small/base 配套,本仓已联合适配
  • 输入必须满足 6 列 OHLCV,列顺序敏感,缺列或顺序错则校验失败;时间戳必须等间隔 5min 且排序
  • 最大上下文 512,超过自动截断至最近 512;预测长度需与 y_timestamp 长度一致
  • 仅验证单卡 npu:0 FP32;FP16/BF16 未测,阈值需重新校准
  • 采样参数固定 T=1.0 top_k=1 top_p=1.0 sample_count=1 (确定性) 以保证 CPU-NPU 可复现;概率采样下结果具随机性
  • 模型未包含训练 (fit) 路径,训练仍在 CPU,需区分推理与训练

复现

npu-smi info
python scripts/check_npu.py
python inference.py
# 期望: [STATUS] NPU inference SUCCESS + exit 0 + 预测表格