Restore dmatrix.__init__ even if validate_data_initialization raises - #12398
Open
Nityahapani wants to merge 1 commit into
Open
Restore dmatrix.__init__ even if validate_data_initialization raises#12398Nityahapani wants to merge 1 commit into
Nityahapani wants to merge 1 commit into
Conversation
validate_data_initialization() monkey-patches dmatrix.__init__ (the
REAL, shared class passed in -- e.g. the actual xgboost.QuantileDMatrix,
not a copy) with a counting wrapper, then restores the original
implementation at the very end of the function.
There was no try/finally protecting that restoration. This means: if
either of the internal `fit()` calls or, notably, either of the two
`assert count[0] == ...` checks raises -- which is exactly the failure
mode this function exists to detect (a regression causing an extra
DMatrix to be constructed) -- the restoration line is skipped, and
the real, shared dmatrix class stays permanently monkey-patched with
the counting wrapper for the rest of the test session. Every
subsequent test constructing that class would silently carry this
instrumentation.
Fix: wrap the body in try/finally so dmatrix.__init__ is always
restored, whether the function succeeds or one of the internal calls/
assertions raises. Also removed the now-redundant duplicate
restoration line that was left after the original final statement.
Verified standalone with a toy stand-in class and both an
assertion-failure path and the normal success path:
- before the fix: an assertion failure genuinely leaves __init__
permanently patched (confirmed via `is not` identity check)
- after the fix: __init__ is correctly restored in both the
exception case and the normal success case
mypy and ruff both pass on the modified file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Restore dmatrix.init even if validate_data_initialization raises
Summary
validate_data_initialization()monkey-patchesdmatrix.__init__(thereal, shared class passed in — e.g. the actual
xgboost.QuantileDMatrix,not a copy) with a counting wrapper, then restores the original
implementation at the very end of the function.
There was no
try/finallyprotecting that restoration. This means: ifeither of the internal
fit()calls or, notably, either of the twoassert count[0] == ...checks raises — which is exactly the failure modethis function exists to detect (a regression causing an extra
DMatrixtobe constructed) — the restoration line is skipped, and the real, shared
dmatrixclass stays permanently monkey-patched with the countingwrapper for the rest of the test session. Every subsequent test constructing
that class would silently carry this instrumentation.
Why this matters
This function is called with
xgb.QuantileDMatrix(the real, importableclass) in
test_with_sklearn.py. If it ever catches the exact regressionit's designed to catch, the class corruption it leaves behind could make
unrelated, later test failures confusing to debug.
Change
Wraps the body in
try/finallysodmatrix.__init__is always restored,whether the function succeeds or one of the internal calls/assertions
raises. Also removed the now-redundant duplicate restoration line that was
left after the original final statement.
Verification
__init__permanently patched (confirmed via an
is notidentity check against theoriginal method)
__init__is correctly restored in both the exceptioncase and the normal success case
mypyandruffboth pass on the modified file.