test.sh 12.7 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++-14
    export CC=gcc-14
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" ]]; 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
    cmake_args=(
        -DBUILD_CPP_TEST=ON
        -DUSE_DEBUG=ON
    )
55
    if [[ $METHOD == "with-sanitizers" ]]; then
56
        cmake_args+=("-DUSE_SANITIZER=ON")
57
        if [[ -n $SANITIZERS ]]; then
58
            cmake_args+=("-DENABLED_SANITIZERS=$SANITIZERS")
59
60
        fi
    fi
61
    cmake -B build -S . "${cmake_args[@]}"
62
63
    cmake --build build --target testlightgbm -j4 || exit 1
    ./testlightgbm || exit 1
64
65
66
    exit 0
fi

67
68
69
# including python=version=[build=*_cp*] to ensure that conda prefers CPython and doesn't fall back to
# other implementations like pypy
CONDA_PYTHON_REQUIREMENT="python=${PYTHON_VERSION}[build=*_cp*]"
70
71

if [[ $TASK == "if-else" ]]; then
72
73
74
    conda create -q -y -n "${CONDA_ENV}" "${CONDA_PYTHON_REQUIREMENT}" numpy
    # shellcheck disable=SC1091
    source activate "${CONDA_ENV}"
75
76
    cmake -B build -S . || exit 1
    cmake --build build --target lightgbm -j4 || exit 1
77
78
79
80
81
    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
82
83
84
    exit 0
fi

85
86
cd "${BUILD_DIRECTORY}"

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

if [[ $TASK == "lint" ]]; then
102
103
    pwsh -command "Install-Module -Name PSScriptAnalyzer -Scope CurrentUser -SkipPublisherCheck"
    echo "Linting PowerShell code"
104
    pwsh -file ./.ci/lint-powershell.ps1 || exit 1
105
106
    conda create -q -y -n "${CONDA_ENV}" \
        "${CONDA_PYTHON_REQUIREMENT}" \
107
        'biome>=1.9.3' \
108
        'cmakelint>=1.4.3' \
109
        'cpplint>=1.6.0' \
110
111
112
113
        'matplotlib-base>=3.9.1' \
        'mypy>=1.11.1' \
        'pre-commit>=3.8.0' \
        'pyarrow-core>=17.0' \
114
        'scikit-learn>=1.5.2' \
115
        'r-lintr>=3.1.2'
116
117
    # shellcheck disable=SC1091
    source activate "${CONDA_ENV}"
118
119
    echo "Linting Python and bash code"
    bash ./.ci/lint-python-bash.sh || exit 1
120
    echo "Linting R code"
121
    Rscript ./.ci/lint-r-code.R "${BUILD_DIRECTORY}" || exit 1
122
    echo "Linting C++ code"
123
    bash ./.ci/lint-cpp.sh || exit 1
124
125
    echo "Linting JavaScript code"
    bash ./.ci/lint-js.sh || exit 1
126
127
128
    exit 0
fi

129
if [[ $TASK == "check-docs" ]] || [[ $TASK == "check-links" ]]; then
130
    conda env create \
131
132
        -n "${CONDA_ENV}" \
        --file ./docs/env.yml || exit 1
133
    conda install \
134
135
        -q \
        -y \
136
        -n "${CONDA_ENV}" \
137
            'doxygen>=1.10.0' \
138
            'rstcheck>=6.2.4' || exit 1
139
140
    # shellcheck disable=SC1091
    source activate "${CONDA_ENV}"
141
    # check reStructuredText formatting
142
143
144
145
    find "${BUILD_DIRECTORY}/python-package" -type f -name "*.rst" \
        -exec rstcheck --report-level warning {} \+ || exit 1
    find "${BUILD_DIRECTORY}/docs" -type f -name "*.rst" \
        -exec rstcheck --report-level warning --ignore-directives=autoclass,autofunction,autosummary,doxygenfile {} \+ || exit 1
146
    # build docs
147
    make -C docs html || exit 1
148
149
    if [[ $TASK == "check-links" ]]; then
        # check docs for broken links
James Lamb's avatar
James Lamb committed
150
151
        pip install 'linkchecker>=10.5.0'
        linkchecker --config=./docs/.linkcheckerrc ./docs/_build/html/*.html || exit 1
152
153
        exit 0
    fi
154
    # check the consistency of parameters' descriptions and other stuff
155
156
    cp ./docs/Parameters.rst ./docs/Parameters-backup.rst
    cp ./src/io/config_auto.cpp ./src/io/config_auto-backup.cpp
157
    python ./.ci/parameter-generator.py || exit 1
158
159
    diff ./docs/Parameters-backup.rst ./docs/Parameters.rst || exit 1
    diff ./src/io/config_auto-backup.cpp ./src/io/config_auto.cpp || exit 1
160
161
    exit 0
fi
162

163
164
if [[ $PYTHON_VERSION == "3.9" ]]; then
    CONDA_REQUIREMENT_FILE="${BUILD_DIRECTORY}/.ci/conda-envs/ci-core-py39.txt"
165
else
166
    CONDA_REQUIREMENT_FILE="${BUILD_DIRECTORY}/.ci/conda-envs/ci-core.txt"
167
168
fi

169
conda create \
170
    -y \
171
172
173
    -n "${CONDA_ENV}" \
    --file "${CONDA_REQUIREMENT_FILE}" \
    "${CONDA_PYTHON_REQUIREMENT}" \
174
|| exit 1
175

176
177
178
179
# print output of 'conda list', to help in submitting bug reports
echo "conda list:"
conda list -n ${CONDA_ENV}

180
# shellcheck disable=SC1091
181
182
source activate $CONDA_ENV

183
cd "${BUILD_DIRECTORY}"
184

Guolin Ke's avatar
Guolin Ke committed
185
if [[ $TASK == "sdist" ]]; then
186
    sh ./build-python.sh sdist || exit 1
187
    sh .ci/check-python-dists.sh ./dist || exit 1
188
    pip install "./dist/lightgbm-${LGB_VER}.tar.gz" -v || exit 1
189
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
190
        cp "./dist/lightgbm-${LGB_VER}.tar.gz" "${BUILD_ARTIFACTSTAGINGDIRECTORY}" || exit 1
191
    fi
192
    pytest ./tests/python_package_test || exit 1
Guolin Ke's avatar
Guolin Ke committed
193
194
    exit 0
elif [[ $TASK == "bdist" ]]; then
195
    if [[ $OS_NAME == "macos" ]]; then
196
        sh ./build-python.sh bdist_wheel || exit 1
197
        sh .ci/check-python-dists.sh ./dist || exit 1
198
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
199
            cp "$(echo "dist/lightgbm-${LGB_VER}-py3-none-macosx"*.whl)" "${BUILD_ARTIFACTSTAGINGDIRECTORY}" || exit 1
200
        fi
Guolin Ke's avatar
Guolin Ke committed
201
    else
202
        if [[ $ARCH == "x86_64" ]]; then
203
            PLATFORM="manylinux_2_28_x86_64"
204
205
206
        else
            PLATFORM="manylinux2014_$ARCH"
        fi
207
        sh ./build-python.sh bdist_wheel --integrated-opencl || exit 1
208
209
        # rename wheel, to fix scikit-build-core choosing the platform 'linux_aarch64' instead of
        # a manylinux tag
210
211
        mv \
            ./dist/*.whl \
212
            ./dist/tmp.whl || exit 1
213
214
        mv \
            ./dist/tmp.whl \
215
            "./dist/lightgbm-${LGB_VER}-py3-none-${PLATFORM}.whl" || exit 1
216
        sh .ci/check-python-dists.sh ./dist || exit 1
217
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
218
            cp "dist/lightgbm-${LGB_VER}-py3-none-${PLATFORM}.whl" "${BUILD_ARTIFACTSTAGINGDIRECTORY}" || exit 1
219
        fi
220
221
        # 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
222
    fi
223
224
    pip install -v ./dist/*.whl || exit 1
    pytest ./tests || exit 1
Guolin Ke's avatar
Guolin Ke committed
225
226
227
228
    exit 0
fi

if [[ $TASK == "gpu" ]]; then
229
230
    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
231
    if [[ $METHOD == "pip" ]]; then
232
        sh ./build-python.sh sdist || exit 1
233
        sh .ci/check-python-dists.sh ./dist || exit 1
234
235
        pip install \
            -v \
236
            --config-settings=cmake.define.USE_GPU=ON \
237
            "./dist/lightgbm-${LGB_VER}.tar.gz" \
238
        || exit 1
239
        pytest ./tests/python_package_test || exit 1
Guolin Ke's avatar
Guolin Ke committed
240
        exit 0
241
    elif [[ $METHOD == "wheel" ]]; then
242
        sh ./build-python.sh bdist_wheel --gpu || exit 1
243
        sh ./.ci/check-python-dists.sh ./dist || exit 1
244
        pip install "$(echo "./dist/lightgbm-${LGB_VER}"*.whl)" -v || exit 1
245
        pytest ./tests || exit 1
246
247
        exit 0
    elif [[ $METHOD == "source" ]]; then
248
        cmake -B build -S . -DUSE_GPU=ON
Guolin Ke's avatar
Guolin Ke committed
249
    fi
250
elif [[ $TASK == "cuda" ]]; then
251
252
    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
253
    # by default ``gpu_use_dp=false`` for efficiency. change to ``true`` here for exact results in ci tests
254
255
    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
256
    if [[ $METHOD == "pip" ]]; then
257
        sh ./build-python.sh sdist || exit 1
258
        sh ./.ci/check-python-dists.sh ./dist || exit 1
259
260
        pip install \
            -v \
261
            --config-settings=cmake.define.USE_CUDA=ON \
262
            "./dist/lightgbm-${LGB_VER}.tar.gz" \
263
        || exit 1
264
        pytest ./tests/python_package_test || exit 1
265
        exit 0
266
    elif [[ $METHOD == "wheel" ]]; then
267
        sh ./build-python.sh bdist_wheel --cuda || exit 1
268
        sh ./.ci/check-python-dists.sh ./dist || exit 1
269
        pip install "$(echo "./dist/lightgbm-${LGB_VER}"*.whl)" -v || exit 1
270
        pytest ./tests || exit 1
271
272
        exit 0
    elif [[ $METHOD == "source" ]]; then
273
        cmake -B build -S . -DUSE_CUDA=ON
274
    fi
275
elif [[ $TASK == "mpi" ]]; then
276
    if [[ $METHOD == "pip" ]]; then
277
        sh ./build-python.sh sdist || exit 1
278
        sh ./.ci/check-python-dists.sh ./dist || exit 1
279
280
        pip install \
            -v \
281
            --config-settings=cmake.define.USE_MPI=ON \
282
            "./dist/lightgbm-${LGB_VER}.tar.gz" \
283
        || exit 1
284
        pytest ./tests/python_package_test || exit 1
285
        exit 0
286
    elif [[ $METHOD == "wheel" ]]; then
287
        sh ./build-python.sh bdist_wheel --mpi || exit 1
288
        sh ./.ci/check-python-dists.sh ./dist || exit 1
289
        pip install "$(echo "./dist/lightgbm-${LGB_VER}"*.whl)" -v || exit 1
290
        pytest ./tests || exit 1
291
292
        exit 0
    elif [[ $METHOD == "source" ]]; then
293
        cmake -B build -S . -DUSE_MPI=ON -DUSE_DEBUG=ON
294
    fi
Guolin Ke's avatar
Guolin Ke committed
295
else
296
    cmake -B build -S .
Guolin Ke's avatar
Guolin Ke committed
297
298
fi

299
cmake --build build --target _lightgbm -j4 || exit 1
Guolin Ke's avatar
Guolin Ke committed
300

301
302
sh ./build-python.sh install --precompile || exit 1
pytest ./tests || exit 1
Guolin Ke's avatar
Guolin Ke committed
303
304

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

    # importing the library should succeed even if all optional dependencies are not present
333
    conda uninstall -n $CONDA_ENV --force --yes \
334
        cffi \
335
        dask \
336
337
        distributed \
        joblib \
338
        matplotlib-base \
339
        pandas \
340
        psutil \
341
        pyarrow \
342
        python-graphviz \
343
344
        scikit-learn || exit 1
    python -c "import lightgbm" || exit 1
Guolin Ke's avatar
Guolin Ke committed
345
fi