Skip to content

Some cat improvements #52

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Some cat improvements #52

wants to merge 2 commits into from

Conversation

mcabbott
Copy link
Member

@mcabbott mcabbott commented Apr 17, 2025

Before:

julia> let 
       x,y,z = [onehotbatch(rand(1:10, 100), 1:10) for _ in 1:3]
       @btime vcat($x, $y, $z)
       @btime hcat($x, $y, $z)
       end;
  4.214 μs (9 allocations: 6.41 KiB)
  120.640 ns (4 allocations: 2.16 KiB)

julia> reduce(hcat, [onehot(2, 1:3), onehot(1, 1:3)])
3×2 Matrix{Bool}:
 0  1
 1  0
 0  0

julia> cat(onehot(2, 1:3), onehot(1, 1:3); dims=(1,2))
ERROR: TypeError: in keyword argument dims, expected Int64, got a value of type Tuple{Int64, Int64}

After:

julia> let 
       x,y,z = [onehotbatch(rand(1:10, 100), 1:10) for _ in 1:3]
       @btime vcat($x, $y, $z)  # now just calls invoke, instead of collecting the pieces first
       @btime hcat($x, $y, $z)  # better code, avoids a bad reduce(hcat, ::Tuple)
       end;
  3.406 μs (3 allocations: 3.08 KiB)
  61.650 ns (2 allocations: 1.25 KiB)

julia> reduce(hcat, [onehot(2, 1:3), onehot(1, 1:3)])  # calls stack
3×2 OneHotMatrix(::Vector{UInt32}) with eltype Bool:
 ⋅  1
 1  ⋅
 ⋅  ⋅

julia> cat(onehot(2, 1:3), onehot(1, 1:3); dims=(1,2))
6×2 Matrix{Bool}:

Closes #32, closes FluxML/Flux.jl#1596

@mcabbott mcabbott marked this pull request as ready for review April 17, 2025 01:47
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.

Reduce hcat creates dense matrix Reduce hcat creates dense matrix
1 participant