-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Defines derivatives for remaining tgmath math functions. #27559
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
Conversation
* Inverse trigonometric functions. * Exponents and logarithms. * Hyperbolic functions. * Error functions ( erf, erfc ).
test/stdlib/tgmath.swift.gyb
Outdated
@@ -248,10 +248,26 @@ MathTests.test("${T}") { | |||
% for T in ['Float', 'Float80']: | |||
MathTests.test("gradient_${T}") { | |||
expectEqualWithTolerance(7.3890560989306502274, gradient(at: 2.0 as ${T}, in: exp), ulps:16) | |||
expectEqualWithTolerance(2.772588722239781145, gradient(at: 2.0 as ${T}, in: exp2), ulps:16) |
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.
expectEqualWithTolerance(2.772588722239781145, gradient(at: 2.0 as ${T}, in: exp2), ulps:16) | |
expectEqualWithTolerance(2.772588722239781145, gradient(at: 2.0 as ${T}, in: exp2), ulps: 16) |
Same for all other occurrences.
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 on c92e34b
|
||
@usableFromInline | ||
func _vjpLog2(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||
return (log2(x), { v in v / (${T}(M_LN2) * x)}) |
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.
Indent by 2.
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 on c92e34b
with respective tests.
public func sqrt<T: FloatingPoint>(_ x: T) -> T { | ||
return x.squareRoot() | ||
} | ||
|
||
@_transparent | ||
@differentiable( |
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.
@differentiable( | |
// SWIFT_ENABLE_TENSORFLOW | |
@differentiable( |
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.
done in 27ca6a2
@@ -20,11 +20,20 @@ public func fabs<T: FloatingPoint>(_ x: T) -> T { | |||
} | |||
|
|||
@_transparent | |||
@differentiable( |
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.
@differentiable( | |
// SWIFT_ENABLE_TENSORFLOW | |
@differentiable( |
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.
done in 27ca6a2
@swift-ci please test tensorflow |
_ z: T | ||
) -> (T, (T) -> (T, T, T)) where T == T.TangentVector { | ||
return (fma(x, y, z), | ||
{v in return (v * y, v * x, v)}) |
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.
- Move this to the end of the previous line.
- Add a space after each
{
before each}
. Same for all other occurrences. - Omit
return
when a closure has a single expression.
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.
done in 6b1ebf2
_ x: T | ||
) -> (T, (T) -> T) where T == T.TangentVector { | ||
let value = x.squareRoot() | ||
return (value, {v in (1 / 2) * ( 1 / value) * v}) |
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.
return (value, {v in (1 / 2) * ( 1 / value) * v}) | |
return (value, { v in v / (2 * value) }) |
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.
done in 6b1ebf2
@usableFromInline | ||
func _vjpLog(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||
return (log(x), { v in v / x }) | ||
} | ||
|
||
@usableFromInline | ||
func _vjpLog10(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||
return (log10(x), { v in v * ${T}(M_LOG10E) / x}) |
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.
return (log10(x), { v in v * ${T}(M_LOG10E) / x}) | |
return (log10(x), { v in v * ${T}(M_LOG10E) / x }) |
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.
done in 6b1ebf2
|
||
@usableFromInline | ||
func _vjpLog2(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||
return (log2(x), { v in v / (${T}(M_LN2) * x)}) |
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.
return (log2(x), { v in v / (${T}(M_LN2) * x)}) | |
return (log2(x), { v in v / (${T}(M_LN2) * x) }) |
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.
done in 6b1ebf2
|
||
@usableFromInline | ||
func _vjpLog1p(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||
return (log1p(x), { v in v / ( x + 1) }) |
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.
return (log1p(x), { v in v / ( x + 1) }) | |
return (log1p(x), { v in v / (x + 1) }) |
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.
done in 6b1ebf2
test/stdlib/tgmath.swift.gyb
Outdated
expectEqualWithTolerance(0.020666985354092053575, gradient(at: 2.0 as ${T}, in: erf), ulps: 16) | ||
expectEqualWithTolerance(-0.020666985354092053575, gradient(at: 2.0 as ${T}, in: erfc), ulps: 16) | ||
expectEqualWithTolerance(0.35355339059327376222, gradient(at: 2.0 as ${T}, in: {x in sqrt(x)}), ulps: 16) | ||
let fma_grad = gradient(at: 4.0 as ${T}, 5.0 as ${T}, 6.0 as ${T}, in: {x, y, z in fma(x, y, z)}) |
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.
let fma_grad = gradient(at: 4.0 as ${T}, 5.0 as ${T}, 6.0 as ${T}, in: {x, y, z in fma(x, y, z)}) | |
let fmaGrad = gradient(at: 4.0 as ${T}, 5.0 as ${T}, 6.0 as ${T}, in: { x, y, z in fma(x, y, z) }) |
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.
done in 6b1ebf2
test/stdlib/tgmath.swift.gyb
Outdated
expectEqualWithTolerance(1.3333333333333333334, gradient(at: 0.5 as ${T}, in: atanh), ulps: 16) | ||
expectEqualWithTolerance(0.020666985354092053575, gradient(at: 2.0 as ${T}, in: erf), ulps: 16) | ||
expectEqualWithTolerance(-0.020666985354092053575, gradient(at: 2.0 as ${T}, in: erfc), ulps: 16) | ||
expectEqualWithTolerance(0.35355339059327376222, gradient(at: 2.0 as ${T}, in: {x in sqrt(x)}), ulps: 16) |
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.
expectEqualWithTolerance(0.35355339059327376222, gradient(at: 2.0 as ${T}, in: {x in sqrt(x)}), ulps: 16) | |
expectEqualWithTolerance(0.35355339059327376222, gradient(at: 2.0 as ${T}, in: { sqrt($0) }), ulps: 16) |
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.
done in 6b1ebf2
test/stdlib/tgmath.swift.gyb
Outdated
expectEqualWithTolerance(0.020666985354092053575, gradient(at: 2.0 as ${T}, in: erf), ulps: 16) | ||
expectEqualWithTolerance(-0.020666985354092053575, gradient(at: 2.0 as ${T}, in: erfc), ulps: 16) | ||
expectEqualWithTolerance(0.35355339059327376222, gradient(at: 2.0 as ${T}, in: {x in sqrt(x)}), ulps: 16) | ||
let fma_grad = gradient(at: 4.0 as ${T}, 5.0 as ${T}, 6.0 as ${T}, in: {x, y, z in fma(x, y, z)}) |
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.
Always use camel case.
let fma_grad = gradient(at: 4.0 as ${T}, 5.0 as ${T}, 6.0 as ${T}, in: {x, y, z in fma(x, y, z)}) | |
let fmaGrad = gradient(at: 4.0 as ${T}, 5.0 as ${T}, 6.0 as ${T}, in: {x, y, z in fma(x, y, z)}) |
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.
done in 6b1ebf2
@stephentyrone Could you help review the usage of math constants in these derivatives? |
@swift-ci please test tensorflow |
1 similar comment
@swift-ci please test tensorflow |
Partially resolves TF-812.