-
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
Changes from 1 commit
79e0fea
c92e34b
62c019b
27ca6a2
6b1ebf2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -102,11 +102,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)}) | ||||||
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. Indent by 2. 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. fixed on c92e34b |
||||||
} | ||||||
|
||||||
@usableFromInline | ||||||
func _vjpSin(_ x: ${T}) -> (${T}, (${T}) -> ${T}) { | ||||||
return (sin(x), { v in v * cos(x) }) | ||||||
|
@@ -122,6 +138,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) }) | ||||||
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 6b1ebf2 |
||||||
} | ||||||
|
||||||
@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 +283,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: | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same for all other occurrences. 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. fixed on c92e34b |
||||||
expectEqualWithTolerance(7.3890560989306502274, gradient(at: 2.0 as ${T}, in: expm1), ulps:16) | ||||||
expectEqualWithTolerance(0.5, gradient(at: 2.0 as ${T}, in: log), ulps:16) | ||||||
expectEqualWithTolerance(0.21714724095162590833, gradient(at: 2.0 as ${T}, in: log10), ulps:16) | ||||||
expectEqualWithTolerance(0.7213475204444817278, gradient(at: 2.0 as ${T}, in: log2), ulps:16) | ||||||
expectEqualWithTolerance(0.33333333333333333334, gradient(at: 2.0 as ${T}, in: log1p), ulps:16) | ||||||
expectEqualWithTolerance(5.774399204041917612, gradient(at: 2.0 as ${T}, in: tan), ulps:16) | ||||||
expectEqualWithTolerance(-0.416146836547142387, gradient(at: 2.0 as ${T}, in: sin), ulps:16) | ||||||
expectEqualWithTolerance(-0.9092974268256816954, gradient(at: 2.0 as ${T}, in: cos), ulps:16) | ||||||
expectEqualWithTolerance(-0.416146836547142387, gradient(at: 2.0 as ${T}, in: sin), ulps:16) | ||||||
expectEqualWithTolerance(1.154700538379251529, gradient(at: 0.5 as ${T}, in: asin), ulps:16) | ||||||
expectEqualWithTolerance(-1.154700538379251529, gradient(at: 0.5 as ${T}, in: acos), ulps:16) | ||||||
expectEqualWithTolerance(0.8, gradient(at: 0.5 as ${T}, in: atan), ulps:16) | ||||||
expectEqualWithTolerance(3.7621956910836314597, gradient(at: 2.0 as ${T}, in: sinh), ulps:16) | ||||||
expectEqualWithTolerance(3.6268604078470187677, gradient(at: 2.0 as ${T}, in: cosh), ulps:16) | ||||||
expectEqualWithTolerance(0.07065082485316446565, gradient(at: 2.0 as ${T}, in: tanh), ulps:16) | ||||||
expectEqualWithTolerance(0.44721359549995793928, gradient(at: 2.0 as ${T}, in: asinh), ulps:16) | ||||||
expectEqualWithTolerance(0.5773502691896257645, gradient(at: 2.0 as ${T}, in: acosh), ulps:16) | ||||||
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) | ||||||
} | ||||||
%end | ||||||
|
||||||
|
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 6b1ebf2