-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
MPC Quantized Machine Learning——Jacobi SVD #1006
base: main
Are you sure you want to change the base?
Conversation
PPML Jacobi SVD
I have read the CLA Document and I hereby sign the CLA |
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request introduces a Jacobi SVD implementation for secure multi-party computation (MPC) using the SPU framework. It includes the core algorithm, an emulation script for testing, and a unit test. Overall, the code is well-structured and addresses the task of implementing Jacobi SVD in a privacy-preserving setting.
Summary of Findings
- Potential Convergence Issues: The
jacobi_svd
function relies on a fixed number of iterations (max_iter
). For larger matrices or matrices with certain properties, the algorithm might not converge within the specified number of iterations, leading to inaccurate results. Consider adding a check for convergence based on the change in singular values between iterations. - Numerical Stability: The code calculates
tau
injacobi_rotation
and uses it to computet
,c
, ands
. WhenA[p, q]
is close to zero,tau
can become very large, potentially leading to numerical instability. Consider adding a small constant to the denominator to prevent division by zero or using an alternative approach to compute the rotation parameters. - Testing with Different Matrix Types: The current test case uses a random symmetric matrix. It would be beneficial to add more test cases with different types of matrices (e.g., diagonal, orthogonal, ill-conditioned) to ensure the algorithm's robustness and accuracy under various conditions.
Merge Readiness
The pull request introduces a new Jacobi SVD implementation with supporting emulation and tests. While the code appears functional, there are potential issues related to convergence and numerical stability that should be addressed before merging. I recommend addressing the convergence and numerical stability concerns before merging. I am unable to directly approve this pull request, and other reviewers should also review this code before merging.
tau = (A[q, q] - A[p, p]) / (2 * A[p, q]) | ||
t = jnp.sign(tau) / (jnp.abs(tau) + jnp.sqrt(1 + tau**2)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When A[p, q]
is close to zero, tau
can become very large, potentially leading to numerical instability. Consider adding a small constant to the denominator to prevent division by zero or using an alternative approach to compute the rotation parameters.
tau = (A[q, q] - A[p, p]) / (2 * A[p, q]) | |
t = jnp.sign(tau) / (jnp.abs(tau) + jnp.sqrt(1 + tau**2)) | |
tau = (A[q, q] - A[p, p]) / (2 * A[p, q] + 1e-12) | |
t = jnp.sign(tau) / (jnp.abs(tau) + jnp.sqrt(1 + tau**2)) |
def jacobi_svd(A, tol=1e-10, max_iter=100): | ||
""" | ||
Perform the Jacobi algorithm to compute the Singular Value Decomposition (SVD) of a symmetric matrix A. | ||
|
||
The algorithm iterates by selecting the largest off-diagonal element and applying a Jacobi rotation | ||
to zero it out. The process continues until the largest off-diagonal element is smaller than the specified tolerance or the maximum number of iterations is reached. | ||
|
||
Arguments: | ||
A -- input symmetric matrix (n x n) | ||
tol -- tolerance for stopping criteria (default 1e-10) | ||
max_iter -- maximum number of iterations (default 100) | ||
|
||
Note: | ||
For matrices of different sizes, the value of `max_iter` should be adjusted according to the matrix dimension. | ||
Typically, as the size of the matrix `n` increases, `max_iter` should also be increased to ensure the algorithm can converge within a larger computational effort. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation mentions adjusting max_iter
for different matrix sizes. However, it does not provide guidance on how to choose an appropriate value. Consider adding a heuristic or a formula to help users determine a suitable max_iter
based on the matrix dimension or other relevant factors. Also, the algorithm might not converge within the specified number of iterations, leading to inaccurate results. Consider adding a check for convergence based on the change in singular values between iterations.
max_off_diag < tol, | ||
lambda val: val, | ||
lambda val: (apply_rotation(val[0]), val[1]), | ||
(A, max_off_diag), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A = random.normal(random.PRNGKey(0), (10, 10)) | ||
A = (A + A.T) / 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request
What problem does this PR solve?
Issue Number: Fixed #
Possible side effects?
Performance:
Backward compatibility: