Skip to content

Commit dedde5a

Browse files
Jammy2211claude
andcommitted
perf(numba): split curvature-matrix F assembly into per-block helpers (#505)
`_curvature_matrix_func_list_and_mapper` assembled all three blocks of F in a single pass, so the breakdown harness could only time F as one step. Split the two loops out into private helpers that write their block into the `curvature_matrix` they are passed and return it: - `_curvature_matrix_mapper_func_blocks_from` — the mapper x linear-func loop - `_curvature_matrix_func_func_blocks_from` — the linear-func x linear-func loop `_curvature_matrix_mapper_diag` (the mapper x mapper block) already existed. `_curvature_matrix_func_list_and_mapper` now composes the three in the same order, and the `curvature_matrix` cached property is untouched (mirror + diag add unchanged). Pure code motion — no behaviour change, bit-identical output. `pytest test_autoarray/inversion`: 392 passed. Step 0 of #505, so autolens_profiling can time each block separately. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SqrSVGPrFcUB1vvDsoTw3n
1 parent caabe2d commit dedde5a

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

  • autoarray/inversion/inversion/imaging_numba

‎autoarray/inversion/inversion/imaging_numba/sparse.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,50 @@ def _curvature_matrix_func_list_and_mapper(self) -> np.ndarray:
505505
curvature matrix given by equation (4) and the letter F.
506506
507507
This function computes the diagonal terms of F using the sparse_operator formalism.
508+
509+
The three blocks of F are assembled by separate private helpers, so that each block can be
510+
computed (and therefore profiled) on its own:
511+
512+
- the mapper x mapper block, `_curvature_matrix_mapper_diag` (via `_curvature_matrix_multi_mapper`);
513+
- the mapper x linear-func blocks, `_curvature_matrix_mapper_func_blocks_from`;
514+
- the linear-func x linear-func blocks, `_curvature_matrix_func_func_blocks_from`.
515+
516+
The helpers write into the `curvature_matrix` they are passed and return it, so composing them
517+
in this order is exactly the single-pass assembly they replaced.
508518
"""
509519

510520
curvature_matrix = self._curvature_matrix_multi_mapper
511521

522+
curvature_matrix = self._curvature_matrix_mapper_func_blocks_from(
523+
curvature_matrix=curvature_matrix
524+
)
525+
526+
curvature_matrix = self._curvature_matrix_func_func_blocks_from(
527+
curvature_matrix=curvature_matrix
528+
)
529+
530+
return curvature_matrix
531+
532+
def _curvature_matrix_mapper_func_blocks_from(
533+
self, curvature_matrix: np.ndarray
534+
) -> np.ndarray:
535+
"""
536+
Writes the mapper x linear-func off-diagonal blocks of the `curvature_matrix` into the input
537+
matrix, returning it.
538+
539+
Each block contracts a mapper's unique data-to-source-pixel mappings against the PSF-convolved,
540+
noise-weighted curvature vector of a linear function (the dense sliding-window correlation in
541+
`curvature_matrix_off_diags_via_mapper_and_linear_func_curvature_vector_from`).
542+
543+
Only the `[mapper, linear_func]` blocks are written; their transposes are filled in by the
544+
global mirror applied in `curvature_matrix`.
545+
546+
Parameters
547+
----------
548+
curvature_matrix
549+
The (total_params, total_params) curvature matrix the blocks are written into.
550+
"""
551+
512552
mapper_list = self.cls_list_from(cls=Mapper)
513553
mapper_param_range_list = self.param_range_list_from(cls=Mapper)
514554

@@ -544,6 +584,28 @@ def _curvature_matrix_func_list_and_mapper(self) -> np.ndarray:
544584
linear_func_param_range[0] : linear_func_param_range[1],
545585
] = off_diag
546586

587+
return curvature_matrix
588+
589+
def _curvature_matrix_func_func_blocks_from(
590+
self, curvature_matrix: np.ndarray
591+
) -> np.ndarray:
592+
"""
593+
Writes the linear-func x linear-func blocks of the `curvature_matrix` into the input matrix,
594+
returning it.
595+
596+
Each block is a BLAS `dot` of two noise-weighted operated mapping matrices.
597+
598+
Parameters
599+
----------
600+
curvature_matrix
601+
The (total_params, total_params) curvature matrix the blocks are written into.
602+
"""
603+
604+
linear_func_list = self.cls_list_from(cls=AbstractLinearObjFuncList)
605+
linear_func_param_range_list = self.param_range_list_from(
606+
cls=AbstractLinearObjFuncList
607+
)
608+
547609
# The linear func x linear func block is symmetric, so each weighted matrix is
548610
# formed once and only the upper triangle of blocks is computed, with the
549611
# mirrored block set from the transpose.

0 commit comments

Comments
 (0)