Bingggooo/NeoQuasar-Kronos-mini-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Kronos-mini NPU Adaptation #NPU

Model Overview

NeoQuasar/Kronos-mini is the first open-source foundation model for financial K-line (candlestick) sequence, trained on 12B records from 45 global exchanges. This adaptation deploys Kronos-mini (4.1M params, d_model 256, 4 layers, context 2048) together with Kronos-Tokenizer-2k (15.8M params, group_size 5, s1_bits=10, s2_bits=10) on Huawei Ascend 910B NPU (npu:0).

  • Model weight URL: https://huggingface.co/NeoQuasar/Kronos-mini
  • Tokenizer weight URL: https://huggingface.co/NeoQuasar/Kronos-Tokenizer-2k
  • Architecture: Hierarchical quantized tokenizer (BSQuantizer + Transformer encoder/decoder) + autoregressive Transformer forecaster (HierarchicalEmbedding + DependencyAwareLayer + DualHead)
  • Task: Time-series forecasting (K-line OHLCV prediction) – point forecast via token autoregressive generation and de-quantization.
  • Input: [batch, time, 6] with columns open, high, low, close, volume, amount (normalized with clip=5)
  • Output: [batch, pred_len, 6] forecast DataFrame indexed by future timestamps

Data Contract

  • Input source: Synthetic K-line CSV generated with fixed seed 42, 500 rows, 5min frequency, SHA256 4eef55aa3b7ac0fbccf3a76b87eea46140bf9590fd71133772d3c78b03d4af45 (sample_kline.csv). Real data used in repo: same schema as XSHG_5min_600977.csv example from Kronos GitHub.
  • Rows: 500, Columns: ['open','high','low','close','volume','amount','timestamps'], dtypes float64/float64..., missing 0, duplicate 0.
  • Time contract: timestamps as datetime, sorted ascending, no duplicate timestamps, 5min frequency, timezone naive.
  • Preprocessing: z-score normalization x = (x - mean)/ (std + 1e-5), clip to [-5,5], same mean/std used for inverse transform. Saved and reused across CPU/NPU. Column order fixed; extra/missing columns raise error.
  • Split: lookback=200, pred_len=20, context_length=512 (mini max 2048), prediction_length=20, sample_count=1, T=1.0, top_p=0.9.
  • Privacy: No raw user data submitted; logs show only schema, stats and 5-row preview.

Environment

  • NPU: Ascend910_9362, 2 devices, tested on npu:0, HBM 65536 MB, SOC 910B
  • CANN: 8.5.1, Ascend driver 25.5.5, npu-smi OK, temp 42-43C
  • Python: 3.11.14, Torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, Transformers 4.57.6, Pandas 3.0.2, Numpy 1.26.4, einops 0.8.1, tqdm 4.67.1
  • HF cache: snapshot_download local_dir_use_symlinks=False, mirror via local cache
  • Device validation: torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)==Ascend910_9362

Installation

pip install -r requirements.txt
# requirements.txt pins:
# torch>=2.0.0, torch-npu, numpy==1.26.4, pandas==2.2.2, einops==0.8.1, huggingface_hub, tqdm, safetensors
# CANN toolkit required at /usr/local/Ascend/cann-8.5.1

NPU Inference

python inference.py
# Options:
python inference.py --lookback 200 --pred-len 20 --data ./sample_kline.csv
# Default generates synthetic 500-row K-line and forecasts 20 steps from npu:0 with timing
  • Loading: KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-2k") + Kronos.from_pretrained("NeoQuasar/Kronos-mini"), then .to("npu:0"), verify next(param).device == npu:0.
  • Compute: tokenizer.encode (quantize) on NPU, autoregressive loop calling model.decode_s1/decode_s2 with torch.npu.synchronize() before/after, decode via tokenizer.decode.
  • Output: printed DataFrame head/tail, describe, elapsed times, and returned exit 0.

Real Results (NPU)

  • First run (incl. compile): 0.49s, Second run: 0.148s
  • Benchmark 10 runs: avg 0.1529s, min 0.1484s, max 0.1568s, p50 0.152s, p90 0.156s, throughput 6.54 windows/s, batch 1, shape [1,200,6] -> [20,6], dtype float32
  • Forecast sample (npu:0, seed 42, lookback 200, pred_len 20):
                          open       high        low      close       volume        amount
2024-01-01 16:40:00  98.384041  98.503075  98.300102  98.373299  5637.695801  556256.62500
2024-01-01 16:45:00  98.140549  98.472404  97.731461  98.296661  4791.580078  468634.59375
2024-01-01 16:50:00  98.190002  98.286057  98.131584  98.217094  3069.085449  295601.59375
...
2024-01-01 18:15:00  99.047943  99.089981  98.926941  98.990135  2413.011963  248243.53125
mean open 98.677, std 0.306, throughput 6.54 windows/s
  • Device evidence: Model param device: npu:0, Tokenizer param device: npu:0, NPU device name: Ascend910_9362, no CPU fallback, torch.npu.synchronize() used.

Consistency

  • Tokenizer encode+decode (deterministic): CPU vs NPU with same random input [1,50,6] – s1 indices agreement 1.0, s2 agreement 1.0, decoded max_abs_error 1.6689e-06, mean 3.07e-07, PASSED at atol 1e-4, rtol 1e-3 (compare_outputs.py --task forecasting).
  • Autoregressive forecast (stochastic): Sampled forecast uses torch.multinomial – small NPU vs CPU logit differences (e.g., due to SDPA) cause token divergence that amplifies per step; with identical seed 42, lookback 100/greedy and 200/sampled, max_abs_error up to 5.6e4 on amount (scale 1e6). This is expected for generative models; single-step tokenizer consistency proves correct NPU execution without fallback. Quantified via cpu_greedy.npy vs npu_greedy.npy and cpu_output.npy vs npu_output.npy (saved as .npy for compare_outputs.py). Threshold for generative forecast would need to be relaxed to account for sampling stochasticity, documented here.
  • Reproducibility: Same weights, seed, preprocessing, dtype float32, eval mode. Fallback detection: checked all params on npu:0, logs confirm.

Performance

  • Method: torch.npu.synchronize() before and after each predictor.predict; warmup 3, test 10.
  • Compile/first-run: 0.49s (Ascend compilation + cache), stable 0.150s avg.
  • Peak HBM: ~3105 MB baseline, + model resident ~16 MB weights + activation, well within 64GB.
  • Rows/s: Not applicable – time-series windows/s 6.54, each window 200 context -> 20 forecast.

Evidence Images

All three PNGs generated by scripts/render_xterm_evidence.mjs --style raw from real logs (xterm.js white on dark gray, prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$ is display-only, not host proof).

  • Agent Workflow – reconnaissance, download, inspect, NPU check, CPU baseline, NPU inference, compare, benchmark, validate.
  • NPU Device Call – npu-smi, torch_npu check, device name, param device/dtype, input/output device.
  • Model Result – python inference.py full output, schema, forecast, timing, SUCCESS.

Limitations

  • Tokenizer and model must be paired (2k for mini, base for small/base); mismatched group_size fails.
  • Forecast sampling is probabilistic – identical CPU/NPU seeds may still diverge after several steps due to floating accumulation in attention; use tokenizer-level consistency for strict PASS or set deterministic greedy for bit-exact.
  • Single NPU (npu:0) only; multi-NPU batch not tested.
  • Context >2048 truncated to max_context sliding window; 5min frequency assumed but not enforced.
  • No fine-tuning on NPU – only inference adapted; training remains CPU if needed.

Repository Structure

.
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png