Skip to content

Add shaped_parameter for rectangular opaque operator parameters - #5328

Open
xtalax-droid wants to merge 17 commits into
SciML:masterfrom
xtalax-droid:forge/shaped-opaque-params
Open

xtalax-droid wants to merge 17 commits into
SciML:masterfrom
xtalax-droid:forge/shaped-opaque-params

Conversation

@xtalax-droid

Copy link
Copy Markdown
Contributor

A parameter of a non-Real type, such as a SparseMatrixCSC or a user operator struct, could not declare an array shape:

  • @parameters K[1:m,1:n]::SparseMatrixCSC is rejected; SymbolicUtils.parse_variable turns name[dims]::T into an array of T.
  • A shapeless K::SparseMatrixCSC{Float64,Int} has an unknown shape, so K * u takes the shape of u and w ~ K * u fails with "Cannot equate an array of different sizes" whenever the operator is rectangular (m != n).
  • @register_array_symbolic cannot size its output from a field of the symbolic operator argument.

This adds shaped_parameter(name, T, dims...; tunable = false) (@public, in src/parameters.jl), which builds the symbol with an explicit shape. With it, size = (size(op, 1),) works inside @register_array_symbolic, and field blocks of unequal size go through the System / complete / DAEProblem codegen path. The macro syntax would need a change in SymbolicUtils, so this is a function for now.

Because the declared shape is now load-bearing for code generation, it is checked against supplied values: at MTKParameters construction and in remake/validate_parameter_type, a value that defines size and disagrees with the declared (non-empty) shape throws a DimensionMismatch (InvalidParameterSizeException). Values without a size method are accepted, and parameters with no declared array shape (@parameters K::SomeOp, p::Any) are not checked. Plain @parameters x[1:3] array parameters are now also checked at construction, which remake already did.

Context: this is the "shaped non-Real array parameters" item of the FiniteElementMethod.jl upstream-blocker list (unequal field sizes on the codegen path).

Tests: new test/shaped_parameters.jl covers declaration, a rectangular registered operator, a two-field DAE with blocks of 7 and 3 unknowns built through codegen and compared against a hand-written residual and a DFBDF solve, mismatch errors at construction and remake for opaque and sparse values, and the no-false-positive cases. Initialization group passes; InterfaceI has one failure, components.jl:446 "Causal connections generate causal equations", which also fails on master. Codex review (four rounds) approved.

Notes: the implementation relies on Sym{SymReal} with type/shape and shape_placeholder in @register_array_symbolic, which were tested with Symbolics 7.47; the declared compat lower bound is 7.45, which was not tested. An operator type needs its own Base.size for the value check to apply.


🤖 Posted by an AI agent · harness: Claude Code 2.1.311 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

xtalax and others added 5 commits October 26, 2026 22:08
A parameter of a non-Real type such as SparseMatrixCSC or a user operator struct had no way to declare an array shape, so K*u took the shape of u and rectangular blocks failed. shaped_parameter builds the symbol with an explicit shape, which also lets @register_array_symbolic use size(op, 1).

Agent-Harness: Claude Code 2.1.311
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Opaque values live in the nonnumeric portion, which only checked the type, so a 5x6 operator could be supplied for a declared 4x6 parameter and silently drop rows. Values that define size are now compared with the declared shape at MTKParameters construction and in remake; values without a size method are accepted.

Agent-Harness: Claude Code 2.1.311
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Numeric arrays such as SparseMatrixCSC go to the constant buffer, which the constructor did not size-check, so a mis-sized matrix was stored for a shaped_parameter. The constructor now applies the same shape check there; remake already compared against the symbolic shape.

Agent-Harness: Claude Code 2.1.311
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Parameters declared as plain opaque or Any types have an empty symbolic shape, so a legitimate value that defines size was rejected at construction. Only explicitly shaped parameters are checked.

Agent-Harness: Claude Code 2.1.311
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…ue-params

# Conflicts:
#	lib/ModelingToolkitBase/test/runtests.jl

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid

Copy link
Copy Markdown
Contributor Author

Pushed 81e7bd7bd2, a merge of current master that resolves the conflict in lib/ModelingToolkitBase/test/runtests.jl (both sides added a @safetestset line; both kept). shaped_parameters.jl and array_varmap_resolve.jl pass, and Runic 1.13.0 is clean on the touched file. The full Core group was not re-run.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

The public shaped_parameter was missing from every @docs block, failing the SciMLTesting 'public API is rendered in docs' check.

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@ChrisRackauckas

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

PR: #5328

Reviewed head: 6bfaac4
Raw SHA-256: 433a40f24c7864453379753853a1c88332410d8ccc6d938ce005ec3d6414dd0a
Source SHA-256: f8a57819484a22e20f0cb37cf23b74514cfefcb804c6ac180372c4d9b8b18372
Source: standard-review-v1 (harness-log)
Reviewer session: 01a1d258-b538-74c2-a1bf-51f2ae9effe3
Slot: 1
Confidence: unknown
Own risk fixed before author rating/discussion: yes

Risk assessment

  • Risk: medium-low (ML7)
  • Verdict: CHANGES
  • Reviewed head: 6bfaac4

Review report

VERDICT: CHANGES
Round: 1
Head: 6bfaac4
Slot: 1
CI: CI is not green
Session: 01a1d258-b538-74c2-a1bf-51f2ae9effe3

Risk

medium-low (ML7: additive public shaped_parameter API with documentation and tests; runtime verification remains outstanding).

Blocking findings

  • [P2] Needs verification — affected tests did not execute. lib/ModelingToolkitBase/test/shaped_parameters.jl:1 and lib/ModelingToolkitBase/src/systems/parameter_buffer.jl:648: the guarded runner exited 75 after over one hour with RESOURCE_GUARD_TIMEOUT: no Julia job started. No sampled launch opportunity satisfied the machine instructions together: at least 16 GiB available, fewer than five existing Julia job roots, and load below 32. Dependency resolution succeeded, but no test assertion ran. Run the new test file, the affected parameter-buffer suite, and the targeted probes before accepting this head. This is missing evidence, not an observed test failure.

  • Verdict: CHANGES

  • Round: 1

  • Blocking priorities: P2

Non-blocking findings

  • lib/ModelingToolkitBase/src/systems/parameter_buffer.jl:648 — Suspected from reading; not runtime-confirmed: shape validation is added to construction and symbolic remake, while the nonnumeric in-place setter at line 593 calls _buffer_setindex! with size validation disabled. An opaque, size-defined operator may therefore accept a differently shaped value through setp or indexed assignment. The prepared probes cover these routes and index-based remake_buffer. Confirm their behavior; if mismatches are accepted, enforce the declared shape on these update paths and add regression coverage while retaining support for values without size.

What I ran

  • Read git diff origin/master...HEAD, repository instructions, affected implementation and test runners. Recorded the independent ML7 assessment before seeing any author rating; no author rating or earlier review was read.
  • git diff --check origin/master...HEAD: exit 0. Final git status --porcelain: empty; HEAD matches the requested SHA.
  • Runic --check on all five changed Julia files: exit 0. typos on parameters.jl, parameter_buffer.jl, shaped_parameters.jl, and the API page: exit 0.
  • julia --version: 1.13.2. Instantiated a scratch environment using the checkout's ModelingToolkitBase and its declared dependencies/extras, preserving JULIA_DEPOT_PATH; exit 0. Resolved ModelingToolkitBase 1.79.0, SymbolicUtils 4.56.1, Symbolics 7.48.0, SciMLBase 3.58.0, DiffEqBase 7.21.6, and OrdinaryDiffEqBDF 2.4.14.
  • bash "$SCRATCH/gated-run.sh": exit 75 as described above. It queued shaped_parameters.jl, probes, and the full official GROUP=SymbolicIndexingInterface runner with public solver packages loaded first. Scratch probes cover mismatched setter/remake updates, range and invalid dimension declarations, and tunable=true for sparse, dense, and opaque operators. They were written but not executed.
  • uv run --locked python --version: Python 3.15.0; uv warned that --locked has no effect outside a project. No Python project/lockfile or repository Julia interpreter pin/Manifest exists; the scratch dependency Manifest was generated during setup.

AI reviewer: Codex, model gpt-6.1-sol; local session 01a1d258-b538-74c2-a1bf-51f2ae9effe3 (no session URL).

What I did not verify

Runtime correctness, the new DAE solve, parameter-update probes, or existing parameter-buffer tests. Full InterfaceI, QA, and documentation builds were not run. No CI or GitHub discussion was inspected. No confirmed runtime defect was established; the numerical test's independently assembled matrix-exponential reference looks appropriate on inspection.
AUTHOR_GRADE: {"task":"feature","score":4,"difficulty":3,"rounds":1,"author_harness":"Claude Code 2.1.312","author_model":"claude-sonnet-5-5","note":"Focused public API addition with documentation, shape-validation tests, and an independent matrix-exponential reference. Formatting and spelling checks pass. Runtime correctness remains unverified because the resource guard prevented test execution; the setter concern is reading-only."}

Head link: 6bfaac4


🤖 Posted by an AI agent — harness: codex-cli 0.164.0 · model: gpt-6.1-sol
Conversation: local session 01a1d258-b538-74c2-a1bf-51f2ae9effe3 (no session URL)

xtalax and others added 3 commits November 13, 2026 01:51
Agent-Harness: Claude Code 2.1.322
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…rameter in QA

setp, indexed assignment and index-based remake_buffer accepted a
differently shaped value for opaque shaped_parameters. Validate against
the stored value when the new value has a size, reject non-positive
dimension declarations, and add shaped_parameter to the approved public
reexports in test/qa/qa.jl (the CI QA failure).

Agent-Harness: Claude Code 2.1.322
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…e_buffer path

handle_parameter strips validate_size and the index-only type check has no
declared shape, so a mismatched opaque or sparse value passed when vals had
a non-concrete eltype. Compare with the existing value instead.

Agent-Harness: Claude Code 2.1.322
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid

Copy link
Copy Markdown
Contributor Author

Pushed d7e22e035e: a clean merge of current master (de96d60e6f, 114 commits behind, no conflicts) plus two commits (500a367489, d7e22e035e) that address both review items.

Runtime evidence for the P2. Both runs were from lib/ModelingToolkitBase via TestEnv and forge julia.

  • test/shaped_parameters.jl: all six testsets pass.
  • GROUP=SymbolicIndexingInterface: "SymbolicIndexingInterface test" 74/74, "SciML Problem Input Test" 1824/1824, "MTKParameters Test" 136/136, "Despecialized MTKParameters Test" 53/53, ending "Testing ModelingToolkitBase tests passed".

The suspected setter gap was real. Before the fix, setp, indexed assignment (ps[parameter_index(...)] = v) and index-based remake_buffer all accepted a wrongly shaped opaque shaped_parameter, while sparse parameters and symbolic remake_buffer already rejected the mismatch with DimensionMismatch. Values with no size were accepted on every route, and still are.

The fix:

  • parameter_index now turns on size validation for opaque parameters that carry a declared shape (new helper has_declared_opaque_shape, shared with validate_opaque_shape), so setp and indexed assignment enforce it.
  • remake_buffer by index has two routes. With a concretely typed vals it reaches setindex!, which honours the flag. With a non-concrete eltype such as Any[bad], handle_parameter called the index-only validate_parameter_type, which has no declared shape, and then dropped the flag through ParameterIndex(idx). The new validate_indexed_shape closes that second route for opaque and sparse parameters.
  • The new regression tests cover mismatched setp, indexed assignment and both remake_buffer routes (including Any[bad] for the opaque and sparse cases), plus accepting a good value and a value without size.

Dimension declarations. shaped_parameter now throws ArgumentError for non-positive or empty dimensions; a nonempty range such as 2:5 is still allowed, and the docstring says so. The probes had shown 0, -1 and 1:0 being accepted silently.

QA. shaped_parameter was missing from the approved public reexports in test/qa/qa.jl; that was the only offender in the tests / QA failure on the previous head. It is added.

Left alone. A tunable=true opaque parameter is accepted and listed in tunable_parameters even though a BlockOp cannot live in the numeric tunable vector. That predates this change and is out of scope here. The remaining red jobs on the previous head (Extensions and docs build, in BifurcationKit's _continuation; ModelOrderReduction; SciMLSensitivity Core8) are not in parameter code, and have not been re-run on the new head yet. The 32-bit and full Core groups were not run locally.

Codex review of the new commits approved without findings.


🤖 Posted by an AI agent · harness: Claude Code 2.1.322 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

xtalax and others added 8 commits November 22, 2026 12:12
Agent-Harness: Claude Code 2.1.325
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
check_bindings compared SU.shape of the binding value, which reports an
opaque value as a scalar, so a correctly sized value bound to a
shaped_parameter was rejected. A shaped_parameter is now compared against
the size of the value itself when the value defines size; wrongly sized
values are still rejected.

Agent-Harness: Claude Code 2.1.325
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
A shaped_parameter bound to a value that defines no size method was
rejected by check_bindings, although parameter construction and updates
accept such values. The comparison is now skipped for them.

Agent-Harness: Claude Code 2.1.325
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
A value bound to a shaped_parameter is substituted into operator
applications as a constant whose shape is scalar, so the declared shape is
lost and building the problem fails with a shape mismatch. check_bindings
now rejects such bindings at System construction with an error pointing to
the problem op map, replacing the size-based acceptance added earlier.

Agent-Harness: Claude Code 2.1.325
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…haped_parameter to dense arrays

check_bindings rejected only values that report a scalar shape, so
matching-size sparse or UniformScaling bindings passed construction and
failed later. The rejection now applies to every shaped_parameter whose
type is not a dense Array, which keeps its shape under substitution.

A tunable shaped_parameter is stored in the flat tunable buffer and is
returned as a dense view, discarding the declared type, so tunable = true
now requires a dense Array type.

Agent-Harness: Claude Code 2.1.325
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Tunable array parameters are flattened into the tunable buffer, which
discards the declared type and reshapes values with a matching element
count, and a dense integer array is not placed in that buffer at all.
shaped_parameter(...; tunable = true) now throws for every type; the
parameter is a constant updated through the problem op map or setp.

Agent-Harness: Claude Code 2.1.325
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…nd scalar shaped parameters

A callable parameter's declared shape is that of its return value, so
comparing it with the size of the callable itself rejected valid values.
has_declared_opaque_shape now excludes FnType parameters.

check_bindings rejected shaped opaque bindings only outside the missing
branch, so a missing binding slipped through; the rejection now precedes
it. shaped_parameter throws for scalar Number types.

Agent-Harness: Claude Code 2.1.325
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Declared ranges that do not start at 1 passed the size check but made
literal indices run out of bounds on the stored value, and a rank-1
declaration was accepted for a rank-2 array type. Both now throw.

Agent-Harness: Claude Code 2.1.325
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid

Copy link
Copy Markdown
Contributor Author

Pushed e1b4c5cca7: a merge of current master (the only conflict was the @docs block in docs/src/API/variables.md, kept both sides) plus fixes for interactions with master's new binding-shape check.

  • check_bindings now rejects any binding of a shaped_parameter that is not a dense Array (including missing) with an error that points to the problem op map. Reason: substituting a bound opaque value as a Const loses the declared shape (SU.shape of an opaque Const is ()), so apply_op(Const(op), u) is rebuilt with shape (1,) and DAEProblem construction fails with "Cannot add arguments of different sizes". Supplying values through the op map works and is covered by the existing tests.
  • shaped_parameter now throws for tunable = true (the flat tunable buffer discards the declared type and silently reshapes), scalar Number types, rank mismatches with the concrete array type, and non-1-based ranges.
  • Callable parameters are exempt from the new value-size validation, with a regression test.

shaped_parameters.jl passes in full and binding_semantics.jl passes (11 testsets).

Known gaps that a last Codex pass still raised, not addressed here: value arrays with offset axes are accepted if lengths match; set_defaults can introduce a binding that construction would reject; setp compares against the stored value's current size, not the declared one. Separately, that pass also flagged a hybrid-linearization stale-value issue in src/linearization_hybrid.jl, which is master code and untouched by this PR.


🤖 Posted by an AI agent · harness: Claude Code 2.1.325 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

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.

3 participants