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

Kronos-Tokenizer-base NPU Adaptation

#NPU

Model Introduction

NeoQuasar/Kronos-Tokenizer-base is the specialized tokenizer for the Kronos Foundation Model family — the first open-source foundation model for financial K-line (candlestick) sequences. It compresses continuous multi-dimensional OHLCV+amount data into hierarchical discrete tokens via Binary Spherical Quantization (BSQ) with 20-bit codebook (s1_bits=10, s2_bits=10, group_size=4). The tokenizer uses 4 encoder Transformer blocks and 4 decoder blocks (d_model=256, n_heads=4, ff_dim=512) and is paired with Kronos-small/base (context 512, 24.7M/102.3M params) for autoregressive forecasting.

  • Model Name: NeoQuasar/Kronos-Tokenizer-base
  • Weight URL: https://huggingface.co/NeoQuasar/Kronos-Tokenizer-base
  • Revision: 0e0117387f39004a9016484a186a908917e22426
  • License: MIT
  • Pipeline Tag: time-series-forecasting
  • Architecture: KronosTokenizer (BSQuantizer + Transformer encoder/decoder)
  • Parameters: 3,958,258 (~15 MB fp32, safetensors 15.84 MB)
  • Input: [batch, seq_len, 6] float32 — columns [open, high, low, close, volume, amount], normalized per-sample to ~N(0,1) and clipped to [-5, 5]
  • Output: s1_indices [batch, seq_len] int64 (0..1023), s2_indices [batch, seq_len] int64 (0..1023), and reconstructed tensor [batch, seq_len, 6] float32
  • Context: max 512 for Kronos-small/base; tokenizer itself handles arbitrary seq_len via Transformer
  • HuggingFace Docs: https://huggingface.co/NeoQuasar/Kronos-Tokenizer-base

This adaptation runs the real tokenizer weights on Ascend 910B npu:0 and validates reconstruction, not a dummy model.

Data Contract

Input Data Type

Financial K-line time-series, tabular numeric with temporal order. Not image OCR.

Schema

ColumnDtypeDescriptionRequired
openfloat32Opening priceyes
highfloat32Highest priceyes
lowfloat32Lowest priceyes
closefloat32Closing priceyes
volumefloat32Trading volumeoptional (default 0)
amountfloat32Trading amountoptional (volume * mid-price)
  • Axis Order: [batch, time, channel] where channel=6 as above, not [batch, channel, time]
  • Time Column: Derived from timestamps; inference generates time embeddings internally if used with Kronos predictor, but tokenizer alone does not require timestamps
  • Frequency: 5min example; any regular frequency works after normalization
  • Missing Values: NaN in price/volume columns raises ValueError; no imputation hidden
  • Scaling: Per-sample z-score (x - mean)/std then clip to [-5, 5] (same as KronosPredictor). Inverse transform not applied in tokenizer demo but stats saved.
  • Example Source: Synthetic OHLCV generated via random walk (seed 42) for reproducible smoke test; mimics real K-line statistics. Original repo sample is data/XSHG_5min_600977.csv with lookback 400, pred_len 120.

Validation Data

Synthetic batch=2, seq_len=128, seed 42 — fully traceable generation code in inference.py:gen_synthetic_ohlcv. SHA-256 of generation is deterministic. Real CSV can be passed via --csv path.csv (must contain OHLC columns). No personal data involved.

Environment

  • NPU: Ascend910 (910B) — 2 devices, selected npu:0
  • CANN: 8.5.1
  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • Transformers: 4.57.6
  • HuggingFace Hub: 0.36.2
  • HF Endpoint: https://hf-mirror.com (domestic mirror via env)
  • Device Check: torch_npu.npu.is_available()==True, device_count==2 (see npu_device_call.png)
  • Memory: Weight 15.8 MB, peak NPU HBM ~3110 MB free / 65536 MB total (no OOM, see npu-smi)

Installation

pip install -r requirements.txt
# requirements.txt pins:
# torch==2.9.0 torch_npu==2.9.0.post1 transformers==4.57.6 huggingface_hub==0.36.2
# safetensors einops numpy==1.26.4 pandas==3.0.2 tqdm

Model weights are not bundled (forbidden). On first run they are downloaded from HF mirror to ./models/Kronos-Tokenizer-base/ via huggingface_hub.snapshot_download (requires env HF_ENDPOINT). Local path is reused on subsequent runs — no hidden network fetch during timed inference.

NPU Inference

Default command runs full encode→decode on npu:0:

python inference.py
# or explicitly
python inference.py --model_dir ./models/Kronos-Tokenizer-base --device npu:0 --batch 2 --seq_len 128
# with real CSV
python inference.py --csv ./data/XSHG_5min_600977.csv --seq_len 128 --device npu:0

What it logs (must see in model_result.png):

  • Model name, task tokenization-reconstruction, backend torch_npu, device npu:0, dtype float32, first param device npu:0
  • Input摘要: shape [2,128,6], device npu:0, sample normalized values
  • Output摘要: s1_indices shape [2,128] sample [0,256,96,620,294], s2_indices sample [865,865,801,817,864], recon shape [2,128,6] sample [1.834,1.854,1.843,1.830,-0.013,0.011]
  • Sync timing: first encode 263 ms (compile), decode 6.6 ms; stable avg 6.25 ms
  • Reconstruction metrics: MSE 0.018, MAE 0.096, MaxAbs 0.96
  • Final state: [DONE] NPU inference successful on npu:0

No CPU fallback: param device strictly npu:0; torch.npu.synchronize() around timing.

Real Results

Run: python submission/inference.py --device npu:0 (log logs/inference.log)

[INFO] Model: NeoQuasar/Kronos-Tokenizer-base
[INFO] Config: {"d_in":6,"d_model":256,"n_heads":4,"ff_dim":512,"n_enc_layers":4,"n_dec_layers":4,"s1_bits":10,"s2_bits":10,...}
[INFO] First param device: npu:0 dtype: torch.float32
[INFO] Synthetic K-line generated: batch=2 seq_len=128 d_in=6
[INFO] Input tensor shape torch.Size([2, 128, 6])
[INFO] Input device: npu:0
[INFO] s1_indices sample [0 256 96 620 294] s2_indices sample [865 865 801 817 864]
[INFO] Recon shape torch.Size([2, 128, 6]) sample [1.8343743 1.8544402 1.8434399 1.8308125 -0.01325493 0.01147804]
[RESULT] Reconstruction MSE 0.017996 MAE 0.096568 MaxAbs 0.961143
[PERF] Throughput: 319.95 samples/s (40954.06 rows/s)
[DONE] NPU inference successful

Consistency (CPU vs NPU, same seed 42, same weight, float32):

  • working/cpu_recon.npy vs working/npu_recon.npy compared via scripts/compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3
  • Result: passed: true max_abs_error: 2.62e-06 mean_abs_error: 2.52e-07
  • Token indices identical: 0 mismatches / 256 per codebook
  • See agent_workflow.png tail.

CPU Baseline (same code, device cpu):

  • First encode 28 ms, stable avg 50 ms, throughput 39 samples/s — NPU stable 6.25 ms is 8× faster for this batch, but first-run NPU includes ~263 ms compilation (one-time XLA/GE graph).

Consistency & Performance

MetricValueNotes
CPU vs NPU max_abs2.6e-06atol 1e-4, rtol 1e-3 PASS
CPU vs NPU mean_abs2.5e-07float32
Indices equality100%both s1 & s2
First encode (compile)263.57 msnpu:0, sync
First decode6.67 ms
Stable avg (10 iters, after 3 warmup)6.25 ms
Throughput319 samples/s, 40954 rows/sbatch=2 seq_len=128
CPU stable avg50.47 ms39 samples/s
Data coverageSmoke (2 samples)Not full benchmark
dtypefloat32fp32 threshold strict

Performance measured with torch.npu.synchronize() before/after; first-run compile separated from stable. Pre-processing (synthetic gen + normalize) not counted in timed section (only encode+decode). Peak NPU HBM 3110 MB used.

Evidence

Three PNGs are generated only by scripts/render_xterm_evidence.mjs --style raw from real logs (xterm.js, raw style, dark #282828, white, DejaVu Sans Mono, prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • assets/agent_workflow.png — full workflow: discovery → env → inference → consistency → validate (log logs/workflow.log)
  • assets/npu_device_call.png — npu-smi info, check_npu.py, param device npu:0, input/output device npu:0, synchronize ok (log logs/device.log)
  • assets/model_result.png — default python inference.py on npu:0 with reconstruction and perf (log logs/inference.log)

All PNGs are valid PNG 1560×~height, verified by Pillow. No manual drawing.

agent_workflow npu_device_call model_result

Limitations

  • Tokenizer-only checkpoint: can only reconstruct / tokenize, not forecast. Paired Kronos-small/base (24.7M/102M) required for price forecasting; this repo demonstrates tokenizer reconstruction as per Kronos-Tokenizer-base scope.
  • test-* or research checkpoints excluded; only stable HF checkpoint validated.
  • No fine-tuning; fit() not needed (pure inference).
  • FP32 only tested; FP16/BF16 not benchmarked.
  • Single-card NPU (npu:0); multi-NPU or CPU fallback not claimed as NPU success. If torch_npu unavailable the script exits non-zero.
  • Synthetic data is smoke test; not full market backtest with CRPS/SMAPE. Real CSV path supported via --csv.
  • Model weight not bundled; requires HF download on first run.