#3202·catboost

CPU CTR split rounding causes cached validation and predict to disagree

Author: LambdadertaCreated Sep 28, 2026Updated Sep 28, 2026

Problem

With CPU training and CtrBorderCount=255, cached validation predictions from get_test_eval() differ from predict(..., prediction_type="RawFormulaVal") on the same rows immediately after fitting one tree. The example disables model selection and early stopping, and uses counter_calc_method="SkipTest".

This compares validation predictions, so it is distinct from the expected difference between ordered training CTRs and full-history inference CTRs.

Environment

  • CatBoost version: 1.2.10, installed wheel
  • Python 3.13.12, NumPy 2.4.2, pandas 3.0.1
  • Operating system: macOS 27.0
  • CPU: ARM64; one training thread; exact processor model not collected
  • GPU: not used

Reproduction

python
import numpy as np
import pandas as pd
from catboost import CatBoostClassifier

positive_counts = np.arange(4, 254, 8)
categories = np.arange(len(positive_counts)).astype(str)
X = pd.DataFrame({"category": np.repeat(categories, 254)})
y = np.concatenate([
    np.r_[np.ones(s), np.zeros(254 - s)] for s in positive_counts
])
validation = pd.DataFrame({"category": categories})

for border_count in (15, 255):
    ctr = f"Borders:CtrBorderCount={border_count}:Prior=0"
    model = CatBoostClassifier(
        iterations=1, depth=4, learning_rate=0.1,
        loss_function="Logloss", boosting_type="Plain",
        one_hot_max_size=1, max_ctr_complexity=1,
        simple_ctr=[ctr], combinations_ctr=[ctr],
        counter_calc_method="SkipTest", random_seed=0,
        random_strength=0, bootstrap_type="No",
        use_best_model=False, allow_writing_files=False,
        verbose=False, thread_count=1, task_type="CPU",
    )
    model.fit(X, y, cat_features=["category"],
              eval_set=(validation, np.ones(len(validation))))
    cached = np.asarray(model.get_test_eval())
    predicted = model.predict(validation, prediction_type="RawFormulaVal")
    print(border_count, np.max(np.abs(cached - predicted)),
          np.flatnonzero(np.abs(cached - predicted) > 1e-12))

Expected: cached and applied raw predictions agree to floating-point tolerance.

Observed:

15  0.0                 []
255 0.0644120218413676  [8]

For validation row 8, the cached prediction is -0.045380264661133776, whereas predict returns -0.10979228650250138. Validation Logloss reported during fitting is 0.6942357003305782; recomputing it from model predictions gives 0.6952811581125944. This is a synthetic consistency check, not a quality benchmark.

Boundary conversion

The affected category has 68 positive labels among 254 training rows. With prior 0 and scale 255, its float32 CTR is exactly 68. Training converts it to uint8 and tests bin border 67, so 68 > 67 is true.

TSplit::EmulateUi8Rounding instead produces 67 + 0.999999f, which rounds to 68.0f. The converted model split therefore evaluates 68.0 > 68.0 as false. See the conversion call and training CTR calculation.

Reconstructing this tree from exported JSON gives leaf 12 under the training comparisons and leaf 8 under inference comparisons; their values equal the two predictions above. In a temporary JSON model, replacing integer-valued CTR thresholds with their immediate float32 predecessors restores parity for all 32 validation rows in this fixture. This is an inference-only diagnostic, not a validated native fix.

Controls and scope

  • CBM save/load preserves predict exactly, so serialization is not the source of this mismatch.
  • Flipping validation labels changes neither cached nor applied predictions when model selection is disabled.
  • The same one-tree fixture with CtrBorderCount=15, and a separate one-tree Ordered control, did not show a mismatch. This does not establish that every other configuration is unaffected.
  • The helper and conversion call are still present at master commit b99101ee790f1f00d9d085116c9f0d7a4394810a. The original behavioral results above are from the 1.2.10 wheel; the same failure has now also been reproduced on a source build of this master commit (see below).
  • Historical #125 has a similar symptom, but its documented cause was Counter CTR configuration propagation. I have not established that it covers this rounding mechanism.

Proposed narrow fix and regression coverage

For the same float32 CTR input x in [0, 256) and integer bin border b, the predicate uint8(x) > b can be expressed as x > nextafter_float32(b + 1, -infinity). Using this predecessor when constructing a new model split appears preferable to a fixed epsilon.

This needs native verification across callers, CTR types, priors and normalization paths: train and inference arithmetic can have different operation order, so fixing the threshold alone does not prove parity for every configuration. Tests should cover supported integer borders and adjacent float32 values, plus an end-to-end high-resolution CPU validation/predict comparison and save/load.

The change should affect construction of new splits. Already serialized models should retain their existing predictions, rather than having stored thresholds silently rewritten during loading.

Native before/after verification

I reproduced this on an unmodified production build of master b99101ee790f1f00d9d085116c9f0d7a4394810a, then built a local patch changing only the threshold helper (plus <cmath>):

cpp
return std::nextafter(static_cast<float>(value + 1), 0.0f);

Both arguments are float, and the supported bin borders are nonnegative, so this selects the float32 predecessor. Tested on macOS ARM64, Apple Clang 21.0.0, Release CPU build, Python 3.13.12.

  • A C++ regression checks uint8(x) > b against the converted float comparison for all b=0..254, at the predecessor, exact boundary and successor of b+1. It fails before the patch and passes all 765 comparisons after it; five existing model-application tests also pass.
  • Twelve Python cases cover three target-dependent CTR types, border counts 15/255, Plain/Ordered, and CBM save/load: two fail before the patch, all twelve pass afterward.
  • A separate 96-case matrix adds Counter and priors 0, 0.5, -1, 2, -2, 3: cached validation versus apply mismatches go from 15 to zero, with exact numerical equality. Cached training-time validation predictions are unchanged in all 96 before/after fits. Counter has equal category counts here, so it is only a limited control.
  • All 96 pre-patch CBM models retain exactly the same predictions when loaded by the patched library. New-model save/load also preserves predictions.

This is bounded CPU validation of a local patch, not a claim of exhaustive cross-platform or arbitrary-prior coverage. The existing serialized-model format and loader are unchanged.