Skip to content

Commit

Permalink
Fix performance problems with jac_batch_size=None
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Nov 21, 2024
1 parent a81af8a commit 8c43999
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/jaxls/_factor_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ def compute_jac_with_perturb(factor: _AnalyzedFactor) -> jax.Array:
)(jnp.zeros((val_subset._get_tangent_dim(),)))

# Compute Jacobian for each factor.
stacked_jac = jax.lax.map(
compute_jac_with_perturb, factor, batch_size=factor.jac_batch_size
)
if factor.jac_batch_size is None:
stacked_jac = jax.vmap(compute_jac_with_perturb)(factor)
else:
# When `batch_size` is `None`, jax.lax.map reduces to a scan
# (similar to `batch_size=1`).
stacked_jac = jax.lax.map(
compute_jac_with_perturb, factor, batch_size=factor.jac_batch_size
)
(num_factor,) = factor._get_batch_axes()
assert stacked_jac.shape == (
num_factor,
Expand Down

0 comments on commit 8c43999

Please sign in to comment.