问题出现
使用XGBoost时,开启cuda模式,混用CPU张量和GPU张量时,将出现这样的错误:
UserWarning: [17:42:01] WARNING: C:\buildkite-agent\builds\buildkite-windows-cpu-autoscaling-group-i-0b3782d1791676daf-1\xgboost\xgboost-ci-windows\src\common\error_msg.cc:58: Falling back to prediction using DMatrix due to mismatched devices. This might lead to higher memory usage and slower performance. XGBoost is
running on: cpu, while the input data is on: cuda:0.
Potential solutions:
- Use a data structure that matches the device ordinal in the booster.
- Set the device for booster before call to inplace_predict.
This warning will only be shown once.
warnings.warn(smsg, UserWarning)
问题解决
通过警告内容的“潜在解决方案”提示,我们可以通过调整数据结构(将适配CPU的数组调整到适应GPU的数组)或将设备修改回CPU即可解决。我们不妨采用GPU数组的方法,这时候,需要安装CuPy进行支持(安装的时候要指定本地CUDA版本,具体参见他们的官方文档:Installation — CuPy 13.1.0 documentation)。
两种类型的数组测试代码如下:
import cupy as cp
import xgboost as xgb
from sklearn.datasets import make_regression
X, y = make_regression()
reg = xgb.XGBRegressor()
reg.fit(X, y)
# No warning, reg and X are on CPU
reg.predict(X)
# Put X into GPU
X = cp.array(X)
# Put reg to GPU
reg.set_params(device="cuda")
# No warning, both on GPU
reg.predict(X)
# Warning, reg is on CPU, but X on GPU
reg.set_params(device="cpu")
reg.predict(X)
X = cp.asnumpy(X)
reg.set_params(device="cuda")
# Warning, reg is on GPU, but X on CPU
reg.predict(X)