【问题标题】:How to check if XGBoost uses the GPU如何检查 XGBoost 是否使用 GPU
【发布时间】:2022-01-27 02:30:13
【问题描述】:

我正在编写一个 pytest 文件来检查我的机器学习库是否使用 GPU。对于 Tensorflow,我可以使用 tf.config.list_physical_devices() 进行检查。对于 XGBoost,到目前为止,我在运行我的软件时通过查看 GPU 利用率 (nvdidia-smi) 对其进行了检查。但是我怎样才能在一个简单的测试中检查呢?类似于我对 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?

【问题讨论】:

    标签: python tensorflow gpu


    【解决方案1】:

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

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

    • xgb_model.fit(X, y) 上提出XGBoostError
    • xgb_model.fit(X, y) 上打印警告。这将由 pytest 提供的 capsys 夹具捕获,captured.outcaptured.err 不会为空。因此,其中一个断言将引发AssertionError
    from sklearn.datasets import load_boston
    
    def test_xgb_finds_gpu(capsys):
        """Check if XGBoost finds the GPU."""
        boston = load_boston()
        X = boston["data"]
        y = boston["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 == ""
    

    我认为可以通过为 Xy 使用更小的数组来加快这个测试,但是实现这个需要我太多的时间,因为在没有 GPU 的情况下测试只需要几秒钟并且不到第二个是 GPU。

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2019-01-12
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 2017-11-16
      • 2020-07-06
      相关资源
      最近更新 更多