Skip to content

Commit

Permalink
fix: preserve object when device is same (#1133)
Browse files Browse the repository at this point in the history
* fix: preserve object when device is same

* test: object not copied
  • Loading branch information
avik-pal authored Dec 14, 2024
1 parent 59c0c69 commit fdb0170
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/MLDataDevices/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MLDataDevices"
uuid = "7e8f7934-dd98-4c1a-8fe8-92b47a384d40"
authors = ["Avik Pal <[email protected]> and contributors"]
version = "1.6.3"
version = "1.6.4"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
6 changes: 5 additions & 1 deletion lib/MLDataDevices/ext/MLDataDevicesAMDGPUExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ function Internal.unsafe_free_internal!(::Type{AMDGPUDevice}, x::AbstractArray)
end

# Device Transfer
Adapt.adapt_storage(::AMDGPUDevice{Nothing}, x::AbstractArray) = AMDGPU.roc(x)
function Adapt.adapt_storage(::AMDGPUDevice{Nothing}, x::AbstractArray)
MLDataDevices.get_device_type(x) <: AMDGPUDevice && return x
return AMDGPU.roc(x)
end

function Adapt.adapt_storage(to::AMDGPUDevice, x::AbstractArray)
old_dev = AMDGPU.device() # remember the current device
dev = MLDataDevices.get_device(x)
Expand Down
5 changes: 4 additions & 1 deletion lib/MLDataDevices/ext/MLDataDevicesCUDAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ function Internal.unsafe_free_internal!(::Type{CUDADevice}, x::AbstractArray)
end

# Device Transfer
Adapt.adapt_storage(::CUDADevice{Nothing}, x::AbstractArray) = CUDA.cu(x)
function Adapt.adapt_storage(::CUDADevice{Nothing}, x::AbstractArray)
MLDataDevices.get_device_type(x) <: CUDADevice && return x
return CUDA.cu(x)
end

function Adapt.adapt_storage(to::CUDADevice, x::AbstractArray)
old_dev = CUDA.device() # remember the current device
Expand Down
5 changes: 4 additions & 1 deletion lib/MLDataDevices/ext/MLDataDevicesMetalExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ function Internal.unsafe_free_internal!(::Type{MetalDevice}, x::AbstractArray)
end

# Device Transfer
Adapt.adapt_storage(::MetalDevice, x::AbstractArray) = Metal.mtl(x)
function Adapt.adapt_storage(::MetalDevice, x::AbstractArray)
MLDataDevices.get_device_type(x) <: MetalDevice && return x
return Metal.mtl(x)
end

end
6 changes: 5 additions & 1 deletion lib/MLDataDevices/ext/MLDataDevicesoneAPIExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ end
# Device Transfer
for (T1, T2) in ((Float64, Float32), (ComplexF64, ComplexF32))
@eval function Adapt.adapt_storage(::oneAPIDevice, x::AbstractArray{$(T1)})
MLDataDevices.get_device_type(x) <: oneAPIDevice && return x
if !SUPPORTS_FP64[oneAPI.device()]
@warn LazyString(
"Double type is not supported on this device. Using `", $(T2), "` instead.")
Expand All @@ -50,6 +51,9 @@ for (T1, T2) in ((Float64, Float32), (ComplexF64, ComplexF32))
return oneArray(x)
end
end
Adapt.adapt_storage(::oneAPIDevice, x::AbstractArray) = oneArray(x)
function Adapt.adapt_storage(::oneAPIDevice, x::AbstractArray)
MLDataDevices.get_device_type(x) <: oneAPIDevice && return x
return oneArray(x)
end

end
5 changes: 4 additions & 1 deletion lib/MLDataDevices/src/public.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ for op in (:get_device, :get_device_type)
end

# Adapt Interface
Adapt.adapt_storage(::CPUDevice, x::AbstractArray) = Array(x)
function Adapt.adapt_storage(::CPUDevice, x::AbstractArray)
get_device_type(x) <: CPUDevice && return x
return Array(x)
end
Adapt.adapt_storage(to::AbstractDevice, ::Random.TaskLocalRNG) = default_device_rng(to)
Adapt.adapt_storage(::AbstractDevice, rng::AbstractRNG) = rng

Expand Down
6 changes: 6 additions & 0 deletions lib/MLDataDevices/test/amdgpu_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ using FillArrays, Zygote # Extensions
return_val(x) = Val(get_device_type(x)) # If it is a compile time constant then type inference will work
@test @inferred(return_val(ps)) isa Val{parameterless_type(typeof(device))}
end

@testset "Issue #1129: no new object" begin
x = rand(Float32, 10, 10) |> device
y = x |> device
@test x === y
end
end

@testset "Functions" begin
Expand Down
6 changes: 6 additions & 0 deletions lib/MLDataDevices/test/cuda_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ using FillArrays, Zygote # Extensions
return_val2(x) = Val(get_device(x))
@test_throws ErrorException @inferred(return_val2(ps))
end

@testset "Issue #1129: no new object" begin
x = rand(Float32, 10, 10) |> device
y = x |> device
@test x === y
end
end

@testset "Functions" begin
Expand Down
6 changes: 6 additions & 0 deletions lib/MLDataDevices/test/metal_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ using FillArrays, Zygote # Extensions
return_val2(x) = Val(get_device(x))
@test @inferred(return_val2(ps)) isa Val{get_device(x)}
end

@testset "Issue #1129: no new object" begin
x = rand(Float32, 10, 10) |> device
y = x |> device
@test x === y
end
end

@testset "Functions" begin
Expand Down
6 changes: 6 additions & 0 deletions lib/MLDataDevices/test/oneapi_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ using FillArrays, Zygote # Extensions
return_val2(x) = Val(get_device(x))
@test @inferred(return_val2(ps)) isa Val{get_device(x)}
end

@testset "Issue #1129: no new object" begin
x = rand(Float32, 10, 10) |> device
y = x |> device
@test x === y
end
end

@testset "Functions" begin
Expand Down
6 changes: 6 additions & 0 deletions lib/MLDataDevices/test/xla_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ using FillArrays, Zygote # Extensions
return_val2(x) = Val(get_device(x))
@test_throws TypeError @inferred(return_val2(ps))
end

@testset "Issue #1129: no new object" begin
x = rand(Float32, 10, 10) |> device
y = x |> device
@test x === y
end
end

@testset "Functions" begin
Expand Down

2 comments on commit fdb0170

@avik-pal
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register subdir=lib/MLDataDevices

@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/121403

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 MLDataDevices-v1.6.4 -m "<description of version>" fdb017022a5a9a62984f89a128c1f35ed80cb1ca
git push origin MLDataDevices-v1.6.4

Please sign in to comment.