-
Notifications
You must be signed in to change notification settings - Fork 10.6k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79e0fea
Defines derivatives for remaning tgmath math functions.
vguerra c92e34b
addressing feedback on formating
vguerra 62c019b
Adding derivatives for generic functions: sqrt and fma.
vguerra 27ca6a2
adding SWIFT_ENABLE_TENSORFLOW comments where needed
vguerra 6b1ebf2
addressing formating issues
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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,11 +20,22 @@ public func fabs<T: FloatingPoint>(_ x: T) -> T { | |||||||
| } | ||||||||
|
|
||||||||
| @_transparent | ||||||||
| // SWIFT_ENABLE_TENSORFLOW | ||||||||
| @differentiable( | ||||||||
| vjp: _vjpSqrt | ||||||||
| where T : Differentiable & FloatingPoint, T == T.TangentVector | ||||||||
| ) | ||||||||
| public func sqrt<T: FloatingPoint>(_ x: T) -> T { | ||||||||
| return x.squareRoot() | ||||||||
| } | ||||||||
|
|
||||||||
| @_transparent | ||||||||
| // SWIFT_ENABLE_TENSORFLOW | ||||||||
| @differentiable( | ||||||||
|
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.
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. done in 27ca6a2 |
||||||||
| wrt: (x, y, z), | ||||||||
| vjp: _vjpFma | ||||||||
| where T : Differentiable & FloatingPoint, T == T.TangentVector | ||||||||
| ) | ||||||||
| public func fma<T: FloatingPoint>(_ x: T, _ y: T, _ z: T) -> T { | ||||||||
| return z.addingProduct(x, y) | ||||||||
| } | ||||||||
|
|
@@ -82,6 +93,24 @@ public func frexp<T: BinaryFloatingPoint>(_ x: T) -> (T, Int) { | |||||||
| return (x.significand / 2, Int(x.exponent + 1)) | ||||||||
| } | ||||||||
|
|
||||||||
| // SWIFT_ENABLE_TENSORFLOW | ||||||||
| @usableFromInline | ||||||||
| func _vjpSqrt<T: FloatingPoint & Differentiable> ( | ||||||||
| _ x: T | ||||||||
| ) -> (T, (T) -> T) where T == T.TangentVector { | ||||||||
| let value = x.squareRoot() | ||||||||
| return (value, { v in v / (2 * value) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpFma<T: FloatingPoint & Differentiable> ( | ||||||||
| _ x: T, | ||||||||
| _ y: T, | ||||||||
| _ z: T | ||||||||
| ) -> (T, (T) -> (T, T, T)) where T == T.TangentVector { | ||||||||
| return (fma(x, y, z), { v in (v * y, v * x, v) }) | ||||||||
| } | ||||||||
|
|
||||||||
| %for T in ['Float','Double']: | ||||||||
| @available(swift, deprecated: 4.2, renamed: "scalbn") | ||||||||
| @_transparent | ||||||||
|
|
@@ -102,11 +131,27 @@ func _vjpExp(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | |||||||
| return (value, { v in value * v }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpExp2(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| let value = exp2(x) | ||||||||
| return (value, { v in v * ${T}(M_LN2) * value }) | ||||||||
| } | ||||||||
|
|
||||||||
| @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 }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpLog2(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (log2(x), { v in v / (${T}(M_LN2) * x) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpSin(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (sin(x), { v in v * cos(x) }) | ||||||||
|
|
@@ -122,6 +167,72 @@ func _vjpTan(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | |||||||
| let value = tan(x) | ||||||||
| return (value, { v in v * (1 + value * value) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpAsin(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (asin(x), { v in v / sqrt(1 - x * x) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpAcos(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (acos(x), { v in -v / sqrt(1 - x * x) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpAtan(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (atan(x), { v in v / (1 + x * x) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpSinh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (sinh(x), { v in v * cosh(x) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpCosh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (cosh(x), { v in v * sinh(x) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpTanh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| let value = tanh(x) | ||||||||
| return (value, { v in v * (1 - value * value) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpAsinh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (asinh(x), { v in v / sqrt(1 + x * x) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpAcosh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (acosh(x), { v in v / sqrt(x * x - 1) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpAtanh(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (atanh(x), { v in v / (1 - x * x) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpExpm1(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (expm1(x), { v in exp(x) * v }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpLog1p(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (log1p(x), { v in v / (x + 1) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpErf(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (erf(x), { v in v * ${T}(M_2_SQRTPI) * exp(-x * x) }) | ||||||||
| } | ||||||||
|
|
||||||||
| @usableFromInline | ||||||||
| func _vjpErfc(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||||
| return (erfc(x), { v in v * -${T}(M_2_SQRTPI) * exp(-x * x) }) | ||||||||
| } | ||||||||
| % if T == 'Float80': | ||||||||
| #endif | ||||||||
| % end | ||||||||
|
|
@@ -201,7 +312,14 @@ UnaryIntrinsicFunctions = [ | |||||||
| ] | ||||||||
|
|
||||||||
| # SWIFT_ENABLE_TENSORFLOW | ||||||||
| HasVJP = ["exp", "log", "tan", "cos", "sin"] | ||||||||
| HasVJP = [ | ||||||||
| 'acos', 'asin', 'atan', 'tan', | ||||||||
| 'acosh', 'asinh', 'atanh', 'cosh', 'sinh', 'tanh', | ||||||||
| 'expm1', | ||||||||
| 'log1p', | ||||||||
| 'erf', 'erfc', | ||||||||
| 'cos', 'sin', 'exp', 'exp2', 'log', 'log10', 'log2' | ||||||||
| ] | ||||||||
|
|
||||||||
| def AllFloatTypes(): | ||||||||
| for bits in allFloatBits: | ||||||||
|
|
||||||||
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
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.
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