Skip to content

Build the matrices of a linear instrument one wavelength at a time - #54

Merged
roytsmart merged 2 commits into
mainfrom
feature/streamed-operators
Nov 24, 2026
Merged

roytsmart merged 2 commits into
mainfrom
feature/streamed-operators

Conversation

@roytsmart

@roytsmart roytsmart commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

AbstractLinearInstrument assembled its forward matrix from the weights of the whole scene at once, and transposed it into the backprojection on first use. At the production grid of ESIS that fits two spectral lines on a 128 GB host, but not the seven of the flight model, which would peak at about 134 GB.

This PR builds both matrices together with regridding.RegridderBuilder (sun-data/regridding#75, released in regridding 3.8.0), in two passes over blocks of weights:

  1. The first pass computes each block and counts its entries.
  2. The second computes it again and places it, with its grids, in the forward matrix and in its conservative transpose.

A block is every channel of one wavelength of one spectral line, so the weights of the whole scene are never held at once. This is the host half of M6 of the roadmap.

Changes

  • IdealInstrument and OptikaInstrument provide the weights, grids and input weights of a block on demand, through _weights_block(), _grids_weights() and _weights_input().
    • IdealInstrument slices its coordinates and point-spread function. An input with one element along the wavelengths or the spectral lines is kept whole, since it broadcasts against every block.
    • OptikaInstrument asks optika for the weights of that block's part of the scene only. Every block holds whole any axis along which the optika system varies, such as a distortion for each spectral line, since the system is evaluated whole for each block.
  • Operators gets the backprojection as a field, built in the same pass. It no longer keeps the grids it needed to transpose the forward matrix later.
  • _operators.piece(), _operators.unit_weights() and _operators.dimensionless() replace _operators.regridder().
    • piece() turns a block of weights into the arguments of the builder.
    • unit_weights() gives the unit of a block of weights, so that weights in a unit that isn't dimensionless are refused on the first block, before either pass.
    • dimensionless() folds a dimensionless unit, such as a percentage, into the entries of a matrix.
  • A subclass that defines the factors around the operators but not the methods that compute its weights and grids a block at a time gets a reason, and falls back on the named arrays.
  • Any whole-scene weights the instrument has cached for the named arrays are released once the first block shows that the matrices can be built, so the two never have to fit in memory together, and an instrument that falls back keeps them.
  • regridding 3.8 is now required, as the first release with RegridderBuilder.

A block per wavelength, not per line

The first version built a block for each spectral line. At production that was just as fast and peaked just as high (466 s, 54.1 GiB), but a scene of one band is then a single block. The PSF tutorial, which the docs build runs within Read the Docs' 7 GB, then peaked at 6.30 GiB, against 5.21 GiB on main. A block for each wavelength brings it down to 4.76 GiB.

Checks

  • New test test__operators_at_once checks both matrices bit for bit against Regridder.from_weights() and Regridder.transpose_conservative() on the weights of the whole scene, once exact zeros are dropped. It covers every case of IdealInstrument and OptikaInstrument, in float64 and float32.

  • The same comparison covers a point-spread function with one element along the wavelengths and the lines, a coordinate with one element along the lines, and an optika system with a distortion or a field stop for each spectral line.

  • New test test__operators_blocks checks that the weights of each block are computed twice, once in each pass, and that no weights stay cached.

  • Other new tests check that the unit of the weights is refused after one block, that an instrument which falls back keeps its weights, and that a subclass without the block methods gets a reason.

  • On the benchmark problems of the roadmap, MART gives the same images, solutions, ⟨χ²⟩ and iteration counts as main, bit for bit, in float64 and float32:

    Problem Iterations Peak memory, main Peak memory, this PR
    Tutorial 11 0.37 to 0.38 GiB 0.38 to 0.39 GiB
    PSF tutorial 35 5.21 GiB 4.76 GiB
    Tutorial on a 256 by 256 scene 4 1.05 GiB 1.07 GiB
  • At production, over two runs: seven spectral lines of the ESIS flight model at 0.75″ with 24 velocity bins, in float32. The lines were He I 584, O III 600, He II 304 in second order, O IV 608, Mg X 609, Mg X 625 and O V 630.

    • Both matrices took 441 to 475 s to build, the weights included, with a peak of 53.9 to 54.1 GiB.
    • Each matrix has 2.13 billion entries, and the two take 38.7 GiB.
    • One iteration of MART took 5.4 to 6.1 s for one frame.
  • 635 tests pass, with 100% coverage of the two changed modules, and black and ruff are clean.

The bit-for-bit match holds on sensors whose pixels form a uniform lattice, which every OptikaInstrument and every tutorial has. On any other sensor grid, regridding perturbs the grid with a random draw whose shape depends on the grid's, so a block's draw is not a slice of the whole scene's, and the matrices differ from the whole-scene weights by about 3e-7 in the images.

Roadmap

Not in this PR

🤖 Generated with Claude Code

https://claude.ai/code/session_01StcJvVHMXbjJmyFFwf4Pcn

`AbstractLinearInstrument` assembled its forward matrix from the weights
of the whole scene at once, and transposed it into the backprojection on
first use.  At the production grid of ESIS that fits two spectral lines
on a 128 GB host, not the seven of the flight model, which would peak at
about 134 GB.

Both matrices are now built together with `regridding.RegridderBuilder`,
in two passes over blocks of weights: the first computes each block and
counts its entries, the second computes it again and places it, with its
grids, in the forward matrix and in its conservative transpose.  A block
is every channel of one wavelength of one spectral line, so the weights
of the whole scene are never held at once.

- `IdealInstrument` and `OptikaInstrument` give the weights, the grids
  and the input weights of a block on demand (`_weights_block()`,
  `_grids_weights()` and `_weights_input()`).  `OptikaInstrument` asks
  optika for the weights of the scene of the block alone.
- `Operators` takes the backprojection as a field, built in the same
  pass, and no longer keeps the grids to transpose the forward matrix.
- `_operators.piece()` and `_operators.dimensionless()` replace
  `_operators.regridder()`: the first turns a block of weights into the
  arguments of the builder, and the second makes the unit of a matrix
  dimensionless, or says that it cannot.
- Weights the instrument cached for the named arrays are released before
  the matrices are built, so that they need not fit beside them.
- regridding 3.8 is required, the first release with `RegridderBuilder`.

Both matrices are bit for bit what `Regridder.from_weights()` and
`Regridder.transpose_conservative()` give on the weights of the whole
scene, once exact zeros are dropped, in float64 and float32.  MART gives
the same results as before, bit for bit, on the benchmark problems of
the roadmap.  The seven lines of ESIS at production build in 7.4 to 7.9
minutes, with a peak of 58 GB.

The roadmap marks the host half of M6 done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01StcJvVHMXbjJmyFFwf4Pcn
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (44a2f61) to head (dbfd0e3).

Additional details and impacted files
@@            Coverage Diff             @@
##              main       #54    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           29        29            
  Lines         4437      4655   +218     
==========================================
+ Hits          4437      4655   +218     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

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

☔ View full report in Codecov by Harness.
📢 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.

The review of the matrices built a block at a time found these:

- An input with one element along the wavelengths or the spectral lines,
  such as a point-spread function which broadcasts along them, was
  sliced past its end, so every block after the first had no entries,
  without an error.  Such an input is now kept whole along that axis, as
  it broadcasts against the whole instrument, and a vector whose
  components differ in that way is broadcast to its own shape before it
  is sliced.
- `OptikaInstrument` evaluates its whole system for each block, so a
  system which varies along the spectral lines, such as one with a
  distortion for each line, was not sliced with the scene, and the
  instrument fell back on the named arrays, or raised.  Every block now
  holds whole any axis along which the system varies.
- A subclass which defines the factors around the operators, but not
  the methods which compute its weights and their grids a block at a
  time, raised `NotImplementedError`, and now gets a reason.
- Weights in a unit which is not dimensionless are refused from the
  first block, by `_operators.unit_weights()`, instead of after both
  passes, and `_operators.dimensionless()` raises for them.
- Weights which were computed before are released only once the
  operators are known to be assembled, so an instrument which cannot
  provide them keeps its weights for the named arrays.
- `_blocks()` uses `na.ndindex()`, and `_scene_block()` takes any slice
  which `piece()` takes.

The roadmap marks #40 and #41 released in 0.6.0, and gives this pull
request a row of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01StcJvVHMXbjJmyFFwf4Pcn
@roytsmart
roytsmart merged commit dd10b61 into main Nov 24, 2026
12 checks passed
@roytsmart
roytsmart deleted the feature/streamed-operators branch November 24, 2026 03:28
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.

1 participant