Skip to content
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

use MLUtils #1874

Merged
merged 9 commits into from
Feb 18, 2022
Merged

use MLUtils #1874

merged 9 commits into from
Feb 18, 2022

Conversation

CarloLucibello
Copy link
Member

@CarloLucibello CarloLucibello commented Feb 14, 2022

This is replacing some utility functions and the DataLoader with their equivalent versions in MLUtils.jl.

On the DataLoader inferrability

After this, the DataLoader will accept generic datasets exposing the MLUtils.numobs and MLUtils.getobs interface (something that we should probably document somewhere). This has been discussed in #1282.

Unfortunately we lose inferrability, e. g. @inferred first(loader) will fail. This is because the previous implementation was just wrong for generic datasets. It was

Base.eltype(loader::DataLoader{D}) where D = D

where D is the underlying data type. So this PR will reopen #1159. This is an example of the current problematic behavior on master:

julia> using Flux.Data: Dataloader

julia> d = DataLoader(1:5);

julia> [x for x in d]  # iteration is fine
5-element Vector{Vector{Int64}}:
 [1]
 [2]
 [3]
 [4]
 [5]

julia> collect(d)
ERROR: MethodError: Cannot `convert` an object of type Vector{Int64} to an object of type UnitRange{Int64}
Closest candidates are:
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/LinearAlgebra/src/factorization.jl:58
  convert(::Type{T}, ::AbstractRange) where T<:AbstractRange at ~/julia/julia-1.7.1/share/julia/base/range.jl:215
  convert(::Type{T}, ::T) where T<:AbstractArray at ~/julia/julia-1.7.1/share/julia/base/abstractarray.jl:16
  ...
Stacktrace:
 [1] setindex!(A::Vector{UnitRange{Int64}}, x::Vector{Int64}, i1::Int64)
   @ Base ./array.jl:903
 [2] copyto!(dest::Vector{UnitRange{Int64}}, src::DataLoader{UnitRange{Int64}, Random._GLOBAL_RNG})
   @ Base ./abstractarray.jl:893
 [3] _collect(cont::UnitRange{Int64}, itr::DataLoader{UnitRange{Int64}, Random._GLOBAL_RNG}, #unused#::Base.HasEltype, isz::Base.HasLength)
   @ Base ./array.jl:655
 [4] collect(itr::DataLoader{UnitRange{Int64}, Random._GLOBAL_RNG})
   @ Base ./array.jl:649
 [5] top-level scope
   @ REPL[9]:1
 [6] top-level scope
   @ ~/.julia/packages/CUDA/bki2w/src/initialization.jl:52

TODO:

Close #1227

docs/src/utilities.md Outdated Show resolved Hide resolved
src/onehot.jl Outdated Show resolved Hide resolved
@CarloLucibello
Copy link
Member Author

Merge?

@CarloLucibello CarloLucibello added this to the v0.13 milestone Feb 16, 2022
@CarloLucibello
Copy link
Member Author

mergity merge?

@darsnack
Copy link
Member

I guess the main issue here is that the new DataLoader does not work with something that isn't an array but implemented getindex and not getobs. Can we just add the getindex fallback to MLUtils.jl?

Otherwise this is a drop-in replacement, so good enough for me.

@darsnack
Copy link
Member

Other question is what happens when MLUtils -> MLDataUtils?

Copy link
Member

@darsnack darsnack left a comment

Choose a reason for hiding this comment

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

No more concerns on my end

@ToucheSir ToucheSir removed their request for review February 17, 2022 19:25
@CarloLucibello CarloLucibello merged commit 13a65be into master Feb 18, 2022
@mcabbott
Copy link
Member

There are many warnings like this in the tests:

┌ Warning: `unsqueeze(x::AbstractArray, dims::Int)` is deprecated, use `unsqueeze(x; dims = dims)` instead.
│   caller = macro expansion at Test.jl:463 [inlined]
└ @ Core ~/.julia/dev/julia/usr/share/julia/stdlib/v1.8/Test/src/Test.jl:463
┌ Warning: `stack(x, dims)` is deprecated, use `stack(x; dims = dims)` instead.
│   caller = macro expansion at utils.jl:249 [inlined]
└ @ Core ~/.julia/dev/Flux/test/utils.jl:249

No rush but it would be nice to update all of them.

@cossio
Copy link
Contributor

cossio commented Feb 22, 2022

Can we have a release with the nicer DataLoader?

@darsnack
Copy link
Member

These are the blockers: https://github.com/FluxML/Flux.jl/milestone/3

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.

Cannot do double iteration of DataLoader
5 participants