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
[AutoDiff] Registers VJPs for FloatingPoint.[maximum|minimum]
#35379
Conversation
Resolves TF-1134.
static func _vjpMinimum(_ x: Self, _ y: Self) -> ( | ||
value: Self, pullback: (TangentVector) -> (TangentVector, TangentVector) | ||
) { | ||
func pullback(_ v: TangentVector) -> (TangentVector, TangentVector) { |
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.
This pullback is defined according to the logic of FloatingPoint.minimum
so that the corner cases for returning either x
or y
are respected.
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.
This implementation is running the identical control flow in the VJP and the pullback. It should be simplified so that the pullback is just straight line code and the control flow is executed only once. You can do:
@inlinable
@derivative(of: minimum)
static func _vjpMinimum(_ x: Self, _ y: Self) -> (
value: Self, pullback: (TangentVector) -> (TangentVector, TangentVector)
) {
if x <= y || y.isNaN { return (x, { v in (v, .zero) }) }
return (y, { v in (.zero, v) })
}
The same suggestion applies to maximum
.
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.
Ah I see.. yes, thanks for the remark @rxwei .. i am fixing that.
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.
fixed in bc40409.
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.
Thanks.
static func _vjpMaximum(_ x: Self, _ y: Self) -> ( | ||
value: Self, pullback: (TangentVector) -> (TangentVector, TangentVector) | ||
) { | ||
func pullback(_ v: TangentVector) -> (TangentVector, TangentVector) { |
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.
same comment as above but for FloatingPoint.maximum
.
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.
IANAM (I am not a mathematician) but LGTM! Let me ping one.
@swift-ci Please test |
To avoid executing control flow code twice ( once in VJP and once in the pullback ).
@swift-ci please test |
Resolves TF-1134.