r
redannancy/Kronos-Tokenizer-2k-20260820
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Kronos-Tokenizer-2k 昇腾 NPU 模型适配

1. 简介

1.1 模型基本功能

Kronos-Tokenizer-2k 是 Kronos(金融 K 线基础模型,来自论文 Kronos: A Foundation Model for the Language of Financial Markets, arXiv:2508.02739)配套的离散化 Tokenizer。它将连续的多维 K 线数据(OHLCV)通过 BSQuantizer(Binary Spherical Quantizer,二值球面量化器) 编码为层级离散 token,供 Kronos 自回归模型使用。

该模型是编码器-解码器(Encoder-Decoder)结构,不是生成式 LLM:

能力说明
Encode(编码)OHLCV 序列 → s1(10bit) + s2(10bit) 两个层级 token 索引
Decode(解码)token 索引 → 重建 OHLCV 序列
Forward(完整前向)同时输出 s1-only 重建 + full codebook 重建 + BSQ loss

1.2 本次适配的目标和意义

将 Kronos-Tokenizer-2k 在华为昇腾(Ascend)NPU 上推理服务化,在国产化算力平台上稳定运行:

  • 国产化算力支持:在昇腾 910A 上实现完整 encode / decode / forward 推理;
  • 零算子替换:纯 PyTorch 算子全部 NPU 原生支持,model.to('npu:0').eval() 直接运行;
  • 服务化部署:通过 FastAPI 提供标准 HTTP 接口(/encode、/decode、/forward);
  • 内置归一化:服务端自动做 z-score 归一化(与 KronosPredictor 一致),并返回 norm_mean/norm_std 供解码反归一化。

1.3 模型原始来源及许可证

  • 原始来源:Hugging Face(通过 hf-mirror.com 镜像拉取)
  • 仓库:NeoQuasar/Kronos-Tokenizer-2k
  • 许可证:MIT License

1.4 基础信息

项目值
模型全名NeoQuasar/Kronos-Tokenizer-2k
参数量3,958,042(约 3.96M)
模型结构编码器-解码器 Transformer + BSQuantizer
编码器层数4 层(d_model=256, 4 头)
解码器层数4 层(d_model=256, 4 头)
输入维度 d_in6(open, high, low, close, volume, amount)
token 位宽s1=10bit, s2=10bit(codebook_dim=20)
BSQ group_size5
数值精度FP32
权重格式safetensors(15.8MB)

2. 验证环境

2.1 硬件环境

项目值
NPU 型号Ascend 910A
NPU 数量1 卡(npu:0)
显存(HBM)65536 MB / 卡
CPU 架构aarch64

2.2 昇腾软件栈

项目值
昇腾驱动npu-smi 25.5.5
固件版本CANN 8.5.1
CANN 路径/usr/local/Ascend/cann-8.5.1

2.3 软件依赖

项目值
Python3.11.14
PyTorch2.9.0+cpu
torch-npu2.9.0.post1+gitee7ba04
safetensors0.4.x
推理后端TorchNPU(torch_npu + FastAPI)

注:vLLM 不可用。Kronos-Tokenizer-2k 为编码器-解码器非生成架构,vLLM registry 无此架构,且无文本 tokenizer。

3. 服务启动

3.1 启动命令

cd /opt/atomgit/work/Kronos-Tokenizer-2k
python3 inference.py

3.2 配置说明

服务配置在 inference.py 顶部:

变量默认值说明
MODEL_DIR/opt/atomgit/models/Kronos-Tokenizer-2k权重目录(config.json + model.safetensors)
DEVICEnpu:0推理设备
SERVICE_PORT8000服务端口

3.3 预期启动日志

[Init] Tokenizer config: {'attn_dropout_p': 0.0, 'beta': 0.05, ...}
[Init] Tokenizer loaded: 3,958,042 params on npu:0
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000

4. API 说明

4.1 健康检查

curl -s http://127.0.0.1:8000/health

输出:

{"status":"ok","device":"npu:0","model":"Kronos-Tokenizer-2k"}

4.2 编码 /encode

将 OHLCV 序列编码为量化 token 索引。输入需 z-score 归一化(服务端自动处理)。

curl -s -X POST http://127.0.0.1:8000/encode \
  -H 'Content-Type: application/json' \
  -d '{
    "df": [
      {"open": 100.5, "high": 101.2, "low": 100.3, "close": 100.8, "volume": 5000, "amount": 504000},
      {"open": 100.8, "high": 101.5, "low": 100.6, "close": 101.1, "volume": 4800, "amount": 485280}
    ],
    "half": true,
    "normalize": true
  }'

输出:

{
  "status": "success",
  "shape": [1, 2],
  "s1_indices": [[1019, 768]],
  "s2_indices": [[735, 12]],
  "norm_mean": [100.65, 101.35, 100.45, 100.95, 4900.0, 494640.0],
  "norm_std": [0.21, 0.21, 0.21, 0.21, 141.4, 13245.0],
  "timing_ms": 308.84
}
  • normalize=true 时服务端做 (x-mean)/(std+1e-5) 并 clip 到 [-5, 5],返回 norm_mean/norm_std;
  • half=true 返回 s1、s2 两组索引;half=false 返回完整 codebook 索引。

4.3 解码 /decode

将 token 索引解码回 OHLCV。传入 norm_mean/norm_std 可自动反归一化。

curl -s -X POST http://127.0.0.1:8000/decode \
  -H 'Content-Type: application/json' \
  -d '{
    "s1_indices": [[1019, 768]],
    "s2_indices": [[735, 12]],
    "half": true,
    "norm_mean": [100.65, 101.35, 100.45, 100.95, 4900.0, 494640.0],
    "norm_std": [0.21, 0.21, 0.21, 0.21, 141.4, 13245.0]
  }'

输出:

{
  "status": "success",
  "shape": [1, 2, 6],
  "columns": ["open", "high", "low", "close", "volume", "amount"],
  "data": [[[100.52, 101.18, 100.28, 100.81, 5012.3, 505312.1], ...]],
  "timing_ms": 10.84
}

4.4 完整前向 /forward

curl -s -X POST http://127.0.0.1:8000/forward \
  -H 'Content-Type: application/json' \
  -d '{
    "df": [{"open": 100.5, "high": 101.2, "low": 100.3, "close": 100.8, "volume": 5000, "amount": 504000}]
  }'

返回 reconstructed_pre(s1-only 重建)+ reconstructed_full(full codebook 重建)+ bsq_loss。

5. Smoke 验证

cd /opt/atomgit/work/Kronos-Tokenizer-2k
python3 smoke_test.py

输出:

[Load] Tokenizer: 3,958,042 params, loaded in 1.1s
[Test 1] Encode done in 264.3 ms
[Test 1] s1_indices unique values: 20, s2_indices unique values: 16
[Test 2] Decode done in 5.9 ms
[Test 2] MSE (normalized): 0.028612
[Test 2] Recons (denorm) close[:5]: [100.54 100.04 100.50 101.48 101.33]
[Test 3] Forward done in 53.7 ms
[Test 3] BSQ loss: -0.067647
=== ALL TESTS PASSED ===

验证要点:

  • 编码后索引有足够多样性(不塌缩);
  • 解码重建与原输入接近(归一化 MSE ~0.03);
  • 反归一化后的 close 值与原始价格接近(误差 < 0.2%)。

6. 性能参考

指标值说明
模型加载时间~1.1s权重加载到 NPU
Encode(3 条 K 线)~309ms含归一化
Decode(3 条 K 线)~11ms含反归一化
Forward(3 条 K 线)~70ms完整前向 + BSQ loss

7. 注意事项

7.1 已知限制

  • 输入必须归一化:直接喂原始 OHLCV 会导致编码全塌缩到相同索引。务必使用 normalize=true(服务端自动 z-score + clip);
  • 非生成模型:Kronos-Tokenizer-2k 是编码器-解码器,不是自回归 LLM,不支持 vLLM;
  • half 模式:encode/decode 的 half 参数必须一致(/decode 的 half=true 必须传 s2_indices)。

7.2 推荐输入格式

{
  "df": [
    {"open": 100.5, "high": 101.2, "low": 100.3, "close": 100.8, "volume": 5000, "amount": 504000}
  ],
  "half": true,
  "normalize": true
}
  • 必填列:open, high, low, close;可选:volume, amount(缺省填 0);
  • 列顺序固定为 open, high, low, close, volume, amount。

7.3 适配过程中的关键问题

  1. 编码塌缩

    • 根因:模型训练时输入为归一化尺度(约 [-5, 5]),直接喂原始价格(如 ~100)超出模型分布;
    • 解决:服务端内置 z-score 归一化 (x-mean)/(std+1e-5) + clip to [-5, 5],与 KronosPredictor 完全一致。
  2. vLLM 不可用

    • 根因:编码器-解码器非生成架构,vLLM registry 无此架构,无文本 tokenizer/processor;
    • 解决:torch_npu + FastAPI 服务化路径。
  3. GitCode 下载 429 限流

    • 下载权重时 ai.gitcode.com hf_mirrors 被 429 限流;
    • 解决:改用 hf-mirror.com 镜像直接拉取。

适配平台:Ascend 910A / CANN 8.5.1 / torch_npu 2.9.0.post1 权重来源:Hugging Face (NeoQuasar/Kronos-Tokenizer-2k) 推理后端:TorchNPU