Optimize direct PZ two-jet squared integration - #189
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d4d3dd783e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def integrate_pz_twojet_squared( | ||
| jet: PZTwoJet, cell: PZIntegrationCell, integrand_kind: TwoJetIntegrandKind | ||
| ): |
There was a problem hiding this comment.
Route adaptive norm cells through the direct integrator
When applications call model.pz_l2norm(...) or model.pz_sobolev_norm(...), _pz_adaptive_squared_integral still reaches _evaluate_squared_contribution_cache, which constructs _squared_twojet_integrand(jet, integrand_kind) and passes it to integrate_over_cell; it never calls this new helper. Consequently, the primary adaptive API—and the notebook's PZ API/trace benchmarks—continues materializing the expensive squared PZ for every active cell, so the intended optimization is absent from the path where repeated Hessian convolution matters most.
AGENTS.md reference: AGENTS.md:L45-L47
Useful? React with 👍 / 👎.
| if not isinstance(density, Real) or not isfinite(float(density)) or float(density) < 0.0: | ||
| return explicit_fallback() | ||
|
|
||
| num_noise, noise_kinds = _validate_twojet_metadata(jet) |
There was a problem hiding this comment.
Validate only components used by the selected norm
When a manually constructed PZTwoJet has valid Y metadata but unused J or H components with different num_noise metadata, pz_twojet_l2_norm(jet, cell) now raises here even though the previous explicit L2 path only squared Y and succeeded; the same problem affects W12 when only H differs. Validate metadata after selecting the coordinates, or limit validation to the components required by integrand_kind, so supplying a cell does not change otherwise supported public behavior.
AGENTS.md reference: AGENTS.md:L30-L32
Useful? React with 👍 / 👎.
Motivation
Description
integrate_pz_twojet_squared(jet, cell, integrand_kind)and private helpers insrc/intervalnets/pz_integration.pythat select/flatten two-jet coordinates, build a deterministically sorted union support, form a weighted coefficient matrix, compute the weighted Gram and center cross terms, traverse unordered exponent pairs, canonicalize contributions before absolute-value collapse, route pointwise and retained terms by domain moments, and return the result viaIntegratedPZResult.interval_enclosure(); fallback to the explicit-squared pipeline whencell.jacobian_densityis unsupported.pz_twojet_l2_norm,pz_twojet_w12_norm, andpz_twojet_w22_norminsrc/intervalnets/pz_norms.pyuse the new direct integration path when a supportedPZIntegrationCellis supplied while retaining the explicit integrand helpers for diagnostics andoutput="pz".tests/test_pz_integration.pyandtests/test_pz_norms.pycovering direct vs explicit equivalence forl2,w12,w22, unequal tensor supports, cancellation examples, odd-domain pointwise terms, Hessian upper-triangle weighting, constants/zero jets, and the polynomial-density fallback; update the affine two-jet benchmark notebook to call the optimized public norm API and record separate phase timings.Testing
pytest -q tests/test_polynomial_zonotope.py tests/test_pz_integration.py tests/test_pz_norms.py, which passed.pytest -q, which completed successfully (all automated tests passed).pytest -q tests/test_pz_integration.py tests/test_pz_norms.pyandpython -m compileall -q src tests, all succeeded; test runs emitted a benign PyTorch/NumPy initialization warning but no failures.Codex Task