/** * Copyright 2025, XGBoost Contributors */ #pragma once #include // for GradientPair #include // for Matrix #include #include // for size_t namespace xgboost { /** * @brief Container for gradient produced by objective. */ struct GradientContainer { /** @brief Gradient used for multi-target tree split and linear model. */ linalg::Matrix gpair; /** @brief Gradient used for tree leaf value, optional. */ linalg::Matrix value_gpair; [[nodiscard]] bool HasValueGrad() const noexcept { return !value_gpair.Empty(); } [[nodiscard]] std::size_t NumSplitTargets() const noexcept { return gpair.Shape(1); } [[nodiscard]] std::size_t NumTargets() const noexcept { return HasValueGrad() ? value_gpair.Shape(1) : this->gpair.Shape(1); } linalg::MatrixView ValueGrad(Context const* ctx) const { if (HasValueGrad()) { return this->value_gpair.View(ctx->Device()); } return this->gpair.View(ctx->Device()); } [[nodiscard]] linalg::Matrix const* Grad() const { return &gpair; } [[nodiscard]] linalg::Matrix* Grad() { return &gpair; } [[nodiscard]] linalg::Matrix const* FullGradOnly() const { if (this->HasValueGrad()) { LOG(FATAL) << "Reduced gradient is not yet supported."; } return this->Grad(); } [[nodiscard]] linalg::Matrix* FullGradOnly() { if (this->HasValueGrad()) { LOG(FATAL) << "Reduced gradient is not yet supported."; } return this->Grad(); } }; } // namespace xgboost