XGBoost warns that the model and the input data sit on different devices
Your error
Falling back to prediction using DMatrix due to mismatched devices. This might lead to higher memory usage and slower performance. XGBoost is running on: cuda:0, while the input data is on: cpu.1 more line it can showFix it fast
Most common cause, 1 of 3A GPU model receives NumPy or pandas data from host memory
Choose where inference should run.
# inference on the CPU
model.set_params(device="cpu")
preds = model.predict(X)
# or inference on the GPU
import cupy as cp
model.set_params(device="cuda")
preds = model.predict(cp.asarray(X)) # returns a cupy arrayShort answer
XGBoost prints Falling back to prediction using DMatrix due to mismatched devices when in-place prediction, which the scikit-learn predict uses, gets data on another device than the booster, such as a cuda model fed a NumPy array. Results stay correct but cost more memory and time. Match the model's device to the data, or move the data to the GPU.
Which cause do you have?
The warning names both devices, so read that one line first. Booster on cuda with data on cpu is cause 1, booster on cpu with data on cuda is cause 2, and two different cuda numbers is cause 3. It is printed only once per process, so run a fresh process to see it.
python -c "import xgboost; print(xgboost.__version__)"
python predict.py 2>&1 | grep "mismatched devices"Then find the line you see in this list and open its fix:
- 1A GPU model receives NumPy or pandas data from host memory
XGBoost is running on: cuda:0, while the input data is on: cpu - 2A CPU booster is given CuPy or cuDF data
XGBoost is running on: cpu, while the input data is on: cuda:0 - 3Model and data are on two different GPUs
XGBoost is running on: cuda:0, while the input data is on: cuda:1 (or the reverse)
What does the error look like?
Falling back to prediction using DMatrix due to mismatched devices. This might lead to higher memory usage and slower performance. XGBoost is running on: cuda:0, while the input data is on: cpu.XGBoost is running on: cpu, while the input data is on: cuda:0.
Cause 1 of 3
A GPU model receives NumPy or pandas data from host memory
You seeXGBoost is running on: cuda:0, while the input data is on: cpu
Check
The mismatched devices warning appears here because the model was built with device="cuda" and predict() is handed an ordinary array that lives in host memory. In-place prediction cannot read it on the GPU, so XGBoost builds a DMatrix copy instead, which is the slow path the message describes.
grep -rn "device" train.py predict.pyFix
Choose where inference should run. For CPU inference, switch the model's device to cpu after loading it. For GPU inference, convert the batch to a GPU array (CuPy) or a cuDF DataFrame before calling predict, so in-place prediction runs on the card.
# inference on the CPU
model.set_params(device="cpu")
preds = model.predict(X)
# or inference on the GPU
import cupy as cp
model.set_params(device="cuda")
preds = model.predict(cp.asarray(X)) # returns a cupy arrayVerify
Run the script in a new process. The warning line no longer appears and predictions match the earlier output.
python predict.py 2>&1 | grep -c "mismatched devices"Confirmed by raw.githubusercontent.com
Cause 2 of 3
A CPU booster is given CuPy or cuDF data
You seeXGBoost is running on: cpu, while the input data is on: cuda:0
Check
Here the direction is reversed. device defaults to cpu, so a booster that never had device set, or one switched to cpu earlier, sees GPU-resident input and falls back to building a DMatrix from it.
grep -rnE "cupy|cudf|set_param|device" predict.pyFix
Set the booster's device to the GPU that holds the data before predicting. The GPU guide shows the same set_param call for moving a booster between devices.
import xgboost as xgb
booster = xgb.Booster()
booster.load_model("model.json")
booster.set_param({"device": "cuda:0"})
preds = booster.inplace_predict(X_gpu) # X_gpu: cupy array or cudf DataFrameVerify
A new run prints no fallback warning and preds is a cupy.ndarray, which in-place prediction returns for GPU input.
print(type(preds)) # add to the script; expect cupy.ndarrayConfirmed by xgboost.readthedocs.io
Cause 3 of 3
Model and data are on two different GPUs
You seeXGBoost is running on: cuda:0, while the input data is on: cuda:1 (or the reverse)
Check
On a machine with several cards, the device ordinal has to match too. XGBoost uses ordinal 0 when only cuda is given, while the data may have been created on another card, so the warning lists two cuda devices with different numbers.
nvidia-smi --query-gpu=index,name --format=csvFix
Point the booster at the card that holds the data with the cuda:<ordinal> form, or create the data on the card the booster already uses.
model.set_params(device="cuda:1") # the ordinal shown for the input data
preds = model.predict(X_gpu)Verify
A fresh process predicts without printing the fallback line.
python predict.py 2>&1 | grep "mismatched devices" || echo "no fallback"Confirmed by xgboost.readthedocs.io
How do I stop it coming back?
- Set device explicitly in the serving code right after loading a model, and write down there whether inference runs on CPU or GPU.
- Convert each inference batch to the target device once, before the predict call, rather than mixing host and GPU arrays in one pipeline.
- Add a test that runs one prediction in a subprocess and fails if stderr contains mismatched devices.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.