Skip to content

Improve the performance of scalar models by detecting them - #32

Draft
Transurgeon wants to merge 16 commits into
python-apifrom
scalar-perf
Draft

Transurgeon wants to merge 16 commits into
python-apifrom
scalar-perf

Conversation

@Transurgeon

@Transurgeon Transurgeon commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

This is a simple PR which is able to automatically detect and convert scalar models to an equivalent vectorized one.
Most of the changes are just for adding an opcode to the operators (sort of like an attribute which holds the name of the operator).
The bulk of the changes are in vectorize.py, and are run through compact.py.

@Transurgeon
Transurgeon marked this pull request as ready for review November 8, 2026 23:42
@Transurgeon
Transurgeon marked this pull request as draft November 8, 2026 23:56
Comment thread sparsediffpy/vectorize.py Resolved
Comment thread sparsediffpy/vectorize.py Resolved
Transurgeon and others added 16 commits November 8, 2026 17:00
sp.compact(terms) is a drop-in for sp.vstack(terms). Terms with the same
template (same operations, differing only in variable entries and scalar
constants) are rebuilt once as a vector expression; other terms are kept.
It returns (expr, perm), where row j of expr is terms[perm[j]], or perm is
None when the order is unchanged.

Nodes now carry an op name, and index/power carry the attrs the template
reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Modules: _atoms -> atoms, _expression -> expression, _compile -> problem
(compile.py would clash with sp.compile), _compact -> vectorize.

compact now lowers each term to (opcode, *operands) instructions whose
operands are value numbers, plus per-term args for each load/const.
Node.op is renamed Node.opcode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tes, errors

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…P RK4)

Each problem is written vectorized and as a scalar loop; the loop form is
checked through vstack and compact against the vectorized reference
(f, grad, g, J, Lagrangian Hessian), with group counts pinned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replaces _n_groups (which read the vstack structure of compact's result)
with count_nodes in conftest: compacting a loop at n and 2n must give the
same number of nodes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
_lower recurses, so a single very deep term (e.g. a 3000-term sum chain)
raised RecursionError; it now falls back to being kept unmerged. The
Brusselator forcing test duplicated what test_compact_matches_vectorized
already checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rename insts -> instructions and lanes -> rows, and document compact and
its helpers with Parameters/Returns sections, following CVXPY.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
compact stacks scalar expressions into rows; it does not sum them, so
"term" was misleading. Rename UnsupportedTermError to match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Like an LLVM pass pipeline, Problem now runs compact on each run of
consecutive scalar constraints. Merged rows are gathered back into the
given order with one index node, so constraint values, Jacobian rows and
Lagrange multipliers keep their order. test_vectorize_problems now builds
the scalar problems directly with Problem.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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