Pool cannot reload saved quantization borders containing infinity
Problem
Pool.quantize accepts a feature containing infinity. Pool.save_quantization_borders then writes -inf or inf, but a new Pool cannot reload that same file through input_borders. No fitting, targets or missing values are needed to reproduce this.
Environment
- CatBoost version: 1.2.10, installed wheel
- Python 3.13.12, NumPy 2.4.2
- Operating system: macOS 27.0
- CPU: ARM64; exact processor model not collected
- GPU: not used
Reproduction
from pathlib import Path
from tempfile import TemporaryDirectory
import numpy as np
from catboost import Pool
X = np.array([[-np.inf], [0.0], [1.0]])
with TemporaryDirectory() as directory:
path = str(Path(directory) / "borders.tsv")
pool = Pool(X)
pool.quantize(border_count=16)
pool.save_quantization_borders(path)
print(Path(path).read_text())
Pool(X).quantize(input_borders=path)The file contains these two tab-separated lines:
0\t-inf
0\t0.5Here \t denotes a literal tab. Reloading raises:
Incorrect file with borders. Error while parsing line #0:
util/string/cast.cpp:623: cannot parse float(-inf)Expected: borders saved for an accepted input can be read back, preserving quantization semantics.
Controls and source investigation
X=[[-1.0], [0.0], [np.inf]]writesinfand fails withcannot parse float(inf).- Finite
X=[[-1.0], [0.0], [1.0]]saves and reloads successfully. - Separate small probes of CBM and binary quantized Pool roundtrips preserved predictions with infinities. The reproduced failure concerns text quantization borders.
- Replacing only the temporary file's
inf/-inftokens with1e999/-1e999lets the existing reader reconstruct the borders. Saving them again reproduces the original file byte-for-byte, and model predictions on the original/reconstructed quantized Pools agree. This is a diagnostic, not a proposed file-format convention.
The borders I/O helper writes borders using ToString<double> but reads them through FromString<float>. The generic parser intentionally rejects special literals, as covered by its NaN/Infinity tests. This appears to be a writer/reader contract mismatch at the borders I/O layer, not a reason to change the generic parser's contract.
The analogous borders source path was inspected at master commit b99101ee790f1f00d9d085116c9f0d7a4394810a. The original reproduction is on the 1.2.10 wheel; the same failure has now also been reproduced on a source build of this master commit (see below).
For context, the original API PR #1223 included a save/load/re-save equality test. #2696 explains the intended treatment of input infinity, but does not describe this text roundtrip failure.
Proposed narrow fix and regression coverage
Handle exactly the writer's inf and -inf tokens locally in the borders reader, keeping ordinary values on the existing parser path. This would also support files already emitted by the writer without changing the public output format or the generic string-to-float parser.
Do not implicitly broaden this to arbitrary infinity spellings or NaN borders; those require separate semantic decisions. Regression tests should cover finite and both infinite boundaries, save/load/re-save equality, and equivalent quantization/model predictions, while preserving malformed-input rejection and existing nan-mode behavior.
Native before/after verification
I reproduced this on an unmodified production build of master b99101ee790f1f00d9d085116c9f0d7a4394810a, then built a local patch that recognizes exactly inf and -inf in ParseBordersFileLine and keeps all other tokens on the original FromString<float> path. Tested on macOS ARM64, Apple Clang 21.0.0, Release CPU build, Python 3.13.12.
- Two new C++ infinity tests fail on the original reader and pass after the patch. Together with finite/malformed-input checks and existing Save/Load tests, all five selected native borders tests pass. Coverage includes optional Min/Max modes and categorical/ignored columns before numeric features.
- Eight Python cases cover finite, negative-infinite, positive-infinite and both-infinite data, with/without a preceding categorical column. Six fail to reload before the patch; all eight pass afterward, including byte-identical re-export and equal predictions of a numeric-only model on the original/reconstructed quantized Pools.
- Additional finite-plus-NaN controls retain Min/Max behavior; malformed tokens and NaN border literals are still rejected.
Scope: mixed NaN and infinite values can trigger a separate pre-existing nan-sentinel/border-ordering failure when predicting the original quantized Pool, without saving or reloading any borders. It reproduces on both baseline and patched builds and is not solved by this parser change. The minimal reproduction above has no NaN values.
Source: catboost/catboost