Skip to content

xgboost/dmatrix-rebuilt-for-predict

Rule dmatrix-rebuilt-for-predict
Pack XGBoost
Severity warning
Fix none, edit by hand
Library versions every version

XGBoost's Booster.inplace_predict can score supported batches for tree boosters without allocating a new DMatrix for each batch.

for batch in batches:
yield model.predict(xgb.DMatrix(batch))
yield model.inplace_predict(batch)

The rule checks a native booster that calls predict(DMatrix(batch)) in the body of the loop that binds batch. It looks for a single positional argument in both calls and skips boosters with a known gblinear setting. It recognizes a literal parameter dictionary passed inline or assigned to the argument name directly before the booster assignment.

Use inplace_predict for a tree booster when the input type and device match what it supports and you only need ordinary predictions. For gblinear, keep DMatrix because in-place prediction raises XGBoostError. Keep DMatrix for inputs or prediction features that require it. When scoring the same matrix repeatedly, you can reuse it and benefit from native prediction caching.

In-place and staged prediction

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

XGBoost builds a DMatrix for each batch in this loop. For tree boosters and supported inputs on a compatible device, use $MODEL.inplace_predict($BATCH) to avoid that conversion.

Browse the other rules of the XGBoost pack.