GPU-Tutorial.rst 7.12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
LightGBM GPU Tutorial
=====================

The purpose of this document is to give you a quick step-by-step tutorial on GPU training.

For Windows, please see `GPU Windows Tutorial <./GPU-Windows.rst>`__.

We will use the GPU instance on `Microsoft Azure cloud computing platform`_ for demonstration,
but you can use any machine with modern AMD or NVIDIA GPUs.

GPU Setup
---------

You need to launch a ``NV`` type instance on Azure (available in East US, North Central US, South Central US, West Europe and Southeast Asia zones)
and select Ubuntu 16.04 LTS as the operating system.

For testing, the smallest ``NV6`` type virtual machine is sufficient, which includes 1/2 M60 GPU, with 8 GB memory, 180 GB/s memory bandwidth and 4,825 GFLOPS peak computation power.
Don't use the ``NC`` type instance as the GPUs (K80) are based on an older architecture (Kepler).

First we need to install minimal NVIDIA drivers and OpenCL development environment:

::

    sudo apt-get update
    sudo apt-get install --no-install-recommends nvidia-375
    sudo apt-get install --no-install-recommends nvidia-opencl-icd-375 nvidia-opencl-dev opencl-headers

After installing the drivers you need to restart the server.

::

    sudo init 6

After about 30 seconds, the server should be up again.

If you are using a AMD GPU, you should download and install the `AMDGPU-Pro`_ driver and also install package ``ocl-icd-libopencl1`` and ``ocl-icd-opencl-dev``.

Build LightGBM
--------------

Now install necessary building tools and dependencies:

::

    sudo apt-get install --no-install-recommends git cmake build-essential libboost-dev libboost-system-dev libboost-filesystem-dev

The ``NV6`` GPU instance has a 320 GB ultra-fast SSD mounted at ``/mnt``.
Let's use it as our workspace (skip this if you are using your own machine):

::

    sudo mkdir -p /mnt/workspace
    sudo chown $(whoami):$(whoami) /mnt/workspace
    cd /mnt/workspace

Now we are ready to checkout LightGBM and compile it with GPU support:

::

    git clone --recursive https://github.com/Microsoft/LightGBM
    cd LightGBM
    mkdir build ; cd build
    cmake -DUSE_GPU=1 .. 
64
65
    # if you have installed NVIDIA CUDA to a customized location, you should specify paths to OpenCL headers and library like the following:
    # cmake -DUSE_GPU=1 -DOpenCL_LIBRARY=/usr/local/cuda/lib64/libOpenCL.so -DOpenCL_INCLUDE_DIR=/usr/local/cuda/include/ ..
66
67
68
69
70
    make -j$(nproc)
    cd ..

You will see two binaries are generated, ``lightgbm`` and ``lib_lightgbm.so``.

71
If you are building on macOS, you probably need to remove macro ``BOOST_COMPUTE_USE_OFFLINE_CACHE`` in ``src/treelearner/gpu_tree_learner.h`` to avoid a known crash bug in Boost.Compute.
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

Install Python Interface (optional)
-----------------------------------

If you want to use the Python interface of LightGBM, you can install it now (along with some necessary Python-package dependencies):

::

    sudo apt-get -y install python-pip
    sudo -H pip install setuptools numpy scipy scikit-learn -U
    cd python-package/
    sudo python setup.py install --precompile
    cd ..

You need to set an additional parameter ``"device" : "gpu"`` (along with your other options like ``learning_rate``, ``num_leaves``, etc) to use GPU in Python.

88
You can read our `Python-package Examples`_ for more information on how to use the Python interface.
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

Dataset Preparation
-------------------

Using the following commands to prepare the Higgs dataset:

::

    git clone https://github.com/guolinke/boosting_tree_benchmarks.git
    cd boosting_tree_benchmarks/data
    wget "https://archive.ics.uci.edu/ml/machine-learning-databases/00280/HIGGS.csv.gz"
    gunzip HIGGS.csv.gz
    python higgs2libsvm.py
    cd ../..
    ln -s boosting_tree_benchmarks/data/higgs.train
    ln -s boosting_tree_benchmarks/data/higgs.test

Now we create a configuration file for LightGBM by running the following commands (please copy the entire block and run it as a whole):

::

    cat > lightgbm_gpu.conf <<EOF
    max_bin = 63
    num_leaves = 255
    num_iterations = 50
    learning_rate = 0.1
    tree_learner = serial
    task = train
    is_training_metric = false
    min_data_in_leaf = 1
    min_sum_hessian_in_leaf = 100
    ndcg_eval_at = 1,3,5,10
    sparse_threshold = 1.0
    device = gpu
    gpu_platform_id = 0
    gpu_device_id = 0
    EOF
    echo "num_threads=$(nproc)" >> lightgbm_gpu.conf

GPU is enabled in the configuration file we just created by setting ``device=gpu``.
Huan Zhang's avatar
Huan Zhang committed
129
130
In this configuration we use the first GPU installed on the system (``gpu_platform_id=0`` and ``gpu_device_id=0``). If ``gpu_platform_id`` or ``gpu_device_id`` is not set, the default platform and GPU will be selected.
You might have multiple platforms (AMD/Intel/NVIDIA) or GPUs. You can use the `clinfo`_ utility to identify the GPUs on each platform. On Ubuntu, you can install ``clinfo`` by executing ``sudo apt-get install clinfo``. If you have a discrete GPU by AMD/NVIDIA and an integrated GPU by Intel, make sure to select the correct ``gpu_platform_id`` to use the discrete GPU.
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

Run Your First Learning Task on GPU
-----------------------------------

Now we are ready to start GPU training!

First we want to verify the GPU works correctly.
Run the following command to train on GPU, and take a note of the AUC after 50 iterations:

::

    ./lightgbm config=lightgbm_gpu.conf data=higgs.train valid=higgs.test objective=binary metric=auc

Now train the same dataset on CPU using the following command. You should observe a similar AUC:

::

    ./lightgbm config=lightgbm_gpu.conf data=higgs.train valid=higgs.test objective=binary metric=auc device=cpu

Now we can make a speed test on GPU without calculating AUC after each iteration.

::

    ./lightgbm config=lightgbm_gpu.conf data=higgs.train objective=binary metric=auc

Speed test on CPU:

::

    ./lightgbm config=lightgbm_gpu.conf data=higgs.train objective=binary metric=auc device=cpu

You should observe over three times speedup on this GPU.

The GPU acceleration can be used on other tasks/metrics (regression, multi-class classification, ranking, etc) as well.
For example, we can train the Higgs dataset on GPU as a regression task:

::

    ./lightgbm config=lightgbm_gpu.conf data=higgs.train objective=regression_l2 metric=l2

Also, you can compare the training speed with CPU:

::

    ./lightgbm config=lightgbm_gpu.conf data=higgs.train objective=regression_l2 metric=l2 device=cpu

Further Reading
---------------

- `GPU Tuning Guide and Performance Comparison <./GPU-Performance.rst>`__

- `GPU SDK Correspondence and Device Targeting Table <./GPU-Targets.rst>`__

- `GPU Windows Tutorial <./GPU-Windows.rst>`__

Reference
---------

Please kindly cite the following article in your publications if you find the GPU acceleration useful:

Huan Zhang, Si Si and Cho-Jui Hsieh. "`GPU Acceleration for Large-scale Tree Boosting`_." arXiv:1706.08359, 2017.

.. _Microsoft Azure cloud computing platform: https://azure.microsoft.com/

Nikita Titov's avatar
Nikita Titov committed
195
.. _AMDGPU-Pro: https://www.amd.com/en/support
196

197
.. _Python-package Examples: https://github.com/Microsoft/LightGBM/tree/master/examples/python-guide
198
199

.. _GPU Acceleration for Large-scale Tree Boosting: https://arxiv.org/abs/1706.08359
Huan Zhang's avatar
Huan Zhang committed
200
201

.. _clinfo: https://github.com/Oblomov/clinfo