-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
105 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module LuxDeviceUtilsReverseDiffExt | ||
|
||
using LuxDeviceUtils: LuxDeviceUtils | ||
using ReverseDiff: ReverseDiff | ||
|
||
@inline function LuxDeviceUtils.get_device(x::ReverseDiff.TrackedArray) | ||
return LuxDeviceUtils.get_device(ReverseDiff.value(x)) | ||
end | ||
@inline function LuxDeviceUtils.get_device(x::AbstractArray{<:ReverseDiff.TrackedReal}) | ||
return LuxDeviceUtils.get_device(ReverseDiff.value.(x)) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module LuxDeviceUtilsTrackerExt | ||
|
||
using Adapt: Adapt | ||
using LuxDeviceUtils: LuxDeviceUtils, LuxAMDGPUDevice, LuxCUDADevice, LuxMetalDevice, | ||
LuxoneAPIDevice, LuxCPUDevice | ||
using Tracker: Tracker | ||
|
||
@inline function LuxDeviceUtils.get_device(x::Tracker.TrackedArray) | ||
return LuxDeviceUtils.get_device(Tracker.data(x)) | ||
end | ||
@inline function LuxDeviceUtils.get_device(x::AbstractArray{<:Tracker.TrackedReal}) | ||
return LuxDeviceUtils.get_device(Tracker.data.(x)) | ||
end | ||
|
||
@inline LuxDeviceUtils.__special_aos(::AbstractArray{<:Tracker.TrackedReal}) = true | ||
|
||
for T in (LuxAMDGPUDevice, LuxAMDGPUDevice{Nothing}, LuxCUDADevice, | ||
LuxCUDADevice{Nothing}, LuxMetalDevice, LuxoneAPIDevice, LuxCPUDevice) | ||
@eval function Adapt.adapt_storage(to::$(T), x::AbstractArray{<:Tracker.TrackedReal}) | ||
@warn "AbstractArray{<:Tracker.TrackedReal} is not supported for $(to). Converting \ | ||
to Tracker.TrackedArray." maxlog=1 | ||
return to(Tracker.collect(x)) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using LuxDeviceUtils, ComponentArrays, Random | ||
using ArrayInterface: parameterless_type | ||
|
||
@testset "https://github.com/LuxDL/LuxDeviceUtils.jl/issues/10 patch" begin | ||
dev = LuxCPUDevice() | ||
ps = (; weight=randn(10, 1), bias=randn(1)) | ||
|
||
ps_ca = ps |> ComponentArray | ||
|
||
ps_ca_dev = ps_ca |> dev | ||
|
||
@test ps_ca_dev isa ComponentArray | ||
|
||
@test ps_ca_dev.weight == ps.weight | ||
@test ps_ca_dev.bias == ps.bias | ||
|
||
@test ps_ca_dev == (ps |> dev |> ComponentArray) | ||
end | ||
|
||
using ReverseDiff, Tracker, ForwardDiff | ||
|
||
@testset "AD Types" begin | ||
x = randn(Float32, 10) | ||
|
||
x_rdiff = ReverseDiff.track(x) | ||
@test get_device(x_rdiff) isa LuxCPUDevice | ||
x_rdiff = ReverseDiff.track.(x) | ||
@test get_device(x_rdiff) isa LuxCPUDevice | ||
|
||
gdev = gpu_device() | ||
|
||
x_tracker = Tracker.param(x) | ||
@test get_device(x_tracker) isa LuxCPUDevice | ||
x_tracker = Tracker.param.(x) | ||
@test get_device(x_tracker) isa LuxCPUDevice | ||
x_tracker_dev = Tracker.param(x) |> gdev | ||
@test get_device(x_tracker_dev) isa parameterless_type(typeof(gdev)) | ||
x_tracker_dev = Tracker.param.(x) |> gdev | ||
@test get_device(x_tracker_dev) isa parameterless_type(typeof(gdev)) | ||
|
||
x_fdiff = ForwardDiff.Dual.(x) | ||
@test get_device(x_fdiff) isa LuxCPUDevice | ||
x_fdiff_dev = ForwardDiff.Dual.(x) |> gdev | ||
@test get_device(x_fdiff_dev) isa parameterless_type(typeof(gdev)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters