Loading...
Searching...
No Matches
RgminScg.inl
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_ML_RGMIN_SCG_INL_
6#define GPR_ML_RGMIN_SCG_INL_
7
8#include <algorithm>
9#include <cmath>
10
11#include "RgminObjective.h"
12
13namespace funcmin {
14
15template <typename ClassName, typename FuncName>
16void RgminScg::optimize(const gpr::EigenMatrix& x, const Eigen::VectorXd& x_ind,
17 const Eigen::VectorXd& y, Eigen::VectorXd& w,
18 FuncName func_to_min, ClassName& holder,
19 double current_barrier_strength,
20 const Eigen::VectorXd& lengthscale_rho0,
21 const MapObjectiveConfig* objective_config)
22{
23 failedOptim = false;
27 if (settings.common.stop_requested && settings.common.stop_requested()) {
28 failedOptim = true;
29 return;
30 }
31
32 // Without the product nlml_hessian_vector runs as finite_difference:
33 // no curvature callback, so rgmin probes the gradient itself.
34 bool hessian_vector_curvature =
35 settings.curvature_mode == gpr::ScgCurvatureMode::NLML_HESSIAN_VECTOR;
36 if (hessian_vector_curvature && !holderHasNlmlHessianVector(holder)) {
37 hessian_vector_curvature = false;
39 if (settings.common.report_level >= 1) {
40 gpr::io::LogManager log_man;
41 log_man << " nlml_hessian_vector: this objective has no exact NLML "
42 "Hessian-vector product; finite_difference curvature\n";
43 }
44 }
45
46 const MapObjectiveConfig map_cfg =
47 objective_config != nullptr
48 ? *objective_config
49 : makeMapObjectiveConfig(x, y, w, current_barrier_strength,
50 lengthscale_rho0, settings);
51
53 Ctx ctx(x, x_ind, y, holder, func_to_min, map_cfg,
54 settings.curvature_mode == gpr::ScgCurvatureMode::EXACT, false,
55 settings.common.stop_requested);
56 ctx.trace_evaluations = settings.common.report_level >= 3;
57
58 std::vector<double> wbuf(static_cast<std::size_t>(w.size()));
59 Eigen::Map<Eigen::VectorXd>(wbuf.data(), w.size()) = w;
61 const RgminTensorHandle xt(
62 rgmin_tensor_borrow_cpu_f64(wbuf.data(), wbuf.size()));
63 if (!xt) {
64 failedOptim = true;
65 return;
66 }
67
68 rgmin_control_t ctrl{};
69 // rgmin_minimize_scg still reads 0 as unlimited and stops its own
70 // plateau inside that one-shot. A positive budget is the caller's.
71 ctrl.maxiter = static_cast<size_t>(settings.common.max_iter);
72 // Rust run_scg also stops on ||g||_2 < gtol. Leave gtol inert so
73 // only ScgParams.tol_sol / tol_func apply.
74 ctrl.gtol = 0.0;
75 ctrl.istep = 1.0;
76 ctrl.memory = 0;
77 // Hyperparameters, not atoms: no step cap of any kind.
78 ctrl.maxmove = -1.0;
79
80 rgmin_scg_params_t params{};
81 params.sigma0 = 1e-4;
82 params.lambda = settings.lambda;
83 params.lambda_limit = settings.lambda_limit;
84 params.tol_sol = settings.common.tolerance_sol;
85 params.tol_func = settings.common.tolerance_func;
86 params.conjugacy = RGMIN_CONJUGACY_LIU_STOREY;
87
88 rgmin_scg_options_t options{};
89 options.step_tolerance = settings.stationarity_step_stop
90 ? RGMIN_SCG_RELATIVE_EUCLIDEAN
91 : RGMIN_SCG_ABSOLUTE_INFINITY;
92
93 rgmin_report_t report{};
94 const rgmin_status_t st = rgmin_minimize_scg_with_options(
96 hessian_vector_curvature ? &rgminNlmlHessianVectorCurv<Ctx>
97 : ctx.exact_curvature ? &rgminCurv<Ctx>
98 : nullptr,
99 &ctx, xt.get(), &ctrl, &params, &options, &report);
100 completedIterations = static_cast<int>(report.steps);
101 if (settings.common.stop_requested && settings.common.stop_requested()) {
102 failedOptim = true;
103 return;
104 }
105 if (ctx.hessian_vector_fallback) {
107 if (settings.common.report_level >= 1) {
108 gpr::io::LogManager log_man;
109 log_man << " nlml_hessian_vector: the exact NLML Hessian-vector "
110 "product failed during the fit; rgmin's "
111 "finite_difference probe took the later curvatures\n";
112 }
113 }
114 if (ctx.failure.failed()) {
115 if (settings.common.report_level >= 1) ctx.failure.report();
116 failedOptim = true;
117 return;
118 }
119 w = Eigen::Map<Eigen::VectorXd>(wbuf.data(), w.size());
120
121 if (st != RGMIN_SUCCESS) {
122 failedOptim = true;
123 return;
124 }
125 if (ctrl.maxiter > 0 && report.steps >= ctrl.maxiter) {
126 failedOptim = true;
128 }
129}
130
131} // namespace funcmin
132
133#endif /* GPR_ML_RGMIN_SCG_INL_ */
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
bool exhaustedIterationBudget
Definition RgminScg.h:32
int completedIterations
Definition RgminScg.h:33
bool hessianVectorFallback
Definition RgminScg.h:36
void optimize(const gpr::EigenMatrix &x, const Eigen::VectorXd &x_ind, const Eigen::VectorXd &y, Eigen::VectorXd &w, FuncName func_to_min, ClassName &holder, double, const Eigen::VectorXd &lengthscale_rho0=Eigen::VectorXd(), const MapObjectiveConfig *objective_config=nullptr)
Definition RgminScg.inl:16
Responsible for logging.
Definition LogManager.h:28
MapObjectiveConfig makeMapObjectiveConfig(const gpr::EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::VectorXd &w, double current_barrier_strength, const Eigen::VectorXd &lengthscale_rho0, const gpr::ScgOptimizationSettings &settings, bool robust_signal_anchor=true)
bool holderHasNlmlHessianVector(Holder &holder)
rgmin_status_t rgminNlmlHessianVectorCurv(void *user, const DLManagedTensorVersioned *x, const DLManagedTensorVersioned *d, double *curv_out) noexcept
rgmin_status_t rgminCurv(void *user, const DLManagedTensorVersioned *x, const DLManagedTensorVersioned *d, double *curv_out) noexcept
std::unique_ptr< DLManagedTensorVersioned, RgminTensorDeleter > RgminTensorHandle
rgmin_status_t rgminEval(void *user, const DLManagedTensorVersioned *x, double *value_out) noexcept
rgmin_status_t rgminGrad(void *user, const DLManagedTensorVersioned *x, DLManagedTensorVersioned *grad_out) noexcept
void requireCompatibleLibrary()
Definition RgminAbi.h:90
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20