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

Commit

Permalink
Merge pull request #16 from LuxDL/ap/fix_lux
Browse files Browse the repository at this point in the history
Fix CA in FiniteDifferences
  • Loading branch information
avik-pal authored Feb 6, 2024
2 parents 072452b + f186d85 commit 93f8860
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

2 comments on commit 93f8860

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

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 v0.1.15 -m "<description of version>" 93f8860f548f09e20caa96323122437fa62a5ccd
git push origin v0.1.15

Please sign in to comment.