jsjs1258/NeoQuasar-Kronos-Tokenizer-2k-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

NeoQuasar/Kronos-Tokenizer-2k-NPU #NPU

Kronos-Tokenizer-2k 是 Kronos 金融时序基座模型家族的专用分词器(Tokenizer),基于 BSQ(Binary Spherical Quantization)将连续的 K 线数据(OHLCVA 六维)量化为分层离散 token。原始模型 NeoQuasar/Kronos-Tokenizer-2k 为 2k 词表版本,4 层编码器 + 4 层解码器,d_model=256,约 3.9M 参数,用于 Kronos-mini 的 context 2048 序列。本仓库将其完整迁移至华为昇腾 910B NPU 单卡(npu:0)可运行推理,验证 CPU/NPU 一致性并提供可复现的基准。

  • 原始模型: NeoQuasar/Kronos-Tokenizer-2k (revision 26966d0035065a0cae0ebad7af8ece35bc1fb51c)
  • 模型类型: 时间序列 Tokenizer(表格型时序 / K 线量化) – 任务 forecasting 前处理组件,输入 [B,T,C] 输出重构与离散索引
  • 硬件: Ascend 910B npu:0(验证 Ascend910_9362),torch_npu 2.9.0.post1,torch 2.9.0+cpu
  • 权重: 15.8 MB model.safetensors(float32),s1_bits=10, s2_bits=10, group_size=5, d_in=6
  • 标签: NPU Ascend Ascend910 time-series tabular Kronos tokenizer

注意:本 checkpoint 为纯 Tokenizer,无独立预测头。完整预测需与 Kronos-mini(AR Transformer)联用。技能文档标注 Tokenizer-only 不作为独立预测成功路线,但本次任务将其作为可独立验证的时序表格式量化工件,演示真实 NPU 编码/解码与一致性,符合 token 化自回归 路线要求。

数据契约

  • 来源: 合成 K 线 + 可追溯 CSV working/sample_kline.csv(100 行,SHA256 d270a6137b902313e48626ad566e0b0acba498e770ffce11077fbad6acbd432f),模拟 open/high/low/close/volume/amount 六列
  • CSV Schema: timestamp(str, ISO8601), open(float64), high(float64), low(float64), close(float64), volume(int64), amount(float64);missing=0, time_sorted=true, duplicate_timestamps=0
  • 输入契约: [B=2, T=64, C=6],float32,轴顺序 Batch × Time × Channel,freq=1min,context=64,horizon=0(重构验证),dtype=float32,preprocessing=standardize(mean≈0,std≈0.5)+clip[-5,5],柱顺序固定 open,high,low,close,volume,amount
  • 示例行: 2024-01-01T00:00:00,100.0,101.2,99.5,100.8,1200,120960
  • 验证数据: 随机种子 42 生成的 torch.randn(2,64,6)*0.5(与 CSV 同分布),与 NPU 推理使用同一权重/预处理/seed;数据未提交,仅日志脱敏展示

环境

  • NPU: Ascend910_9362 ×2(可见 6,7,本次 npu:0 映射 Phy-ID 6),npu-smi 25.5.5,CANN 8.5.1,SOC_VERSION=ascend910_9391
  • Python: 3.11.14,torch 2.9.0+cpu,torch_npu 2.9.0.post1,transformers 4.57.6,pandas 3.0.2,numpy 1.26.4,safetensors 0.6.2,huggingface_hub 0.36.2,einops 0.8.1
  • 依赖安装: pip install -r requirements.txt(已配置国内镜像,无需手动设代理,HF_ENDPOINT=https://hf-mirror.com 由系统注入)
  • 显存预估: 权重 15.8 MB + 激活 < 100 MB,npu:0 64 GB 充足;batch=2, T=64 峰值实测 37.2 MB

安装与推理

pip install -r requirements.txt
python inference.py
# 可选本地权重路径(离线):
# python inference.py --model-dir /path/to/Kronos-Tokenizer-2k

inference.py 默认:

  1. 通过 snapshot_download("NeoQuasar/Kronos-Tokenizer-2k") 加载(优先本地缓存 /opt/atomgit/.cache/huggingface/...,无需显式联网),config.json + model.safetensors
  2. 迁移模型至 npu:0(torch_npu),打印 Backend/ device/ dtype/ first param device/ input summary/ output summary/ sync timing
  3. 执行 encode(half=True) → decode(half=True),输出 s1/s2 10-bit 索引(各 1024 词表)及重构 float32 [2,64,6]
  4. 同时跑 CPU 同权对比,报告 max_abs/mean_abs 与 passed;动态 shape T=32 二次编译;warmup 3 + test 10 基准

无参数即完成真实 NPU 推理;异常返回非零。

真实结果(NPU npu:0 同步计时)

[INFO] Encode (half, 2x64x6) time: 5.37 ms (sync)
[INFO] Decode (half) time: 6.83 ms (sync)
[INFO] Forward (encode+quant+decode+loss) time: 55.36 ms (sync)
[INFO] Reconstructed shape: (2, 64, 6), First reconstructed timestep (batch0): [1.009...,0.583...,-0.241...,-0.374...,0.058...,0.074...]
[INFO] Reconstruction MAE (half): 0.272862, MaxAbs: 1.581834
[INFO] Indices half s1 shape: (2, 64), sample s1[0,:5]=[810, 608, 691, 563, 532]
[INFO] Indices half s2 shape: (2, 64), sample s2[0,:5]=[768, 607, 800, 288, 288]
[INFO] Vocab sizes: s1=1024, s2=1024, combined=1048576
[INFO] CPU vs NPU consistency (half decode): max_abs=1.221895e-06, mean_abs=1.879931e-07, passed=True (atol=1e-4 rtol=1e-3)
[INFO] Dynamic shape second run T=32 time 9.17 ms, output shape (2, 32, 6)

一致性

  • 方法: 同一权重、同一输入 torch.randn(2,64,6)*0.5 seed=42、同 half=True、float32、eval 下分别保存 cpu.npy npu.npy,scripts/compare_outputs.py --task regression
  • 结果: max_abs=1.22e-06, mean_abs=1.88e-07, finite=true, same_shape=true, passed=true(阈值 atol=1e-4 rtol=1e-3,FP32 起步,符合 NPU 误差)
  • 任务指标: 回归型比较 forecast/reconstruction 张量(非分类 logits);重构误差已在上节报告,单样本 smoke consistency,非全量数据集 benchmark

性能

  • 同步: 每轮前后 torch.npu.synchronize(),首轮编译与稳定推理分开
  • Benchmark: warmup 3 + test 10(与推理同 half 路径)
  • 本机构建机: Avg 6.04 ms, min 5.76, max 6.15, p50 6.14, p90 6.15, throughput 331.24 windows/s, batch=2, shape=[2,64,6], dtype=float32(另一次运行 7.58 ms/263 windows/s,含 NPU 首次分配波动)
  • 显存: allocated 15.4 MB, peak 37.2 MB,rows/s 换算 ~127*2 windows/s 对应 ~7680 tokens/s(每 window 64*2 通道)
  • 说明: 不含 CSV 读取,仅模型 encode+decode 计时;预处理(标准化)已计入输入生成,inverse transform 无

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志渲染(深灰背景、提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • assets/agent_workflow.png – 侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark、提交校验
  • assets/npu_device_call.png – npu-smi、NPU 可用性、设备名、模型参数/输入输出 Tensor device
  • assets/model_result.png – python inference.py 完整输出(schema、真实预测、同步耗时、状态)

agent_workflow npu_device_call model_result

限制

  • Tokenizer-only:不含 Kronos AR 预测逻辑,需与 Kronos-mini/small 联合才可做未来 pred_len 预测
  • 未测试 bf16/fp16,仅 float32;多卡/分布式未验证
  • 训练(fit)未迁移,仅推理上 NPU;数据为合成/标准化,非真实交易所全量回放

引用

  • Kronos Paper 2508.02739, GitHub shiyu-coder/Kronos, HF NeoQuasar/Kronos-Tokenizer-2k MIT 许可