Skip to content

Feature Proposal: specialize cat on the last dim #50405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ctarn opened this issue Jul 4, 2023 · 3 comments
Open

Feature Proposal: specialize cat on the last dim #50405

ctarn opened this issue Jul 4, 2023 · 3 comments
Labels
arrays [a, r, r, a, y, s] feature Indicates new feature / enhancement requests

Comments

@ctarn
Copy link
Contributor

ctarn commented Jul 4, 2023

Hi, I would like to open the issue to see whether we are interested in specializing cat on the last dim.

Currently, we have specialized cat on 1st and 2nd dim as vcat and hcat respectively, and reduce on vcat and hcat have their own implementations for better performance.

 [4] reduce(::typeof(vcat), A::AbstractVector{<:AbstractVecOrMat})
     @ abstractarray.jl:1703
...
 [6] reduce(::typeof(hcat), A::AbstractVector{<:AbstractVecOrMat})
     @ abstractarray.jl:1706
...

Additionally, we also have stack for similar purposes, but stack requires that all items have the same size. (#21672, #43334)

In my own code base, I call it scat (stack-like cat), and simply define it as below:

scat(X...) = cat(X...; dims=ndims(first(X)))

"""
example:
julia> scat(rand(2, 3, 4, 5), rand(2, 3, 4, 6))
2×3×4×11 Array{Float64, 4}:
[:, :, 1, 1] =
 0.134323  0.369241  0.178764
 0.713772  0.323157  0.903367

[:, :, 2, 1] =
 0.646836   0.142949  0.234398
 0.0158574  0.874712  0.65062

...

"""

An extra method for reduce for better performance is preferred:

reduce(::typeof(scat), A) = ...
@Seelengrab
Copy link
Contributor

This may need a bikeshed on the name...

@mcabbott
Copy link
Contributor

mcabbott commented Jul 5, 2023

Note also that "stack-like" seems misleading, in that this uses dims=ndims(x) where stack would use dims=ndims(x)+1. It's along the last dimension, rather than always a new dimension.

julia> x4 = rand(3, 4, 5, 6);

julia> scat(x4, x4) |> size
(3, 4, 5, 12)

julia> stack((x4, x4)) |> size
(3, 4, 5, 6, 2)

@ctarn
Copy link
Contributor Author

ctarn commented Jul 6, 2023

Some names listed as below.

  1. scat: stack-like cat
  2. tcat: cat on the trunk dimension
  3. lcat: cat on leading/last dimension
  4. mcat: cat on major/main dimension, or merging cat
  5. bcat: cat as batches
  6. ccat: cat as chunks
  7. pcat: cat on the primary dimension
  8. ocat: cat on the outermost dimension.

Not recommended:

  1. dcat: the third dimension is commonly referred as depth
  2. ncat: (not recommended since the existing hvncat)
  3. vcat
  4. hcat

@ctarn ctarn changed the title Feature Proposal: specialize cat on the last dim as scat (stack-like cat) Feature Proposal: specialize cat on the last dim Jul 6, 2023
@brenhinkeller brenhinkeller added the feature Indicates new feature / enhancement requests label Aug 4, 2023
@mcabbott mcabbott added the arrays [a, r, r, a, y, s] label Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] feature Indicates new feature / enhancement requests
Projects
None yet
Development

No branches or pull requests

4 participants