Skip to content

Conversation

@mcabbott
Copy link
Member

@mcabbott mcabbott commented Apr 24, 2025

Before:

julia> @allowscalar cx[1,1]
ERROR: MethodError: getindex(::OneHotMatrix{UInt32, JLArray{UInt32, 1}}, ::Int64, ::Int64) is ambiguous.

Candidates:
  getindex(x::OneHotArray{var"#s3", N, var"N+1", I} where {var"#s3", var"N+1", I<:Union{AbstractArray{var"#s3", N}, var"#s3"}}, i::Int64, I::Vararg{Int64, N}) where N
    @ OneHotArrays ~/.julia/packages/OneHotArrays/0JMW1/src/array.jl:65
  getindex(x::OneHotArray{<:Any, N, <:Any, <:GPUArraysCore.AbstractGPUArray}, i::Int64, I::Vararg{Any, N}) where N
    @ OneHotArrays ~/.julia/packages/OneHotArrays/0JMW1/src/array.jl:71

Possible fix, define
  getindex(::OneHotArray{var"#s3", N, <:Any, <:Union{…}} where var"#s3", ::Int64, ::Vararg{Int64, N}) where N

Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/GPUArraysCore/GMsgk/src/GPUArraysCore.jl:210 [inlined]
 [2] top-level scope
   @ REPL[64]:1
Some type information was truncated. Use `show(err)` to see complete types.

julia> @allowscalar collect(cx)
ERROR: MethodError: getindex(::OneHotMatrix{UInt32, JLArray{UInt32, 1}}, ::Int64, ::Int64) is ambiguous.

julia> x = onehotbatch(rand(1:99, 100), 1:100);

julia> @btime Matrix($x);
  8.958 μs (3 allocations: 10.08 KiB)

After:

julia> @allowscalar cx[1,1]
true

julia> collect(cx)
3×4 Matrix{Bool}:
 1  0  0  0
 0  1  0  1
 0  0  1  0

julia> convert(AbstractArray{Float32}, cx)
3×4 JLArray{Float32, 2}:
 1.0  0.0  0.0  0.0
 0.0  1.0  0.0  1.0
 0.0  0.0  1.0  0.0

julia> x = onehotbatch(rand(1:99, 100), 1:100);

julia> @btime Matrix($x);
  2.375 μs (4 allocations: 10.12 KiB)

Unchanged is the need for @allowscalar in order to get a column:

julia> @allowscalar cx[:,4]
3-element OneHotVector(::UInt32) with eltype Bool:
 
 1
 

julia> cx[:,4]
ERROR: Scalar indexing is disallowed.

@mcabbott mcabbott marked this pull request as ready for review April 24, 2025 03:37
@mcabbott
Copy link
Member Author

Maybe @ExpandingMan wants to review?

@ExpandingMan
Copy link
Contributor

Sorry, I had forgotten about this, but it was still in my list of github notifications so I came back to it.

Yes, this looks good to me. Certainly an improvement.

@mcabbott
Copy link
Member Author

Ok let's do this.

@mcabbott mcabbott merged commit 4e2d74a into FluxML:main Apr 28, 2025
7 of 11 checks passed
@mcabbott mcabbott deleted the getindex branch April 28, 2025 21:46
@codecov
Copy link

codecov bot commented Apr 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.00%. Comparing base (0ea288d) to head (49fd2ea).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #53      +/-   ##
==========================================
- Coverage   96.26%   95.00%   -1.27%     
==========================================
  Files           3        3              
  Lines         134      160      +26     
==========================================
+ Hits          129      152      +23     
- Misses          5        8       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants