← Back to Pull requests

Fix single-precision routing for models with float64 thresholds #245

Closed
unknown closed this pull request without merging on Oct 10, 2026 04:43 UTC
View on GitHub
unknown unknown commented on Oct 06, 2026 23:17 UTC
Author

Models with float64 thresholds (LightGBM, scikit-learn) loaded with precision="single" can send a float32 input down a different branch than the source model does.

Cause

The importer narrows each threshold with threshold_type(node.threshold()), which rounds to nearest. For inclusive operators (<=, >), decision_forest_builder::add_node then stores nextafter(t32, +inf) and inference evaluates x < stored.

When rounding goes up (t32 > t), the float32 input x == t32 satisfies x <= t in nvForest, but the source model sees double(x) > t and goes the other way. Exclusive operators (<, >=) have the mirror problem when rounding goes down.

Only one float32 value per split is affected, x == t32, but across a large model that adds up. It is more likely with rounded data such as prices or percentages: there the midpoint thresholds are themselves rounded values, which then show up as inputs.

Fix

Round in the direction that preserves each comparison:

  • inclusive operators: the largest float32 <= t

  • exclusive operators: the smallest float32 >= t
  • For every float32 x, x <= t gives the same result as x <= floor32(t), and x < t the same as x < ceil32(t). Single precision then routes each float32 input exactly as a float64 comparison would.

  • scikit-learn casts inputs to float32 itself, so single precision now matches it for any input.

  • LightGBM compares inputs in float64. float64 inputs can still differ near a threshold, because nvForest casts inputs to float32 in single precision; precision="double" avoids that.
  • The change is a small helper, detail::cast_threshold, next to safe_cast_floating_point in decision_forest_builder.hpp. treelite_importer calls it at load time. Nothing changes for:

  • inference kernels and speed

  • double precision

  • models whose thresholds are already float32, such as XGBoost
  • Thresholds beyond the float range map to ±FLT_MAX or ±inf on the side that keeps the comparison; LightGBM stores infinite thresholds as ±1e300. NaN thresholds are left unchanged.

    Why the importer and not add_node: add_node only receives the already-narrowed value.

    Suggested labels: bug, non-breaking. The API doesn't change. Single-precision predictions change only for inputs that were misrouted, and they now match the source model.

    Tests

  • TreeliteImporter.CastThreshold (C++) tests the conversion directly: ties, round-down cases, out-of-range values, values below the smallest denormal, NaN, and widening.

  • TreeliteImporter.SinglePrecisionDoubleThresholds (C++) builds a float64-threshold model with one tree per operator (<, <=, >, >=).

  • - It loads the model in single precision with each layout.
    - It checks the float32 neighbors of thresholds that round up, round down, and overflow (±1e300) against float64 comparisons.
  • test_sklearn_single_precision_float64_threshold and test_lightgbm_single_precision_float64_threshold (Python, CPU and GPU) train a one-split model on synthetic data with each framework.

  • - Each checks nvForest against its framework's own predictions.
    - Each asserts that the trained threshold really rounds up, so it can't silently stop covering the bug.
    - LightGBM isn't in the test dependencies, so in CI only the scikit-learn test runs. The LightGBM test is skipped unless LightGBM is installed, as test_lightgbm is.

    Validated on an NVIDIA B300 with a CUDA 12.9 conda build of this branch:

  • C++: all ctest tests pass. With the importer change reverted, SinglePrecisionDoubleThresholds fails for <=/> at the round-up threshold and for </>= at the round-down and -1e300 thresholds.

  • Python: both new tests pass on CPU and GPU, and all four cases fail against the nvforest-cu13 26.08 wheels.

  • Full suite: the rest of python/nvforest/tests passes, apart from test_lightgbm_train_with_gpu_device_param. That test fails only because the CPU-only LightGBM wheel I installed reports "No OpenCL device found".
  • Reproducer

    import numpy as np
    import nvforest
    from sklearn.ensemble import RandomForestRegressor
    
    eps = float(np.finfo(np.float32).eps)
    X_train = np.array([[1.0], [1.0 + 3 * eps]], dtype=np.float32)
    model = RandomForestRegressor(n_estimators=1, max_depth=1, bootstrap=False).fit(X_train, [0.0, 1.0])
    
    # The split threshold is halfway between the training values, 1 + 1.5 * eps
    # (float64). Its nearest float32 is 1 + 2 * eps, which lies above the threshold.
    X = np.array([[1.0 + 2 * eps]], dtype=np.float32)
    print(model.predict(X))  # [1.]
    fm = nvforest.load_from_sklearn(model, precision="single", device="cpu")
    print(fm.predict(X))     # [0.] on 26.08, [1.] with this change
    

    The same happens on GPU, and with LightGBM: a one-split model trained on the same data predicts 0.55, and nvForest 26.08 gives 0.45.

    Diff: 606 lines (+454 / -152) across 7 files Reviewer: chyunsu3 Review duration: 4645m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 606 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.383
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #245"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.1% (MEDIUM) · Higher = larger blast radius
    50.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.7% · Safe to merge
    21.7%