Skip to content

Commit

Permalink
NoPullback for hash (#240)
Browse files Browse the repository at this point in the history
* Add failing tests

* Make tests pass

* Bump patch

* Tweak CoDual _copy method

* Add failing test

* Fix rdata_type method

* Tidy up + fix fdata_type
  • Loading branch information
willtebbutt authored Sep 2, 2024
1 parent ce24e00 commit 1ed7b40
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tapir"
uuid = "07d77754-e150-4737-8c94-cd238a1fb45b"
authors = ["Will Tebbutt, Hong Ge, and contributors"]
version = "0.2.43"
version = "0.2.44"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
2 changes: 1 addition & 1 deletion src/codual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end
primal(x::CoDual) = x.x
tangent(x::CoDual) = x.dx
Base.copy(x::CoDual) = CoDual(copy(primal(x)), copy(tangent(x)))
_copy(x::P) where {P<:CoDual} = P(_copy(x.x), _copy(x.dx))
_copy(x::P) where {P<:CoDual} = x

"""
zero_codual(x)
Expand Down
12 changes: 8 additions & 4 deletions src/fwds_rvs_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,10 @@ fdata_type(::Type{T}) where {T<:Ptr} = T
isa(P, Union) && return Union{fdata_type(P.a), fdata_type(P.b)}
isempty(P.parameters) && return NoFData
isa(last(P.parameters), Core.TypeofVararg) && return Any
all(p -> fdata_type(p) == NoFData, P.parameters) && return NoFData
return Tuple{map(fdata_type, fieldtypes(P))...}
nofdata_tt = Tuple{Vararg{NoFData, length(P.parameters)}}
fdata_tt = Tuple{map(fdata_type, fieldtypes(P))...}
fdata_tt <: nofdata_tt && return NoFData
return nofdata_tt <: fdata_tt ? Union{NoFData, fdata_tt} : fdata_tt
end

@generated function fdata_type(::Type{NamedTuple{names, T}}) where {names, T<:Tuple}
Expand Down Expand Up @@ -444,8 +446,10 @@ rdata_type(::Type{<:Ptr}) = NoRData
isa(P, Union) && return Union{rdata_type(P.a), rdata_type(P.b)}
isempty(P.parameters) && return NoRData
isa(last(P.parameters), Core.TypeofVararg) && return Any
all(p -> rdata_type(p) == NoRData, P.parameters) && return NoRData
return Tuple{map(rdata_type, fieldtypes(P))...}
nordata_tt = Tuple{Vararg{NoRData, length(P.parameters)}}
rdata_tt = Tuple{map(rdata_type, fieldtypes(P))...}
rdata_tt <: nordata_tt && return NoRData
return nordata_tt <: rdata_tt ? Union{NoRData, rdata_tt} : rdata_tt
end

function rdata_type(::Type{NamedTuple{names, T}}) where {names, T<:Tuple}
Expand Down
11 changes: 6 additions & 5 deletions src/rrules/foreigncall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,12 @@ function rrule!!(f::CoDual{<:Type{UnionAll}}, x::CoDual{<:TypeVar}, y::CoDual{<:
return zero_fcodual(UnionAll(primal(x), primal(y))), NoPullback(f, x, y)
end

@is_primitive MinimalCtx Tuple{typeof(hash), Union{String, SubString{String}}, UInt}
function rrule!!(
f::CoDual{typeof(hash)}, s::CoDual{P}, h::CoDual{UInt}
) where {P<:Union{String, SubString{String}}}
return zero_fcodual(hash(primal(s), primal(h))), NoPullback(f, s, h)
@is_primitive MinimalCtx Tuple{typeof(hash), Vararg}
function rrule!!(f::CoDual{typeof(hash)}, x::Vararg{CoDual, N}) where {N}
return zero_fcodual(hash(map(primal, x)...)), NoPullback(f, x...)
end


function rrule!!(
::CoDual{typeof(_foreigncall_)}, ::CoDual{Val{:jl_string_ptr}}, args::Vararg{CoDual, N}
) where {N}
Expand Down Expand Up @@ -613,6 +612,8 @@ function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:foreigncall})
(false, :stability, nothing, deepcopy, (a=5.0, b=randn(5))),
(false, :none, nothing, UnionAll, TypeVar(:a), Real),
(false, :none, nothing, hash, "5", UInt(3)),
(false, :none, nothing, hash, Float64, UInt(5)),
(false, :none, nothing, hash, Float64),
(
true, :none, nothing,
_foreigncall_,
Expand Down
15 changes: 15 additions & 0 deletions test/fwds_rvs_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ module FwdsRvsDataTestResources
end

@testset "fwds_rvs_data" begin
@testset "fdata_type / rdata_type($P)" for (P, F, R) in Any[
(
Tuple{Any, Vector{Float64}},
Tuple{Any, Vector{Float64}},
Union{NoRData, Tuple{Any, NoRData}},
),
(
Tuple{Any, Float64},
Union{NoFData, Tuple{Any, NoFData}},
Tuple{Any, Float64},
),
]
@test fdata_type(tangent_type(P)) == F
@test rdata_type(tangent_type(P)) == R
end
@testset "$(typeof(p))" for (_, p, _...) in Tapir.tangent_test_cases()
TestUtils.test_fwds_rvs_data(Xoshiro(123456), p)
end
Expand Down

2 comments on commit 1ed7b40

@willtebbutt
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/114386

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.2.44 -m "<description of version>" 1ed7b400e0013cee5ba79e21226ea0469737f6c8
git push origin v0.2.44

Please sign in to comment.