Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

BingLiu0
Copy link

Pull Request

What problem does this PR solve?

Issue Number: Fixed #

Possible side effects?

  • Performance:

  • Backward compatibility:

@BingLiu0
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

@rivertalk
Copy link

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a 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 in jacobi_rotation and uses it to compute t, c, and s. 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.
  • 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.

Comment on lines +9 to +10
tau = (A[q, q] - A[p, p]) / (2 * A[p, q])
t = jnp.sign(tau) / (jnp.abs(tau) + jnp.sqrt(1 + tau**2))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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))

Comment on lines +31 to +46
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Comment on lines +71 to +75
max_off_diag < tol,
lambda val: val,
lambda val: (apply_rotation(val[0]), val[1]),
(A, max_off_diag),
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The condition max_off_diag < tol only checks if the maximum off-diagonal element is below the tolerance. It does not guarantee that the singular values have converged. Consider adding a check based on the change in singular values between iterations to ensure convergence.

Comment on lines +46 to +47
A = random.normal(random.PRNGKey(0), (10, 10))
A = (A + A.T) / 2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The 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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants