-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Controlling which optimizer rule you're adjusting. #202
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -144,3 +144,33 @@ function _adjust(r::T, nt::NamedTuple) where T <: AbstractRule | |||||||||||||||
end | ||||||||||||||||
T(vals...) # relies on having the default constructor | ||||||||||||||||
end | ||||||||||||||||
|
||||||||||||||||
### | ||||||||||||||||
#adjust with type control | ||||||||||||||||
### | ||||||||||||||||
|
||||||||||||||||
adjust!(ℓ::Leaf, oT::Type, eta::Real) = (ℓ.rule = adjust(ℓ.rule, oT, eta); nothing) | ||||||||||||||||
adjust!(ℓ::Leaf, oT::Type; kw...) = (ℓ.rule = adjust(ℓ.rule, oT; kw...); nothing) | ||||||||||||||||
|
||||||||||||||||
adjust(ℓ::Leaf, oT::Type, eta::Real) = Leaf(adjust(ℓ.rule, oT, eta), ℓ.state, ℓ.frozen) | ||||||||||||||||
adjust(ℓ::Leaf, oT::Type; kw...) = Leaf(adjust(ℓ.rule, oT; kw...), ℓ.state, ℓ.frozen) | ||||||||||||||||
|
||||||||||||||||
adjust!(tree, oT::Type, eta::Real) = foreach(st -> adjust!(st, oT, eta), tree) | ||||||||||||||||
adjust!(tree, oT::Type; kw...) = foreach(st -> adjust!(st, oT; kw...), tree) | ||||||||||||||||
|
||||||||||||||||
adjust(r::AbstractRule, oT::Type, eta::Real) = ifelse(isa(r, oT), adjust(r, eta), r) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These overloads are allocating the output of
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would guess the branch is resolved when compiling? Possibly it will prefer this style:
Suggested change
I think you can also make dispatch do this, although whether it's a good idea I don't know... less obvious to read:
Edit, less obscure:
Suggested change
|
||||||||||||||||
adjust(r::AbstractRule, oT::Type; kw...) = ifelse(isa(r, oT), adjust(r; kw...), r) | ||||||||||||||||
|
||||||||||||||||
adjust!(r::AbstractRule, oT::Type, eta::Real) = ifelse(isa(r, oT), adjust!(r, eta), r) | ||||||||||||||||
adjust!(r::AbstractRule, oT::Type; kw...) = ifelse(isa(r, oT), adjust!(r; kw...), r) | ||||||||||||||||
|
||||||||||||||||
function adjust(tree, oT::Type, eta::Real) | ||||||||||||||||
t′ = fmap(copy, tree; exclude = maywrite) | ||||||||||||||||
adjust!(t′, oT, eta) | ||||||||||||||||
t′ | ||||||||||||||||
end | ||||||||||||||||
function adjust(tree, oT::Type; kw...) | ||||||||||||||||
t′ = fmap(copy, tree; exclude = maywrite) | ||||||||||||||||
adjust!(t′, oT; kw...) | ||||||||||||||||
t′ | ||||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can all the
Type
s here be changed toType{<:AbstractRule}
? It would provide some more information for users of the interface and ensure we're not promising support for arbitrary types.