Skip to content

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
@mcabbott mcabbott merged commit ebed6ab into FluxML:main Apr 21, 2025
8 of 11 checks passed
@mcabbott mcabbott deleted the bettercat branch April 21, 2025 21:07
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