perf(decorators): to_grid propagates only an explicit over_sampled; Grid2D.over_sampled short-circuits at sub-size 1 (#514) - #516
Merged
Conversation
) `GridMaker.via_grid_2d` read `result.over_sampled` / `result.over_sampler` through the public properties. When the wrapped result is already a `Grid2D` — which it is for every spherical mass profile, whose `@to_grid` decorated `transformed_to_reference_frame_grid_from` delegates to another `@to_grid` method — that read *materialises* the over sampled grid, running a per-pixel Python loop over the whole mask. On a 15k-pixel HST grid that is ~0.5-1.2 s per deflection-angle call against ~1 ms of actual profile maths, and the value built is mask-derived, so for a translated/rotated grid it is wrong as well as expensive. Nothing reads it. Both call sites now read the private `_over_sampled` / `_over_sampler`, so only a value a caller explicitly passed in propagates (the load-bearing case: `Galaxy.traced_grid_2d_from` and `Tracer.traced_grid_2d_list_from`, which both construct `Grid2D(..., over_sampled=...)`). Anything else is left as `None` for the new grid to compute lazily, if it is ever asked for. `Grid2D.over_sampled` additionally short circuits at a uniform sub size of 1, where every sub pixel is the pixel itself and the over sampled grid is just the slim grid in the same order — equal to the loop's output bit for bit at the default origin, and within 1 ULP of it otherwise. The values are copied so an in-place edit of `over_sampled` cannot write through to the grid. Measured on hst (15361 image pixels, OMP_NUM_THREADS=1), `Grid2D` s/call: IsothermalSph 699.2 ms -> 0.92 ms PowerLawSph 701.0 ms -> 1.37 ms NFWSph 590.9 ms -> 5.79 ms gNFWSph 1.20 s -> 348.6 ms (the remainder is real quadrature) All pinned deflection values still PASS at rtol 1e-6. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HWjPT94MPbEHT45kJmDpDh
This was referenced Sep 2, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Phase 1 of the
numpy-deflections-cpuepic (#514). Two zero-numerics fixes on the grid-decorator path that every mass-profile deflection call goes through:GridMaker.via_grid_2dno longer materialises the over-sampled grid of an already-wrappedGrid2D. It propagatedgetattr(result, "over_sampled")/getattr(result, "over_sampler"); when the decorated body returns aGrid2D(the chained@to_gridmethods of every spherical mass profile in PyAutoGalaxy) thosegetattrs fire the lazy properties, which run a per-pixel Python loop (over_sample_util.grid_2d_slim_over_sampled_via_mask_from: twonp.linspace+ onenp.meshgridper pixel) on every call. The value built that way was also mask-derived, so it ignored the profile's centre and rotation; nothing ever read it. Both sites now read the private_over_sampled/_over_sampler, so only a value the caller explicitly set propagates (the two load-bearing callers,Galaxy.traced_grid_2d_fromandTracer.traced_grid_2d_list_from, set it explicitly and round-trip unchanged).Grid2D.over_sampledshort-circuits at uniform sub-size 1 to a copy of the slim grid as aGrid2DIrregular(the loop's output at sub-size 1 is the slim grid in the same order; with a non-zero mask origin the two differ by at most 1 ULP, 2.8e-16). This skips the same loop for every sub-size-1 grid, e.g. the pixelization grid of every CPU likelihood evaluation.Measured with the new
autolens_profiling/scripts/lens/deflections/cells (hst, 15,361 pixels,OMP_NUM_THREADS=1, directGrid2Dcall, before → after):IsothermalSph699 ms → 0.92 ms,PowerLawSph701 ms → 1.37 ms,NFWSph591 ms → 5.8 ms,gNFWSph1.20 s → 349 ms (the remainder is the MGE, phase 2). Elliptical profiles were never affected (their bodies return a bare array). Every deflection pin passes at rtol 1e-6. Note the direct-Grid2Dpenalty did not reach the likelihood's ray-trace, which wraps each plane inGrid2DIrregularfirst; the likelihood-side gain of this phase is the companion tracer change (PyAutoLens / PyAutoGalaxy PRs).API Changes
None — internal changes only.
Grid2D.over_sampledat uniform sub-size 1 now returns a copy of the slim grid (aGrid2DIrregular, as before) instead of the loop's identical output.See full details below.
Test Plan
test_autoarray: 1362 passed (4 new). Negative pintest__to_grid__does_not_materialise_over_sampled_of_wrapped_gridcontrol-tested: fails onmain, passes here.over_sampled=sentinel survivesvia_grid_2d.Grid2D.over_sampledat sub-size 1 equals the slim grid; at sub-size 2 it has 4× the points and differs.autolens_profiling/scripts/lens/deflections/{total,dark}.py --instrument hst: pins PASSED (rtol 1e-6), timings above.Full API Changes (for automation & release notes)
Changed Behaviour
autoarray.structures.decorators.to_grid.GridMaker.via_grid_2d— propagates only an explicitly-set_over_sampled/_over_samplerfrom a wrappedGrid2Dresult; never triggers the lazy properties.autoarray.structures.grids.uniform_2d.Grid2D.over_sampled— at uniformover_sample_size == 1returns aGrid2DIrregularcopy of the slim grid without running the per-pixel over-sampling loop (≤ 1 ULP from the loop's output for a non-zero mask origin; identical otherwise).Generated by the PyAutoLabs agent workflow. Epic ledger:
PyAutoMind/draft/feature/autogalaxy/numpy_deflections_cpu_speedup.md. Companion PRs: PyAutoGalaxy + PyAutoLens (tracer double trace at sub-size 1), autolens_profiling (scripts/lens/measurement package + before/after).