w
gcw_uQ09W7jl/NeoQuasar-Kronos-base-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

NeoQuasar/Kronos-base NPU

#NPU

Model Introduction

Kronos is the first open-source foundation model for financial candlesticks (K-lines), trained on 12B K-line records from 45 global exchanges. It uses a hierarchical tokenizer (BSQuantizer) to discretize OHLCV data into tokens, and a decoder-only Transformer to autoregressively predict future K-lines.

  • Original Model: NeoQuasar/Kronos-base (102.3M params, context 512)
  • Tokenizer: NeoQuasar/Kronos-Tokenizer-base (3.96M, d_in=6, d_model=256)
  • Combined Params: 106.27M
  • Revisions: Kronos-base 2b554741eca47781b64468546e77fef3e85130e6, Tokenizer 0e0117387f39004a9016484a186a908917e22426
  • License: MIT
  • Task: K-line time-series forecasting (tokenized autoregressive)
  • This Repository: Ascend NPU (Ascend910) adaptation running on single npu:0, deterministic inference with top_k=1.

Data Contract

  • Input: pandas DataFrame with columns open, high, low, close, volume, amount (float64, shape [T, 6]), where T=lookback. Example 256 rows. Order must be exactly OHLC+vol+amount. NaNs are rejected.
  • Timestamps: Two pd.Series of timestamps aligned to x and y: x_timestamp length T, y_timestamp length H (horizon). Derived time features are [minute, hour, weekday, day, month] (5 dims). Stamp dtype float32 after extraction.
  • Axis Order: Stored as [batch, time, feature] with batch=1 for single series. Inside model: [B, T, 6] -> z-score normalize (x - mean)/ (std+1e-5), clipped to [-5,5], then tokenizer encode to [B, T] token indices (s1 and s2).
  • Frequency: 5min K-line (example), but any frequency works as long as timestamps are continuous.
  • Preprocessing: Per-series causal z-score (mean/std computed from lookback only, applied to both input and inverse to output). Missing volume/amount auto-filled with 0, or derived from close*volume.
  • Context: max_context=512 for Kronos-base. Lookback 256 used in validation (deterministic regression). Horizon 8 used for smoke test (can extend to 120).
  • Output: pandas DataFrame indexed by y_timestamp, columns open, high, low, close, volume, amount (float32, shape [H,6]). Example SHA 05370bfe88550fcc for 256-row input. Real data from NeoQuasar regression fixture (/tmp/kronos_repo/tests/data/regression_input.csv).

Environment

  • NPU: Ascend910_9362, 2 devices, tested on npu:0 (npu-smi 25.5.5)
  • CANN: 8.5.1 (/usr/local/Ascend/cann-8.5.1)
  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • Transformers: 5.9.0 (only for version print, model uses custom Kronos code)
  • Dependencies: pandas==2.2.2, numpy==1.26.4, einops==0.8.1, huggingface_hub==0.33.1, safetensors==0.6.2, tqdm==4.67.1
  • Device Check: torch.npu.is_available()==True, torch.npu.device_count()==2, torch.npu.get_device_name(0)=="Ascend910_9362"

Installation

pip install -r requirements.txt
# torch_npu requires Ascend CANN toolkit 8.5.1

Model weights are not bundled. First run will auto-download via huggingface_hub (HF_ENDPOINT=https://hf-mirror.com, fallback to https://huggingface.co via proxy). Offline alternative: place snapshot under ./working/Kronos-base and ./working/Kronos-Tokenizer-base.

python -c "from huggingface_hub import snapshot_download; snapshot_download('NeoQuasar/Kronos-base', local_dir='working/Kronos-base'); snapshot_download('NeoQuasar/Kronos-Tokenizer-base', local_dir='working/Kronos-Tokenizer-base')"

NPU Inference

Default command runs full NPU pipeline including CPU consistency check:

python inference.py

Expected output highlights:

model_name: NeoQuasar/Kronos-base
tokenizer: NeoQuasar/Kronos-Tokenizer-base
target_device: npu:0
npu_available: True device_name: Ascend910_9362
first_param device: npu:0
[NPU] first run time 537.76 ms
[NPU] pred_df shape (8, 6) ...
[CONSISTENCY] allclose(atol=1e-4,rtol=1e-3)=True -> PASS
[RESULT] PASS
[FINAL] SUCCESS - NPU inference validated

The script explicitly calls torch.npu.synchronize() before and after timing, moves both tokenizer and model to npu:0, and prints first_param device and input device for proof.

Real Results

Using deterministic regression fixture (lookback 256, horizon 8, T=1.0 top_k=1 top_p=1.0 sample_count=1 seed=123):

  • CPU output (mean): 69607.3203, shape [8,6], finite True
  • NPU output (mean): 69607.3203, shape [8,6], finite True
  • Per-column max_abs:
    • open 9.5e-07, high 0, low 9.5e-07, close 0, volume 7.3e-04, amount 0.5625
  • Mean_abs: 0.04824
  • AllClose (atol=1e-4, rtol=1e-3): True

Detailed forecast head (NPU):

                          open       high        low      close      volume       amount
2024-06-25 14:05:00  10.766402  10.778437  10.755835  10.769899  463.833618  479256.8750
2024-06-25 14:10:00  10.769841  10.780478  10.758960  10.771648  415.909302  434510.5000
2024-06-25 14:15:00  10.771282  10.781633  10.760545  10.773098  396.625061  416207.0000

Matches expected regression tolerance from official tests (REL_TOLERANCE 1e-5).

Consistency

  • Method: Same weights, same DataFrame slice, same timestamps, same seed (123), same dtype float32, eval mode, top_k=1 deterministic.
  • Files: /tmp/cpu_kronos.npy vs /tmp/npu_kronos.npy (forecast tensor [8,6])
  • Tool: python scripts/compare_outputs.py --cpu cpu.npy --npu npu.npy --task forecasting --atol 1e-4 --rtol 1e-3
  • Result:
{
  "task": "forecasting",
  "cpu_shape": [8, 6],
  "npu_shape": [8, 6],
  "finite": true,
  "atol": 0.0001,
  "rtol": 0.001,
  "max_abs_error": 0.5625,
  "mean_abs_error": 0.048243,
  "passed": true
}
  • Claim: Smoke consistency on 8-step forecast window, not full benchmark. Amount column dominates absolute error due to scale (4e5) but relative error <1e-6.

Performance

Measured on Ascend910_9362 single npu:0, lookback 256 horizon 8 batch 1 dtype float32:

  • Warmup: 3 runs
  • Test: 10 runs with torch.npu.synchronize() around each
  • First run (compile): 438-445 ms
  • Stable: avg 143.96 ms (min 140.64 max 148.23 p50 144.00 p90 147.33 p95 147.78) ; second run 104-114 ms
  • Throughput: ~6.95 series/s (batch=1) = 9.6 windows/s
  • Memory: allocated 412.4 MB reserved 560.0 MB peak 470.8 MB
  • Preproc: DataFrame timestamp extraction + z-score + clip included in timing? No, timing covers only predictor.predict including preprocessing and model forward. Inverse scaling included.
  • Note: First run includes compilation and KV buffer allocation; stable runs exclude compilation. For longer horizon (e.g., 120) throughput scales linearly.

Evidence

Three xterm.js screenshots generated from real logs via node scripts/render_xterm_evidence.mjs --style raw:

  • agent workflow
  • npu device call
  • model result

Manifest records command, exit_code, executed_at ISO8601, and source_sha256 for each log.

Limitations

  • Only single-series batch=1 tested; predict_batch not validated on NPU (should work with same code path but not benchmarked).
  • Stochastic mode (top_k=0 top_p=0.9) not validated for consistency; use top_k=1 for deterministic reproducibility.
  • Tokenizer and model must be on same device; CPU fallback is detected as failure.
  • Amount scale large => absolute tolerance must be interpreted relative; use atol scaled to feature magnitude for practical trading.
  • Requires CANN 8.5.1 and torch_npu 2.9.0; other versions not tested.
  • Weights not included; offline use requires manual snapshot.