From d485a45251b1066ff0b126c801bc64d6bb28710c Mon Sep 17 00:00:00 2001 From: Ajay Date: Mon, 6 Jul 2026 23:17:34 -0400 Subject: [PATCH 01/16] cost_analysis: add driver-based cost-analysis utility A standalone utility that estimates the cost of evaluating a serialized tensor equation. Reads a JSON driver (space sizes + equation files), strips the (anti)symmetrizer, flattens, then per summand optimizes and binarizes once, reusing that tree for the intermediate catalog, cache simulation, and --dump_tree, mirroring MPQC's SeQuantEngine. Emits a Markdown report on largest intermediates, most expensive contractions, shape census, peak storage, FLOPs, and a gated cache simulation. Includes a README and CCSD/DF example drivers. --- utilities/CMakeLists.txt | 1 + utilities/cost_analysis/CMakeLists.txt | 10 + utilities/cost_analysis/README.md | 83 ++++ utilities/cost_analysis/cost_analysis.cpp | 367 ++++++++++++++++++ utilities/cost_analysis/cost_analysis.hpp | 97 +++++ utilities/cost_analysis/examples/ccsd_r2.inp | 1 + utilities/cost_analysis/examples/ccsd_r2.json | 29 ++ utilities/cost_analysis/examples/df_r1.inp | 1 + utilities/cost_analysis/examples/df_r1.json | 8 + utilities/cost_analysis/report.cpp | 144 +++++++ utilities/cost_analysis/report.hpp | 22 ++ 11 files changed, 763 insertions(+) create mode 100644 utilities/cost_analysis/CMakeLists.txt create mode 100644 utilities/cost_analysis/README.md create mode 100644 utilities/cost_analysis/cost_analysis.cpp create mode 100644 utilities/cost_analysis/cost_analysis.hpp create mode 100644 utilities/cost_analysis/examples/ccsd_r2.inp create mode 100644 utilities/cost_analysis/examples/ccsd_r2.json create mode 100644 utilities/cost_analysis/examples/df_r1.inp create mode 100644 utilities/cost_analysis/examples/df_r1.json create mode 100644 utilities/cost_analysis/report.cpp create mode 100644 utilities/cost_analysis/report.hpp diff --git a/utilities/CMakeLists.txt b/utilities/CMakeLists.txt index 2dd0cd05fb..76b6aad338 100644 --- a/utilities/CMakeLists.txt +++ b/utilities/CMakeLists.txt @@ -12,3 +12,4 @@ foreach(current IN LISTS EXECUTABLE_SOURCES) endforeach() add_subdirectory(external-interface) +add_subdirectory(cost_analysis) diff --git a/utilities/cost_analysis/CMakeLists.txt b/utilities/cost_analysis/CMakeLists.txt new file mode 100644 index 0000000000..78c4fc8deb --- /dev/null +++ b/utilities/cost_analysis/CMakeLists.txt @@ -0,0 +1,10 @@ +include(FindOrFetchCLI11) +include(FindOrFetchJSON) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") + +add_executable(cost_analysis cost_analysis.cpp report.cpp) +set_target_properties(cost_analysis PROPERTIES CXX_SCAN_FOR_MODULES OFF) +target_link_libraries(cost_analysis PRIVATE + SeQuant::mbpt nlohmann_json::nlohmann_json CLI11::CLI11) +target_set_warning_flags(cost_analysis) diff --git a/utilities/cost_analysis/README.md b/utilities/cost_analysis/README.md new file mode 100644 index 0000000000..bb1607f411 --- /dev/null +++ b/utilities/cost_analysis/README.md @@ -0,0 +1,83 @@ +# Cost analysis + +A standalone utility that estimates the cost of evaluating a tensor equation +without running it. It reads a serialized equation, optimizes and binarizes it, +then writes a Markdown report on the largest intermediates, the most expensive +contractions, peak storage, total FLOPs, and (optionally) cache reuse. All +figures are symbolic `AsyCost` polynomials in the index-space sizes, evaluated +at the sizes given in the driver. + +The optimize/binarize step mirrors the choices MPQC's `SeQuantEngine` makes +(symmetrizer stripping, per-summand factorization, volatile-leaf weighting, +cache gating), so the estimates track what a real MPQC evaluation would do. + +## Build & run + +Built as part of SeQuant (target `cost_analysis`). It takes a single argument, a +[JSON](https://www.json.org/) _driver_: + +```bash +cost_analysis --driver examples/ccsd_r2.json +``` + +Paths inside the driver (`equation_file`, `output.path`) are resolved relative +to the driver's own directory. + +## Driver + +| Block | Key | Meaning | Default | +|---|---|---|---| +| `context` | `spbasis` | `spinor` or `spinfree` | `spinor` | +| | `field` | `real` or `complex` | `complex` | +| | `convention` | registry: `min_sr`/`sr`/`mr`/`f12` | `sr` | +| | `aux` | factorization spaces to register: `["df"]` (Κ) and/or `["thc"]` (L) | none | +| `sizes` | `