Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
fix: handle bitstypes and wrapped arrays in isleaf (#88)
Browse files Browse the repository at this point in the history
* bitstype and wrapped arrays

* fixes

* fix import

* bound

* cleanup

* chore: fix min version of LinearAlgebra

* chore: run formatter

---------

Co-authored-by: Avik Pal <[email protected]>
Co-authored-by: Avik Pal <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2024
1 parent 7a39f50 commit e9a2ed7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name = "MLDataDevices"
uuid = "7e8f7934-dd98-4c1a-8fe8-92b47a384d40"
authors = ["Avik Pal <[email protected]> and contributors"]
version = "1.4.0"
version = "1.4.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

Expand Down Expand Up @@ -53,6 +54,7 @@ Compat = "4.15"
FillArrays = "1"
Functors = "0.4.8"
GPUArrays = "10, 11"
LinearAlgebra = "1.10"
MLUtils = "0.4.4"
Metal = "1"
Preferences = "1.4"
Expand Down
1 change: 1 addition & 0 deletions src/MLDataDevices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Functors: Functors, fleaves
using Preferences: @delete_preferences!, @load_preference, @set_preferences!
using Random: AbstractRNG, Random
using Compat: @compat
using LinearAlgebra: Transpose, Adjoint

abstract type AbstractDevice <: Function end
abstract type AbstractCPUDevice <: AbstractDevice end
Expand Down
3 changes: 3 additions & 0 deletions src/public.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,6 @@ data movement if `isleaf(x::T) == true`.
If `MLDataDevices.isleaf(x::T)` is not defined, then it will fall back to `Functors.isleaf(x)`.
"""
isleaf(x) = Functors.isleaf(x)

isleaf(::AbstractArray{T}) where {T} = isbitstype(T)
isleaf(::Union{Transpose, Adjoint, PermutedDimsArray}) = false
59 changes: 44 additions & 15 deletions test/misc_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,50 @@ end
end

@testset "isleaf" begin
# Functors.isleaf fallback
@test MLDataDevices.isleaf(rand(2))
@test !MLDataDevices.isleaf((rand(2),))
@testset "basics" begin
# Functors.isleaf fallback
@test MLDataDevices.isleaf(rand(2))
@test !MLDataDevices.isleaf((rand(2),))

struct Tleaf
x::Any
end
Functors.@functor Tleaf
MLDataDevices.isleaf(::Tleaf) = true
Adapt.adapt_structure(dev::CPUDevice, t::Tleaf) = Tleaf(2 .* dev(t.x))

cpu = cpu_device()
t = Tleaf(ones(2))
y = cpu(t)
@test y.x == 2 .* ones(2)
y = cpu([(t,)])
@test y[1][1].x == 2 .* ones(2)
end

@testset "shared parameters" begin
# from
x = rand(1)
m = (; a=x, b=x')
count = Ref(0)
mcopy = Functors.fmap(m; exclude=MLDataDevices.isleaf) do x
count[] += 1
return copy(x)
end
@test count[] == 1
@test mcopy.a === mcopy.b'
end

struct Tleaf
x::Any
@testset "bitstypes and wrapped types" begin
struct BitsType
x::Int32
y::Float64
end

for x in [1.0, 'a', BitsType(1, 2.0)]
@test MLDataDevices.isleaf([x])
@test !MLDataDevices.isleaf([x]')
@test !MLDataDevices.isleaf(transpose([x]))
@test !MLDataDevices.isleaf(PermutedDimsArray([x;;], (1, 2)))
end
end
Functors.@functor Tleaf
MLDataDevices.isleaf(::Tleaf) = true
Adapt.adapt_structure(dev::CPUDevice, t::Tleaf) = Tleaf(2 .* dev(t.x))

cpu = cpu_device()
t = Tleaf(ones(2))
y = cpu(t)
@test y.x == 2 .* ones(2)
y = cpu([(t,)])
@test y[1][1].x == 2 .* ones(2)
end

2 comments on commit e9a2ed7

@avik-pal
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/117817

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.1 -m "<description of version>" e9a2ed795dc1b4c357ebaf5c5c8b1753896dd235
git push origin v1.4.1

Please sign in to comment.