Skip to content

Commit 7265ff4

Browse files
committed
add pst back
Signed-off-by: Hao Wu <skyw@nvidia.com>
1 parent 25fd2a4 commit 7265ff4

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

emerging_optimizers/soap/soap_utils.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ def get_eigenbasis_eigh(
8585
# We use an empty tensor so that the `precondition` function will skip this factor.
8686
updated_eigenbasis_list.append(torch.empty(0, device=kronecker_factor.device))
8787
continue
88+
# Construct approximated eigenvalues using QL^T@L@QL or QR^T@R@QR.
89+
# The approximated eigenvalues should be close to diagonal if the eigenbasis is close to the true
90+
# eigenbasis of the kronecker factor (i.e. the approximated eigenvectors diagonalize the kronecker factor)
91+
approx_eigenvalue_matrix = eigenbasis.T @ kronecker_factor @ eigenbasis
8892
# Update eigenbasis when necessary. Update is skipped only when adaptive update criteria is met.
89-
if utils.eig.met_approx_eigvals_criteria(kronecker_factor, adaptive_update_tolerance):
93+
if utils.eig.met_approx_eigvals_criteria(approx_eigenvalue_matrix, adaptive_update_tolerance):
9094
_, Q = utils.eig.eigh_with_fallback(
9195
kronecker_factor,
9296
force_double=False,
@@ -203,14 +207,17 @@ def get_eigenbasis_qr(
203207
# Update eigenbasis when necessary. Update is skipped only when use_adaptive_criteria is True
204208
# but criteria is not met.
205209
if_update = True
206-
if use_adaptive_criteria and utils.eig.met_approx_eigvals_criteria(
207-
kronecker_factor, adaptive_update_tolerance
208-
):
209-
if_update = False
210-
if if_update:
211-
# construct approximated eigenvalues using QL^T@L@QL or QR^T@R@QR, which should be close to diagonal
212-
# if the eigenbasis is close to the true eigenbasis of the kronecker factor (i.e. diagonalizes it)
210+
# construct approximated eigenvalues using QL^T@L@QL or QR^T@R@QR, which should be close to diagonal
211+
# if the eigenbasis is close to the true eigenbasis of the kronecker factor (i.e. diagonalizes it)
212+
if use_adaptive_criteria:
213+
approx_eigenvalue_matrix = _conjugate(kronecker_factor, eigenbasis)
214+
if_update = not utils.eig.met_approx_eigvals_criteria(approx_eigenvalue_matrix, adaptive_update_tolerance)
215+
if if_update:
216+
approx_eigvals = torch.diag(approx_eigenvalue_matrix)
217+
else:
213218
approx_eigvals = _conjugate(kronecker_factor, eigenbasis, diag=True)
219+
220+
if if_update:
214221
Q, exp_avg_sq = _orthogonal_iteration(
215222
approx_eigvals=approx_eigvals,
216223
kronecker_factor=kronecker_factor,

0 commit comments

Comments
 (0)