test.sh 12.1 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
#!/bin/bash

3
4
5
set -e -E -o -u pipefail

# defaults
6
CONDA_ENV="test-env"
7
8
9
10
IN_UBUNTU_BASE_CONTAINER=${IN_UBUNTU_BASE_CONTAINER:-"false"}
METHOD=${METHOD:-""}
PRODUCES_ARTIFACTS=${PRODUCES_ARTIFACTS:-"false"}
SANITIZERS=${SANITIZERS:-""}
11

12
13
ARCH=$(uname -m)

14
LGB_VER=$(head -n 1 "${BUILD_DIRECTORY}/VERSION.txt")
15

16
if [[ $OS_NAME == "macos" ]] && [[ $COMPILER == "gcc" ]]; then
17
18
    export CXX=g++-12
    export CC=gcc-12
19
elif [[ $OS_NAME == "linux" ]] && [[ $COMPILER == "clang" ]]; then
20
21
    export CXX=clang++
    export CC=clang
22
23
24
elif [[ $OS_NAME == "linux" ]] && [[ $COMPILER == "clang-17" ]]; then
    export CXX=clang++-17
    export CC=clang-17
Guolin Ke's avatar
Guolin Ke committed
25
26
fi

27
28
29
30
31
if [[ $IN_UBUNTU_BASE_CONTAINER == "true" ]]; then
    export LANG="en_US.UTF-8"
    export LC_ALL="en_US.UTF-8"
fi

32
33
34
35
36
37
38
39
40
41
42
43
44
# Setting MACOSX_DEPLOYMENT_TARGET prevents CMake from building against too-new
# macOS features, and helps tools like Python build tools determine the appropriate
# wheel compatibility tags.
#
# ref:
#   * https://cmake.org/cmake/help/latest/envvar/MACOSX_DEPLOYMENT_TARGET.html
#   * https://github.com/scikit-build/scikit-build-core/blob/acb7d0346e4a05bcb47a4ea3939c705ab71e3145/src/scikit_build_core/builder/macos.py#L36
if [[ $ARCH == "x86_64" ]]; then
    export MACOSX_DEPLOYMENT_TARGET=10.15
else
    export MACOSX_DEPLOYMENT_TARGET=12.0
fi

45
if [[ "${TASK}" == "r-package" ]] || [[ "${TASK}" == "r-rchk" ]]; then
46
    bash "${BUILD_DIRECTORY}/.ci/test-r-package.sh" || exit 1
47
48
49
    exit 0
fi

50
if [[ "$TASK" == "cpp-tests" ]]; then
51
52
53
54
55
56
57
58
    if [[ $METHOD == "with-sanitizers" ]]; then
        extra_cmake_opts="-DUSE_SANITIZER=ON"
        if [[ -n $SANITIZERS ]]; then
            extra_cmake_opts="$extra_cmake_opts -DENABLED_SANITIZERS=$SANITIZERS"
        fi
    else
        extra_cmake_opts=""
    fi
59
60
61
    cmake -B build -S . -DBUILD_CPP_TEST=ON -DUSE_OPENMP=OFF -DUSE_DEBUG=ON $extra_cmake_opts
    cmake --build build --target testlightgbm -j4 || exit 1
    ./testlightgbm || exit 1
62
63
64
    exit 0
fi

65
# including python=version[build=*cpython] to ensure that conda doesn't fall back to pypy
66
67
68
CONDA_PYTHON_REQUIREMENT="python=$PYTHON_VERSION[build=*cpython]"

if [[ $TASK == "if-else" ]]; then
69
    conda create -q -y -n $CONDA_ENV ${CONDA_PYTHON_REQUIREMENT} numpy
70
    source activate $CONDA_ENV
71
72
    cmake -B build -S . || exit 1
    cmake --build build --target lightgbm -j4 || exit 1
73
74
75
76
77
    cd "$BUILD_DIRECTORY/tests/cpp_tests"
    ../../lightgbm config=train.conf convert_model_language=cpp convert_model=../../src/boosting/gbdt_prediction.cpp
    ../../lightgbm config=predict.conf output_result=origin.pred
    ../../lightgbm config=predict.conf output_result=ifelse.pred
    python test.py
78
79
80
    exit 0
fi

81
82
cd "${BUILD_DIRECTORY}"

83
if [[ $TASK == "swig" ]]; then
84
    cmake -B build -S . -DUSE_SWIG=ON
85
    cmake --build build -j4 || exit 1
86
    if [[ $OS_NAME == "linux" ]] && [[ $COMPILER == "gcc" ]]; then
87
88
        objdump -T ./lib_lightgbm.so > ./objdump.log || exit 1
        objdump -T ./lib_lightgbm_swig.so >> ./objdump.log || exit 1
89
        python ./.ci/check-dynamic-dependencies.py ./objdump.log || exit 1
90
91
    fi
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
92
        cp ./build/lightgbmlib.jar $BUILD_ARTIFACTSTAGINGDIRECTORY/lightgbmlib_$OS_NAME.jar
93
94
95
96
97
    fi
    exit 0
fi

if [[ $TASK == "lint" ]]; then
98
    conda create -q -y -n $CONDA_ENV \
99
        ${CONDA_PYTHON_REQUIREMENT} \
100
        'cmakelint>=1.4.3' \
101
        'cpplint>=1.6.0' \
102
103
104
105
        'matplotlib-base>=3.9.1' \
        'mypy>=1.11.1' \
        'pre-commit>=3.8.0' \
        'pyarrow-core>=17.0' \
106
        'scikit-learn>=1.5.2' \
107
        'r-lintr>=3.1.2'
108
109
    source activate $CONDA_ENV
    echo "Linting Python code"
110
    bash ./.ci/lint-python.sh || exit 1
111
    echo "Linting R code"
112
    Rscript ./.ci/lint-r-code.R "${BUILD_DIRECTORY}" || exit 1
113
    echo "Linting C++ code"
114
    bash ./.ci/lint-cpp.sh || exit 1
115
116
117
    exit 0
fi

118
if [[ $TASK == "check-docs" ]] || [[ $TASK == "check-links" ]]; then
119
    cd "${BUILD_DIRECTORY}/docs"
120
    conda env create \
121
        -n $CONDA_ENV \
122
        --file ./env.yml || exit 1
123
    conda install \
124
125
126
        -q \
        -y \
        -n $CONDA_ENV \
127
            'doxygen>=1.10.0' \
128
            'rstcheck>=6.2.4' || exit 1
129
    source activate $CONDA_ENV
130
    # check reStructuredText formatting
131
    cd "${BUILD_DIRECTORY}/python-package"
132
    rstcheck --report-level warning $(find . -type f -name "*.rst") || exit 1
133
    cd "${BUILD_DIRECTORY}/docs"
134
    rstcheck --report-level warning --ignore-directives=autoclass,autofunction,autosummary,doxygenfile $(find . -type f -name "*.rst") || exit 1
135
    # build docs
136
    make html || exit 1
137
138
    if [[ $TASK == "check-links" ]]; then
        # check docs for broken links
139
        pip install linkchecker
140
        linkchecker --config=.linkcheckerrc ./_build/html/*.html || exit 1
141
142
        exit 0
    fi
143
    # check the consistency of parameters' descriptions and other stuff
144
145
146
    cd "${BUILD_DIRECTORY}"
    cp ./docs/Parameters.rst ./docs/Parameters-backup.rst
    cp ./src/io/config_auto.cpp ./src/io/config_auto-backup.cpp
147
    python ./.ci/parameter-generator.py || exit 1
148
149
    diff ./docs/Parameters-backup.rst ./docs/Parameters.rst || exit 1
    diff ./src/io/config_auto-backup.cpp ./src/io/config_auto.cpp || exit 1
150
151
    exit 0
fi
152

153
if [[ $PYTHON_VERSION == "3.7" ]]; then
154
    CONDA_REQUIREMENT_FILES="--file ${BUILD_DIRECTORY}/.ci/conda-envs/ci-core-py37.txt"
155
156
elif [[ $PYTHON_VERSION == "3.8" ]]; then
    CONDA_REQUIREMENT_FILES="--file ${BUILD_DIRECTORY}/.ci/conda-envs/ci-core-py38.txt"
157
158
else
    CONDA_REQUIREMENT_FILES="--file ${BUILD_DIRECTORY}/.ci/conda-envs/ci-core.txt"
159
160
fi

161
conda create \
162
163
164
    -y \
    -n $CONDA_ENV \
    ${CONDA_REQUIREMENT_FILES} \
165
    ${CONDA_PYTHON_REQUIREMENT} \
166
|| exit 1
167

168
169
source activate $CONDA_ENV

170
cd "${BUILD_DIRECTORY}"
171

Guolin Ke's avatar
Guolin Ke committed
172
if [[ $TASK == "sdist" ]]; then
173
    sh ./build-python.sh sdist || exit 1
174
    sh .ci/check-python-dists.sh ./dist || exit 1
175
    pip install ./dist/lightgbm-$LGB_VER.tar.gz -v || exit 1
176
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
177
        cp ./dist/lightgbm-$LGB_VER.tar.gz $BUILD_ARTIFACTSTAGINGDIRECTORY || exit 1
178
    fi
179
    pytest ./tests/python_package_test || exit 1
Guolin Ke's avatar
Guolin Ke committed
180
181
    exit 0
elif [[ $TASK == "bdist" ]]; then
182
    if [[ $OS_NAME == "macos" ]]; then
183
        sh ./build-python.sh bdist_wheel || exit 1
184
        sh .ci/check-python-dists.sh ./dist || exit 1
185
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
186
            cp dist/lightgbm-$LGB_VER-py3-none-macosx*.whl $BUILD_ARTIFACTSTAGINGDIRECTORY || exit 1
187
        fi
Guolin Ke's avatar
Guolin Ke committed
188
    else
189
        if [[ $ARCH == "x86_64" ]]; then
190
            PLATFORM="manylinux_2_28_x86_64"
191
192
193
        else
            PLATFORM="manylinux2014_$ARCH"
        fi
194
        sh ./build-python.sh bdist_wheel --integrated-opencl || exit 1
195
196
        # rename wheel, to fix scikit-build-core choosing the platform 'linux_aarch64' instead of
        # a manylinux tag
197
198
        mv \
            ./dist/*.whl \
199
            ./dist/tmp.whl || exit 1
200
201
        mv \
            ./dist/tmp.whl \
202
            ./dist/lightgbm-$LGB_VER-py3-none-$PLATFORM.whl || exit 1
203
        sh .ci/check-python-dists.sh ./dist || exit 1
204
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
205
            cp dist/lightgbm-$LGB_VER-py3-none-$PLATFORM.whl $BUILD_ARTIFACTSTAGINGDIRECTORY || exit 1
206
        fi
207
208
        # Make sure we can do both CPU and GPU; see tests/python_package_test/test_dual.py
        export LIGHTGBM_TEST_DUAL_CPU_GPU=1
Guolin Ke's avatar
Guolin Ke committed
209
    fi
210
211
    pip install -v ./dist/*.whl || exit 1
    pytest ./tests || exit 1
Guolin Ke's avatar
Guolin Ke committed
212
213
214
215
    exit 0
fi

if [[ $TASK == "gpu" ]]; then
216
217
    sed -i'.bak' 's/std::string device_type = "cpu";/std::string device_type = "gpu";/' ./include/LightGBM/config.h
    grep -q 'std::string device_type = "gpu"' ./include/LightGBM/config.h || exit 1  # make sure that changes were really done
Guolin Ke's avatar
Guolin Ke committed
218
    if [[ $METHOD == "pip" ]]; then
219
        sh ./build-python.sh sdist || exit 1
220
        sh .ci/check-python-dists.sh ./dist || exit 1
221
222
        pip install \
            -v \
223
            --config-settings=cmake.define.USE_GPU=ON \
224
            ./dist/lightgbm-$LGB_VER.tar.gz \
225
        || exit 1
226
        pytest ./tests/python_package_test || exit 1
Guolin Ke's avatar
Guolin Ke committed
227
        exit 0
228
    elif [[ $METHOD == "wheel" ]]; then
229
        sh ./build-python.sh bdist_wheel --gpu || exit 1
230
        sh ./.ci/check-python-dists.sh ./dist || exit 1
231
232
        pip install ./dist/lightgbm-$LGB_VER*.whl -v || exit 1
        pytest ./tests || exit 1
233
234
        exit 0
    elif [[ $METHOD == "source" ]]; then
235
        cmake -B build -S . -DUSE_GPU=ON
Guolin Ke's avatar
Guolin Ke committed
236
    fi
237
elif [[ $TASK == "cuda" ]]; then
238
239
    sed -i'.bak' 's/std::string device_type = "cpu";/std::string device_type = "cuda";/' ./include/LightGBM/config.h
    grep -q 'std::string device_type = "cuda"' ./include/LightGBM/config.h || exit 1  # make sure that changes were really done
240
    # by default ``gpu_use_dp=false`` for efficiency. change to ``true`` here for exact results in ci tests
241
242
    sed -i'.bak' 's/gpu_use_dp = false;/gpu_use_dp = true;/' ./include/LightGBM/config.h
    grep -q 'gpu_use_dp = true' ./include/LightGBM/config.h || exit 1  # make sure that changes were really done
243
    if [[ $METHOD == "pip" ]]; then
244
        sh ./build-python.sh sdist || exit 1
245
        sh ./.ci/check-python-dists.sh ./dist || exit 1
246
247
        pip install \
            -v \
248
            --config-settings=cmake.define.USE_CUDA=ON \
249
            ./dist/lightgbm-$LGB_VER.tar.gz \
250
        || exit 1
251
        pytest ./tests/python_package_test || exit 1
252
        exit 0
253
    elif [[ $METHOD == "wheel" ]]; then
254
        sh ./build-python.sh bdist_wheel --cuda || exit 1
255
        sh ./.ci/check-python-dists.sh ./dist || exit 1
256
257
        pip install ./dist/lightgbm-$LGB_VER*.whl -v || exit 1
        pytest ./tests || exit 1
258
259
        exit 0
    elif [[ $METHOD == "source" ]]; then
260
        cmake -B build -S . -DUSE_CUDA=ON
261
    fi
262
elif [[ $TASK == "mpi" ]]; then
263
    if [[ $METHOD == "pip" ]]; then
264
        sh ./build-python.sh sdist || exit 1
265
        sh ./.ci/check-python-dists.sh ./dist || exit 1
266
267
        pip install \
            -v \
268
            --config-settings=cmake.define.USE_MPI=ON \
269
            ./dist/lightgbm-$LGB_VER.tar.gz \
270
        || exit 1
271
        pytest ./tests/python_package_test || exit 1
272
        exit 0
273
    elif [[ $METHOD == "wheel" ]]; then
274
        sh ./build-python.sh bdist_wheel --mpi || exit 1
275
        sh ./.ci/check-python-dists.sh ./dist || exit 1
276
277
        pip install ./dist/lightgbm-$LGB_VER*.whl -v || exit 1
        pytest ./tests || exit 1
278
279
        exit 0
    elif [[ $METHOD == "source" ]]; then
280
        cmake -B build -S . -DUSE_MPI=ON -DUSE_DEBUG=ON
281
    fi
Guolin Ke's avatar
Guolin Ke committed
282
else
283
    cmake -B build -S .
Guolin Ke's avatar
Guolin Ke committed
284
285
fi

286
cmake --build build --target _lightgbm -j4 || exit 1
Guolin Ke's avatar
Guolin Ke committed
287

288
289
sh ./build-python.sh install --precompile || exit 1
pytest ./tests || exit 1
Guolin Ke's avatar
Guolin Ke committed
290
291

if [[ $TASK == "regular" ]]; then
292
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
293
        if [[ $OS_NAME == "macos" ]]; then
294
            cp ./lib_lightgbm.dylib $BUILD_ARTIFACTSTAGINGDIRECTORY/lib_lightgbm.dylib
295
        else
296
            if [[ $COMPILER == "gcc" ]]; then
297
                objdump -T ./lib_lightgbm.so > ./objdump.log || exit 1
298
                python ./.ci/check-dynamic-dependencies.py ./objdump.log || exit 1
299
            fi
300
            cp ./lib_lightgbm.so $BUILD_ARTIFACTSTAGINGDIRECTORY/lib_lightgbm.so
301
        fi
Guolin Ke's avatar
Guolin Ke committed
302
    fi
303
    cd "$BUILD_DIRECTORY/examples/python-guide"
Guolin Ke's avatar
Guolin Ke committed
304
305
306
307
    sed -i'.bak' '/import lightgbm as lgb/a\
import matplotlib\
matplotlib.use\(\"Agg\"\)\
' plot_example.py  # prevent interactive window mode
308
    sed -i'.bak' 's/graph.render(view=True)/graph.render(view=False)/' plot_example.py
309
    # requirements for examples
310
    conda install -y -n $CONDA_ENV \
311
312
313
        'h5py>=3.10' \
        'ipywidgets>=8.1.2' \
        'notebook>=7.1.2'
314
    for f in *.py **/*.py; do python $f || exit 1; done  # run all examples
315
    cd "$BUILD_DIRECTORY/examples/python-guide/notebooks"
316
    sed -i'.bak' 's/INTERACTIVE = False/assert False, \\"Interactive mode disabled\\"/' interactive_plot_example.ipynb
317
    jupyter nbconvert --ExecutePreprocessor.timeout=180 --to notebook --execute --inplace *.ipynb || exit 1  # run all notebooks
318
319

    # importing the library should succeed even if all optional dependencies are not present
320
    conda uninstall -n $CONDA_ENV --force --yes \
321
        cffi \
322
        dask \
323
324
        distributed \
        joblib \
325
        matplotlib-base \
326
        psutil \
327
        pyarrow \
328
        python-graphviz \
329
330
        scikit-learn || exit 1
    python -c "import lightgbm" || exit 1
Guolin Ke's avatar
Guolin Ke committed
331
fi