Skip to content

[ML] Eagerly discard node statistics for leaves which we will never split #1125

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 6 commits into from
Apr 15, 2020
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
2 changes: 2 additions & 0 deletions docs/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
* Improve robustness of anomaly detection to bad input data. (See {ml-pull}1114[#1114].)
* Adds new `num_matches` and `preferred_to_categories` fields to category output.
(See {ml-pull}1062[#1062].)
* Reduce peak memory usage and memory estimates for classification and regression.
(See {ml-pull}1125[#1125].)
* Reduce variability of classification and regression results across our target operating systems.
(See {ml-pull}1127[#1127].)
* Switched data frame analytics model memory estimates from kilobytes to megabytes.
Expand Down
8 changes: 4 additions & 4 deletions lib/api/unittest/CDataFrameAnalyzerTrainingTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ BOOST_AUTO_TEST_CASE(testRunBoostedTreeRegressionTraining) {
<< "ms");

BOOST_TEST_REQUIRE(core::CProgramCounters::counter(
counter_t::E_DFTPMEstimatedPeakMemoryUsage) < 6000000);
BOOST_TEST_REQUIRE(core::CProgramCounters::counter(counter_t::E_DFTPMPeakMemoryUsage) < 1500000);
counter_t::E_DFTPMEstimatedPeakMemoryUsage) < 4500000);
BOOST_TEST_REQUIRE(core::CProgramCounters::counter(counter_t::E_DFTPMPeakMemoryUsage) < 1600000);
Copy link
Contributor Author

@tveasey tveasey Apr 4, 2020

Choose a reason for hiding this comment

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

We're not using more memory; however, we now properly account for the memory used by container for the leaf statistics because we resize it before estimating its memory usage.

BOOST_TEST_REQUIRE(
core::CProgramCounters::counter(counter_t::E_DFTPMPeakMemoryUsage) <
core::CProgramCounters::counter(counter_t::E_DFTPMEstimatedPeakMemoryUsage));
Expand Down Expand Up @@ -643,8 +643,8 @@ BOOST_AUTO_TEST_CASE(testRunBoostedTreeClassifierTraining) {
<< "ms");

BOOST_TEST_REQUIRE(core::CProgramCounters::counter(
counter_t::E_DFTPMEstimatedPeakMemoryUsage) < 6000000);
BOOST_TEST_REQUIRE(core::CProgramCounters::counter(counter_t::E_DFTPMPeakMemoryUsage) < 1500000);
counter_t::E_DFTPMEstimatedPeakMemoryUsage) < 4500000);
BOOST_TEST_REQUIRE(core::CProgramCounters::counter(counter_t::E_DFTPMPeakMemoryUsage) < 1600000);
BOOST_TEST_REQUIRE(
core::CProgramCounters::counter(counter_t::E_DFTPMPeakMemoryUsage) <
core::CProgramCounters::counter(counter_t::E_DFTPMEstimatedPeakMemoryUsage));
Expand Down
5 changes: 3 additions & 2 deletions lib/api/unittest/CDataFrameMockAnalysisRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@

#include <test/CRandomNumbers.h>

#include <functional>
#include <cinttypes>
#include <string>

class CDataFrameMockAnalysisState final : public ml::api::CDataFrameAnalysisInstrumentation {
public:
CDataFrameMockAnalysisState(const std::string& jobId)
: ml::api::CDataFrameAnalysisInstrumentation(jobId) {}
void writeAnalysisStats(std::int64_t /* timestamp */) override{};
void writeAnalysisStats(std::int64_t /* timestamp */) override {}

protected:
ml::counter_t::ECounterTypes memoryCounterType() override;
Expand Down
59 changes: 44 additions & 15 deletions lib/maths/CBoostedTreeImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

#include "maths/CBoostedTreeUtils.h"
#include <maths/CBoostedTreeImpl.h>

#include <core/CContainerPrinter.h>
Expand All @@ -20,13 +19,18 @@
#include <maths/CBoostedTree.h>
#include <maths/CBoostedTreeLeafNodeStatistics.h>
#include <maths/CBoostedTreeLoss.h>
#include <maths/CBoostedTreeUtils.h>
#include <maths/CDataFrameAnalysisInstrumentationInterface.h>
#include <maths/CDataFrameCategoryEncoder.h>
#include <maths/CQuantileSketch.h>
#include <maths/CSampling.h>
#include <maths/CSetTools.h>
#include <maths/CTreeShapFeatureImportance.h>

#include <boost/circular_buffer.hpp>

#include <algorithm>

namespace ml {
namespace maths {
using namespace boosted_tree;
Expand Down Expand Up @@ -313,10 +317,14 @@ std::size_t CBoostedTreeImpl::estimateMemoryUsage(std::size_t numberRows,
std::size_t foldRoundLossMemoryUsage{m_NumberFolds * m_NumberRounds *
sizeof(TOptionalDouble)};
std::size_t hyperparametersMemoryUsage{numberColumns * sizeof(double)};
// We only maintain statistics for leaves we know we may possibly split this
// halves the peak number of statistics we maintain.
std::size_t leafNodeStatisticsMemoryUsage{
maximumNumberLeaves * CBoostedTreeLeafNodeStatistics::estimateMemoryUsage(
numberRows, maximumNumberFeatures, m_NumberSplitsPerFeature,
m_Loss->numberParameters())};
maximumNumberLeaves *
CBoostedTreeLeafNodeStatistics::estimateMemoryUsage(
numberRows, maximumNumberFeatures, m_NumberSplitsPerFeature,
m_Loss->numberParameters()) /
2};
std::size_t dataTypeMemoryUsage{maximumNumberFeatures * sizeof(CDataFrameUtils::SDataType)};
std::size_t featureSampleProbabilities{maximumNumberFeatures * sizeof(double)};
std::size_t missingFeatureMaskMemoryUsage{
Expand Down Expand Up @@ -724,14 +732,13 @@ CBoostedTreeImpl::trainTree(core::CDataFrame& frame,
LOG_TRACE(<< "Training one tree...");

using TLeafNodeStatisticsPtr = CBoostedTreeLeafNodeStatistics::TPtr;
using TLeafNodeStatisticsPtrQueue =
std::priority_queue<TLeafNodeStatisticsPtr, std::vector<TLeafNodeStatisticsPtr>, COrderings::SLess>;
using TLeafNodeStatisticsPtrQueue = boost::circular_buffer<TLeafNodeStatisticsPtr>;

TNodeVec tree(1);
tree.reserve(2 * maximumTreeSize + 1);

TLeafNodeStatisticsPtrQueue leaves;
leaves.push(std::make_shared<CBoostedTreeLeafNodeStatistics>(
TLeafNodeStatisticsPtrQueue leaves(maximumTreeSize / 2 + 3);
leaves.push_back(std::make_shared<CBoostedTreeLeafNodeStatistics>(
0 /*root*/, m_NumberInputColumns, m_Loss->numberParameters(),
m_NumberThreads, frame, *m_Encoder, m_Regularization, candidateSplits,
this->featureBag(), 0 /*depth*/, trainingRowMask));
Expand All @@ -755,10 +762,16 @@ CBoostedTreeImpl::trainTree(core::CDataFrame& frame,

double totalGain{0.0};

COrderings::SLess less;

for (std::size_t i = 0; i < maximumTreeSize; ++i) {

auto leaf = leaves.top();
leaves.pop();
if (leaves.empty()) {
break;
}

auto leaf = leaves.back();
leaves.pop_back();

scopeMemoryUsage.remove(leaf);

Expand All @@ -767,7 +780,8 @@ CBoostedTreeImpl::trainTree(core::CDataFrame& frame,
}

totalGain += leaf->gain();
LOG_TRACE(<< "splitting " << leaf->id() << " total gain = " << totalGain);
LOG_TRACE(<< "splitting " << leaf->id() << " leaf gain = " << leaf->gain()
<< " total gain = " << totalGain);

std::size_t splitFeature;
double splitValue;
Expand All @@ -786,11 +800,26 @@ CBoostedTreeImpl::trainTree(core::CDataFrame& frame,
leftChildId, rightChildId, m_NumberThreads, frame, *m_Encoder,
m_Regularization, candidateSplits, this->featureBag(), tree[leaf->id()]);

scopeMemoryUsage.add(leftChild);
scopeMemoryUsage.add(rightChild);
if (less(rightChild, leftChild)) {
std::swap(leftChild, rightChild);
}

leaves.push(std::move(leftChild));
leaves.push(std::move(rightChild));
std::size_t n{leaves.size()};
if (leftChild->gain() >= MINIMUM_RELATIVE_GAIN_PER_SPLIT * totalGain) {
scopeMemoryUsage.add(leftChild);
leaves.push_back(std::move(leftChild));
}
if (rightChild->gain() >= MINIMUM_RELATIVE_GAIN_PER_SPLIT * totalGain) {
scopeMemoryUsage.add(rightChild);
leaves.push_back(std::move(rightChild));
}
std::inplace_merge(leaves.begin(), leaves.begin() + n, leaves.end(), less);

// Drop any leaves which can't possibly be split.
while (leaves.size() + i + 1 > maximumTreeSize) {
scopeMemoryUsage.remove(leaves.front());
leaves.pop_front();
}
}

tree.shrink_to_fit();
Expand Down