Skip to content

Fix BatchLM vmap+jacfwd failures caused by in-place tensor mutations#301

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-batchlm-inplace-operation-issue
Draft

Fix BatchLM vmap+jacfwd failures caused by in-place tensor mutations#301
Copilot wants to merge 4 commits intomainfrom
copilot/fix-batchlm-inplace-operation-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 4, 2026

BatchLM uses vmap(jacfwd(...)) to compute batched Jacobians. PyTorch's forward-mode AD cannot trace through in-place tensor mutations, causing failures when any integrate_mode other than "none" was used.

Core fix: backend index operations

_fill_at_indices_torch and _add_at_indices_torch now use torch.index_put (functional, out-of-place) for LongTensor indices — the path taken by topk-based adaptive integration. Bool/tuple/slice indices fall back to clone() + in-place, which preserves compatibility for non-differentiated callers.

# Before — breaks vmap+jacfwd
def _fill_at_indices_torch(self, array, indices, values):
    array[indices] = values
    return array

# After — compatible with vmap+jacfwd for LongTensor indices
def _fill_at_indices_torch(self, array, indices, values):
    if isinstance(indices, self.module.Tensor) and indices.dtype != self.module.bool:
        return self.module.index_put(array, (indices,), values)
    result = array.clone()
    result[indices] = values
    return result

Sweep for bool-mask in-place operations in brightness functions

vmap additionally rejects operations that produce dynamic shapes (bool-indexed selection). Fixed in three places:

  • spline.py — extrapolation boundary: replaced fill_at_indices(I, R > profR[-1], 0) with backend.where(R > profR[-1], zeros, I).
  • WedgeMixin.polar_model — replaced add_at_indices(model, bool_mask, iradial_model(s, R[mask])) with model + where(mask, iradial_model(s, R), zeros). The radial model is now evaluated over the full pixel array; the mask is applied via where to maintain static shapes.
  • RayMixin.polar_model — same pattern for cosine-weighted segment accumulation.

New tests

Added BatchLM-specific tests in test_fit.py covering:

  • All integrate_mode values: "none", "bright", "curvature"
  • Common sampling_mode values: "midpoint", "simpsons", "quad:3"
  • Images with rotated CD matrices (non-trivial WCS orientations)
  • Poisson likelihood path

Copilot AI linked an issue Apr 4, 2026 that may be closed by this pull request
Copilot AI and others added 3 commits April 4, 2026 02:56
Agent-Logs-Url: https://github.com/Autostronomy/AstroPhot/sessions/d13bb976-bdfb-4fba-8140-58687f11e916

Co-authored-by: ConnorStoneAstro <78555321+ConnorStoneAstro@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Autostronomy/AstroPhot/sessions/d13bb976-bdfb-4fba-8140-58687f11e916

Co-authored-by: ConnorStoneAstro <78555321+ConnorStoneAstro@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix in-place operations handling in BatchLM Fix BatchLM vmap+jacfwd failures caused by in-place tensor mutations Apr 4, 2026
Copilot AI requested a review from ConnorStoneAstro April 4, 2026 03:15
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 4, 2026

Codecov Report

❌ Patch coverage is 86.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.48%. Comparing base (f9c9463) to head (30b6dd5).

Files with missing lines Patch % Lines
astrophot/backend_obj.py 69.23% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #301      +/-   ##
==========================================
+ Coverage   91.23%   91.48%   +0.25%     
==========================================
  Files         113      114       +1     
  Lines        6198     6216      +18     
==========================================
+ Hits         5655     5687      +32     
+ Misses        543      529      -14     
Flag Coverage Δ
unittests 91.48% <86.66%> (+0.25%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

updates for BatchLM

2 participants