Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GPU RF #6755

Merged
merged 3 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/tree/updater_gpu_hist.cu
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ struct GPUHistMakerDevice {
std::unique_ptr<RowPartitioner> row_partitioner;
DeviceHistogram<GradientSumT> hist{};

dh::caching_device_vector<GradientPair> d_gpair; // storage for gpair;
common::Span<GradientPair> gpair;

dh::caching_device_vector<int> monotone_constraints;
Expand Down Expand Up @@ -269,7 +270,13 @@ struct GPUHistMakerDevice {
std::fill(node_sum_gradients.begin(), node_sum_gradients.end(),
GradientPair());

auto sample = sampler->Sample(dh_gpair->DeviceSpan(), dmat);
if (d_gpair.size() != dh_gpair->Size()) {
d_gpair.resize(dh_gpair->Size());
}
thrust::copy(thrust::device, dh_gpair->ConstDevicePointer(),
dh_gpair->ConstDevicePointer() + dh_gpair->Size(),
d_gpair.begin());
auto sample = sampler->Sample(dh::ToSpan(d_gpair), dmat);
Copy link
Collaborator

@hcho3 hcho3 Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to make a copy here since Sample() is expected to modify the incoming gpair parameter? Mutating gpairs would be bad when we fit multiple trees in a single round, and this fix would make sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modify the incoming gpair parameter?

Yes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks guys!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix confirmed. Thanks.

page = sample.page;
gpair = sample.gpair;

Expand Down
5 changes: 4 additions & 1 deletion tests/cpp/tree/test_gpu_hist.cu
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,15 @@ TEST(GpuHist, ExternalMemoryWithSampling) {
auto gpair = GenerateRandomGradients(kRows);

// Build a tree using the in-memory DMatrix.
auto rng = common::GlobalRandom();

RegTree tree;
HostDeviceVector<bst_float> preds(kRows, 0.0, 0);
UpdateTree(&gpair, dmat.get(), 0, &tree, &preds, kSubsample, kSamplingMethod,
kRows);

// Build another tree using multiple ELLPACK pages.
common::GlobalRandom() = rng;
RegTree tree_ext;
HostDeviceVector<bst_float> preds_ext(kRows, 0.0, 0);
UpdateTree(&gpair, dmat_ext.get(), kPageSize, &tree_ext, &preds_ext,
Expand All @@ -518,7 +521,7 @@ TEST(GpuHist, ExternalMemoryWithSampling) {
auto preds_h = preds.ConstHostVector();
auto preds_ext_h = preds_ext.ConstHostVector();
for (int i = 0; i < kRows; i++) {
EXPECT_NEAR(preds_h[i], preds_ext_h[i], 2e-3);
EXPECT_NEAR(preds_h[i], preds_ext_h[i], 1e-3);
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/python-gpu/test_gpu_with_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ def test_gpu_binary_classification():


def test_boost_from_prediction_gpu_hist():
cpu_test = twskl.run_boost_from_prediction('gpu_hist')
twskl.run_boost_from_prediction('gpu_hist')


def test_num_parallel_tree():
twskl.run_boston_housing_rf_regression("gpu_hist")
15 changes: 9 additions & 6 deletions tests/python/test_with_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,26 @@ def test_boston_housing_regression():
assert mean_squared_error(preds4, labels) < 350


def test_boston_housing_rf_regression():
def run_boston_housing_rf_regression(tree_method):
from sklearn.metrics import mean_squared_error
from sklearn.datasets import load_boston
from sklearn.model_selection import KFold

boston = load_boston()
y = boston['target']
X = boston['data']
X, y = load_boston(return_X_y=True)
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
for train_index, test_index in kf.split(X, y):
xgb_model = xgb.XGBRFRegressor(random_state=42).fit(
X[train_index], y[train_index])
xgb_model = xgb.XGBRFRegressor(random_state=42, tree_method=tree_method).fit(
X[train_index], y[train_index]
)
preds = xgb_model.predict(X[test_index])
labels = y[test_index]
assert mean_squared_error(preds, labels) < 35


def test_boston_housing_rf_regression():
run_boston_housing_rf_regression("hist")


def test_parameter_tuning():
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_boston
Expand Down