给你的大杀器装备核弹——Windows下安装使用GPU版XGBoot详细参考指南

很早就知道XGBoost支持GPU了,不过一直没有配置,一方面是GPU版本的xgb配置,中文安装教程根本找不到,还有就是支持GPU的xgb还不是稳定版的。
花了两天时间踩遍各种坑,特意整理好这个教程,方便各位。

安装CUDA

这应该算是第一个坑,我的电脑在装tensorflow的时候就已经配置好CUDA了,看了下官方文档

Windows requirements for GPU build: only Visual C++ 2015 or 2013 with CUDA v8.0 were fully tested. Either install Visual C++ 2015 Build Tools separately, or as a part of Visual Studio 2015.

刚好我电脑上已经装好了VS2015还有CUDA v8.0,结果在下面的构建过程中炸了,各种尝试后,装了最新版的CUDA v9.0,再也没有出过问题。

构建XGBoost

先说下官网的教程

官网教程链接 http://xgboost.readthedocs.io/en/latest/build.html

github上clone源码

1
git clone --recursive https://github.com/dmlc/xgboost

在根目录下新建文件夹build,并进入该文件夹执行cmake相关命令

1
2
3
mkdir build
cd build
cmake .. -G"Visual Studio 14 2015 Win64" -DUSE_CUDA=ON

然后cmake构建

1
cmake --build . --target xgboost --config Release

最后安装python包
进入python-package目录安装

1
2
cd ..\python-package
python setup.py install

注意

  • cmake需要事先安装好,下载地址 https://cmake.org/download/ ,下载对应的msi文件,直接安装就好了
  • 根据你电脑的VS版本来更改Visual Studio 14 2015 Win64,比如你电脑上装的是vs2013的,就应该改为Visual Studio 12 2013 Win64
  • 如果命令报错,别手动输入,直接复制去执行,还有问题那就继续往下看我的方法

接下来是我自己的方法

  1. 克隆最新的xgboost源码

    1
    git clone --recursive https://github.com/dmlc/xgboost
  2. 安装CMake
    https://cmake.org/download/
    下载Windows的msi文件,直接安装

  3. 生成构建文件
    打开桌面的CMake (cmake-gui)
    分别选择源码目录以及源码目录下的build文件夹(没有就新建一个)

    点击Configure,选择Yes,然后选择自己的VS版本,点击Finish结束

    然后你会发现一片红,这不是错误的意思,不用担心,把USE_CUDA勾上

    再点击Configure

    啥也不管再来Configure!! 发现没有红的了,就说明Configure结束了

    点击Generate,你会发现build文件夹下多了vs的工程文件

  4. 开始构建
    CMake GUI中,点击Open Project,会自动调用VS打开工程,直接右键生成解决方案等待就好了,大概十分钟左右

    中途会出现各种锟斤拷,不必理会,只要最后不报错就成

  5. 安装Python包
    进入python-package目录

    1
    2
    cd ..\python-package
    python setup.py install

开跑

官方提供了一个基准(Benchmarks)

Training time time on 1,000,000 rows x 50 columns with 500 boosting iterations and 0.25/0.75 test/train split on i7-6700K CPU @ 4.00GHz and Pascal Titan X.

i7-6700K的CPU(4.00GHz)和Pascal Titan X,数据集大小为 1,000,000行 x 50列,按0.25/0.75划分测试集和训练集,训练时间如下

tree_method Time (s)
gpu_hist 13.87
hist 63.55
gpu_exact 161.08
exact 1082.20

我拿我自己的机器尝试了一下,E3-1230V5 CPU (3.40GHz),GTX 1060 GPU
tests\benchmark目录下
分别执行

1
2
3
4
python benchmark.py --tree_method gpu_hist
python benchmark.py --tree_method hist
python benchmark.py --tree_method gpu_exact
python benchmark.py --tree_method exact

训练时间如下

tree_method Time (s)
gpu_hist 23.32
hist 57.34
gpu_exact 237.32
exact 689.52

吃惊!CPU性能爆炸!
(⊙﹏⊙) 我的E3咋这么快呢。。。
暂时不管了,反正快也不是啥坏事。。。。

总的来说,训练时间缩短了不少

顺带一提,跑的过程中如果没有报错,那说明你安装是成功的,要是炸了,重头看看哪里出了问题吧~

使用方法

大体上和以前没区别,只要把 tree_method 改为 gpu_hist 或者 gpu_exact 就行
两者的区别可以简单归为:gpu_exact准确,但耗时耗内存,gpu_hist速度快,但不那么准确

1
2
param['gpu_id'] = 0
param['tree_method'] = 'gpu_hist' # or 'gpu_exact'

如果有多个GPU,想要让指定GPU跑,那就改下gpu_id,至于多卡一起跑,我暂时还没设备,以后有机会再更新吧

总结

大杀器配上核弹,效果棒极了!

参考

How to build XGBoost on Windows – Now with GPU support
Installation Guide — xgboost 0.6 documentation
XGBoost GPU Support — xgboost 0.6 documentation
XGBoost Parameters — xgboost 0.6 documentation