Skip to content

Commit eccb894

Browse files
committed
wrap up code review
1 parent ecd2bb6 commit eccb894

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

docs/src/autodiff/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ There are 3 main ways to access ChainRules rule sets in your AutoDiff system.
1919
- If an applicable `rrule`/`frule` exists in the method table then use it, else generate normal AD path.
2020
- This avoids having branches in your generated code.
2121
- This requires maintaining your own back-edges.
22-
- This is pretty hard-code even by the standard of source code tranformations
22+
- This is pretty hardcore even by the standard of source code tranformations.

src/rules.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ true
9191
9292
See also: [`frule`](@ref), [`@scalar_rule`](@ref)
9393
"""
94-
rrule(::Any, ::Vararg{Any}; kwargs...) = nothing
94+
rrule(::Any, ::Vararg{Any}; kwargs...) = nothing

src/ruleset_loading.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ function __init__()
44
push!(Base.package_callbacks, pkgid -> refresh_rules())
55
end
66

7-
8-
const NEW_RRULE_HOOKS = Function[]
9-
const NEW_FRULE_HOOKS = Function[]
10-
_hook_list(::typeof(rrule)) = NEW_RRULE_HOOKS
11-
_hook_list(::typeof(frule)) = NEW_FRULE_HOOKS
7+
# Holds all the hook functions that are invokes when a new rule is defined
8+
const RRULE_DEFINITION_HOOKS = Function[]
9+
const FRULE_DEFINITION_HOOKS = Function[]
10+
_hook_list(::typeof(rrule)) = RRULE_DEFINITION_HOOKS
11+
_hook_list(::typeof(frule)) = FRULE_DEFINITION_HOOKS
1212

1313
"""
1414
on_new_rule(hook, frule | rrule)
1515
1616
Register a `hook` function to run when new rules are defined.
1717
The hook receives a signature type-type as input, and generally will use `eval` to define
18-
and overload of AD systems overloaded type.
18+
an overload of an AD system's overloaded type
1919
For example, using the signature type `Tuple{typeof(+), Real, Real}` to make
2020
`+(::DualNumber, ::DualNumber)` call the `frule` for `+`.
2121
A signature type tuple always has the form:
22-
`Tuple{typeof(operation), typeof{pos_arg1}, typeof{pos_arg2...}}`, where `pos_arg1` is the
22+
`Tuple{typeof(operation), typeof{pos_arg1}, typeof{pos_arg2}...}`, where `pos_arg1` is the
2323
first positional argument.
2424
2525
The hooks are automatically run on new rules whenever a package is loaded.
@@ -76,8 +76,13 @@ It is *automatically* run when ever a package is loaded.
7676
It can also be manually called to run it directly, for example if a rule was defined
7777
in the REPL or within the same file as the AD function.
7878
"""
79-
refresh_rules() = (refresh_rules(frule); refresh_rules(rrule))
79+
function refresh_rules()
80+
refresh_rules(frule);
81+
refresh_rules(rrule)
82+
end
83+
8084
function refresh_rules(rule_kind)
85+
isempty(_rule_list(rule_kind)) && return # if no hooks, exit early, nothing to run
8186
already_done_world_age = last_refresh(rule_kind)[]
8287
for method in _rule_list(rule_kind)
8388
_defined_world(method) < already_done_world_age && continue

0 commit comments

Comments
 (0)