test.sh 10.5 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
17
18
# create the artifact upload directory if it doesn't exist yet
mkdir -p "${BUILD_ARTIFACTSTAGINGDIRECTORY}"

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

30
31
32
33
34
if [[ $IN_UBUNTU_BASE_CONTAINER == "true" ]]; then
    export LANG="en_US.UTF-8"
    export LC_ALL="en_US.UTF-8"
fi

35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

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

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
cd "${BUILD_DIRECTORY}"

if [[ $TASK == "swig" ]]; then
    cmake -B build -S . -DUSE_SWIG=ON
    cmake --build build -j4 || exit 1
    if [[ $OS_NAME == "linux" ]] && [[ $COMPILER == "gcc" ]]; then
        objdump -T ./lib_lightgbm.so > ./objdump.log || exit 1
        objdump -T ./lib_lightgbm_swig.so >> ./objdump.log || exit 1
        python ./.ci/check-dynamic-dependencies.py ./objdump.log || exit 1
    fi
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
        cp ./build/lightgbmlib.jar "${BUILD_ARTIFACTSTAGINGDIRECTORY}/lightgbmlib_${OS_NAME}.jar"
    fi
    exit 0
fi

69
if [[ "$TASK" == "cpp-tests" ]]; then
70
71
72
73
    cmake_args=(
        -DBUILD_CPP_TEST=ON
        -DUSE_DEBUG=ON
    )
74
    if [[ $METHOD == "with-sanitizers" ]]; then
75
        cmake_args+=("-DUSE_SANITIZER=ON")
76
        if [[ -n $SANITIZERS ]]; then
77
            cmake_args+=("-DENABLED_SANITIZERS=$SANITIZERS")
78
79
        fi
    fi
80
    cmake -B build -S . "${cmake_args[@]}"
81
82
    cmake --build build --target testlightgbm -j4 || exit 1
    ./testlightgbm || exit 1
83
84
85
    exit 0
fi

86
87
88
# 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*]"
89
90

if [[ $TASK == "if-else" ]]; then
91
92
93
    conda create -q -y -n "${CONDA_ENV}" "${CONDA_PYTHON_REQUIREMENT}" numpy
    # shellcheck disable=SC1091
    source activate "${CONDA_ENV}"
94
95
    cmake -B build -S . || exit 1
    cmake --build build --target lightgbm -j4 || exit 1
96
97
98
99
100
    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
101
102
103
    exit 0
fi

104
105
if [[ $PYTHON_VERSION == "3.9" ]]; then
    CONDA_REQUIREMENT_FILE="${BUILD_DIRECTORY}/.ci/conda-envs/ci-core-py39.txt"
106
else
107
    CONDA_REQUIREMENT_FILE="${BUILD_DIRECTORY}/.ci/conda-envs/ci-core.txt"
108
109
fi

110
conda create \
111
    -y \
112
113
114
    -n "${CONDA_ENV}" \
    --file "${CONDA_REQUIREMENT_FILE}" \
    "${CONDA_PYTHON_REQUIREMENT}" \
115
|| exit 1
116

117
118
119
120
# print output of 'conda list', to help in submitting bug reports
echo "conda list:"
conda list -n ${CONDA_ENV}

121
# shellcheck disable=SC1091
122
123
source activate $CONDA_ENV

124
cd "${BUILD_DIRECTORY}"
125

Guolin Ke's avatar
Guolin Ke committed
126
if [[ $TASK == "sdist" ]]; then
127
    sh ./build-python.sh sdist || exit 1
128
    sh .ci/check-python-dists.sh ./dist || exit 1
129
    pip install "./dist/lightgbm-${LGB_VER}.tar.gz" -v || exit 1
130
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
131
        cp "./dist/lightgbm-${LGB_VER}.tar.gz" "${BUILD_ARTIFACTSTAGINGDIRECTORY}" || exit 1
132
    fi
133
    pytest ./tests/python_package_test || exit 1
Guolin Ke's avatar
Guolin Ke committed
134
135
    exit 0
elif [[ $TASK == "bdist" ]]; then
136
    if [[ $OS_NAME == "macos" ]]; then
137
        sh ./build-python.sh bdist_wheel || exit 1
138
        sh .ci/check-python-dists.sh ./dist || exit 1
139
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
140
            cp "$(echo "dist/lightgbm-${LGB_VER}-py3-none-macosx"*.whl)" "${BUILD_ARTIFACTSTAGINGDIRECTORY}" || exit 1
141
        fi
Guolin Ke's avatar
Guolin Ke committed
142
    else
143
        if [[ $ARCH == "x86_64" ]]; then
144
            PLATFORM="manylinux_2_28_x86_64"
145
146
147
        else
            PLATFORM="manylinux2014_$ARCH"
        fi
148
        sh ./build-python.sh bdist_wheel --integrated-opencl || exit 1
149
150
        # rename wheel, to fix scikit-build-core choosing the platform 'linux_aarch64' instead of
        # a manylinux tag
151
152
        mv \
            ./dist/*.whl \
153
            ./dist/tmp.whl || exit 1
154
155
        mv \
            ./dist/tmp.whl \
156
            "./dist/lightgbm-${LGB_VER}-py3-none-${PLATFORM}.whl" || exit 1
157
        sh .ci/check-python-dists.sh ./dist || exit 1
158
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
159
            cp "dist/lightgbm-${LGB_VER}-py3-none-${PLATFORM}.whl" "${BUILD_ARTIFACTSTAGINGDIRECTORY}" || exit 1
160
        fi
161
162
        # 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
163
    fi
164
165
    pip install -v ./dist/*.whl || exit 1
    pytest ./tests || exit 1
Guolin Ke's avatar
Guolin Ke committed
166
167
168
169
    exit 0
fi

if [[ $TASK == "gpu" ]]; then
170
171
    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
172
    if [[ $METHOD == "pip" ]]; then
173
        sh ./build-python.sh sdist || exit 1
174
        sh .ci/check-python-dists.sh ./dist || exit 1
175
176
        pip install \
            -v \
177
            --config-settings=cmake.define.USE_GPU=ON \
178
            "./dist/lightgbm-${LGB_VER}.tar.gz" \
179
        || exit 1
180
        pytest ./tests/python_package_test || exit 1
Guolin Ke's avatar
Guolin Ke committed
181
        exit 0
182
    elif [[ $METHOD == "wheel" ]]; then
183
        sh ./build-python.sh bdist_wheel --gpu || exit 1
184
        sh ./.ci/check-python-dists.sh ./dist || exit 1
185
        pip install "$(echo "./dist/lightgbm-${LGB_VER}"*.whl)" -v || exit 1
186
        pytest ./tests || exit 1
187
188
        exit 0
    elif [[ $METHOD == "source" ]]; then
189
        cmake -B build -S . -DUSE_GPU=ON
Guolin Ke's avatar
Guolin Ke committed
190
    fi
191
elif [[ $TASK == "cuda" ]]; then
192
193
    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
194
    # by default ``gpu_use_dp=false`` for efficiency. change to ``true`` here for exact results in ci tests
195
196
    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
197
    if [[ $METHOD == "pip" ]]; then
198
        sh ./build-python.sh sdist || exit 1
199
        sh ./.ci/check-python-dists.sh ./dist || exit 1
200
201
        pip install \
            -v \
202
            --config-settings=cmake.define.USE_CUDA=ON \
203
            "./dist/lightgbm-${LGB_VER}.tar.gz" \
204
        || exit 1
205
        pytest ./tests/python_package_test || exit 1
206
        exit 0
207
    elif [[ $METHOD == "wheel" ]]; then
208
        sh ./build-python.sh bdist_wheel --cuda || exit 1
209
        sh ./.ci/check-python-dists.sh ./dist || exit 1
210
        pip install "$(echo "./dist/lightgbm-${LGB_VER}"*.whl)" -v || exit 1
211
        pytest ./tests || exit 1
212
213
        exit 0
    elif [[ $METHOD == "source" ]]; then
214
        cmake -B build -S . -DUSE_CUDA=ON
215
    fi
216
elif [[ $TASK == "mpi" ]]; then
217
    if [[ $METHOD == "pip" ]]; then
218
        sh ./build-python.sh sdist || exit 1
219
        sh ./.ci/check-python-dists.sh ./dist || exit 1
220
221
        pip install \
            -v \
222
            --config-settings=cmake.define.USE_MPI=ON \
223
            "./dist/lightgbm-${LGB_VER}.tar.gz" \
224
        || exit 1
225
        pytest ./tests/python_package_test || exit 1
226
        exit 0
227
    elif [[ $METHOD == "wheel" ]]; then
228
        sh ./build-python.sh bdist_wheel --mpi || exit 1
229
        sh ./.ci/check-python-dists.sh ./dist || exit 1
230
        pip install "$(echo "./dist/lightgbm-${LGB_VER}"*.whl)" -v || exit 1
231
        pytest ./tests || exit 1
232
233
        exit 0
    elif [[ $METHOD == "source" ]]; then
234
        cmake -B build -S . -DUSE_MPI=ON -DUSE_DEBUG=ON
235
    fi
Guolin Ke's avatar
Guolin Ke committed
236
else
237
    cmake -B build -S .
Guolin Ke's avatar
Guolin Ke committed
238
239
fi

240
cmake --build build --target _lightgbm -j4 || exit 1
Guolin Ke's avatar
Guolin Ke committed
241

242
243
sh ./build-python.sh install --precompile || exit 1
pytest ./tests || exit 1
Guolin Ke's avatar
Guolin Ke committed
244
245

if [[ $TASK == "regular" ]]; then
246
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
247
        if [[ $OS_NAME == "macos" ]]; then
248
            cp ./lib_lightgbm.dylib "${BUILD_ARTIFACTSTAGINGDIRECTORY}/lib_lightgbm.dylib"
249
        else
250
            if [[ $COMPILER == "gcc" ]]; then
251
                objdump -T ./lib_lightgbm.so > ./objdump.log || exit 1
252
                python ./.ci/check-dynamic-dependencies.py ./objdump.log || exit 1
253
            fi
254
            cp ./lib_lightgbm.so "${BUILD_ARTIFACTSTAGINGDIRECTORY}/lib_lightgbm.so"
255
        fi
Guolin Ke's avatar
Guolin Ke committed
256
    fi
257
    cd "$BUILD_DIRECTORY/examples/python-guide"
Guolin Ke's avatar
Guolin Ke committed
258
259
260
261
    sed -i'.bak' '/import lightgbm as lgb/a\
import matplotlib\
matplotlib.use\(\"Agg\"\)\
' plot_example.py  # prevent interactive window mode
262
    sed -i'.bak' 's/graph.render(view=True)/graph.render(view=False)/' plot_example.py
263
    # requirements for examples
264
    conda install -y -n $CONDA_ENV \
265
266
267
        'h5py>=3.10' \
        'ipywidgets>=8.1.2' \
        'notebook>=7.1.2'
268
    for f in *.py **/*.py; do python "${f}" || exit 1; done  # run all examples
269
    cd "$BUILD_DIRECTORY/examples/python-guide/notebooks"
270
    sed -i'.bak' 's/INTERACTIVE = False/assert False, \\"Interactive mode disabled\\"/' interactive_plot_example.ipynb
271
    jupyter nbconvert --ExecutePreprocessor.timeout=180 --to notebook --execute --inplace ./*.ipynb || exit 1  # run all notebooks
272
273

    # importing the library should succeed even if all optional dependencies are not present
274
    conda uninstall -n $CONDA_ENV --force --yes \
275
        cffi \
276
        dask \
277
278
        distributed \
        joblib \
279
        matplotlib-base \
280
        pandas \
281
        psutil \
282
        pyarrow \
283
        python-graphviz \
284
285
        scikit-learn || exit 1
    python -c "import lightgbm" || exit 1
Guolin Ke's avatar
Guolin Ke committed
286
fi