test_dual.py 1.16 KB
Newer Older
1
# coding: utf-8
2
3
4
"""Tests for dual GPU+CPU support."""

import os
5
import platform
6

7
import pytest
8
from sklearn.metrics import log_loss
9

10
11
import lightgbm as lgb

12
from .utils import load_breast_cancer
13
14
15
16
17
18


@pytest.mark.skipif(
    os.environ.get("LIGHTGBM_TEST_DUAL_CPU_GPU", None) is None,
    reason="Only run if appropriate env variable is set",
)
19
20
21
22
23
24
25
26
27
28
29
def test_cpu_and_gpu_work():
    # If compiled appropriately, the same installation will support both GPU and CPU.
    X, y = load_breast_cancer(return_X_y=True)
    data = lgb.Dataset(X, y)

    params_cpu = {"verbosity": -1, "num_leaves": 31, "objective": "binary", "device": "cpu"}
    cpu_bst = lgb.train(params_cpu, data, num_boost_round=10)
    cpu_score = log_loss(y, cpu_bst.predict(X))

    params_gpu = params_cpu.copy()
    params_gpu["device"] = "gpu"
30
31
    # Double-precision floats are only supported on x86_64 with PoCL
    params_gpu["gpu_use_dp"] = (platform.machine() == "x86_64")
32
33
34
    gpu_bst = lgb.train(params_gpu, data, num_boost_round=10)
    gpu_score = log_loss(y, gpu_bst.predict(X))

35
36
    rel = 1e-6 if params_gpu["gpu_use_dp"] else 1e-4
    assert cpu_score == pytest.approx(gpu_score, rel=rel)
37
    assert gpu_score < 0.242