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

Commit

Permalink
Fix CA in FiniteDifferences
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Feb 6, 2024
1 parent 072452b commit f186d85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LuxTestUtils"
uuid = "ac9de150-d08f-4546-94fb-7472b5760531"
authors = ["Avik Pal <[email protected]>"]
version = "0.1.14"
version = "0.1.15"

[deps]
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
Expand Down
23 changes: 17 additions & 6 deletions src/LuxTestUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ function test_gradients_expr(__module__, __source__, f, args...;
skip=skip_reverse_diff)
reverse_diff_broken = $reverse_diff_broken && !skip_reverse_diff

arr_len = length.(filter(Base.Fix2(isa, AbstractArray) __correct_arguments,
arr_len = length.(filter(Base.Fix2(isa, AbstractArray)
Base.Fix1(__correct_arguments, identity),
tuple($(esc.(args)...))))
large_arrays = any(x -> x $large_array_length, arr_len) ||
sum(arr_len) $max_total_array_size
Expand Down Expand Up @@ -333,8 +334,8 @@ end

__test_broken(test_type, orig_expr, source) = Test.Broken(test_type, orig_expr)

__correct_arguments(x::AbstractArray) = x
function __correct_arguments(x::NamedTuple)
__correct_arguments(f::F, x::AbstractArray) where {F} = x
function __correct_arguments(f::F, x::NamedTuple) where {F}
cpu_dev = cpu_device()
gpu_dev = gpu_device()
xc = cpu_dev(x)
Expand All @@ -343,19 +344,19 @@ function __correct_arguments(x::NamedTuple)
typeof(xc) == typeof(x) && return ca
return gpu_dev(ca)
end
__correct_arguments(x) = x
__correct_arguments(f::F, x) where {F} = x

__uncorrect_arguments(x::ComponentArray, ::NamedTuple, z::ComponentArray) = NamedTuple(x)
function __uncorrect_arguments(x::AbstractArray, nt::NamedTuple, z::ComponentArray)
return __uncorrect_arguments(ComponentArray(vec(x), getaxes(z)), nt, z)
end
__uncorrect_arguments(x, y, z) = x

function __gradient(gradient_function, f, args...; skip::Bool)
function __gradient(gradient_function::F, f, args...; skip::Bool) where {F}
if skip
return ntuple(_ -> GradientComputationSkipped(), length(args))
else
corrected_args = map(__correct_arguments, args)
corrected_args = map(Base.Fix1(__correct_arguments, gradient_function), args)
aa_inputs = [map(Base.Fix2(isa, AbstractArray), corrected_args)...]
__aa_input_idx = cumsum(aa_inputs)
if sum(aa_inputs) == length(args)
Expand Down Expand Up @@ -392,6 +393,16 @@ function _finitedifferences_gradient(f, args...)
args...))
end

function __correct_arguments(::typeof(_finitedifferences_gradient), x::NamedTuple)
cpu_dev = cpu_device()
gpu_dev = gpu_device()
xc = cpu_dev(x)
ca = ComponentArray(xc)
# Hacky check to see if there are any non-CPU arrays in the NamedTuple
typeof(xc) == typeof(x) && return x
return gpu_dev(x)
end

function __fdiff_compatible_function(f, ::Val{N}) where {N}
N == 1 && return f
inputs = ntuple(i -> Symbol("x.input_$i"), N)
Expand Down

0 comments on commit f186d85

Please sign in to comment.