Skip to content

Adds an abstract type to NamedArrayPartition - #447

Open
htsnowden wants to merge 2 commits into
SciML:arraypart_zerofrom
htsnowden:SubtypingNamedArrayPartition
Open

htsnowden wants to merge 2 commits into
SciML:arraypart_zerofrom
htsnowden:SubtypingNamedArrayPartition

Conversation

@htsnowden

Copy link
Copy Markdown
Contributor

In order to resolve #443 where I would like to be able to subtype from NamedArrayPartition. This is a large restructuring of NamedArrayPartition but all tests within RecursiveArrayTools do pass. I'm not sure of other tests that I would need to run to ensure further compatibility. This maintains all current functionality but allows a user to create a new subtype of AbstractNamedArrayPartition as long as it maintains the same structure. I am pulling into this branch as this is where my previous fix to the ArrayInterface.zeromatrix lives.

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

In order to resolve SciML#443 where I would like to be able to subtype from NamedArrayPartition. This is a large restructuring of NamedArrayPartition but all tests within RecursiveArrayTools do pass. I'm not sure of other tests that I would need to run to ensure further compatibility. This maintains all current functionality but allows a user to create a new subtype of AbstractNamedArrayPartition as long as it maintains the same structure
Comment thread src/named_array_partition.jl Outdated
@@ -1,3 +1,5 @@
abstract type AbstractNamedArrayPartition{T, A, NT} <: AbstractVector{T} end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should have an AbstractArrayPartition, and that needs a docstring to describe its interface?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is ArrayPartition an AbstractNamedArrayPartition with x being its only names?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a higher level called AbstractArrayPartition{T} if you want and apply the same level of abstraction to the ArrayPartition interface as well (either or both) such that the same subtyping can be performed there also. I will add to the documentation, a docstring about how you can subtype from the AbstractNamedArrayPartition


function Base.similar(A::NamedArrayPartition)
NamedArrayPartition(
# With new type structure this function does the same as Base.similar(x::AbstractNamedArrayPartition{T, S, NT}) where {T, S, NT}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this one commented?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I couldn't manage in either the old or the new method to call the less specific function as providing an NamedArrayPartition will have the type structure X{N, A, NT} and therefore a later version would have instead been called. In the original code the two functions are:

function Base.similar(A::NamedArrayPartition)
    NamedArrayPartition(
        similar(getfield(A, :array_partition)), getfield(A, :names_to_indices))
end

function Base.similar(x::NamedArrayPartition{T, S, NT}) where {T, S, NT}
    NamedArrayPartition{T, S, NT}(
        similar(ArrayPartition(x)), getfield(x, :names_to_indices))
end

But in all cases I tested the second always overwrites the first as any NamedArrayPartition has the structure of the second

After @ChrisRackauckas comments I have documented how to subtype from AbstractNamedArrayPartition as well as added an AbstractArrayPartition type which both AbstractNamedArrayPartition and ArrayPartition subtype from
@ChrisRackauckas

Copy link
Copy Markdown
Member

It's still missing what is meant by the interface. See for example https://github.com/SciML/RecursiveArrayTools.jl/blob/master/src/RecursiveArrayTools.jl#L16-L110. The questions that have to be answered are:

  1. What does it mean to be an AbstractNamedArrayPartition / AbstractArrayPartition? What things must be true about the definition in order to satisfy the interface? For example, is there a allowednames function or something that everything should assume exists for AbstractNamedArrayPartition
  2. What are the functions and allowed operations on any member of this group?
  3. What is the relation to any other interface (for example AbstractArray)?

Then we need to make sure the abstract library implementations actually match this. For example, for ArrayPartition, the code currently always assumes A.x. See a chunk like https://github.com/SciML/RecursiveArrayTools.jl/blob/master/src/array_partition.jl#L127-L177. So there's a few decisions that have to be made here:

  1. Is ArrayPartition <: AbstractNamedArrayPartition where the names are just x?
    • If that's the case, then we probably need to generalize those functions I linked to so that they match the interface, rather than being specific to ArrayPartition. Seems like a lot of code code could be deleted.
  2. Are AbstractNamedArrayPartition expected to be non-allocating if they .=?
  3. How is AbstractNamedArrayPartition expected to interact with LinearAlgebra?

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

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

PR: #447

Reviewed head: d2823c0
Raw SHA-256: 0a5a6c6ee41048a4102af838d6831ddc185d6dd0eceec9736894f5013d7ca561
Source SHA-256: fadd1868c14354048b12b6cf00c8605c2eda86c32288ba4d2c9c90f1601efa49
Source: standard-review-v1 (harness-log)
Reviewer session: 01a216dd-416a-7252-b262-2eeaadf9712f
Slot: 1
Confidence: unknown
Own risk fixed before author rating/discussion: yes
Redaction: ran (substitutions=0)

Risk assessment

  • Risk: high (H1)
  • Verdict: CHANGES
  • Reviewed head: d2823c0

Review report

VERDICT: CHANGES
Round: 1
Head: d2823c0
Slot: 1
CI: CI unreadable; review only; merge gates unchanged
Session: 01a216dd-416a-7252-b262-2eeaadf9712f
Reviewer: codex-cli 0.170.0; model: gpt-6.2-sol

Risk

high (H1: existing public NamedArrayPartition broadcasts silently change values and return types, and an in-place broadcast accepts a partition shape it used to reject)

Blocking findings

  • Blocking priorities: P1, P2, P2
  1. [P1] Mixed parameter types silently switch to flat broadcasting (src/named_array_partition.jl:134). Confirmed on Julia 1.10.12. With u = NamedArrayPartition(a = [1], b = [2, 3, 4]) and v = NamedArrayPartition(a = [10.0, 20.0, 30.0], b = [40.0]), the merge base returns a named result whose parts are [11.0, 21.0, 31.0] and [42.0, 43.0, 44.0]. The head returns the plain vector [11.0, 22.0, 33.0, 44.0]. On the same inputs, broadcast!(+, copy(u), u, v) throws DimensionMismatch at the merge base and silently writes [11, 22, 33, 44] at the head. A vector versus a view in otherwise matching partitions also loses the wrapper. The concrete input type in each broadcast style makes Base combine the two styles as Broadcast.ArrayConflict().

    Implementation fix. In src/named_array_partition.jl, parameterize Base.BroadcastStyle by the unparameterized wrapper family so instances of one family share a style across element and storage types. Make the broadcast similar, unpack, copy and copyto! methods dispatch on that family style. Preserve the explicit plain-vector promotion rule. Distinct wrapper families can keep Base's conflict fallback because there is no established named result type for them. In test/named_array_partition_tests.jl, assert the result wrapper and both literal partition values for the mixed-eltype example, assert DimensionMismatch for the in-place call, and assert that the vector/view pair retains named storage. Keep the existing mixed plain-vector coverage.

  2. [P2] Inherited copy and similar assume the subtype's parameter layout (src/named_array_partition.jl:124, src/named_array_partition.jl:156). Confirmed with a VectorPartition{T, NT} subtype specializing the parent's storage parameter to ArrayPartition{T, Tuple{Vector{T}, Vector{T}}} and providing both documented fields. Its keyword constructor, size and scalar broadcast work. Both copy(x) and similar(x) throw TypeError because the parent storage parameter is passed into the subtype's NT slot by Tconstr{T,A,NT}.

    Implementation fix. Use typeof(x)(parts, names) in the two same-type methods, following the existing zero implementation. The alternative is to require the exact parameter count and order in the public subtype contract. Using the instance type is preferable because it preserves subtype-specific parameters and needs only the same constructor as zero. Document that constructor requirement. In test/named_array_partition_tests.jl, add a two-parameter subtype with fixed storage. Assert that copy retains its exact type, names and values with independent storage, and that similar retains its exact type, names and partition sizes.

  3. [P2] The new public extension point lacks a rendered docs entry and a test using the documented subtype (src/RecursiveArrayTools.jl:153, docs/src/array_types.md:8, test/named_array_partition_tests.jl:1). Confirmed by inspecting the export, docs list and test diff. The public type has a docstring, the Abstract Types section is empty, and the sole test-file change imports ArrayInterface. All 21 passing assertions exercise the concrete built-in wrapper.

    Implementation fix. Add AbstractNamedArrayPartition to a Documenter @docs block under Abstract Types in docs/src/array_types.md. Explain the required fields, that names_to_indices maps property names to one-based partition indices, and the constructors required by operations that preserve or change storage types. Add a test fixture matching the documented three-parameter subtype in test/named_array_partition_tests.jl. Assert that both user-facing constructors preserve names and values, a type-changing map preserves the wrapper with the expected new values, and a broadcast assignment updates the named fields to independently calculated values. Build the docs and confirm the type appears on the Recursive Array Types page.

Non-blocking findings

  • src/named_array_partition.jl:51, src/named_array_partition.jl:125. The diff adds a commented-out duplicate of Base.similar and the comment “Safely extract the concrete type parameters.” Implementation fix. Delete the block beginning “With new type structure” through its closing =#, and delete “Safely extract the concrete type parameters.” These are comment-only removals and require no additional test.

What I ran

  • git diff --name-status -M origin/master...HEAD and git diff --check origin/master...HEAD. Inspected all four source and test paths for authority-path changes. No renames, authority-path changes or whitespace errors appeared.
  • GROUP=Core <path> +1.10 --startup-file=no --project -e 'using Pkg; Pkg.test()' on a scratch copy of the head with Julia 1.10.12. Quality Assurance reported 9 passed and 1 broken, Utils 65 passed, and NamedArrayPartition 21 passed. Partitions reported 46 passed and 1 error at test/partitions_test.jl:133: return type Float64 does not match inferred return type Any. The run exited 1.
  • <path> +1.10 --startup-file=no --project --check-bounds=yes test/partitions_test.jl separately on the head and merge base. Both exited 1 with the identical inference error at line 133.
  • <path> +1.10 --startup-file=no --project ../broadcast_regression.jl on both revisions with the same resolved package dependencies. Merge base 4 passed. Head 4 failed, covering the values, wrapper and expected dimension error.
  • <path> +1.10 --startup-file=no --project ../probes.jl on both revisions. Confirmed the broadcast outcomes above and that the documented subtype handles construction, scalar broadcast, map, zero and zeromatrix. <path> +1.10 --startup-file=no --project ../subtype_regression.jl on the head reported 2 passed and 2 errors.
  • uv run --locked python --version printed Python 3.13.13 and warned that --locked has no effect outside a project. The checkout has no Python lockfile. The Julia checkout also has no committed manifest, so the review resolved its declared Julia dependencies in scratch.

What I did not verify

The Core groups scheduled after Partitions did not run. Downstream, GPU, other Julia versions, and the rendered docs build were outside the local checks. CI was unreadable.

AUTHOR_GRADE: {"task":"feature","score":2,"difficulty":3,"rounds":1,"author_harness":"unknown","author_model":"unknown","note":"The documented subtype works for several basic operations, but the head silently changes existing mixed-partition broadcast results and lacks subtype regression coverage."}

Head link: d2823c0


🤖 Posted by an AI agent — harness: codex-cli 0.170.0 · model: gpt-6.2-sol
Conversation: local session 01a216dd-416a-7252-b262-2eeaadf9712f (no session URL)

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