g
gcw_coj3XaOd/NeoQuasar_Kronos-Tokenizer-base
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Kronos-Tokenizer-base - 昇腾 NPU 推理部署

1. 模型简介

模型名称: 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)

  • Encoder: 3 层 Transformer,MHA (4 heads, d_model=256),SwiGLU FFN,RoPE 位置编码
  • Decoder: 3 层 Transformer(同 Encoder 结构)
  • 量化: 20 维分层二进制标量量化 (s1_bits=10, s2_bits=10)
  • 总参数量: ~3.96M

输入规格:

  • 类型: float32 张量
  • Shape: [batch_size, seq_len, 6] (OHLCVA: Open, High, Low, Close, Volume, Amount)

输出规格:

  • 重建 K 线数据: [batch_size, seq_len, 6] float32
  • 离散 Token: [batch_size, seq_len, 20] binary (0/1)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
huggingface_hub>= 0.19.0HuggingFace 模型下载
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

3. 推理步骤

3.1 环境准备

# 检查 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))"

3.2 模型下载

模型权重自动从 HuggingFace Hub 下载(首次运行):

# 方式一:自动下载(推荐)
# 模型将在首次运行时自动从 HuggingFace 下载到缓存目录

# 方式二:手动下载
huggingface-cli download NeoQuasar/Kronos-Tokenizer-base --local-dir ./kronos_tokenizer

3.3 启动服务

# 完整编码-解码推理(推荐)
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

3.4 推理调用

# 仅编码模式(获取离散 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

3.5 推理参数说明

参数类型默认值说明
--model-idstrNeoQuasar/Kronos-Tokenizer-baseHuggingFace 模型 ID
--model-pathstrNone本地模型文件路径
--devicestrnpu运行设备 (npu/cuda/cpu)
--batch-sizeint1批处理大小
--seq-lenint128K 线序列长度
--modestrfull推理模式 (full/encode/decode)
--dtypestrfloat32模型精度 (float32/float16/bfloat16)
--seedint42随机种子

3.6 NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 卡
  • CANN 版本: 8.5.1
  • 模型精度: FP32 (支持 FP16/BF16)
  • 显存占用: ~200MB per NPU

4. 推理成功日志

4.1 编码-解码推理日志

[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%

4.2 吞吐量基准测试

[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)

4.3 NPU 设备状态

$ 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

5. 测试样例及输出结果

样例 1:完整编码-解码推理

运行命令:

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

样例 2:批量推理

运行命令:

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

样例 3:仅编码模式

运行命令:

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

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

模型类型说明: Kronos-Tokenizer-base 是一个 VQ-VAE 分词器(非标准 LLM/VLM), 其核心输出是离散二进制 token(用于下游 Kronos 预测模型),重建 MSE 不是主要评测指标。

评测指标:

指标结果说明
参数量3,958,298模型规模
Token 稀疏度54.3%零值比例(越高越好,表示高效编码)
编码吞吐量24246 tokens/sseq_len=512 时
推理延迟21.12msseq_len=512 单样本
NPU 利用率Ascend9102 卡可用
量化 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

8. 模型文件说明

文件说明
model.pyKronosTokenizer 完整实现(从 state dict 逆向工程)
inference.py昇腾 NPU 推理入口脚本
requirements.txtPython 依赖清单
model.safetensors预训练权重 (15.1 MB, 从 HuggingFace 下载)

9. 技术细节

架构创新点

Kronos-Tokenizer-base 的核心创新是 Binary Scalar Quantization (BSQ):

  1. Encoder 将 6 维 OHLCV 特征映射到 20 维连续表示
  2. 通过符号函数二值化为 20 bit 的离散 token
  3. 分层解码:前 10 bit (s1) 提供粗粒度特征,全 20 bit (s2) 提供细粒度特征
  4. decoder 通过 Transformer 自回归重建 K 线数据

已知限制

  1. 重建 MSE: 由于离散 token 量化导致信息损失,重建 MSE 较高(~23.7 on random input)
  2. 数据归一化: 原始模型训练时使用特定的金融数据归一化,未归一化数据重建质量下降
  3. final_norm: 预训练权重中不包含 final_norm 层(随机初始化,推理时自动跳过)
  4. 层数差异: config.json 声明 4 层 encoder/decoder,实际权重为 3 层

10. 引用

@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}
}