Skip to content

[ML] Trap out-of-bounds read initialising boosted tree training #709

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

Merged
merged 2 commits into from
Oct 1, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/maths/CBoostedTreeImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,9 @@ CBoostedTreeImpl::gainAndCurvatureAtPercentile(double percentile,
return {0.0, 0.0};
}

std::size_t index{static_cast<std::size_t>(
percentile * static_cast<double>(gains.size()) / 100.0 + 0.5)};
std::size_t index{std::min(
static_cast<std::size_t>(percentile * static_cast<double>(gains.size()) / 100.0 + 0.5),
gains.size() - 1)};
std::nth_element(gains.begin(), gains.begin() + index, gains.end());
std::nth_element(curvatures.begin(), curvatures.begin() + index, curvatures.end());

Expand Down
46 changes: 46 additions & 0 deletions lib/maths/unittest/CBoostedTreeTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,50 @@ void CBoostedTreeTest::testIntegerRegressor() {
CPPUNIT_ASSERT(modelRSquared > 0.99);
}

void CBoostedTreeTest::testSingleSplit() {

// We were getting an out-of-bound read in initialization when running on
// data with only two distinct values for the target variable. This test
// fails intermittently without the fix.

test::CRandomNumbers rng;

std::size_t rows{100};
std::size_t cols{2};

TDoubleVec x;
rng.generateUniformSamples(0.0, 1.0, rows, x);
for (auto& xi : x) {
xi = std::floor(xi + 0.5);
}

auto frame = core::makeMainStorageDataFrame(cols).first;
frame->categoricalColumns({false, false});
for (std::size_t i = 0; i < rows; ++i) {
frame->writeRow([&](core::CDataFrame::TFloatVecItr column, std::int32_t&) {
*(column++) = x[i];
*column = 10.0 * x[i];
});
}
frame->finishWritingRows();

auto regression = maths::CBoostedTreeFactory::constructFromParameters(
1, std::make_unique<maths::boosted_tree::CMse>())
.buildFor(*frame, cols - 1);

regression->train();

double modelBias;
double modelRSquared;
std::tie(modelBias, modelRSquared) = computeEvaluationMetrics(
*frame, 0, rows, regression->columnHoldingPrediction(frame->numberColumns()),
[](const TRowRef& row) { return 10.0 * row[0]; }, 0.0);

LOG_DEBUG(<< "bias = " << modelBias);
LOG_DEBUG(<< " R^2 = " << modelRSquared);
CPPUNIT_ASSERT(modelRSquared > 0.99);
}

void CBoostedTreeTest::testTranslationInvariance() {

// We should get similar performance independent of fixed shifts for the target.
Expand Down Expand Up @@ -1070,6 +1114,8 @@ CppUnit::Test* CBoostedTreeTest::suite() {
&CBoostedTreeTest::testCategoricalRegressors));
suiteOfTests->addTest(new CppUnit::TestCaller<CBoostedTreeTest>(
"CBoostedTreeTest::testIntegerRegressor", &CBoostedTreeTest::testIntegerRegressor));
suiteOfTests->addTest(new CppUnit::TestCaller<CBoostedTreeTest>(
"CBoostedTreeTest::testSingleSplit", &CBoostedTreeTest::testSingleSplit));
suiteOfTests->addTest(new CppUnit::TestCaller<CBoostedTreeTest>(
"CBoostedTreeTest::testTranslationInvariance",
&CBoostedTreeTest::testTranslationInvariance));
Expand Down
1 change: 1 addition & 0 deletions lib/maths/unittest/CBoostedTreeTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CBoostedTreeTest : public CppUnit::TestFixture {
void testConstantTarget();
void testCategoricalRegressors();
void testIntegerRegressor();
void testSingleSplit();
void testTranslationInvariance();
void testDepthBasedRegularization();
void testEstimateMemoryUsedByTrain();
Expand Down