Skip to content
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
62 changes: 62 additions & 0 deletions include/treelite/optional.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*!
* Copyright (c) 2021 by Contributors
* \file optional.h
* \brief Backport of std::optional from C++17
* \author Hyunsu Cho
*/

#ifndef TREELITE_OPTIONAL_H_
#define TREELITE_OPTIONAL_H_

namespace treelite {

template <typename T>
class optional { // C++17: Switch to std::optional
public:
optional() : empty_{}, has_value_{false} {}

explicit optional(const T& input_value) : value_{input_value}, has_value_{true} {}
optional(optional<T>&& other) : has_value_{other} {
if (other) {
value_ = *other;
}
}

~optional() {
if (has_value_) {
value_.~T();
} else {
empty_.~empty_byte();
}
}

explicit operator bool() const {
return has_value_;
}
T& operator*() {
return value_;
}
const T& operator*() const {
return value_;
}
T* operator->() {
return &value_;
}
optional& operator=(const T& new_value) {
value_ = new_value;
has_value_ = true;
return *this;
}

private:
struct empty_byte {};
union {
empty_byte empty_;
T value_;
};
bool has_value_;
};

} // namespace treelite

#endif // TREELITE_OPTIONAL_H_
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ target_sources(objtreelite
${PROJECT_SOURCE_DIR}/include/treelite/frontend_impl.h
${PROJECT_SOURCE_DIR}/include/treelite/gtil.h
${PROJECT_SOURCE_DIR}/include/treelite/omp.h
${PROJECT_SOURCE_DIR}/include/treelite/optional.h
${PROJECT_SOURCE_DIR}/include/treelite/thread_local.h
${PROJECT_SOURCE_DIR}/include/treelite/tree.h
${PROJECT_SOURCE_DIR}/include/treelite/tree_impl.h
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef TREELITE_COMPILER_AST_AST_H_
#define TREELITE_COMPILER_AST_AST_H_

#include <dmlc/optional.h>
#include <treelite/optional.h>
#include <treelite/base.h>
#include <fmt/format.h>
#include <limits>
Expand All @@ -25,8 +25,8 @@ class ASTNode {
std::vector<ASTNode*> children;
int node_id;
int tree_id;
dmlc::optional<uint64_t> data_count;
dmlc::optional<double> sum_hess;
optional<uint64_t> data_count;
optional<double> sum_hess;
virtual std::string GetDump() const = 0;
virtual ~ASTNode() = 0; // force ASTNode to be abstract class
protected:
Expand Down Expand Up @@ -107,12 +107,12 @@ class ConditionNode : public ASTNode {
: split_index(split_index), default_left(default_left) {}
unsigned split_index;
bool default_left;
dmlc::optional<double> gain;
optional<double> gain;

std::string GetDump() const override {
if (gain) {
return fmt::format("ConditionNode {{ split_index: {}, default_left: {}, gain: {} }}",
split_index, default_left, gain.value());
split_index, default_left, *gain);
} else {
return fmt::format("ConditionNode {{ split_index: {}, default_left: {} }}",
split_index, default_left);
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/ast/fold_code.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ bool fold_code(ASTNode* node, CodeFoldingContext* context,
ASTBuilder<ThresholdType, LeafOutputType>* builder) {
if (node->node_id == 0) {
if (node->data_count) {
context->log_root_data_count = std::log(node->data_count.value());
context->log_root_data_count = std::log(*node->data_count);
} else {
context->log_root_data_count = std::numeric_limits<double>::quiet_NaN();
}
if (node->sum_hess) {
context->log_root_sum_hess = std::log(node->sum_hess.value());
context->log_root_sum_hess = std::log(*node->sum_hess);
} else {
context->log_root_sum_hess = std::numeric_limits<double>::quiet_NaN();
}
}

if ( (node->data_count && !std::isnan(context->log_root_data_count)
&& context->log_root_data_count - std::log(node->data_count.value())
&& context->log_root_data_count - std::log(*node->data_count)
>= context->magnitude_req)
|| (node->sum_hess && !std::isnan(context->log_root_sum_hess)
&& context->log_root_sum_hess - std::log(node->sum_hess.value())
&& context->log_root_sum_hess - std::log(*node->sum_hess)
>= context->magnitude_req) ) {
// fold the subtree whose root is [node]
ASTNode* parent_node = node->parent;
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/ast_native.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ class ASTNativeCompiler : public Compiler {
condition_with_na_check = ExtractCategoricalCondition(t2);
}
if (node->children[0]->data_count && node->children[1]->data_count) {
const uint64_t left_freq = node->children[0]->data_count.value();
const uint64_t right_freq = node->children[1]->data_count.value();
const uint64_t left_freq = *node->children[0]->data_count;
const uint64_t right_freq = *node->children[1]->data_count;
condition_with_na_check
= fmt::format(" {keyword}( {condition} ) ",
"keyword"_a = ((left_freq > right_freq) ? "LIKELY" : "UNLIKELY"),
Expand Down
11 changes: 8 additions & 3 deletions src/predictor/predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#include <treelite/typeinfo.h>
#include <dmlc/logging.h>
#include <dmlc/io.h>
#include <dmlc/timer.h>
#include <cstdint>
#include <algorithm>
#include <memory>
#include <fstream>
#include <limits>
#include <functional>
#include <type_traits>
#include <chrono>
#include "thread_pool/thread_pool.h"

#ifdef _WIN32
Expand All @@ -29,6 +29,11 @@

namespace {

inline double GetTime(void) {
return std::chrono::duration<double>(
std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}

struct InputToken {
const treelite::DMatrix* dmat; // input data
bool pred_margin; // whether to store raw margin or transformed scores
Expand Down Expand Up @@ -411,7 +416,7 @@ class DeallocateOutputVector {
size_t
Predictor::PredictBatch(
const DMatrix* dmat, int verbose, bool pred_margin, PredictorOutputHandle out_result) const {
const double tstart = dmlc::GetTime();
const double tstart = GetTime();

const size_t num_row = dmat->GetNumRow();
auto* pool = static_cast<PredThreadPool*>(thread_pool_handle_);
Expand Down Expand Up @@ -449,7 +454,7 @@ Predictor::PredictBatch(
DispatchWithTypeInfo<ShrinkResultToFit>(
leaf_output_type_, num_row, query_size_per_instance, num_class_, out_result);
}
const double tend = dmlc::GetTime();
const double tend = GetTime();
if (verbose > 0) {
LOG(INFO) << "Treelite: Finished prediction in " << tend - tstart << " sec";
}
Expand Down
6 changes: 6 additions & 0 deletions src/predictor/thread_pool/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
#else
#include <sched.h>
#endif

#if defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>
#include <mach/mach_init.h>
#include <mach/thread_policy.h>
#endif
Comment on lines +20 to +24

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is needed to avoid build failure on MacOS.

#include "spsc_queue.h"

namespace treelite {
Expand Down