test.sh 12.6 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" ]]; 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
# including python=version[build=*cpython] to ensure that conda doesn't fall back to pypy
68
CONDA_PYTHON_REQUIREMENT="python=${PYTHON_VERSION}[build=*cpython]"
69
70

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

84
85
cd "${BUILD_DIRECTORY}"

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

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

128
if [[ $TASK == "check-docs" ]] || [[ $TASK == "check-links" ]]; then
129
    conda env create \
130
131
        -n "${CONDA_ENV}" \
        --file ./docs/env.yml || exit 1
132
    conda install \
133
134
        -q \
        -y \
135
        -n "${CONDA_ENV}" \
136
            'doxygen>=1.10.0' \
137
            'rstcheck>=6.2.4' || exit 1
138
139
    # shellcheck disable=SC1091
    source activate "${CONDA_ENV}"
140
    # check reStructuredText formatting
141
142
143
144
    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
145
    # build docs
146
    make -C docs html || exit 1
147
148
    if [[ $TASK == "check-links" ]]; then
        # check docs for broken links
149
        pip install linkchecker
150
        linkchecker --config=.linkcheckerrc ./docs/_build/html/*.html || exit 1
151
152
        exit 0
    fi
153
    # check the consistency of parameters' descriptions and other stuff
154
155
    cp ./docs/Parameters.rst ./docs/Parameters-backup.rst
    cp ./src/io/config_auto.cpp ./src/io/config_auto-backup.cpp
156
    python ./.ci/parameter-generator.py || exit 1
157
158
    diff ./docs/Parameters-backup.rst ./docs/Parameters.rst || exit 1
    diff ./src/io/config_auto-backup.cpp ./src/io/config_auto.cpp || exit 1
159
160
    exit 0
fi
161

162
if [[ $PYTHON_VERSION == "3.7" ]]; then
163
    CONDA_REQUIREMENT_FILE="${BUILD_DIRECTORY}/.ci/conda-envs/ci-core-py37.txt"
164
elif [[ $PYTHON_VERSION == "3.8" ]]; then
165
    CONDA_REQUIREMENT_FILE="${BUILD_DIRECTORY}/.ci/conda-envs/ci-core-py38.txt"
166
else
167
    CONDA_REQUIREMENT_FILE="${BUILD_DIRECTORY}/.ci/conda-envs/ci-core.txt"
168
169
fi

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

177
# shellcheck disable=SC1091
178
179
source activate $CONDA_ENV

180
cd "${BUILD_DIRECTORY}"
181

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

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

296
cmake --build build --target _lightgbm -j4 || exit 1
Guolin Ke's avatar
Guolin Ke committed
297

298
299
sh ./build-python.sh install --precompile || exit 1
pytest ./tests || exit 1
Guolin Ke's avatar
Guolin Ke committed
300
301

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

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