如何检查 XGBoost 是否使用 GPU

Joo*_*eey 5 python gpu tensorflow

我正在编写一个 pytest 文件来检查我的机器学习库是否使用 GPU。对于 Tensorflow,我可以使用 进行检查tf.config.list_physical_devices()。对于 XGBoost,到目前为止,我已经通过nvdidia-smi在运行软件时查看 GPU 利用率 ( ) 来检查它。但我如何通过简单的测试来检查这一点呢?类似于我对 Tensorflow 的测试就可以做到。

import pytest
import tensorflow as tf
import xgboost

# Marking all tests to be GPU dependent
pytestmark = pytest.mark.gpu

def test_tf_finds_gpu():
    """Check if Tensorflow finds the GPU."""
    assert tf.config.list_physical_devices("GPU")

def test_xgb_finds_gpu():
    """Check if XGBoost finds the GPU."""
    ...
    # What can I write here?
Run Code Online (Sandbox Code Playgroud)

Joo*_*eey 5

我采用的测试方法是使用tree_method="gpu_hist". 根据我无法确定的情况,如果找不到 GPU,这要么会引发错误,要么会打印警告。

因此,如果找不到 GPU,以下测试将通过以下两种方式之一捕获它:

  • 提出一个XGBoostError关于xgb_model.fit(X, y)
  • 在 上打印警告xgb_model.fit(X, y)。这将由 pytest 提供的固定装置捕获capsys,并且captured.outcaptured.err不会为空。因此,其中一个断言将引发AssertionError.
from sklearn.datasets import load_diabetes
import xgboost as xgb

def test_xgb_finds_gpu(capsys):
    """Check if XGBoost finds the GPU."""
    dataset = load_diabetes()
    X = dataset["data"]
    y = dataset["target"]
    xgb_model = xgb.XGBRegressor(
        # If there is no GPU, the tree_method kwarg will cause either
        # - an error in `xgb_model.fit(X, y)` (seen with pytest) or
        # - a warning printed to the console (seen in Spyder)
        # It's unclear which of the two happens under what circumstances.
        tree_method="gpu_hist"
    )
    xgb_model.fit(X, y)
    # Check that no warning was printed.
    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == ""
Run Code Online (Sandbox Code Playgroud)

我认为可以通过使用较小的数组来加快该测试的速度X,但是y实现这一点需要花费我太多的时间,因为在没有 GPU 的情况下测试只需要几秒钟,而在使用 GPU 的情况下测试只需不到一秒。


mir*_*phd 5

请注意,tree_method="gpu_hist"已弃用并将停止/自 以来已停止工作xgboost==2.0.0。直方图类型和设备当前分为两个参数:(tree_method不幸的是,覆盖了现有参数,但具有一组不同的允许级别)和一个名为 的新参数device

import numpy as np
import xgboost as xgb

xgb_model = xgb.XGBRegressor( # tree_method="gpu_hist" # deprecated
    tree_method="hist",
    device="cuda"
)

X = np.random.rand(50, 2)
y = np.random.randint(2, size=50)

xgb_model.fit(X, y)

xgb_model 
Run Code Online (Sandbox Code Playgroud)

GPU 访问正常时的输出(无警告):

XGBRegressor(base_score=None, booster=None, callbacks=None,
             colsample_bylevel=None, colsample_bynode=None,
             colsample_bytree=None, device='cuda',
             early_stopping_rounds=None,
             enable_categorical=False, eval_metric=None, feature_types=None,
             gamma=None, grow_policy=None, importance_type=None,
             interaction_constraints=None, learning_rate=None, max_bin=None,
             max_cat_threshold=None, max_cat_to_onehot=None,
             max_delta_step=None, max_depth=None, max_leaves=None,
             min_child_weight=None, missing=nan, monotone_constraints=None,
             multi_strategy=None, n_estimators=None, n_jobs=None,
             num_parallel_tree=None, random_state=None, ...)
Run Code Online (Sandbox Code Playgroud)

device无 GPU 访问 -未使用该参数的警告:

[11:43:35] WARNING: ../src/learner.cc:767: 
Parameters: { "device" } are not used.
Run Code Online (Sandbox Code Playgroud)