Remove internal use of gpu_id. (#9568)
This commit is contained in:
@@ -100,7 +100,7 @@ inline void UpdateTreeLeaf(Context const* ctx, HostDeviceVector<bst_node_t> cons
|
||||
detail::UpdateTreeLeafHost(ctx, position.ConstHostVector(), group_idx, info, learning_rate,
|
||||
predt, alpha, p_tree);
|
||||
} else {
|
||||
position.SetDevice(ctx->gpu_id);
|
||||
position.SetDevice(ctx->Device());
|
||||
detail::UpdateTreeLeafDevice(ctx, position.ConstDeviceSpan(), group_idx, info, learning_rate,
|
||||
predt, alpha, p_tree);
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ class AFTObj : public ObjFunction {
|
||||
|
||||
template <typename Distribution>
|
||||
void GetGradientImpl(const HostDeviceVector<bst_float>& preds, const MetaInfo& info,
|
||||
linalg::Matrix<GradientPair>* out_gpair, size_t ndata, int device,
|
||||
linalg::Matrix<GradientPair>* out_gpair, size_t ndata, DeviceOrd device,
|
||||
bool is_null_weight, float aft_loss_distribution_scale) {
|
||||
common::Transform<>::Init(
|
||||
[=] XGBOOST_DEVICE(size_t _idx,
|
||||
@@ -75,7 +75,7 @@ class AFTObj : public ObjFunction {
|
||||
CHECK_EQ(info.labels_upper_bound_.Size(), ndata);
|
||||
out_gpair->SetDevice(ctx_->Device());
|
||||
out_gpair->Reshape(ndata, 1);
|
||||
const int device = ctx_->gpu_id;
|
||||
const auto device = ctx_->Device();
|
||||
const float aft_loss_distribution_scale = param_.aft_loss_distribution_scale;
|
||||
const bool is_null_weight = info.weights_.Size() == 0;
|
||||
if (!is_null_weight) {
|
||||
@@ -108,7 +108,7 @@ class AFTObj : public ObjFunction {
|
||||
_preds[_idx] = exp(_preds[_idx]);
|
||||
},
|
||||
common::Range{0, static_cast<int64_t>(io_preds->Size())}, this->ctx_->Threads(),
|
||||
io_preds->DeviceIdx())
|
||||
io_preds->Device())
|
||||
.Eval(io_preds);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Copyright 2018-2022 by XGBoost Contributors
|
||||
/**
|
||||
* Copyright 2018-2023, XGBoost Contributors
|
||||
* \file hinge.cc
|
||||
* \brief Provides an implementation of the hinge loss function
|
||||
* \author Henry Gouk
|
||||
@@ -13,8 +13,7 @@
|
||||
#include "../common/transform.h"
|
||||
#include "../common/common.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace obj {
|
||||
namespace xgboost::obj {
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
DMLC_REGISTRY_FILE_TAG(hinge_obj_gpu);
|
||||
@@ -63,7 +62,7 @@ class HingeObj : public ObjFunction {
|
||||
_out_gpair[_idx] = GradientPair(g, h);
|
||||
},
|
||||
common::Range{0, static_cast<int64_t>(ndata)}, this->ctx_->Threads(),
|
||||
ctx_->gpu_id).Eval(
|
||||
ctx_->Device()).Eval(
|
||||
out_gpair->Data(), &preds, info.labels.Data(), &info.weights_);
|
||||
}
|
||||
|
||||
@@ -73,11 +72,11 @@ class HingeObj : public ObjFunction {
|
||||
_preds[_idx] = _preds[_idx] > 0.0 ? 1.0 : 0.0;
|
||||
},
|
||||
common::Range{0, static_cast<int64_t>(io_preds->Size()), 1}, this->ctx_->Threads(),
|
||||
io_preds->DeviceIdx())
|
||||
io_preds->Device())
|
||||
.Eval(io_preds);
|
||||
}
|
||||
|
||||
const char* DefaultEvalMetric() const override {
|
||||
[[nodiscard]] const char* DefaultEvalMetric() const override {
|
||||
return "error";
|
||||
}
|
||||
|
||||
@@ -93,5 +92,4 @@ XGBOOST_REGISTER_OBJECTIVE(HingeObj, "binary:hinge")
|
||||
.describe("Hinge loss. Expects labels to be in [0,1f]")
|
||||
.set_body([]() { return new HingeObj(); });
|
||||
|
||||
} // namespace obj
|
||||
} // namespace xgboost
|
||||
} // namespace xgboost::obj
|
||||
|
||||
@@ -20,8 +20,8 @@ void FitIntercept::InitEstimation(MetaInfo const& info, linalg::Vector<float>* b
|
||||
CheckInitInputs(info);
|
||||
}
|
||||
// Avoid altering any state in child objective.
|
||||
HostDeviceVector<float> dummy_predt(info.labels.Size(), 0.0f, this->ctx_->gpu_id);
|
||||
linalg::Matrix<GradientPair> gpair(info.labels.Shape(), this->ctx_->gpu_id);
|
||||
HostDeviceVector<float> dummy_predt(info.labels.Size(), 0.0f, this->ctx_->Device());
|
||||
linalg::Matrix<GradientPair> gpair(info.labels.Shape(), this->ctx_->Device());
|
||||
|
||||
Json config{Object{}};
|
||||
this->SaveConfig(&config);
|
||||
|
||||
@@ -103,10 +103,10 @@ class LambdaRankObj : public FitIntercept {
|
||||
|
||||
// Update position biased for unbiased click data
|
||||
void UpdatePositionBias() {
|
||||
li_full_.SetDevice(ctx_->gpu_id);
|
||||
lj_full_.SetDevice(ctx_->gpu_id);
|
||||
li_.SetDevice(ctx_->gpu_id);
|
||||
lj_.SetDevice(ctx_->gpu_id);
|
||||
li_full_.SetDevice(ctx_->Device());
|
||||
lj_full_.SetDevice(ctx_->Device());
|
||||
li_.SetDevice(ctx_->Device());
|
||||
lj_.SetDevice(ctx_->Device());
|
||||
|
||||
if (ctx_->IsCPU()) {
|
||||
cpu_impl::LambdaRankUpdatePositionBias(ctx_, li_full_.View(ctx_->Device()),
|
||||
|
||||
@@ -290,12 +290,12 @@ void Launch(Context const* ctx, std::int32_t iter, HostDeviceVector<float> const
|
||||
linalg::VectorView<double> li, linalg::VectorView<double> lj,
|
||||
linalg::Matrix<GradientPair>* out_gpair) {
|
||||
// boilerplate
|
||||
std::int32_t device_id = ctx->gpu_id;
|
||||
dh::safe_cuda(cudaSetDevice(device_id));
|
||||
auto device = ctx->Device();
|
||||
dh::safe_cuda(cudaSetDevice(device.ordinal));
|
||||
auto n_groups = p_cache->Groups();
|
||||
|
||||
info.labels.SetDevice(device_id);
|
||||
preds.SetDevice(device_id);
|
||||
info.labels.SetDevice(device);
|
||||
preds.SetDevice(device);
|
||||
out_gpair->SetDevice(ctx->Device());
|
||||
out_gpair->Reshape(preds.Size(), 1);
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ class SoftmaxMultiClassObj : public ObjFunction {
|
||||
const int nclass = param_.num_class;
|
||||
const auto ndata = static_cast<int64_t>(preds.Size() / nclass);
|
||||
|
||||
auto device = ctx_->gpu_id;
|
||||
auto device = ctx_->Device();
|
||||
out_gpair->SetDevice(device);
|
||||
info.labels.SetDevice(device);
|
||||
info.weights_.SetDevice(device);
|
||||
@@ -133,7 +133,7 @@ class SoftmaxMultiClassObj : public ObjFunction {
|
||||
const int nclass = param_.num_class;
|
||||
const auto ndata = static_cast<int64_t>(io_preds->Size() / nclass);
|
||||
|
||||
auto device = io_preds->DeviceIdx();
|
||||
auto device = io_preds->Device();
|
||||
if (prob) {
|
||||
common::Transform<>::Init(
|
||||
[=] XGBOOST_DEVICE(size_t _idx, common::Span<bst_float> _preds) {
|
||||
|
||||
@@ -70,15 +70,15 @@ class QuantileRegression : public ObjFunction {
|
||||
out_gpair->Reshape(info.num_row_, n_targets);
|
||||
auto gpair = out_gpair->View(ctx_->Device());
|
||||
|
||||
info.weights_.SetDevice(ctx_->gpu_id);
|
||||
info.weights_.SetDevice(ctx_->Device());
|
||||
common::OptionalWeights weight{ctx_->IsCPU() ? info.weights_.ConstHostSpan()
|
||||
: info.weights_.ConstDeviceSpan()};
|
||||
|
||||
preds.SetDevice(ctx_->gpu_id);
|
||||
preds.SetDevice(ctx_->Device());
|
||||
auto predt = linalg::MakeVec(&preds);
|
||||
auto n_samples = info.num_row_;
|
||||
|
||||
alpha_.SetDevice(ctx_->gpu_id);
|
||||
alpha_.SetDevice(ctx_->Device());
|
||||
auto alpha = ctx_->IsCPU() ? alpha_.ConstHostSpan() : alpha_.ConstDeviceSpan();
|
||||
|
||||
linalg::ElementWiseKernel(
|
||||
@@ -103,7 +103,7 @@ class QuantileRegression : public ObjFunction {
|
||||
CHECK(!alpha_.Empty());
|
||||
|
||||
auto n_targets = this->Targets(info);
|
||||
base_score->SetDevice(ctx_->gpu_id);
|
||||
base_score->SetDevice(ctx_->Device());
|
||||
base_score->Reshape(n_targets);
|
||||
|
||||
double sw{0};
|
||||
@@ -129,7 +129,7 @@ class QuantileRegression : public ObjFunction {
|
||||
}
|
||||
} else {
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
alpha_.SetDevice(ctx_->gpu_id);
|
||||
alpha_.SetDevice(ctx_->Device());
|
||||
auto d_alpha = alpha_.ConstDeviceSpan();
|
||||
auto d_labels = info.labels.View(ctx_->Device());
|
||||
auto seg_it = dh::MakeTransformIterator<std::size_t>(
|
||||
@@ -148,7 +148,7 @@ class QuantileRegression : public ObjFunction {
|
||||
val_it + n, base_score->Data());
|
||||
sw = info.num_row_;
|
||||
} else {
|
||||
info.weights_.SetDevice(ctx_->gpu_id);
|
||||
info.weights_.SetDevice(ctx_->Device());
|
||||
auto d_weights = info.weights_.ConstDeviceSpan();
|
||||
auto weight_it = dh::MakeTransformIterator<float>(thrust::make_counting_iterator(0ul),
|
||||
[=] XGBOOST_DEVICE(std::size_t i) {
|
||||
|
||||
@@ -116,7 +116,7 @@ class RegLossObj : public FitIntercept {
|
||||
|
||||
size_t const ndata = preds.Size();
|
||||
out_gpair->SetDevice(ctx_->Device());
|
||||
auto device = ctx_->gpu_id;
|
||||
auto device = ctx_->Device();
|
||||
|
||||
bool is_null_weight = info.weights_.Size() == 0;
|
||||
auto scale_pos_weight = param_.scale_pos_weight;
|
||||
@@ -124,7 +124,7 @@ class RegLossObj : public FitIntercept {
|
||||
additional_input_.HostVector().begin()[1] = is_null_weight;
|
||||
|
||||
const size_t nthreads = ctx_->Threads();
|
||||
bool on_device = device >= 0;
|
||||
bool on_device = device.IsCUDA();
|
||||
// On CPU we run the transformation each thread processing a contigious block of data
|
||||
// for better performance.
|
||||
const size_t n_data_blocks = std::max(static_cast<size_t>(1), (on_device ? ndata : nthreads));
|
||||
@@ -175,7 +175,7 @@ class RegLossObj : public FitIntercept {
|
||||
_preds[_idx] = Loss::PredTransform(_preds[_idx]);
|
||||
},
|
||||
common::Range{0, static_cast<int64_t>(io_preds->Size())}, this->ctx_->Threads(),
|
||||
io_preds->DeviceIdx())
|
||||
io_preds->Device())
|
||||
.Eval(io_preds);
|
||||
}
|
||||
|
||||
@@ -246,14 +246,14 @@ class PseudoHuberRegression : public FitIntercept {
|
||||
CHECK_NE(slope, 0.0) << "slope for pseudo huber cannot be 0.";
|
||||
auto labels = info.labels.View(ctx_->Device());
|
||||
|
||||
out_gpair->SetDevice(ctx_->gpu_id);
|
||||
out_gpair->SetDevice(ctx_->Device());
|
||||
out_gpair->Reshape(info.num_row_, this->Targets(info));
|
||||
auto gpair = out_gpair->View(ctx_->Device());
|
||||
|
||||
preds.SetDevice(ctx_->gpu_id);
|
||||
preds.SetDevice(ctx_->Device());
|
||||
auto predt = linalg::MakeVec(&preds);
|
||||
|
||||
info.weights_.SetDevice(ctx_->gpu_id);
|
||||
info.weights_.SetDevice(ctx_->Device());
|
||||
common::OptionalWeights weight{ctx_->IsCPU() ? info.weights_.ConstHostSpan()
|
||||
: info.weights_.ConstDeviceSpan()};
|
||||
|
||||
@@ -327,7 +327,7 @@ class PoissonRegression : public FitIntercept {
|
||||
size_t const ndata = preds.Size();
|
||||
out_gpair->SetDevice(ctx_->Device());
|
||||
out_gpair->Reshape(info.num_row_, this->Targets(info));
|
||||
auto device = ctx_->gpu_id;
|
||||
auto device = ctx_->Device();
|
||||
label_correct_.Resize(1);
|
||||
label_correct_.Fill(1);
|
||||
|
||||
@@ -369,7 +369,7 @@ class PoissonRegression : public FitIntercept {
|
||||
_preds[_idx] = expf(_preds[_idx]);
|
||||
},
|
||||
common::Range{0, static_cast<int64_t>(io_preds->Size())}, this->ctx_->Threads(),
|
||||
io_preds->DeviceIdx())
|
||||
io_preds->Device())
|
||||
.Eval(io_preds);
|
||||
}
|
||||
void EvalTransform(HostDeviceVector<bst_float> *io_preds) override {
|
||||
@@ -512,7 +512,7 @@ class GammaRegression : public FitIntercept {
|
||||
CHECK_NE(info.labels.Size(), 0U) << "label set cannot be empty";
|
||||
CHECK_EQ(preds.Size(), info.labels.Size()) << "labels are not correctly provided";
|
||||
const size_t ndata = preds.Size();
|
||||
auto device = ctx_->gpu_id;
|
||||
auto device = ctx_->Device();
|
||||
out_gpair->SetDevice(ctx_->Device());
|
||||
out_gpair->Reshape(info.num_row_, this->Targets(info));
|
||||
label_correct_.Resize(1);
|
||||
@@ -555,7 +555,7 @@ class GammaRegression : public FitIntercept {
|
||||
_preds[_idx] = expf(_preds[_idx]);
|
||||
},
|
||||
common::Range{0, static_cast<int64_t>(io_preds->Size())}, this->ctx_->Threads(),
|
||||
io_preds->DeviceIdx())
|
||||
io_preds->Device())
|
||||
.Eval(io_preds);
|
||||
}
|
||||
void EvalTransform(HostDeviceVector<bst_float> *io_preds) override {
|
||||
@@ -613,7 +613,7 @@ class TweedieRegression : public FitIntercept {
|
||||
out_gpair->SetDevice(ctx_->Device());
|
||||
out_gpair->Reshape(info.num_row_, this->Targets(info));
|
||||
|
||||
auto device = ctx_->gpu_id;
|
||||
auto device = ctx_->Device();
|
||||
label_correct_.Resize(1);
|
||||
label_correct_.Fill(1);
|
||||
|
||||
@@ -660,7 +660,7 @@ class TweedieRegression : public FitIntercept {
|
||||
_preds[_idx] = expf(_preds[_idx]);
|
||||
},
|
||||
common::Range{0, static_cast<int64_t>(io_preds->Size())}, this->ctx_->Threads(),
|
||||
io_preds->DeviceIdx())
|
||||
io_preds->Device())
|
||||
.Eval(io_preds);
|
||||
}
|
||||
|
||||
@@ -711,9 +711,9 @@ class MeanAbsoluteError : public ObjFunction {
|
||||
out_gpair->Reshape(info.num_row_, this->Targets(info));
|
||||
auto gpair = out_gpair->View(ctx_->Device());
|
||||
|
||||
preds.SetDevice(ctx_->gpu_id);
|
||||
preds.SetDevice(ctx_->Device());
|
||||
auto predt = linalg::MakeVec(&preds);
|
||||
info.weights_.SetDevice(ctx_->gpu_id);
|
||||
info.weights_.SetDevice(ctx_->Device());
|
||||
common::OptionalWeights weight{ctx_->IsCPU() ? info.weights_.ConstHostSpan()
|
||||
: info.weights_.ConstDeviceSpan()};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user