You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MacOS上强制Python版XGBoost仅使用CPU运行?

问题描述

在MacOS 12.6.6系统、Python 3.9.12环境下,原本使用xgboost 1.5.1版本通过CPU训练模型正常。因SHAP工具需求升级至1.6.0版本后,XGBClassifier训练失败,报错提示"XGBoost version not compiled with GPU support",但仅需CPU运行模式。添加tree_method='hist'参数强制CPU运行后问题仍未解决。

最简测试代码

#!/usr/bin/env python3
import xgboost as xgb
import pandas as pd
import numpy as np

# Create dummy data
X_train = pd.DataFrame(np.random.randn(100, 10))
y_train = np.random.randint(2, size=100)

# Initialize the model
model = xgb.XGBClassifier(tree_method='hist', objective='binary:logistic', max_depth=4, learning_rate=0.1, n_estimators=40)
model.fit(X_train, y_train)
print("Model trained successfully on CPU.")

报错信息

% ./test_xgboost.py                   
Traceback (most recent call last):
  File "/Users/timconverse/Dropbox/venture_prediction/code/oneoffs/./test_xgboost.py", line 12, in <module>
    model.fit(X_train, y_train)
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/core.py", line 532, in inner_f
    return f(**kwargs)
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/sklearn.py", line 1382, in fit
    train_dmatrix, evals = _wrap_evaluation_matrices(
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/sklearn.py", line 401, in _wrap_evaluation_matrices
    train_dmatrix = create_dmatrix(
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/sklearn.py", line 1396, in <lambda>
    create_dmatrix=lambda **kwargs: DMatrix(nthread=self.n_jobs, **kwargs),
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/core.py", line 532, in inner_f
    return f(**kwargs)
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/core.py", line 654, in __init__
    self.set_info(
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/core.py", line 532, in inner_f
    return f(**kwargs)
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/core.py", line 719, in set_info
    self.set_label(label)
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/core.py", line 850, in set_label
    dispatch_meta_backend(self, label, 'label', 'float')
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/data.py", line 1035, in dispatch_meta_backend
    _meta_from_numpy(data, name, dtype, handle)
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/data.py", line 976, in _meta_from_numpy
    _check_call(_LIB.XGDMatrixSetInfoFromInterface(handle, c_str(field), interface_str))
  File "/Users/timconverse/opt/anaconda3/lib/python3.9/site-packages/xgboost/core.py", line 203, in _check_call
    raise XGBoostError(py_str(_LIB.XGBGetLastError()))
xgboost.core.XGBoostError: [14:10:03] /Users/runner/miniforge3/conda-bld/xgboost-split_1645117948562/work/src/data/../common/common.h:157: XGBoost version not compiled with GPU support.
Stack trace:
  [bt] (0) 1   libxgboost.dylib                    0x0000000133342364 dmlc::LogMessageFatal::~LogMessageFatal() + 116
  [bt] (1) 2   libxgboost.dylib                    0x000000013338d31c xgboost::MetaInfo::SetInfo(char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 92
  [bt] (2) 3   libxgboost.dylib                    0x0000000133343a0c XGDMatrixSetInfoFromInterface + 252
  [bt] (3) 4   libffi.7.dylib                      0x000000010bf2fead ffi_call_unix64 + 85
  [bt] (4) 5   ???                                 0x0000000307439bd0 0x0 + 13006773200
解决方案

1. 重新安装CPU专属的xgboost 1.6.0版本

从报错路径看,当前安装的xgboost 1.6.0是GPU编译版本,需卸载后重新安装CPU专用包:

# 卸载现有版本
pip uninstall xgboost -y
conda remove xgboost -y  # 若用conda安装过需执行

# 安装CPU版本
pip install xgboost==1.6.0 --force-reinstall
# 或用conda安装
conda install -c conda-forge xgboost-cpu==1.6.0

2. 显式指定CPU运行参数

初始化模型时,在tree_method='hist'基础上添加device='cpu',强制模型使用CPU:

model = xgb.XGBClassifier(
    tree_method='hist',
    device='cpu',
    objective='binary:logistic',
    max_depth=4,
    learning_rate=0.1,
    n_estimators=40
)

3. 临时降级至兼容版本

若重新安装1.6.0仍有问题,可降级到1.5.2版本(兼容SHAP且CPU运行稳定):

pip install xgboost==1.5.2 --force-reinstall

4. 检查并清理GPU相关环境变量

确认是否存在强制启用GPU的环境变量,若有则取消设置:

# 查看变量值
echo $XGBOOST_USE_GPU
# 若输出为1,临时取消(永久生效需修改bashrc/zshrc)
unset XGBOOST_USE_GPU

内容的提问来源于stack exchange,提问作者user1521999

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 16:47:31