Upload folder using huggingface_hub
Browse files- ballast/__pycache__/gp.cpython-312.pyc +0 -0
- ballast/__pycache__/policies.cpython-312.pyc +0 -0
- ballast/gp.py +26 -13
- ballast/policies.py +22 -25
ballast/__pycache__/gp.cpython-312.pyc
CHANGED
|
Binary files a/ballast/__pycache__/gp.cpython-312.pyc and b/ballast/__pycache__/gp.cpython-312.pyc differ
|
|
|
ballast/__pycache__/policies.cpython-312.pyc
CHANGED
|
Binary files a/ballast/__pycache__/policies.cpython-312.pyc and b/ballast/__pycache__/policies.cpython-312.pyc differ
|
|
|
ballast/gp.py
CHANGED
|
@@ -190,7 +190,7 @@ def base_factor(
|
|
| 190 |
return L, 2.0 * jnp.sum(jnp.log(jnp.diag(L)))
|
| 191 |
|
| 192 |
|
| 193 |
-
def
|
| 194 |
L_base: jnp.ndarray,
|
| 195 |
logdet_base: jnp.ndarray,
|
| 196 |
S_base: jnp.ndarray,
|
|
@@ -202,27 +202,40 @@ def rank_q_logdet(
|
|
| 202 |
p: HelmParams,
|
| 203 |
sigma: float,
|
| 204 |
) -> jnp.ndarray:
|
| 205 |
-
"""logdet(I + sigma^-2 K(Z u
|
|
|
|
| 206 |
|
| 207 |
det [[A, B], [B^T, D]] = det(A) det(D - B^T A^{-1} B)
|
| 208 |
|
| 209 |
-
with A = I + sigma^-2 K(Z) already factorised (independent of the
|
| 210 |
-
B = sigma^-2 K(Z,
|
| 211 |
-
candidate instead of O((n+q)^3)
|
| 212 |
-
20-sample inner loop of Algorithm 2 affordable.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
Masked-out new points contribute e_i rows to the Schur complement and hence
|
| 215 |
nothing to the logdet.
|
| 216 |
"""
|
| 217 |
-
|
| 218 |
-
|
|
|
|
| 219 |
|
| 220 |
-
|
|
|
|
| 221 |
B = B * mnew[None, :]
|
| 222 |
if base_mask is not None:
|
| 223 |
B = B * _expand_mask(base_mask).astype(B.dtype)[:, None]
|
| 224 |
|
| 225 |
-
V = solve_triangular(L_base, B, lower=True) # (2n,
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
| 190 |
return L, 2.0 * jnp.sum(jnp.log(jnp.diag(L)))
|
| 191 |
|
| 192 |
|
| 193 |
+
def rank_q_logdet_batch(
|
| 194 |
L_base: jnp.ndarray,
|
| 195 |
logdet_base: jnp.ndarray,
|
| 196 |
S_base: jnp.ndarray,
|
|
|
|
| 202 |
p: HelmParams,
|
| 203 |
sigma: float,
|
| 204 |
) -> jnp.ndarray:
|
| 205 |
+
"""logdet(I + sigma^-2 K(Z u P_c)) for a batch of candidates c, via the
|
| 206 |
+
App. E.2 block-determinant update
|
| 207 |
|
| 208 |
det [[A, B], [B^T, D]] = det(A) det(D - B^T A^{-1} B)
|
| 209 |
|
| 210 |
+
with A = I + sigma^-2 K(Z) already factorised (Z is independent of the
|
| 211 |
+
candidate), B = sigma^-2 K(Z, P_c), D = I + sigma^-2 K(P_c). Cost is
|
| 212 |
+
O(n^2 q + q^3) per candidate instead of O((n+q)^3) -- this is what makes the
|
| 213 |
+
625-candidate x 20-sample inner loop of Algorithm 2 affordable.
|
| 214 |
+
|
| 215 |
+
Shapes: S_new (C, q, 2), t_new (q,), new_mask (C, q) -> returns (C,).
|
| 216 |
+
|
| 217 |
+
All C candidates share the single triangular factor L_base, so they are
|
| 218 |
+
solved as ONE solve against a (2n, C*2q) right-hand side. Doing this with
|
| 219 |
+
vmap instead makes JAX's triangular-solve batching rule materialise one copy
|
| 220 |
+
of L_base per candidate (C x 2n x 2n -- tens of GB, and correspondingly
|
| 221 |
+
slow); the shared-factor structure is the whole point of the rank-q trick.
|
| 222 |
|
| 223 |
Masked-out new points contribute e_i rows to the Schur complement and hence
|
| 224 |
nothing to the logdet.
|
| 225 |
"""
|
| 226 |
+
C, q = new_mask.shape
|
| 227 |
+
Sn = S_new.reshape(C * q, 2)
|
| 228 |
+
tn = jnp.tile(t_new, C)
|
| 229 |
|
| 230 |
+
B = k_thelm_mat(S_base, t_base, Sn, tn, p) / sigma**2 # (2n, 2Cq)
|
| 231 |
+
mnew = _expand_mask(new_mask.reshape(-1)).astype(B.dtype) # (2Cq,)
|
| 232 |
B = B * mnew[None, :]
|
| 233 |
if base_mask is not None:
|
| 234 |
B = B * _expand_mask(base_mask).astype(B.dtype)[:, None]
|
| 235 |
|
| 236 |
+
V = solve_triangular(L_base, B, lower=True) # (2n, 2Cq) -- one solve
|
| 237 |
+
V = V.reshape(V.shape[0], C, 2 * q)
|
| 238 |
+
VtV = jnp.einsum("ncp,ncr->cpr", V, V) # (C, 2q, 2q)
|
| 239 |
+
|
| 240 |
+
D = jax.vmap(lambda s, m: noise_gram(s, t_new, p, sigma, m))(S_new, new_mask)
|
| 241 |
+
return logdet_base + logdet_chol(D - VtV)
|
ballast/policies.py
CHANGED
|
@@ -12,7 +12,7 @@ import jax.numpy as jnp
|
|
| 12 |
import numpy as np
|
| 13 |
from scipy.stats import qmc
|
| 14 |
|
| 15 |
-
from .gp import base_factor,
|
| 16 |
from .kernels import HelmParams
|
| 17 |
from .spde import SpdeOps, propagate
|
| 18 |
from .trajectory import Grid, advect
|
|
@@ -109,16 +109,16 @@ def ballast_sample_utilities(
|
|
| 109 |
m_base = jnp.concatenate([om, eval_.reshape(-1)], axis=0)
|
| 110 |
L, ld = base_factor(S_base, t_base, p, sigma, m_base)
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
L, ld, S_base, t_base, m_base,
|
| 115 |
-
cpos[:,
|
|
|
|
| 116 |
)
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
[jax.vmap(one)(jnp.arange(a, min(a + chunk, grid.n)))
|
| 120 |
-
for a in range(0, grid.n, chunk)]
|
| 121 |
-
)
|
| 122 |
out.append(vals)
|
| 123 |
return jnp.stack(out)
|
| 124 |
|
|
@@ -162,15 +162,14 @@ def true_field_utilities(
|
|
| 162 |
m_base = jnp.concatenate([om, eval_.reshape(-1)], axis=0)
|
| 163 |
L, ld = base_factor(S_base, t_base, p, sigma, m_base)
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
L, ld, S_base, t_base, m_base,
|
|
|
|
|
|
|
| 168 |
)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
[jax.vmap(one)(jnp.arange(a, min(a + chunk, grid.n)))
|
| 172 |
-
for a in range(0, grid.n, chunk)]
|
| 173 |
-
)
|
| 174 |
|
| 175 |
|
| 176 |
# --------------------------------------------------------------------------
|
|
@@ -185,17 +184,15 @@ def eig_utilities(
|
|
| 185 |
"""logdet(I + sigma^-2 K(X_n u {(s, t_n)})) for every candidate s."""
|
| 186 |
L, ld = base_factor(S_obs, t_obs, p, sigma, mask)
|
| 187 |
tv = jnp.array([t_m])
|
|
|
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
| 191 |
L, ld, S_obs, t_obs, mask,
|
| 192 |
-
grid.R[
|
| 193 |
)
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
[jax.vmap(one)(jnp.arange(a, min(a + chunk, grid.n)))
|
| 197 |
-
for a in range(0, grid.n, chunk)]
|
| 198 |
-
)
|
| 199 |
|
| 200 |
|
| 201 |
# --------------------------------------------------------------------------
|
|
|
|
| 12 |
import numpy as np
|
| 13 |
from scipy.stats import qmc
|
| 14 |
|
| 15 |
+
from .gp import base_factor, rank_q_logdet_batch, posterior_ext_state, sample_ext_state
|
| 16 |
from .kernels import HelmParams
|
| 17 |
from .spde import SpdeOps, propagate
|
| 18 |
from .trajectory import Grid, advect
|
|
|
|
| 109 |
m_base = jnp.concatenate([om, eval_.reshape(-1)], axis=0)
|
| 110 |
L, ld = base_factor(S_base, t_base, p, sigma, m_base)
|
| 111 |
|
| 112 |
+
# candidates are chunked only to bound the size of the (2n, C*2q)
|
| 113 |
+
# right-hand side; within a chunk it is a single shared-factor solve
|
| 114 |
+
vals = jnp.concatenate([
|
| 115 |
+
rank_q_logdet_batch(
|
| 116 |
L, ld, S_base, t_base, m_base,
|
| 117 |
+
jnp.swapaxes(cpos[:, a:a + chunk, :], 0, 1), t_traj,
|
| 118 |
+
jnp.swapaxes(cval[:, a:a + chunk], 0, 1), p, sigma,
|
| 119 |
)
|
| 120 |
+
for a in range(0, grid.n, chunk)
|
| 121 |
+
])
|
|
|
|
|
|
|
|
|
|
| 122 |
out.append(vals)
|
| 123 |
return jnp.stack(out)
|
| 124 |
|
|
|
|
| 162 |
m_base = jnp.concatenate([om, eval_.reshape(-1)], axis=0)
|
| 163 |
L, ld = base_factor(S_base, t_base, p, sigma, m_base)
|
| 164 |
|
| 165 |
+
return jnp.concatenate([
|
| 166 |
+
rank_q_logdet_batch(
|
| 167 |
+
L, ld, S_base, t_base, m_base,
|
| 168 |
+
jnp.swapaxes(cpos[:, a:a + chunk, :], 0, 1), t_traj,
|
| 169 |
+
jnp.swapaxes(cval[:, a:a + chunk], 0, 1), p, sigma,
|
| 170 |
)
|
| 171 |
+
for a in range(0, grid.n, chunk)
|
| 172 |
+
])
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
|
| 175 |
# --------------------------------------------------------------------------
|
|
|
|
| 184 |
"""logdet(I + sigma^-2 K(X_n u {(s, t_n)})) for every candidate s."""
|
| 185 |
L, ld = base_factor(S_obs, t_obs, p, sigma, mask)
|
| 186 |
tv = jnp.array([t_m])
|
| 187 |
+
ones = jnp.ones((grid.n, 1), dtype=bool)
|
| 188 |
|
| 189 |
+
return jnp.concatenate([
|
| 190 |
+
rank_q_logdet_batch(
|
| 191 |
L, ld, S_obs, t_obs, mask,
|
| 192 |
+
grid.R[a:a + chunk][:, None, :], tv, ones[a:a + chunk], p, sigma,
|
| 193 |
)
|
| 194 |
+
for a in range(0, grid.n, chunk)
|
| 195 |
+
])
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
|
| 198 |
# --------------------------------------------------------------------------
|