Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
AI and LLM integration

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 show

Fix it fast

Most common cause, 1 of 3

A GPU model receives NumPy or pandas data from host memory

Choose where inference should run.

Fix
# 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 array
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to XGBoost 3.4.1 (Python), XGBoost 2.0 and later (device parameter)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1A GPU model receives NumPy or pandas data from host memoryXGBoost is running on: cuda:0, while the input data is on: cpu
  2. 2A CPU booster is given CuPy or cuDF dataXGBoost is running on: cpu, while the input data is on: cuda:0
  3. 3Model and data are on two different GPUsXGBoost 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.

Check
grep -rn "device" train.py predict.py

Fix

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.

Fix
# 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 array

Verify

Run the script in a new process. The warning line no longer appears and predictions match the earlier output.

Verify
python predict.py 2>&1 | grep -c "mismatched devices"

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
grep -rnE "cupy|cudf|set_param|device" predict.py

Fix

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.

Fix
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 DataFrame

Verify

A new run prints no fallback warning and preds is a cupy.ndarray, which in-place prediction returns for GPU input.

Verify
print(type(preds))   # add to the script; expect cupy.ndarray

Confirmed by xgboost.readthedocs.io

Did this fix it?

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.

Check
nvidia-smi --query-gpu=index,name --format=csv

Fix

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.

Fix
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.

Verify
python predict.py 2>&1 | grep "mismatched devices" || echo "no fallback"

Confirmed by xgboost.readthedocs.io

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.