Debian Codegraph @

src/include/xgboost/gradient.h

raw symbols SCIP
1 /**
2 * Copyright 2025, XGBoost Contributors
3 */
4 #pragma once
5
6 #include <xgboost/base.h> // for GradientPair
7 #include <xgboost/linalg.h> // for Matrix
8 #include <xgboost/logging.h>
9
10 #include <cstddef> // for size_t
11
12 namespace xgboost {
13 /**
14 * @brief Container for gradient produced by objective.
15 */
16 struct GradientContainer {
17 /** @brief Gradient used for multi-target tree split and linear model. */
18 linalg::Matrix<GradientPair> gpair;
19 /** @brief Gradient used for tree leaf value, optional. */
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 } // namespace xgboost