Jax gmres#707
Conversation
| return x | ||
|
|
||
|
|
||
| @partial(jax.jit, static_argnums=(2,)) |
There was a problem hiding this comment.
Is this returning an arnoldi factorization?
There was a problem hiding this comment.
If so, do you think you can use the one that is implemented in the backend?
There was a problem hiding this comment.
Well, the one in the backend returns the eigendecomposition; I need the V and H from the Krylov decomposition (actually I need V and the full QR decomposition of H). If Arnoldi were refactored to produce these it could be used.
However, a more efficient implementation, which I will add later, would also update the GMRES residual by a Givens rotation once per new Krylov vector. This becomes specialized enough that I think it makes more sense to keep things together with GMRES until proved otherwise, which is reflected in the code as written.
There was a problem hiding this comment.
sorry I was not precise in my wording. I meant the arnoldi factorization in jitted_functions.py. This one priduces exactly that. It' called _arnoldi_fact
There was a problem hiding this comment.
would it make sense to move gmres into the jitted_functions.py file?
There was a problem hiding this comment.
My recent push should address the above
|
The build works now |
| self._iram = jitted_functions._implicitly_restarted_arnoldi(libjax) | ||
| return self._iram(_CACHED_MATVECS[A], args, initial_state, num_krylov_vecs, | ||
| numeig, which, tol, maxiter) | ||
| imp_arnoldi = jitted_functions._implicitly_restarted_arnoldi(libjax) |
| """ | ||
| Helper function to generate jitted lanczos function used | ||
| in JaxBackend.eigsh_lanczos. The function `jax_lanczos` | ||
| Helper function to generate jitted lanczos function used |
There was a problem hiding this comment.
Did you remove trailing white space on purpose, or is this from yapfing?
There was a problem hiding this comment.
I removed it on purpose with a vi script
There was a problem hiding this comment.
Trailing whitespace violates PEP8 and is in general Bad because for example it invisible modifies the behaviour of text editor commands to jump to the end of the line. Was there some reason to keep it?
There was a problem hiding this comment.
no, just curious. If you want it removed, then it would be great if you could prepare a PR that fixes this for the whole library (just run your script over all files). This way we'll have a cleaner code history
| _, lambda x: norm_not_too_small) | ||
|
|
||
| return continue_iteration | ||
| initial_norm_typecaster = np.zeros((1,)) + eps + 1. |
There was a problem hiding this comment.
you can do initial_norm = v.real.dtype.type(1.0+eps) instead
| Given a linear mapping with (n x n) matrix representation | ||
| A = A_mv(*A_args) gmres_m solves | ||
| Ax = b (1) | ||
| where x and b are length-b vectors, using the method of |
|
|
||
| A_mv : A function `v0 = A_mv(v, *A_args, **A_kwargs)` where `v0` and | ||
| `v` have the same shape. | ||
| b : The `b` in `A @ x = b`. |
There was a problem hiding this comment.
looks like A_args is missing from the docstring
| return r, beta | ||
|
|
||
|
|
||
| #@partial(jax.jit, static_argnums=(2,)) |
There was a problem hiding this comment.
why is the jit command commented?
| backend.gmres(dummy_mv, b, A_kwargs=A_kwargs) | ||
|
|
||
|
|
||
| #jax_qr_dtypes = [np.float32, np.float64, np.complex64, np.complex128] |
|
|
||
|
|
||
| #jax_qr_dtypes = [np.float32, np.float64, np.complex64, np.complex128] | ||
| jax_qr_dtypes = [np.float32] |
| x, _ = backend.gmres(A_mv, b, x0=x0, num_krylov_vectors=n_kry, tol=tol) | ||
| solution = jax.numpy.array([2., 1.], dtype=dtype) | ||
| eps = jax.numpy.linalg.norm(jax.numpy.abs(solution) - jax.numpy.abs(x)) | ||
| print(eps) |
| def A_mv(x): | ||
| return A @ x | ||
| b = A_mv(solution) | ||
| tol = b.size * jax.numpy.finfo(dtype).eps |
|
Martin's comments should be dealt with |
mganahl
left a comment
There was a problem hiding this comment.
Can you add the A_args to the docstring, then this is good to go
|
Hey @alewis, did you check that your modifications to |
import jax
import tensornetwork as tn
import numpy as np
import time
D=200
dtype=np.float32
matrix = jax.numpy.array(np.random.rand(D,D).astype(dtype))
vector = jax.numpy.array(np.random.rand(D,).astype(dtype))
@jax.jit
def matvec_jax_matrix(vec,matrix):
return jax.numpy.tensordot(matrix, vec,([1],[0]))
jax_backend = tn.backends.jax.jax_backend.JaxBackend()
ncv=10
t1 = time.time()
eta_j, U_j = jax_backend.eigsh_lanczos(matvec_jax_matrix,[matrix],vector,num_krylov_vecs = ncv,numeig=1,
reorthogonalize=False)
print('jax eigvals:', eta_j)
t2 = time.time()
eta_j, U_j = jax_backend.eigsh_lanczos(matvec_jax_matrix,[matrix],vector,num_krylov_vecs = ncv,numeig=1,
reorthogonalize=False)
print('jax eigvals:', eta_j)
t3 = time.time()
print('jax first:', t2 - t1)
print('jax second:', t3 - t2)could you run this and send the result? |
|
[image: Screen Shot 2020-07-08 at 11.44.19 AM.png]
…On Wed, Jul 8, 2020 at 11:39 AM Martin Ganahl ***@***.***> wrote:
import jaximport tensornetwork as tnimport numpy as npimport timeD=200dtype=np.float32matrix = jax.numpy.array(np.random.rand(D,D).astype(dtype))vector = ***@***.*** matvec_jax_matrix(vec,matrix):
return jax.numpy.tensordot(matrix, vec,([1],[0]))jax_backend = tn.backends.jax.jax_backend.JaxBackend()ncv=10t1 = time.time()eta_j, U_j = jax_backend.eigsh_lanczos(matvec_jax_matrix,[matrix],vector,num_krylov_vecs = ncv,numeig=1,
reorthogonalize=False)print('jax eigvals:', eta_j)t2 = time.time()eta_j, U_j = jax_backend.eigsh_lanczos(matvec_jax_matrix,[matrix],vector,num_krylov_vecs = ncv,numeig=1,
reorthogonalize=False)
print('jax eigvals:', eta_j)t3 = time.time()print('jax first:', t2 - t1)print('jax second:', t3 - t2)
could you run this and send the result?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#707 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAINHBQ64NFTMLIT64DEJOLR2SHM5ANCNFSM4OPHYVLQ>
.
|
|
sorry can't see it |
|
looks good, I'll pull it in as soon as building's finished |

Adds a Jax implementation of GMRES.