Loading...
Searching...
No Matches
gpr::optim::SketchedHessianH0 Class Reference

#include <gpr/optimization/SketchedHessianH0.h>

Public Types

using HessianVectorProductFn

Public Member Functions

 SketchedHessianH0 (int n_dof, int sketch_rank, uint64_t seed, double tikhonov_eps=1e-10)
void rebuild (const HessianVectorProductFn &hvp)
Eigen::VectorXd apply (const Eigen::VectorXd &q, double gamma_residual) const
bool ready () const noexcept
int sketch_rank () const noexcept
int n_dof () const noexcept

Detailed Description

Definition at line 67 of file SketchedHessianH0.h.

Member Typedef Documentation

◆ HessianVectorProductFn

Initial value:
std::function<Eigen::VectorXd(const Eigen::VectorXd&)>

Definition at line 73 of file SketchedHessianH0.h.

Constructor & Destructor Documentation

◆ SketchedHessianH0()

gpr::optim::SketchedHessianH0::SketchedHessianH0 ( int n_dof,
int sketch_rank,
uint64_t seed,
double tikhonov_eps = 1e-10 )

Definition at line 46 of file SketchedHessianH0.cpp.

48 : n_dof_(n_dof),
49 sketch_rank_(std::min(std::max(sketch_rank, 1), n_dof)),
50 seed_(seed != 0 ? seed : 1ULL),
51 tikhonov_eps_(tikhonov_eps)
52{
53 if (n_dof_ <= 0) {
54 throw std::invalid_argument(
55 "SketchedHessianH0: n_dof must be positive");
56 }
57 Omega_.resize(n_dof_, sketch_rank_);
58 Y_.setZero(n_dof_, sketch_rank_);
59
60 // Replicated Gaussian sketch. Same seed -> same Omega on every
61 // rank, no MPI needed.
62 GaussianSampler sampler(seed_);
63 for (int j = 0; j < sketch_rank_; ++j) {
64 for (int i = 0; i < n_dof_; ++i) {
65 Omega_(i, j) = sampler.next();
66 }
67 }
68}

Member Function Documentation

◆ apply()

Eigen::VectorXd gpr::optim::SketchedHessianH0::apply ( const Eigen::VectorXd & q,
double gamma_residual ) const
nodiscard

Definition at line 131 of file SketchedHessianH0.cpp.

133{
134 if (!ready_) {
135 // Fall back to the legacy scalar H0 when the sketch hasn't
136 // been built yet (first L-BFGS step of the first outer iter).
137 return gamma_residual * q;
138 }
139 if (q.size() != n_dof_) {
140 throw std::invalid_argument(
141 "SketchedHessianH0::apply: q size mismatch with n_dof");
142 }
143 const Eigen::VectorXd Otq = Omega_.transpose() * q;
144 const Eigen::VectorXd Ytq = Y_.transpose() * q;
145 const Eigen::VectorXd rhs = Otq - gamma_residual * Ytq;
146 const Eigen::VectorXd inner = mid_ldlt_.solve(rhs);
147 return gamma_residual * q + Omega_ * inner;
148}

◆ n_dof()

int gpr::optim::SketchedHessianH0::n_dof ( ) const
inlinenodiscardnoexcept

Definition at line 110 of file SketchedHessianH0.h.

111 {
112 return n_dof_;
113 }

◆ ready()

bool gpr::optim::SketchedHessianH0::ready ( ) const
inlinenodiscardnoexcept

Definition at line 102 of file SketchedHessianH0.h.

103 {
104 return ready_;
105 }

◆ rebuild()

void gpr::optim::SketchedHessianH0::rebuild ( const HessianVectorProductFn & hvp)

Definition at line 70 of file SketchedHessianH0.cpp.

71{
72 if (!hvp) {
73 throw std::invalid_argument(
74 "SketchedHessianH0::rebuild: HvP functor is null");
75 }
76
77 int rank = 0;
78 int nprocs = 1;
79#ifdef GPR_HAVE_MPI
80 int mpi_up = 0;
81 MPI_Initialized(&mpi_up);
82 if (mpi_up) {
83 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
84 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
85 }
86#endif
87
88 // Column-distribute the k Y-columns across ranks. Each rank
89 // evaluates a disjoint slice of HvPs; no rank duplicates work
90 // unless k < nprocs (then trailing ranks idle, as expected for
91 // very small sketches).
92 Eigen::MatrixXd Y_local = Eigen::MatrixXd::Zero(n_dof_, sketch_rank_);
93 for (int j = 0; j < sketch_rank_; ++j) {
94 if (j % nprocs == rank) {
95 Y_local.col(j) = hvp(Omega_.col(j));
96 }
97 }
98
99#ifdef GPR_HAVE_MPI
100 if (mpi_up && nprocs > 1) {
101 // Single Allreduce-SUM stitches the disjoint column stripes
102 // into a globally-replicated Y. Cheaper than Allgather with
103 // strided source offsets and degenerates correctly when
104 // sketch_rank < nprocs (idle ranks contribute zeros).
105 Y_.resize(n_dof_, sketch_rank_);
106 MPI_Allreduce(Y_local.data(), Y_.data(),
107 static_cast<int>(Y_local.size()), MPI_DOUBLE, MPI_SUM,
108 MPI_COMM_WORLD);
109 } else
110#endif
111 {
112 Y_ = std::move(Y_local);
113 }
114
115 // Mid = Omega^T Y. Tiny (k x k); replicated on every rank from
116 // identical Omega and Y. Symmetrise to absorb FD asymmetry, then
117 // LDLT with a Tikhonov shift so the apply stays defined when the
118 // HvP range is rank-deficient (Frangella-Tropp-Udell 2023, eq.
119 // 2.4).
120 Eigen::MatrixXd mid = Omega_.transpose() * Y_;
121 mid = (0.5 * (mid + mid.transpose())).eval();
122 mid.diagonal().array() += tikhonov_eps_;
123 mid_ldlt_.compute(mid);
124 if (mid_ldlt_.info() != Eigen::Success) {
125 throw std::runtime_error(
126 "SketchedHessianH0::rebuild: LDLT of Nystrom mid-matrix failed");
127 }
128 ready_ = true;
129}

◆ sketch_rank()

int gpr::optim::SketchedHessianH0::sketch_rank ( ) const
inlinenodiscardnoexcept

Definition at line 106 of file SketchedHessianH0.h.

107 {
108 return sketch_rank_;
109 }

The documentation for this class was generated from the following files: