Skip to content

Better BTA without allocation - #117

Merged
dance858 merged 4 commits into
mainfrom
btda-fold-diag
Sep 5, 2026
Merged

Better BTA without allocation#117
dance858 merged 4 commits into
mainfrom
btda-fold-diag

Conversation

@dance858

@dance858 dance858 commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

This PR improves the matmul functionality by removing one intermediate allocation for BTDA involving permuted dense. It's been on my todo list for a long time, so it's very nice to get it done.

Claude below:

Removes the last "(Daniel) must remove this allocation. Very important" TODOs: BTDA_pd_pd_fill_values and BTDA_pd_spd_fill_values allocated and freed a diag(d) @ A intermediate on every Hessian evaluation, and the blockwise spd BTDA variants multiplied that by n_blocks.

Each BTA kernel is now a shared core with an optional d (NULL = identity) folded into the existing row-gather into pre-sized kernel_dwork — the pattern BTDA_pd_csc/BTDA_csc_pd already used — so the BTDA entry points are thin wrappers and no fill path allocates. The blockwise BTDA_spd_pd/BTDA_spd_spd inherit the fix automatically.

BTDA_pd_pd_fill_values and BTDA_pd_spd_fill_values allocated, filled,
and freed a diag(d) @ A intermediate on every Hessian evaluation (the
"must remove this allocation. Very important" TODOs), and the blockwise
BTDA_spd_pd/spd_spd variants compounded that to n_blocks temps per
fill. Restructure each BTA kernel into a shared core taking an optional
d (NULL = identity) that scales A's rows by d[global_row] while they
are being gathered into the pre-sized kernel_dwork scratch — the same
pattern BTDA_pd_csc/BTDA_csc_pd already use — so the BTDA entry points
become thin wrappers and no fill allocates. The pd_pd fast path
(identical row_perms) scales into A->kernel_dwork, which
BTA_pd_pd_alloc already sizes to exactly m0*n0 on that path.

New tests: direct BTDA_pd_pd correctness on both the matching-row_perm
and gather paths against the diag-decomposition oracle; a
no-transient-allocation regression test over pd_pd (both paths),
pd_spd, and blockwise spd_pd using the tracked allocator's peak-bytes
counter (fails on the previous kernels); and an end-to-end multiply
Hessian test with dense operators — the first wsum_hess coverage of
multiply's pd/pd dispatch.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRnTPZsCXK6x2PSGUzanmg
@dance858 dance858 changed the title Fold diag(d) into the BTA gathers, delete the per-eval DA temps [WIP] Better BTA without allocation Sep 1, 2026
dance858 and others added 3 commits August 31, 2026 20:19
Replace the hand-written scale-copy loops with an unconditional
memcpy gather followed by a per-row cblas_dscal pass when d is given,
matching the existing DA_pd_fill_values / ATDA_pd_fill_values idiom.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRnTPZsCXK6x2PSGUzanmg
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRnTPZsCXK6x2PSGUzanmg
…CK_MEMORY

Resolve the test_matmul_dispatchers.h conflict by keeping both #115's
transpose-cache test and the branch's BTDA no-transient-alloc test.

#118 made g_allocated_bytes / g_peak_bytes exist only under
SP_TRACK_MEMORY, so the no-alloc test (which reads those counters) and
its registration are now wrapped in #ifdef SP_TRACK_MEMORY. Default
build: 448 tests pass; -DSP_TRACK_MEMORY=ON: 449 tests pass.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016ATepXjts1gBX485mSguzk
@dance858 dance858 changed the title [WIP] Better BTA without allocation Better BTA without allocation Sep 5, 2026
@dance858
dance858 merged commit d8cb9b8 into main Sep 5, 2026
12 checks passed
@dance858
dance858 deleted the btda-fold-diag branch September 5, 2026 15:23
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