Skip to content

Optimize CPU operand materialization with blocked pooled copies - #56

Merged
GiggleLiu merged 1 commit into
tensor4all:mainfrom
exAClior:perf/issue-55-materialization
Jul 21, 2026
Merged

Optimize CPU operand materialization with blocked pooled copies#56
GiggleLiu merged 1 commit into
tensor4all:mainfrom
exAClior:perf/issue-55-materialization

Conversation

@exAClior

Copy link
Copy Markdown
Contributor

Summary

  • replace the scalar coordinate-carry materializer with strided-perm's blocked copy
  • return the packed destination directly instead of cloning the full payload
  • reuse initialized packing storage through a bounded thread-local best-fit pool
  • guard zero-element layouts before entering the blocked kernel
  • add scalar-reference permutation, zero-size, bounded-allocation, and buffer-reuse tests

Closes #55.

Why this is separate

This is stacked on #54 because the profiling was discovered while validating its Complex64 contractions, but the fix is generic CPU operand materialization. It is deliberately a separate commit and PR: no Tenferro dependency, benchmark harness, profiling source, Makefile change, or run artifact is included.

Until #54 lands, GitHub will show its prerequisite commits in this PR's main-branch comparison. The materialization change itself is the single commit cee9fae and touches only:

  • Cargo.toml
  • src/backend/cpu/buffer_pool.rs
  • src/backend/cpu/contract.rs

Evidence

The pre-fix right-layout pack took 137/424 us at chi=64, versus Tenferro's 16/31 us. Prepacking removed 87%/97% of the public-path gap.

With this patch, the reverse-order all-size run improved OMEinsum by 8-20%. Post-fix OMEinsum/Tenferro ratios were 1.00-1.04x across all six right-layout cases. At chi=64:

Case OMEinsum before OMEinsum after Tenferro after
h1-right 1,053.6 us 906.6 us 878.8 us
h2-right 2,145.5 us 1,760.0 us 1,738.9 us

Warm allocation deltas versus prepacked input fell from 2,359,616/4,718,992 bytes to 800/944 metadata bytes.

The first blocked-copy implementation exposed a SIGSEGV for a zero-sized layout. The final code bypasses the external kernel for numel == 0, with a dedicated regression test; the existing zero-dimension integration test also passes.

Verification

  • make check
    • 162 unit tests passed; 11 ignored
    • 333 integration tests passed
    • 16 doctests passed; 4 ignored
  • cargo package --allow-dirty
  • exhaustive small-rank permutation checks against the scalar reference

Depends on #54.

@exAClior

Copy link
Copy Markdown
Contributor Author

Isolated stacked diff against the #54 head branch: exAClior/omeinsum-rs@perf/complex-gemm...perf/issue-55-materialization — three files, one commit (cee9fae).

@codecov-commenter

codecov-commenter commented Jul 19, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@exAClior
exAClior force-pushed the perf/issue-55-materialization branch from 0b2d127 to 96c15ce Compare July 19, 2026 08:09
@exAClior

Copy link
Copy Markdown
Contributor Author

Scope and memory-retention note: This optimization affects CPU operand materialization across all scalar types and algebra paths, not only large Complex64/TDVP workloads. The blocked copy is expected to help large permutations, but the crossover for tiny permutations has not yet been benchmarked. The thread-local cache may retain up to two 64 MiB buffers per scalar type per thread—a theoretical 128 MiB/type/thread high-water mark. The current contraction path generally needs only one packing buffer at a time, so please advise whether the pool should retain only one buffer or use a lower cap before merge.

@exAClior
exAClior marked this pull request as ready for review July 19, 2026 08:41
@GiggleLiu
GiggleLiu merged commit dcd63f9 into tensor4all:main Jul 21, 2026
6 checks passed
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.

Optimize CPU operand materialization with blocked pooled copies

3 participants