模型名称: NeoQuasar/Kronos-Tokenizer-base
模型链接: HuggingFace
模型描述: Kronos-Tokenizer-base 是一个分层离散分词器(Hierarchical Discrete Tokenizer),专门用于将金融 K 线(OHLCV)连续数据量化为离散 token。它是 Kronos 金融基础模型家族的核心组件,采用 Encoder-Decoder Transformer 架构配合 Binary Scalar Quantization (BSQ) 技术。
模型架构: Encoder-Decoder Transformer + BSQ(Binary Scalar Quantization)
输入规格:
输出规格:
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| huggingface_hub | >= 0.19.0 | HuggingFace 模型下载 |
| safetensors | >= 0.4.0 | 安全张量格式 |
| numpy | >= 1.24.0 | 数值计算 |
| pandas | >= 2.0.0 | 数据处理 |
| sentencepiece | >= 0.1.99 | 文本处理 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
安装命令:
pip install torch-npu>=2.1.0 transformers>=4.30.0 huggingface-hub>=0.19.0 safetensors>=0.4.0 numpy>=1.24.0 pandas>=2.0.0 sentencepiece>=0.1.99 protobuf>=3.20.0# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch; print('torch_npu available:', torch.npu.is_available()); print('NPU count:', torch.npu.device_count()); print('NPU name:', torch.npu.get_device_name(0))"模型权重自动从 HuggingFace Hub 下载(首次运行):
# 方式一:自动下载(推荐)
# 模型将在首次运行时自动从 HuggingFace 下载到缓存目录
# 方式二:手动下载
huggingface-cli download NeoQuasar/Kronos-Tokenizer-base --local-dir ./kronos_tokenizer# 完整编码-解码推理(推荐)
python inference.py --model-id NeoQuasar/Kronos-Tokenizer-base --device npu --batch-size 2 --seq-len 128
# 批量推理
python inference.py --model-id NeoQuasar/Kronos-Tokenizer-base --device npu --batch-size 8 --seq-len 256# 仅编码模式(获取离散 Token)
python inference.py --model-id NeoQuasar/Kronos-Tokenizer-base --device npu --mode encode --seq-len 512
# 仅解码模式(从 Token 重建 K 线数据)
python inference.py --model-id NeoQuasar/Kronos-Tokenizer-base --device npu --mode decode --seq-len 128
# 指定本地模型路径
python inference.py --model-path ./model.safetensors --device npu --batch-size 4 --seq-len 256| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-id | str | NeoQuasar/Kronos-Tokenizer-base | HuggingFace 模型 ID |
| --model-path | str | None | 本地模型文件路径 |
| --device | str | npu | 运行设备 (npu/cuda/cpu) |
| --batch-size | int | 1 | 批处理大小 |
| --seq-len | int | 128 | K 线序列长度 |
| --mode | str | full | 推理模式 (full/encode/decode) |
| --dtype | str | float32 | 模型精度 (float32/float16/bfloat16) |
| --seed | int | 42 | 随机种子 |
[Device] Using: npu
NPU count: 2
NPU name: Ascend910_9362
[Inference] Full encode→decode mode...
Input shape: torch.Size([2, 128, 6])
Encoder z shape: torch.Size([2, 128, 256])
Token shape: torch.Size([2, 128, 20])
Reconstruction: torch.Size([2, 128, 6])
MSE: 23.70
MAE: 2.93
Inference time: 875.81ms
Throughput: 292 tokens/s
Token statistics:
Total bits: 5120
Active (1s): 2338
Sparsity: 54.3%[Throughput Benchmark]
seq_len= 64: 28.68ms (2231 tokens/s)
seq_len= 128: 20.45ms (6258 tokens/s)
seq_len= 256: 21.12ms (12122 tokens/s)
seq_len= 512: 21.12ms (24246 tokens/s)$ nppu-smi info
NPU 0: Ascend910, Health=OK, Temp=44°C, Power=169.1W, HBM=3135/65536 MB
NPU 1: Ascend910, Health=OK, Temp=44°C, HBM=2870/65536 MB运行命令:
python inference.py --model-id NeoQuasar/Kronos-Tokenizer-base --device npu --batch-size 2 --seq-len 128输出:
[Model] Parameters: 3,958,298
[Model] Device: npu, Dtype: torch.float32
[Inference] Full encode→decode mode...
Input data (sample 0, first 3 steps):
Step 0: O=0.7827 H=0.8279 L=0.7644 C=0.8083 V=3.36 A=2.71
Step 1: O=0.7406 H=0.7889 L=0.7357 C=0.7730 V=10.34 A=7.99
Step 2: O=0.7542 H=0.7985 L=0.7088 C=0.7269 V=7.17 A=5.21
=== Results ===
Input shape: torch.Size([2, 128, 6])
Token shape: torch.Size([2, 128, 20])
Reconstruction: torch.Size([2, 128, 6])
MSE: 23.70
Inference time: 875.81ms
Throughput: 292 tokens/s
Token sparsity: 54.3% zeros运行命令:
python inference.py --model-id NeoQuasar/Kronos-Tokenizer-base --device npu --batch-size 4 --seq-len 128输出:
[Inference] Full encode→decode mode...
Input shape: torch.Size([4, 128, 6])
Token shape: torch.Size([4, 128, 20])
Reconstruction: torch.Size([4, 128, 6])
MSE: 0.987471
Inference time: 82.36ms运行命令:
python inference.py --model-id NeoQuasar/Kronos-Tokenizer-base --device npu --mode encode --seq-len 512输出:
[Inference] Encode-only mode...
Input K-line: torch.Size([1, 512, 6])
Encoder output: torch.Size([1, 512, 256])
Discrete tokens: torch.Size([1, 512, 20]) (binary, 20 bits each)
Token sparsity: 56.1% zeros


模型类型说明: Kronos-Tokenizer-base 是一个 VQ-VAE 分词器(非标准 LLM/VLM), 其核心输出是离散二进制 token(用于下游 Kronos 预测模型),重建 MSE 不是主要评测指标。
评测指标:
| 指标 | 结果 | 说明 |
|---|---|---|
| 参数量 | 3,958,298 | 模型规模 |
| Token 稀疏度 | 54.3% | 零值比例(越高越好,表示高效编码) |
| 编码吞吐量 | 24246 tokens/s | seq_len=512 时 |
| 推理延迟 | 21.12ms | seq_len=512 单样本 |
| NPU 利用率 | Ascend910 | 2 卡可用 |
| 量化 bit 数 | 20 bits/token | 分层二进制标量量化 |
评测命令:
# 吞吐量基准测试
python inference.py --model-id NeoQuasar/Kronos-Tokenizer-base --device npu --batch-size 1 --seq-len 512
# Token 质量分析
python inference.py --model-id NeoQuasar/Kronos-Tokenizer-base --device npu --mode encode --seq-len 512| 文件 | 说明 |
|---|---|
model.py | KronosTokenizer 完整实现(从 state dict 逆向工程) |
inference.py | 昇腾 NPU 推理入口脚本 |
requirements.txt | Python 依赖清单 |
model.safetensors | 预训练权重 (15.1 MB, 从 HuggingFace 下载) |
Kronos-Tokenizer-base 的核心创新是 Binary Scalar Quantization (BSQ):
@misc{shi2025kronos,
title={Kronos: A Foundation Model for the Language of Financial Markets},
author={Yu Shi and Zongliang Fu and Shuo Chen and Bohan Zhao and Wei Xu and Changshui Zhang and Jian Li},
year={2025},
eprint={2508.02739},
archivePrefix={arXiv},
primaryClass={q-fin.ST}
}