-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor PEPS environment caching for backward pass #76
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
Merged
+133
−54
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6045ad6
Optimize PEPS sequential sampling: reduce 3 passes to 2 passes
fliingelephant a72f678
Update
fliingelephant f191534
Optimize PEPS env cache and contractions
fliingelephant f9c2bc9
Add QR that can be batched
fliingelephant 6500070
Refactor PEPS env/sampling paths and unify compact-WY QR
fliingelephant 4df2cfc
Make QR via Cholesky batch-polymorphical
fliingelephant 9f6e346
Fix CI
fliingelephant 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
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,61 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import jax | ||
| import jax.numpy as jnp | ||
|
|
||
| __all__ = ["_qr_compactwy", "_qr_cholesky"] | ||
|
|
||
|
|
||
| def _qr_compactwy(a: jax.Array) -> tuple[jax.Array, jax.Array]: | ||
| """Householder QR via compact WY representation. | ||
|
|
||
| Computes reduced ``(Q, R)`` on trailing matrix axes, batch-polymorphically. | ||
| """ | ||
| r, tau = jnp.linalg.qr(a, mode="raw") # batchable geqrf by CuSOLVER | ||
| q = _householder_wy(r.mT, tau) | ||
| return q, jnp.triu(r.mT[..., : tau.shape[-1], :]) | ||
fliingelephant marked this conversation as resolved.
Show resolved
Hide resolved
fliingelephant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def _householder_wy(r: jax.Array, tau: jax.Array) -> jax.Array: | ||
| """Build reduced ``Q`` from geqrf reflectors in compact WY form. | ||
|
|
||
| Implements ``Q = I - Y T Y^H`` on trailing matrix axes, batch-polymorphically. | ||
| """ | ||
| m = r.shape[-2] | ||
| k = tau.shape[-1] | ||
| dtype = r.dtype | ||
|
|
||
| Y = jnp.tril(r[..., :, :k], k=-1) + jnp.eye(m, k, dtype=dtype) | ||
| YHY = jnp.einsum("...ki,...kj->...ij", Y.conj(), Y, optimize=True) | ||
| strict_lower = jnp.tril(jnp.ones((k, k), dtype=dtype), k=-1) | ||
| basis = jnp.eye(k, dtype=dtype) | ||
|
|
||
| def update_column(j: int, T: jax.Array) -> jax.Array: | ||
| mask = strict_lower[j, :] | ||
| yhy_col = YHY[..., :, j] * mask | ||
| t_yhy = jnp.einsum("...ab,...b->...a", T, yhy_col, optimize=True) | ||
| tau_j = tau[..., j][..., None] | ||
| new_col = -tau_j * t_yhy * mask + tau_j * basis[j] | ||
| return jax.lax.dynamic_update_slice_in_dim(T, new_col[..., None], j, axis=-1) | ||
|
|
||
| T = jax.lax.fori_loop( | ||
| 0, | ||
| k, | ||
| update_column, | ||
| jnp.zeros(tau.shape[:-1] + (k, k), dtype=dtype), | ||
| ) | ||
|
|
||
| return jnp.eye(m, k, dtype=dtype) - jnp.einsum( | ||
| "...ik,...kl,...jl->...ij", | ||
| Y, | ||
| T, | ||
| Y[..., :k, :].conj(), | ||
| optimize=True, | ||
| ) | ||
|
|
||
|
|
||
| def _qr_cholesky(a: jax.Array) -> tuple[jax.Array, jax.Array]: | ||
| gram = a.mH @ a | ||
| L = jnp.linalg.cholesky(gram) | ||
| q = jax.scipy.linalg.solve_triangular(L, a.mH, lower=True).mH | ||
| return q, L.mH | ||
Oops, something went wrong.
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.