Skip to content

Commit 03445d0

Browse files
committed
JIT-compile likelihood functions for performance
Added JAX JIT compilation to estimate_log_joint_mark_intensity with static arguments for improved performance and memory efficiency. Updated block_estimate_log_joint_mark_intensity to use a JIT-compiled buffer-donating update function. Added documentation notes about JIT usage and buffer donation.
1 parent 604bde5 commit 03445d0

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

src/non_local_detector/likelihoods/clusterless_kde_log.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,18 @@ def estimate_log_joint_mark_intensity(
576576
-------
577577
log_joint_mark_intensity : jnp.ndarray, shape (n_decoding_spikes, n_position_bins)
578578
579+
Notes
580+
-----
581+
This function is JIT-compiled automatically when called from higher-level functions.
582+
For manual JIT compilation with custom settings, use:
583+
584+
jitted_fn = jax.jit(
585+
estimate_log_joint_mark_intensity,
586+
static_argnames=('use_gemm', 'pos_tile_size', 'enc_tile_size', 'use_streaming')
587+
)
588+
589+
Buffer donation can further reduce memory usage for the _update_block helper (already applied).
590+
579591
"""
580592
n_encoding_spikes = encoding_spike_waveform_features.shape[0]
581593

@@ -761,6 +773,14 @@ def process_pos_tile(
761773
return log_joint
762774

763775

776+
# JIT-compile with static arguments for performance
777+
# This allows JAX to specialize the function for different tile sizes and modes
778+
estimate_log_joint_mark_intensity = jax.jit(
779+
estimate_log_joint_mark_intensity,
780+
static_argnames=("use_gemm", "pos_tile_size", "enc_tile_size", "use_streaming"),
781+
)
782+
783+
764784
def block_estimate_log_joint_mark_intensity(
765785
decoding_spike_waveform_features: jnp.ndarray,
766786
encoding_spike_waveform_features: jnp.ndarray,
@@ -818,10 +838,14 @@ def block_estimate_log_joint_mark_intensity(
818838
if n_decoding_spikes == 0:
819839
return jnp.full((0, n_position_bins), LOG_EPS)
820840

821-
# Use dynamic_update_slice to build output
822-
# Note: JAX will JIT-compile this in the calling context
823-
def _update_block(out_array, block_result, start_idx):
824-
return jax.lax.dynamic_update_slice(out_array, block_result, (start_idx, 0))
841+
# Create JIT-compiled update function with buffer donation
842+
# donate_argnums=(0,) allows JAX to reuse the output buffer in-place
843+
_update_block = jax.jit(
844+
lambda out_array, block_result, start_idx: jax.lax.dynamic_update_slice(
845+
out_array, block_result, (start_idx, 0)
846+
),
847+
donate_argnums=(0,),
848+
)
825849

826850
out = jnp.zeros((n_decoding_spikes, n_position_bins))
827851
for start_ind in range(0, n_decoding_spikes, block_size):

0 commit comments

Comments
 (0)