Skip to content

Port do_opt_block: choose block_size per host and data instead of a constant #232

Closed

Description

@neuromechanist

Split out of #216, which raised the static default to 8192 (#229) and deferred the auto-tune.

Fortran picks block_size per host rather than shipping a constant: do_opt_block sweeps 128-1024 in steps of 128 (blk_min/blk_step/blk_max) and keeps the fastest. pamica exposes do_opt_block, blk_min, blk_step, blk_max in numpy_impl but never implements the search, and AMICATorchNG has no equivalent at all.

The reference implementation of this has been broken for some time, so this is not a port to copy — it is a feature to get right. Fortran-faithfulness is not the bar here; not exiting is.

Why it is still worth doing after #229

8192 is a compromise. The measured optimum on the bundled 32-channel sample was 16384-32768 on CPU and a single block on MPS; 8192 was chosen because peak block memory scales with it and the default has to stay safe at high channel counts, where the best value is much smaller. A host- and data-aware search would take the part of the speedup a fixed constant has to leave behind — the gap between 8192 and the per-device optimum was 16-60% depending on backend.

Fortran's own 128-1024 range is far below where any pamica backend peaks, so the sweep bounds should be re-derived rather than copied.

Design requirement: degrade, never exit

The search walks upward into larger blocks, which is exactly where it runs out of memory — and memory limits are the reason the reference version fails. A candidate that cannot allocate must fall back to the last block size that worked and continue the fit, not abort. Concretely:

  • Catch the allocation failure per candidate (torch.cuda.OutOfMemoryError, MPS/MLX equivalents, MemoryError on CPU), free the partial allocation, and stop searching upward.
  • Keep the largest candidate that completed a timed step; never leave the model holding a size that has already failed.
  • A failed candidate is expected, not exceptional: log it at debug level and carry on. It must not surface as a warning that looks like a defect.
  • Test it by driving the search into a real allocation failure rather than a simulated one.

This also argues for searching upward from a known-good size rather than downward from an optimistic one, so there is always a valid fallback in hand.

Scope

  • Time a few candidate block sizes on the actual data and device at the start of fit, and keep the best
  • Bound the search by available memory, not just a fixed range: the useful maximum falls as channels and models rise
  • Fall back to the last working size on allocation failure, per above
  • Off by default. It perturbs the trajectory at the ~1e-6 level like any block-size change, so a parity run must be able to pin the value (see Fortran reference binary is not reproducible; single-run parity gate is unstable #228 on configurations that must match on both sides)
  • Decide whether to honor numpy_impl's existing do_opt_block/blk_* parameters or replace them

Part of #230.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions