-
Notifications
You must be signed in to change notification settings - Fork 8
Try to get the Mooncake & friends tests to consistently pass #293
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
base: ksh/trunc_fwd
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -59,6 +59,27 @@ test in-place Hermitian eigendecomposition rules via Mooncake's non-primitive AD | |||||
| """ | ||||||
| eigh!_wrapper(f!, A, alg) = (F = f!(project_hermitian!(A), alg); MatrixAlgebraKit.zero!(A); F) | ||||||
|
|
||||||
| """ | ||||||
| eig_vals_wrapper(f, A, alg) | ||||||
|
|
||||||
| Wrapper that sorts the eigenvalues returned by `f(A, alg)` by modulus and then by imaginary part. | ||||||
| LAPACK's ordering of the eigenvalues can change discontinuously under small perturbations of | ||||||
| `A`, which breaks finite-difference checks. The ordering imposed here is smooth for matrices | ||||||
| built with `make_eig_matrix`, whose eigenvalues have distinct moduli up to conjugate pairs. | ||||||
| """ | ||||||
| eig_vals_wrapper(f, A, alg) = sort_eigvals(f(A, alg)) | ||||||
|
|
||||||
| """ | ||||||
| eig_vals!_wrapper(f!, A, alg) | ||||||
|
|
||||||
| In-place variant of [`eig_vals_wrapper`](@ref), which zeros `A` after calling `f!`. | ||||||
| """ | ||||||
| eig_vals!_wrapper(f!, A, alg) = sort_eigvals(call_and_zero!(f!, A, alg)) | ||||||
|
|
||||||
| # sortperm is used here because Mooncake CAN differentiate that on CUDA, | ||||||
| # but CANNOT differentiate sort | ||||||
| sort_eigvals(D) = D[sortperm(collect(D); by = λ -> (abs(λ), imag(λ)))] | ||||||
|
|
||||||
| """ | ||||||
| qr_gauge_invariant_wrapper(f, A, alg, r) | ||||||
|
|
||||||
|
|
@@ -149,11 +170,27 @@ function stabilize_eigvals!(D::AbstractVector) | |||||
| n = maximum(p) | ||||||
| # rescale eigenvalues so that they lie on distinct radii in the complex plane | ||||||
| # that are chosen randomly in non-overlapping intervals [10 * k/n, 10 * (k+0.5)/n)] for k=1,...,n | ||||||
| radii = 10 .* ((1:n) .+ rand(real(eltype(D)), n) ./ 2) ./ n | ||||||
| radii = 10 .* ((1:n) .+ rand(rng, real(eltype(D)), n) ./ 2) ./ n | ||||||
| hD = sign.(collect(D)) .* radii[p] | ||||||
| copyto!(D, hD) | ||||||
| return D | ||||||
| end | ||||||
| """ | ||||||
| midgap_tol(vals) | ||||||
|
|
||||||
| Return a truncation tolerance halfway across the widest gap between consecutive values of | ||||||
| `abs.(vals)`, restricted to the middle half so that truncation keeps a nontrivial subset. | ||||||
| This keeps the number of retained values fixed under the perturbations used by | ||||||
| finite-difference checks. | ||||||
| """ | ||||||
| function midgap_tol(vals) | ||||||
| s = sort!(collect(abs.(vals))) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need the
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not if
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I missed the GPU case :) doesn't
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah it does but I was skittish of the logic later on so I just did everything on CPU instead 😭 |
||||||
| n = length(s) | ||||||
| gaps = (max(1, n ÷ 4)):(min(n - 1, (3n) ÷ 4)) | ||||||
| _, i = findmax(i -> s[i + 1] - s[i], gaps) | ||||||
| return (s[gaps[i]] + s[gaps[i] + 1]) / 2 | ||||||
| end | ||||||
|
|
||||||
| function make_eig_matrix(T, sz) | ||||||
| A = instantiate_matrix(T, sz) | ||||||
| D, V = eig_full(A) | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.