Skip to content

[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 5 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions stdlib/public/Differentiation/TgmathDerivatives.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func _${derivative_kind}Trunc<T: FloatingPoint & Differentiable> (
}
%end # for derivative_kind in ['jvp', 'vjp']:

// Unary functions
%for derivative_kind in ['jvp', 'vjp']:
% linear_map_kind = 'differential' if derivative_kind == 'jvp' else 'pullback'
% for T in ['Float', 'Double', 'Float80']:
Expand Down Expand Up @@ -271,3 +272,30 @@ func _${derivative_kind}Erfc(_ x: ${T}) -> (value: ${T}, ${linear_map_kind}: (${
% end # if T == 'Float80':
% end # for T in ['Float', 'Double', 'Float80']:
%end # for derivative_kind in ['jvp', 'vjp']:

// Binary functions
%for T in ['Float', 'Float80']:
% if T == 'Float80':
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
% end
@inlinable
@derivative(of: pow)
func _vjpPow(_ x: ${T}, _ y: ${T}) -> (value: ${T}, pullback: (${T}) -> (${T}, ${T})) {
let value = pow(x, y)
return (value, { v in (
v * y * pow(x, y - 1), v * value * log(x.isLessThanOrEqualTo(0) ? ${T}(1) : x)
) })
}

@inlinable
@derivative(of: pow)
func _jvpPow(_ x: ${T}, _ y: ${T}) -> (value: ${T}, differential: (${T}, ${T}) -> ${T}) {
let value = pow(x, y)
return (value, { (dx, dy) in
dx * y * pow(x, y - 1) + dy * value * log(x.isLessThanOrEqualTo(0) ? ${T}(1) : x)
})
}
% if T == 'Float80':
#endif
% end # if T == 'Float80':
%end # for T in ['Float', 'Float80']:
97 changes: 91 additions & 6 deletions test/AutoDiff/stdlib/tgmath_derivatives.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -38,17 +38,40 @@ func expectEqualWithTolerance<T>(_ expected: TestLiteralType, _ actual: T,
file: file, line: line)
}

func computeDividedDifference<T: BinaryFloatingPoint> (
Copy link
Contributor Author

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 for reminder broke, hence I dropped that change, but maybe we should use it in the future.

_ 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']:
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I compute the needed ulps for comparing values as the resulting numerical derivative depends on the ε used to compute it.


// 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
}
}
Expand Down