Skip to content

Commit

Permalink
Handle undefined type parameters in fcodual_type and implement rule f…
Browse files Browse the repository at this point in the history
…or fpext (#225)

* Fix undefined type parameter problem

* Add support for fpext

* Bump patch version

* Remove redundant test case
  • Loading branch information
willtebbutt authored Aug 12, 2024
1 parent 8aad2d0 commit a896e0d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 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.33"
version = "0.2.34"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
8 changes: 6 additions & 2 deletions src/codual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function codual_type(::Type{P}) where {P}
return isconcretetype(P) ? CoDual{P, tangent_type(P)} : CoDual
end

codual_type(::Type{Type{P}}) where {P} = CoDual{Type{P}, NoTangent}
function codual_type(p::Type{Type{P}}) where {P}
return @isdefined(P) ? CoDual{Type{P}, NoTangent} : CoDual{_typeof(p), NoTangent}
end

struct NoPullback{R<:Tuple}
r::R
Expand Down Expand Up @@ -86,6 +88,8 @@ function fcodual_type(::Type{P}) where {P}
return isconcretetype(P) ? CoDual{P, fdata_type(tangent_type(P))} : CoDual
end

fcodual_type(::Type{Type{P}}) where {P} = CoDual{Type{P}, NoFData}
function fcodual_type(p::Type{Type{P}}) where {P}
return @isdefined(P) ? CoDual{Type{P}, NoFData} : CoDual{_typeof(p), NoFData}
end

zero_rdata(x::CoDual) = zero_rdata(primal(x))
12 changes: 9 additions & 3 deletions src/rrules/builtins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,13 @@ function rrule!!(::CoDual{typeof(fma_float)}, x, y, z)
return CoDual(fma_float(_x, _y, primal(z)), NoFData()), fma_float_pullback!!
end

# fpext -- maybe interesting
@intrinsic fpext
function rrule!!(
::CoDual{typeof(fpext)}, ::CoDual{Type{Pext}}, x::CoDual{P}
) where {Pext<:IEEEFloat, P<:IEEEFloat}
fpext_adjoint!!(dy::Pext) = NoRData(), NoRData(), fptrunc(P, dy)
return zero_fcodual(fpext(Pext, primal(x))), fpext_adjoint!!
end

@inactive_intrinsic fpiseq
@inactive_intrinsic fptosi
Expand All @@ -251,7 +257,7 @@ end
function rrule!!(
::CoDual{typeof(fptrunc)}, ::CoDual{Type{Ptrunc}}, x::CoDual{P}
) where {Ptrunc<:IEEEFloat, P<:IEEEFloat}
fptrunc_adjoint!!(dy) = NoRData(), NoRData(), convert(P, dy)
fptrunc_adjoint!!(dy::Ptrunc) = NoRData(), NoRData(), convert(P, dy)
return zero_fcodual(fptrunc(Ptrunc, primal(x))), fptrunc_adjoint!!
end

Expand Down Expand Up @@ -823,7 +829,7 @@ function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:builtins})
(false, :stability, nothing, IntrinsicsWrappers.flipsign_int, 4, -3),
(false, :stability, nothing, IntrinsicsWrappers.floor_llvm, 4.1),
(false, :stability, nothing, IntrinsicsWrappers.fma_float, 5.0, 4.0, 3.0),
# fpext -- NEEDS IMPLEMENTING AND TESTING
(true, :stability_and_allocs, nothing, IntrinsicsWrappers.fpext, Float64, 5f0),
(false, :stability, nothing, IntrinsicsWrappers.fpiseq, 4.1, 4.0),
(false, :stability, nothing, IntrinsicsWrappers.fptosi, UInt32, 4.1),
(false, :stability, nothing, IntrinsicsWrappers.fptoui, Int32, 4.1),
Expand Down
8 changes: 8 additions & 0 deletions test/codual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
@test codual_type(Real) == CoDual
@test codual_type(Any) == CoDual
@test codual_type(Type{UnitRange{Int}}) == CoDual{Type{UnitRange{Int}}, NoTangent}
@test ==(
codual_type(Type{Tuple{T}} where {T}),
CoDual{Type{Type{Tuple{T}} where {T}}, NoTangent},
)
@test ==(
Tapir.fcodual_type(Type{Tuple{T}} where {T}),
CoDual{Type{Type{Tuple{T}} where {T}}, NoFData},
)
@test(==(
codual_type(Union{Float64, Int}),
Union{CoDual{Float64, Float64}, CoDual{Int, NoTangent}},
Expand Down

2 comments on commit a896e0d

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

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.34 -m "<description of version>" a896e0d5a64593ce2cfe9ab1487597c25385358c
git push origin v0.2.34

Please sign in to comment.