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

Rename and extract Context. #8528

Merged
merged 5 commits into from
Dec 6, 2022
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
1 change: 1 addition & 0 deletions R-package/src/Makevars.in
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ OBJECTS= \
$(PKGROOT)/src/linear/updater_coordinate.o \
$(PKGROOT)/src/linear/updater_shotgun.o \
$(PKGROOT)/src/learner.o \
$(PKGROOT)/src/context.o \
$(PKGROOT)/src/logging.o \
$(PKGROOT)/src/global_config.o \
$(PKGROOT)/src/collective/communicator.o \
Expand Down
1 change: 1 addition & 0 deletions R-package/src/Makevars.win
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ OBJECTS= \
$(PKGROOT)/src/linear/updater_coordinate.o \
$(PKGROOT)/src/linear/updater_shotgun.o \
$(PKGROOT)/src/learner.o \
$(PKGROOT)/src/context.o \
$(PKGROOT)/src/logging.o \
$(PKGROOT)/src/global_config.o \
$(PKGROOT)/src/collective/communicator.o \
Expand Down
9 changes: 5 additions & 4 deletions R-package/src/xgboost_R.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <dmlc/common.h>
#include <dmlc/omp.h>
#include <xgboost/c_api.h>
#include <xgboost/context.h>
#include <xgboost/data.h>
#include <xgboost/generic_parameters.h>
#include <xgboost/logging.h>

#include <cstdio>
Expand All @@ -18,7 +18,8 @@
#include "../../src/c_api/c_api_error.h"
#include "../../src/common/threading_utils.h"

#include "./xgboost_R.h"

#include "./xgboost_R.h" // Must follow other include.

/*!
* \brief macro to annotate begin of api
Expand Down Expand Up @@ -46,14 +47,14 @@

using dmlc::BeginPtr;

xgboost::GenericParameter const *BoosterCtx(BoosterHandle handle) {
xgboost::Context const *BoosterCtx(BoosterHandle handle) {
CHECK_HANDLE();
auto *learner = static_cast<xgboost::Learner *>(handle);
CHECK(learner);
return learner->Ctx();
}

xgboost::GenericParameter const *DMatrixCtx(DMatrixHandle handle) {
xgboost::Context const *DMatrixCtx(DMatrixHandle handle) {
CHECK_HANDLE();
auto p_m = static_cast<std::shared_ptr<xgboost::DMatrix> *>(handle);
CHECK(p_m);
Expand Down
51 changes: 24 additions & 27 deletions include/xgboost/generic_parameters.h → include/xgboost/context.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*!
* Copyright 2014-2019 by Contributors
* \file generic_parameters.h
* Copyright 2014-2022 by Contributors
* \file context.h
*/
#ifndef XGBOOST_GENERIC_PARAMETERS_H_
#define XGBOOST_GENERIC_PARAMETERS_H_
#ifndef XGBOOST_CONTEXT_H_
#define XGBOOST_CONTEXT_H_

#include <xgboost/logging.h>
#include <xgboost/parameter.h>
Expand All @@ -12,31 +12,31 @@

namespace xgboost {

struct GenericParameter : public XGBoostParameter<GenericParameter> {
struct Context : public XGBoostParameter<Context> {
private:
// cached value for CFS CPU limit. (used in containerized env)
int32_t cfs_cpu_count_; // NOLINT
std::int32_t cfs_cpu_count_; // NOLINT

public:
// Constant representing the device ID of CPU.
static int32_t constexpr kCpuId = -1;
static int64_t constexpr kDefaultSeed = 0;
static std::int32_t constexpr kCpuId = -1;
static std::int64_t constexpr kDefaultSeed = 0;

public:
GenericParameter();
Context();

// stored random seed
int64_t seed { kDefaultSeed };
std::int64_t seed{kDefaultSeed};
// whether seed the PRNG each iteration
bool seed_per_iteration{false};
// number of threads to use if OpenMP is enabled
// if equals 0, use system default
int nthread{0};
std::int32_t nthread{0};
// primary device, -1 means no gpu.
int gpu_id{kCpuId};
std::int32_t gpu_id{kCpuId};
// fail when gpu_id is invalid
bool fail_on_invalid_gpu_id {false};
bool validate_parameters {false};
bool fail_on_invalid_gpu_id{false};
bool validate_parameters{false};

/*!
* \brief Configure the parameter `gpu_id'.
Expand All @@ -47,26 +47,25 @@ struct GenericParameter : public XGBoostParameter<GenericParameter> {
/*!
* Return automatically chosen threads.
*/
int32_t Threads() const;
std::int32_t Threads() const;

bool IsCPU() const { return gpu_id == kCpuId; }
bool IsCUDA() const { return !IsCPU(); }

// declare parameters
DMLC_DECLARE_PARAMETER(GenericParameter) {
DMLC_DECLARE_FIELD(seed).set_default(kDefaultSeed).describe(
"Random number seed during training.");
DMLC_DECLARE_PARAMETER(Context) {
DMLC_DECLARE_FIELD(seed)
.set_default(kDefaultSeed)
.describe("Random number seed during training.");
DMLC_DECLARE_ALIAS(seed, random_state);
DMLC_DECLARE_FIELD(seed_per_iteration)
.set_default(false)
.describe("Seed PRNG determnisticly via iterator number.");
DMLC_DECLARE_FIELD(nthread).set_default(0).describe(
"Number of threads to use.");
DMLC_DECLARE_FIELD(nthread).set_default(0).describe("Number of threads to use.");
DMLC_DECLARE_ALIAS(nthread, n_jobs);

DMLC_DECLARE_FIELD(gpu_id)
.set_default(-1)
.set_lower_bound(-1)
.describe("The primary GPU device ordinal.");
DMLC_DECLARE_FIELD(gpu_id).set_default(-1).set_lower_bound(-1).describe(
"The primary GPU device ordinal.");
DMLC_DECLARE_FIELD(fail_on_invalid_gpu_id)
.set_default(false)
.describe("Fail with error when gpu_id is invalid.");
Expand All @@ -75,8 +74,6 @@ struct GenericParameter : public XGBoostParameter<GenericParameter> {
.describe("Enable checking whether parameters are used or not.");
}
};

using Context = GenericParameter;
} // namespace xgboost

#endif // XGBOOST_GENERIC_PARAMETERS_H_
#endif // XGBOOST_CONTEXT_H_
2 changes: 1 addition & 1 deletion include/xgboost/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <dmlc/data.h>
#include <dmlc/serializer.h>
#include <xgboost/base.h>
#include <xgboost/generic_parameters.h>
#include <xgboost/host_device_vector.h>
#include <xgboost/linalg.h>
#include <xgboost/span.h>
Expand All @@ -28,6 +27,7 @@
namespace xgboost {
// forward declare dmatrix.
class DMatrix;
struct Context;

/*! \brief data type accepted by xgboost interface */
enum class DataType : uint8_t {
Expand Down
14 changes: 6 additions & 8 deletions include/xgboost/gbm.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Json;
class FeatureMap;
class ObjFunction;

struct GenericParameter;
struct Context;
struct LearnerModelParam;
struct PredictionCacheEntry;
class PredictionContainer;
Expand All @@ -38,8 +38,8 @@ class PredictionContainer;
*/
class GradientBooster : public Model, public Configurable {
protected:
GenericParameter const* ctx_;
explicit GradientBooster(GenericParameter const* ctx) : ctx_{ctx} {}
Context const* ctx_;
explicit GradientBooster(Context const* ctx) : ctx_{ctx} {}

public:
/*! \brief virtual destructor */
Expand Down Expand Up @@ -193,10 +193,8 @@ class GradientBooster : public Model, public Configurable {
* \param learner_model_param pointer to global model parameters
* \return The created booster.
*/
static GradientBooster* Create(
const std::string& name,
GenericParameter const* generic_param,
LearnerModelParam const* learner_model_param);
static GradientBooster* Create(const std::string& name, Context const* ctx,
LearnerModelParam const* learner_model_param);
};

/*!
Expand All @@ -206,7 +204,7 @@ struct GradientBoosterReg
: public dmlc::FunctionRegEntryBase<
GradientBoosterReg,
std::function<GradientBooster*(LearnerModelParam const* learner_model_param,
GenericParameter const* ctx)> > {};
Context const* ctx)> > {};

/*!
* \brief Macro to register gradient booster.
Expand Down
2 changes: 1 addition & 1 deletion include/xgboost/learner.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#define XGBOOST_LEARNER_H_

#include <xgboost/base.h>
#include <xgboost/context.h> // Context
#include <xgboost/feature_map.h>
#include <xgboost/generic_parameters.h> // Context
#include <xgboost/host_device_vector.h>
#include <xgboost/model.h>
#include <xgboost/predictor.h>
Expand Down
2 changes: 1 addition & 1 deletion include/xgboost/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <dmlc/endian.h>
#include <xgboost/base.h>
#include <xgboost/generic_parameters.h>
#include <xgboost/context.h> // fixme(jiamingy): Remove the dependency on this header.
#include <xgboost/host_device_vector.h>
#include <xgboost/json.h>
#include <xgboost/span.h>
Expand Down
6 changes: 3 additions & 3 deletions include/xgboost/linear_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <dmlc/registry.h>
#include <xgboost/base.h>
#include <xgboost/data.h>
#include <xgboost/generic_parameters.h>
#include <xgboost/host_device_vector.h>
#include <xgboost/model.h>

Expand All @@ -19,6 +18,7 @@
namespace xgboost {

class Json;
struct Context;

namespace gbm {
class GBLinearModel;
Expand All @@ -29,7 +29,7 @@ class GBLinearModel;
*/
class LinearUpdater : public Configurable {
protected:
GenericParameter const* ctx_;
Context const* ctx_;

public:
/*! \brief virtual destructor */
Expand Down Expand Up @@ -57,7 +57,7 @@ class LinearUpdater : public Configurable {
* \brief Create a linear updater given name
* \param name Name of the linear updater.
*/
static LinearUpdater* Create(const std::string& name, GenericParameter const*);
static LinearUpdater* Create(const std::string& name, Context const*);
};

/*!
Expand Down
9 changes: 5 additions & 4 deletions include/xgboost/metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <dmlc/registry.h>
#include <xgboost/model.h>
#include <xgboost/generic_parameters.h>
#include <xgboost/data.h>
#include <xgboost/base.h>
#include <xgboost/host_device_vector.h>
Expand All @@ -20,13 +19,15 @@
#include <utility>

namespace xgboost {
struct Context;

/*!
* \brief interface of evaluation metric used to evaluate model performance.
* This has nothing to do with training, but merely act as evaluation purpose.
*/
class Metric : public Configurable {
protected:
GenericParameter const* tparam_;
Context const* tparam_;

public:
/*!
Expand Down Expand Up @@ -68,10 +69,10 @@ class Metric : public Configurable {
* \param name name of the metric.
* name can be in form metric[@]param and the name will be matched in the
* registry.
* \param tparam A global generic parameter
* \param ctx A global context
* \return the created metric.
*/
static Metric* Create(const std::string& name, GenericParameter const* tparam);
static Metric* Create(const std::string& name, Context const* ctx);
};

/*!
Expand Down
14 changes: 7 additions & 7 deletions include/xgboost/objective.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
#include <dmlc/registry.h>
#include <xgboost/base.h>
#include <xgboost/data.h>
#include <xgboost/model.h>
#include <xgboost/generic_parameters.h>
#include <xgboost/host_device_vector.h>
#include <xgboost/model.h>
#include <xgboost/task.h>

#include <vector>
#include <utility>
#include <string>
#include <functional>
#include <string>
#include <utility>
#include <vector>

namespace xgboost {

class RegTree;
struct Context;

/*! \brief interface of objective function */
class ObjFunction : public Configurable {
Expand Down Expand Up @@ -120,10 +120,10 @@ class ObjFunction : public Configurable {

/*!
* \brief Create an objective function according to name.
* \param tparam Generic parameters.
* \param ctx Pointer to runtime parameters.
* \param name Name of the objective.
*/
static ObjFunction* Create(const std::string& name, GenericParameter const* tparam);
static ObjFunction* Create(const std::string& name, Context const* ctx);
};

/*!
Expand Down
Loading