XGBoost使用cuda警告:Falling back to prediction using DMatrix due to mismatched devices

问题出现

使用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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Humbunklung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值