Skip to content

Commit

Permalink
Some small improvements to low-level rules (#274)
Browse files Browse the repository at this point in the history
* Ensure that - is not a primitive

* Improve some select rules

* Bump patch

* Tweak rrule testing
  • Loading branch information
willtebbutt authored Sep 28, 2024
1 parent 9e390be commit 751e14e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Mooncake"
uuid = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
authors = ["Will Tebbutt, Hong Ge, and contributors"]
version = "0.4.3"
version = "0.4.4"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
12 changes: 10 additions & 2 deletions src/rrules/low_level_maths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ for (M, f, arity) in DiffRules.diffrules(; filter_modules=nothing)
continue # Skip rules for methods not defined in the current scope
end
(f == :rem2pi || f == :ldexp) && continue # not designed for Float64s
(f in [:+, :*, :sin, :cos, :exp]) && continue # use other functionality to implement these
(f in [:+, :*, :sin, :cos, :exp, :-, :abs2, :inv, :abs, :/, :\]) && continue # use other functionality to implement these
if arity == 1
dx = DiffRules.diffrule(M, f, :x)
pb_name = Symbol("$(M).$(f)_pb!!")
Expand Down Expand Up @@ -84,11 +84,19 @@ function generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:low_level_mat
end
arity > 2 && return
(f == :rem2pi || f == :ldexp || f == :(^)) && return
(f == :+ || f == :*) && return # use intrinsics instead
(f in [:+, :*, :sin, :cos, :exp, :-, :abs2, :inv, :abs, :/, :\]) && return # use other functionality to implement these
f = @eval $M.$f
push!(test_cases, (false, :stability, nothing, f, rand_inputs(rng, Float64, f, arity)...))
push!(test_cases, (true, :stability, nothing, f, rand_inputs(rng, Float32, f, arity)...))
end

# test cases for additional rules written in this file.
push!(test_cases, (false, :stability_and_allocs, nothing, sin, 1.1))
push!(test_cases, (true, :stability_and_allocs, nothing, sin, Float32(1.1)))
push!(test_cases, (false, :stability_and_allocs, nothing, cos, 1.1))
push!(test_cases, (true, :stability_and_allocs, nothing, cos, Float32(1.1)))
push!(test_cases, (false, :stability_and_allocs, nothing, exp, 1.1))
push!(test_cases, (true, :stability_and_allocs, nothing, exp, Float32(1.1)))
memory = Any[]
return test_cases, memory
end
Expand Down
4 changes: 3 additions & 1 deletion test/front_matter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ using Mooncake:
InvalidFDataException,
InvalidRDataException,
verify_fdata_value,
verify_rdata_value
verify_rdata_value,
is_primitive,
MinimalCtx

using .TestUtils:
test_rule,
Expand Down
17 changes: 17 additions & 0 deletions test/rrules/low_level_maths.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
@testset "low_level_maths" begin
TestUtils.run_rrule!!_test_cases(StableRNG, Val(:low_level_maths))

# These are all examples of signatures which we do _not_ want to make primitives,
# because they are very shallow wrappers around lower-level primitives for which we
# already have rules.
@testset "$T, $C" for T in [Float16, Float32, Float64], C in [DefaultCtx, MinimalCtx]
@test !is_primitive(C, Tuple{typeof(+), T})
@test !is_primitive(C, Tuple{typeof(-), T})
@test !is_primitive(C, Tuple{typeof(abs2), T})
@test !is_primitive(C, Tuple{typeof(inv), T})
@test !is_primitive(C, Tuple{typeof(abs), T})

@test !is_primitive(C, Tuple{typeof(+), T, T})
@test !is_primitive(C, Tuple{typeof(-), T, T})
@test !is_primitive(C, Tuple{typeof(*), T, T})
@test !is_primitive(C, Tuple{typeof(/), T, T})
@test !is_primitive(C, Tuple{typeof(\), T, T})
end
end

2 comments on commit 751e14e

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

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.4.4 -m "<description of version>" 751e14e424ad418cfc846e347f962d3d530826c0
git push origin v0.4.4

Please sign in to comment.