-
Notifications
You must be signed in to change notification settings - Fork 26
Update QR tests to avoid element-wise comparisons #2785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vlad-perevezentsev
wants to merge
9
commits into
master
Choose a base branch
from
inskip_qr_tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+107
−110
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8cb6d29
Unskip qr tests
vlad-perevezentsev 6c894c9
Update QR tests to avoid element-wise comparisons
vlad-perevezentsev f8729c4
Merge master into inskip_qr_tests
vlad-perevezentsev 26d213a
Fix typos
vlad-perevezentsev 706b3e2
Run TestQr only for float and complex dtypes
vlad-perevezentsev 707c5a0
Move QR helper functions to qr_helper.py
vlad-perevezentsev 9154330
Merge master into inskip_qr_tests
vlad-perevezentsev e9973e6
Update changelog
vlad-perevezentsev dc81277
Apply remarks
vlad-perevezentsev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import numpy | ||
|
|
||
| from .helper import factor_to_tol, has_support_aspect64 | ||
|
|
||
|
|
||
| def gram(x, xp): | ||
| # Return Gram matrix: X^H @ X | ||
| return xp.conjugate(x).swapaxes(-1, -2) @ x | ||
|
|
||
|
|
||
| def get_R_from_raw(h, m, n, xp): | ||
| # Get reduced R from NumPy-style raw QR: | ||
| # R = triu((tril(h))^T), shape (..., k, n) | ||
| k = min(m, n) | ||
| rt = xp.tril(h) | ||
| r = xp.swapaxes(rt, -1, -2) | ||
| r = xp.triu(r[..., :m, :n]) | ||
| return r[..., :k, :] | ||
|
|
||
|
|
||
| def check_qr(a_np, a_xp, mode, xp): | ||
| # QR is not unique: | ||
| # element-wise comparison with NumPy may differ by sign/phase. | ||
| # To verify correctness use mode-dependent functional checks: | ||
| # complete/reduced: check decomposition Q @ R = A | ||
| # raw/r: check invariant R^H @ R = A^H @ A | ||
| if mode in ("complete", "reduced"): | ||
| res = xp.linalg.qr(a_xp, mode) | ||
| assert xp.allclose(res.Q @ res.R, a_xp, atol=1e-5) | ||
|
|
||
| # Since QR satisfies A = Q @ R with orthonormal Q (Q^H @ Q = I), | ||
| # validate correctness via the invariant R^H @ R == A^H @ A | ||
| # for raw/r modes | ||
| elif mode == "raw": | ||
| _, tau_np = numpy.linalg.qr(a_np, mode=mode) | ||
| h_xp, tau_xp = xp.linalg.qr(a_xp, mode=mode) | ||
|
|
||
| m, n = a_np.shape[-2], a_np.shape[-1] | ||
| Rraw_xp = get_R_from_raw(h_xp, m, n, xp) | ||
|
|
||
| rtol = atol = factor_to_tol(Rraw_xp.dtype, 100) | ||
|
|
||
| # Use reduced QR as a reference: | ||
| # reduced is validated via Q @ R == A | ||
| exp_r = xp.linalg.qr(a_xp, mode="reduced").R | ||
| assert xp.allclose(Rraw_xp, exp_r, atol=atol, rtol=rtol) | ||
|
|
||
| exp_xp = gram(a_xp, xp) | ||
|
|
||
| # Compare R^H @ R == A^H @ A | ||
| assert xp.allclose(gram(Rraw_xp, xp), exp_xp, atol=atol, rtol=rtol) | ||
|
|
||
| assert tau_xp.shape == tau_np.shape | ||
| if not has_support_aspect64(tau_xp.sycl_device): | ||
| assert tau_xp.dtype.kind == tau_np.dtype.kind | ||
| else: | ||
| assert tau_xp.dtype == tau_np.dtype | ||
|
|
||
| else: # mode == "r" | ||
| r_xp = xp.linalg.qr(a_xp, mode="r") | ||
|
|
||
| # Use reduced QR as a reference: | ||
| # reduced is validated via Q @ R == A | ||
| exp_r = xp.linalg.qr(a_xp, mode="reduced").R | ||
| rtol = atol = factor_to_tol(exp_r.dtype, 100) | ||
|
|
||
| assert xp.allclose(r_xp, exp_r, atol=atol, rtol=rtol) | ||
|
|
||
| exp_xp = gram(a_xp, xp) | ||
|
|
||
| # Compare R^H @ R == A^H @ A | ||
| assert xp.allclose(gram(r_xp, xp), exp_xp, atol=atol, rtol=rtol) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.