-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff][TF-1200] Adding derivatives for stdlib pow
function.
#30580
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5cb9fd2
[AutoDiff][TF-1200] Adding derivatives for stdlib `pow` function.
vguerra ba46016
Binary functions only defined for `Float` and `Float80`
vguerra 476319e
Fixing VJP and JVP for pow.Extending test cases.
vguerra dbc3396
remove `Double` from comment.
vguerra 24939a5
Splitting `pow`'s derivative test cases.
vguerra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ func expectEqualWithTolerance<T>(_ expected: TestLiteralType, _ actual: T, | |
ulps allowed: T = 3, | ||
file: String = #file, line: UInt = #line) | ||
where T: BinaryFloatingPoint { | ||
if actual == T(expected) || actual.isNaN && expected.isNaN { | ||
if actual == T(expected) || actual.isNaN && expected.isNaN || actual.isInfinite && expected.isInfinite { | ||
return | ||
} | ||
// Compute error in ulp, compare to tolerance. | ||
|
@@ -38,17 +38,40 @@ func expectEqualWithTolerance<T>(_ expected: TestLiteralType, _ actual: T, | |
file: file, line: line) | ||
} | ||
|
||
func computeDividedDifference<T: BinaryFloatingPoint> ( | ||
_ f: (T, T) -> T, | ||
_ x: T, | ||
_ y: T, | ||
eps: T = 0.01 | ||
) -> (dfdx: T, dfdy: T) { | ||
let dfdx = (f(x + eps, y) - f(x, y)) / eps | ||
let dfdy = (f(x, y + eps) - f(x, y)) / eps | ||
return (dfdx, dfdy) | ||
} | ||
|
||
func checkGradient<T: BinaryFloatingPoint & Differentiable>( | ||
_ f: @differentiable (T, T) -> T, | ||
_ x: T, | ||
_ y: T) | ||
_ y: T, | ||
ulps: T = 192) | ||
where T == T.TangentVector { | ||
let eps = T(0.01) | ||
let grad = gradient(at: x, y, in: f) | ||
let dfdx = (f(x + eps, y) - f(x, y)) / eps | ||
let dfdy = (f(x, y + eps) - f(x, y)) / eps | ||
expectEqualWithTolerance(TestLiteralType(dfdx), grad.0, ulps: 192) | ||
expectEqualWithTolerance(TestLiteralType(dfdy), grad.1, ulps: 192) | ||
let (dfdx, dfdy) = computeDividedDifference(f, x, y, eps: eps) | ||
expectEqualWithTolerance(TestLiteralType(dfdx), grad.0, ulps: ulps) | ||
expectEqualWithTolerance(TestLiteralType(dfdy), grad.1, ulps: ulps) | ||
} | ||
|
||
func checkDerivative<T: BinaryFloatingPoint & Differentiable>( | ||
_ f: @differentiable (T, T) -> T, | ||
_ x: T, | ||
_ y: T, | ||
ulps: T = 192) | ||
where T == T.TangentVector { | ||
let eps = T(0.01) | ||
let deriv = derivative(at: x, y, in: f) | ||
let (dfdx, dfdy) = computeDividedDifference(f, x, y, eps: eps) | ||
expectEqualWithTolerance(TestLiteralType(dfdx + dfdy), deriv, ulps: ulps) | ||
} | ||
|
||
%for op in ['derivative', 'gradient']: | ||
|
@@ -111,6 +134,68 @@ DerivativeTests.test("${op}_${T}") { | |
checkGradient({ fmod($0, $1) }, x, y) | ||
%else: # if op == 'derivative' | ||
// TODO(TF-1108): Implement JVPs for `remainder` and `fmod`. | ||
%end | ||
} | ||
} | ||
|
||
// pow | ||
let eps:${T} = 0.01 | ||
let ulps:${T} = eps/eps.ulp | ||
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. here I compute the needed |
||
|
||
// Checks for negative base. | ||
for a in -3..<0 { | ||
let x = ${T}(a) | ||
for b in -3...3 { | ||
let y = ${T}(b) | ||
let expectedDx = y * pow(x, y - 1) | ||
let expectedDy = ${T}.zero | ||
let dpow = ${op}(at: x, y, in: pow) | ||
%if op == 'gradient': | ||
expectEqualWithTolerance(TestLiteralType(expectedDx), dpow.0) | ||
expectEqualWithTolerance(TestLiteralType(expectedDy), dpow.1) | ||
%else: # if op == 'derivative' | ||
expectEqualWithTolerance(TestLiteralType(expectedDx + expectedDy), dpow) | ||
%end | ||
} | ||
} | ||
|
||
// Checks for 0 base. | ||
for b in -3...3 { | ||
let y = ${T}(b) | ||
var expectedValues: (dx: ${T}, dy: ${T})? | ||
if y.isLess(than: 0) { | ||
expectedValues = (dx: ${T}.infinity, dy: ${T}.nan) | ||
} else if y.isZero { | ||
expectedValues = (dx: ${T}.nan, dy: ${T}.zero) | ||
} else if !y.isEqual(to: 1) { | ||
expectedValues = (dx: ${T}.zero, dy: ${T}.zero) | ||
} | ||
if let (expectedDx, expectedDy) = expectedValues { | ||
let dpow = ${op}(at: 0.0, y, in: pow) | ||
%if op == 'gradient': | ||
expectEqualWithTolerance(TestLiteralType(expectedDx), dpow.0) | ||
expectEqualWithTolerance(TestLiteralType(expectedDy), dpow.1) | ||
%else: # if op == 'derivative' | ||
expectEqualWithTolerance(TestLiteralType(expectedDx + expectedDy), dpow) | ||
%end | ||
} else { | ||
%if op == 'gradient': | ||
checkGradient({ pow($0, $1) }, 0.0, y, ulps: ulps) | ||
%else: # if op == 'derivative' | ||
checkDerivative({ pow($0, $1) }, 0.0, y, ulps: ulps) | ||
%end | ||
} | ||
} | ||
|
||
// Checks for positive base. | ||
for a in 1...3 { | ||
let x = ${T}(a) | ||
for b in -3...3 { | ||
let y = ${T}(b) | ||
%if op == 'gradient': | ||
checkGradient({ pow($0, $1) }, x, y, ulps: ulps) | ||
%else: # if op == 'derivative' | ||
checkDerivative({ pow($0, $1) }, x, y, ulps: ulps) | ||
%end | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I tried to use instead symmetric derivative formula to compute the numerical derivative, which gives a more precise result hence allowing to use lower values for
ulps
but some test cases forreminder
broke, hence I dropped that change, but maybe we should use it in the future.