8000 perf(gasci): compact sparse alpha streams in RDM contractions by yideng2025 · Pull Request #246 · pyscf/pyscf-forge · GitHub
[go: up one dir, main page]

Skip to content

perf(gasci): compact sparse alpha streams in RDM contractions - #246

Merged
MatthewRHermes merged 6 commits into
pyscf:masterfrom
yideng2025:gas-rdm-sparse-ab-pr
Dec 2, 2026
Merged

MatthewRHermes merged 6 commits into
pyscf:masterfrom
yideng2025:gas-rdm-sparse-ab-pr

Conversation

@yideng2025
@yideng2025 yideng2025 commented Dec 1, 2026 •
Copy link
Copy Markdown
Contributor

Pack only contributing alpha rows in eligible AB RDM batches and reuse the existing dot kernels. Supports ordinary and transition RDMs and keeps the dense/direct fallbacks. The Python API is unchanged.

Behaviour changes

  • Errors from the AB RDM path other than GAS_ERR_MEMORY now propagate to the caller. Previously, any error fell back silently to the direct path. GAS_ERR_MEMORY still falls back.
  • New C entry point fci_rdm_gas_plan_set_ab_compact_limits(plan, scratch_elems, min_lanes) overrides the compact-path scratch budget and minimum-work cutoff on one plan. It exists so that tests can force the beta-tiled and multi-consumer branches. Production defaults are unchanged.

Benchmarks

Full 1+2-RDM timings with plan reuse, on 32 bound physical AMD EPYC 7713 cores. Baseline is d8cbdbd and PR is 247fddf. All builds used GCC 13.2.0 with -O3 -msse3 -fopenmp. The AVX2 builds added only -mavx2, without -march=native, -mfma, or fast-math.

GAS partition / (Nα,Nβ) RDM Baseline SSE (s) Baseline AVX2 (s) PR SSE (s) PR AVX2 (s) Speedup, both AVX2
12/10/12, (22,12), h=p=2 Ordinary 8.293 6.806 3.343 2.799 2.43×
12/10/12, (22,12), h=p=2 Transition 8.466 6.821 3.351 2.791 2.44×
5/5/5/5, (16,4), all-pair 1e CT Ordinary 2.772 2.062 1.553 1.101 1.87×
5/5/5/5, (16,4), all-pair 1e CT Transition 2.692 2.040 1.562 1.099 1.86×

Each value is the median of two independent process medians, with three repeat calls per process in balanced forward/reverse order. Timings include output allocation and spin summation and exclude plan construction. Inputs were identical across builds. All 128 calls passed numerical checks, and the maximum cross-version absolute difference was 2.66e-15.

An earlier SSE-only measurement on 32 physical AMD EPYC 9534 cores, taken before the AA contraction PR (#243) was integrated, gave 2.15× (ordinary) and 2.21× (transition) on the 12/10/12 case. Polyacetylene with two-orbital GAS subspaces showed no stable benefit. Performance depends on the GAS partition and occupation constraints, and these speedups apply to the tested configurations.

Testing

test_sparse_ab_rdms covers ordinary and transition RDMs against direct_spin1. It uses the plan-local limits to force beta tiling, tail tiles, bra reuse, and repacking for multiple beta consumers. Independent full-FCI comparisons and GASCI/GASSCF regressions passed, including plan reuse, thread-count changes, RDM tests without OpenMP, and targeted ASan/UBSan checks.

Description consolidated from the author's comments by MatthewRHermes with the help of Claude Code.

@MatthewRHermes
Copy link
Copy Markdown
Collaborator

Thanks, Yi. The compact path looks correct to me, and the speedup reproduces (see below). Before merging I'd like the following:

  1. Test coverage of the new branches. At the shipped thresholds, test_sparse_ab_rdms reaches rdm_try_compact_ab but never reaches the beta-tiled branch (room < nb0) or a batch with npair > 1, so the b0 == 0u || npair != 1u repacking logic is untested. Could you add a test hook in the style of fci_rdm_gas_set_abba_t1_bytes that lowers the scratch limit and the lanes cutoff, and use it in the test so that every branch runs?
  2. Named, justified thresholds. 524288u, 1048576u, nactive < 8u, and the 50% density cutoff are bare literals. Please make them named constants, each with a one-line comment on how it was chosen. Also, limit doesn't depend on abba_t1_target_bytes or RDM_WORKSPACE_MAX_ELEMS. Is that intentional?
  3. Build flags in the benchmark. The rdm_dot* kernels have __AVX2__ branches, which the default build (BUILD_MARCH_NATIVE=off) compiles out. Please say in the description how the library was built for your timings.
  4. Minor: please add a comment where the int32_t op_map buffer is reused as the count/start/next arrays and index is stored in the double arena. Also, the description should mention that AB errors other than GAS_ERR_MEMORY now propagate to the caller instead of falling back silently. I think that change is an improvement, but it changes behaviour.

A broader request for future perf PRs on GASCI: please start with an end-to-end profile of a GASSCF run (or at least the time split between sigma and RDM) to show which kernel matters. For the (34e,34o) 12/10/12 case below, one 1+2-RDM call costs about 2.3 sigma calls on master. So this PR is worthwhile, but its effect on a whole GASSCF run is much smaller than 2×. With that context, I'd rather review one PR with a stated end-to-end target than a series of individually tuned kernel paths. The AB RDM now has four code paths, and each one has to be covered and maintained.

Checks run by Claude Code (not independently verified by me)

  • Built libfci_gas.so from e7a4dd1 and from master d8cbdbd with the default flags (-O2, no -march, MKL sequential). Ran pyscf/mcscf/test/test_gasci.py on the PR: 39 passed.
  • Temporarily instrumented rdm_try_compact_ab with counters, then reran test_gasci.py:
    • At the shipped thresholds, the new test enters the compact path 325 times, with 0 beta-tiled batches and 0 batches with npair > 1.
    • With the lanes cutoff set to 0 and the scratch limit lowered to 400 or 60 elements, the full file hits the beta-tiled branch (up to about 13k times) and npair > 1 (180–240 times). All 39 tests still pass, so the new code is numerically correct when forced through these branches.
  • Benchmark: gas_orbs=(12,10,12), RAS max_holes=max_particles=2, nelec=(22,12) (11,535,217 determinants), random unit vectors, 6 OpenMP threads, median of 3 calls after one warm-up call:
master PR speedup
make_rdm12s (ordinary) 159.9 s 64.1 s 2.49×
make_rdm12s (transition) 162.5 s 64.1 s 2.53×
sigma (GasContractPlan.contract) 68.3 s (unchanged code)

Max abs difference in dm2ab between master and the PR: 7e-15.


This comment was drafted with the help of Claude Code. The checks above were run by Claude Code and have not been re-run by me.

@yideng2025
Copy link
Copy Markdown
Contributor Author

Hi Matt, updated in 50a89a1, a51cd58, and 247fddf:

  • Added a plan-local test hook and small FCI cases covering beta tiling, tail handling, bra reuse, and multiple-consumer repacking. Production defaults and the public Python API are unchanged.
  • Named the thresholds and added comments. These are empirical cutoffs; the compact scratch budget is separate from the dense T1 budget because they cover different buffers.
  • The original timings used GCC 13.2.0 with -O3 -msse3 -fopenmp, without -march=native or -mavx2. The AVX2 code paths were not compiled in.
  • Documented buffer reuse. Non-memory AB errors now propagate instead of silently falling back.

Normal GASCI/GASSCF regressions, the additional RDM benchmark matrix, and targeted ASan/UBSan checks passed.

Agreed on profiling a representative GASSCF run before choosing the next optimization.

@yideng2025
Copy link
Copy Markdown
Contributor Author

One additional 8000 check is still pending: a controlled comparison with AVX2 enabled. Since I normally enable AVX2 for production calculations, I’ll benchmark both master and this PR with and without -mavx2, keeping the other settings fixed. This will show whether the sparse AB speedup holds with AVX2 enabled. I’ll post the results separately; the timings above are from builds without AVX2.

@yideng2025
Copy link
Copy Markdown
Contributor Author

The AVX2 comparison is now complete. Both versions benefit from AVX2, and the sparse AB speedup remains.

Full 1+2-RDM timings on 32 bound physical AMD EPYC 7713 cores:

GAS partition / (Nα,Nβ) RDM Baseline SSE (s) Baseline AVX2 (s) PR SSE (s) PR AVX2 (s) Speedup, both AVX2
12/10/12, (22,12), h=p=2 Ordinary 8.293 6.806 3.343 2.799 2.43×
12/10/12, (22,12), h=p=2 Transition 8.466 6.821 3.351 2.791 2.44×
5/5/5/5, (16,4), all-pair 1e CT Ordinary 2.772 2.062 1.553 1.101 1.87×
5/5/5/5, (16,4), all-pair 1e CT Transition 2.692 2.040 1.562 1.099 1.86×

Baseline is d8cbdbd; PR is 247fddf. All builds used GCC 13.2.0 with -O3 -msse3 -fopenmp; AVX2 builds only added -mavx2, without -march=native, -mfma, or fast-math.

Each value is the median of two independent process medians, with three repeat calls per process and balanced forward/reverse order. Timings include output allocation and spin summation, excluding plan construction. Inputs were identical across builds.

All 128 calls passed numerical checks; the maximum cross-version absolute difference was 2.66e-15. Performance depends on the GAS partition and occupation constraints; these speedups apply to the tested configurations.

@MatthewRHermes
MatthewRHermes merged commit 808ec98 into pyscf:master Dec 2, 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.

2 participants

0