| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <xgboost/base.h> |
| 7 |
#include <xgboost/linalg.h> |
| 8 |
#include <xgboost/logging.h> |
| 9 |
|
| 10 |
#include <cstddef> |
| 11 |
|
| 12 |
namespace xgboost { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
struct GradientContainer { |
| 17 |
|
| 18 |
linalg::Matrix<GradientPair> gpair; |
| 19 |
|
| 20 |
linalg::Matrix<GradientPair> value_gpair; |
| 21 |
|
| 22 |
[[nodiscard]] bool HasValueGrad() const noexcept { return !value_gpair.Empty(); } |
| 23 |
|
| 24 |
[[nodiscard]] std::size_t NumSplitTargets() const noexcept { return gpair.Shape(1); } |
| 25 |
[[nodiscard]] std::size_t NumTargets() const noexcept { |
| 26 |
return HasValueGrad() ? value_gpair.Shape(1) : this->gpair.Shape(1); |
| 27 |
} |
| 28 |
|
| 29 |
linalg::MatrixView<GradientPair const> ValueGrad(Context const* ctx) const { |
| 30 |
if (HasValueGrad()) { |
| 31 |
return this->value_gpair.View(ctx->Device()); |
| 32 |
} |
| 33 |
return this->gpair.View(ctx->Device()); |
| 34 |
} |
| 35 |
|
| 36 |
[[nodiscard]] linalg::Matrix<GradientPair> const* Grad() const { return &gpair; } |
| 37 |
[[nodiscard]] linalg::Matrix<GradientPair>* Grad() { return &gpair; } |
| 38 |
|
| 39 |
[[nodiscard]] linalg::Matrix<GradientPair> const* FullGradOnly() const { |
| 40 |
if (this->HasValueGrad()) { |
| 41 |
LOG(FATAL) << "Reduced gradient is not yet supported."; |
| 42 |
} |
| 43 |
return this->Grad(); |
| 44 |
} |
| 45 |
[[nodiscard]] linalg::Matrix<GradientPair>* FullGradOnly() { |
| 46 |
if (this->HasValueGrad()) { |
| 47 |
LOG(FATAL) << "Reduced gradient is not yet supported."; |
| 48 |
} |
| 49 |
return this->Grad(); |
| 50 |
} |
| 51 |
}; |
| 52 |
} |