Skip to content

Commit 845358d

Browse files
authored
---
yaml --- r: 340371 b: refs/heads/rxwei-patch-1 c: 1782d72 h: refs/heads/master i: 340369: 1b991aa 340367: cba1b78
1 parent 1eb8571 commit 845358d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-08-18-a: b10b1fce14385faa6d44f6b933e95
10151015
refs/heads/rdar-43033749-fix-batch-mode-no-diags-swift-5.0-branch: a14e64eaad30de89f0f5f0b2a782eed7ecdcb255
10161016
refs/heads/revert-19006-error-bridging-integer-type: 8a9065a3696535305ea53fe9b71f91cbe6702019
10171017
refs/heads/revert-19050-revert-19006-error-bridging-integer-type: ecf752d54b05dd0a20f510f0bfa54a3fec3bcaca
1018-
refs/heads/rxwei-patch-1: aa6d64c6cca590d93805d8253b5ab9963e91d84f
1018+
refs/heads/rxwei-patch-1: 1782d725aba57c23f44249844c560a53ed27c21b
10191019
refs/heads/shahmishal-patch-1: e58ec0f7488258d42bef51bc3e6d7b3dc74d7b2a
10201020
refs/heads/typelist-existential: 4046359efd541fb5c72d69a92eefc0a784df8f5e
10211021
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-08-20-a: 4319ba09e4fb8650ee86061075c74a016b6baab9

branches/rxwei-patch-1/stdlib/public/core/AutoDiff.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,24 @@ public extension Differentiable where TangentVector == Self {
140140
}
141141

142142
public extension Differentiable {
143-
/// Identity function that stops gradients from propagating.
143+
/// Identity function that stops derivatives from propagating.
144+
@inlinable
144145
@inline(__always)
145146
@_semantics("autodiff.nonvarying")
146147
func withoutDerivative() -> Self { return self }
147148
}
148149

150+
/// Applies the given closure `body` to `x`. When used in a context where `x` is
151+
/// being differentiated with respect to, this function will not produce any
152+
/// derivative at `x`.
153+
// FIXME: Support throws-rethrows.
154+
@inlinable
155+
@inline(__always)
156+
@_semantics("autodiff.nonvarying")
157+
public func withoutDerivative<T, R>(at x: T, in body: (T) -> R) -> R {
158+
body(x)
159+
}
160+
149161
//===----------------------------------------------------------------------===//
150162
// Functional utilities
151163
//===----------------------------------------------------------------------===//

branches/rxwei-patch-1/test/AutoDiff/custom_derivatives.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,12 @@ CustomDerivativesTests.test("ModifyGradientOfSum") {
8787
})
8888
}
8989

90+
CustomDerivativesTests.test("WithoutDerivative") {
91+
expectEqual(0, gradient(at: Float(4)) { x in
92+
withoutDerivative(at: x) { x in
93+
sinf(x) + cosf(x)
94+
}
95+
})
96+
}
97+
9098
runAllTests()

0 commit comments

Comments
 (0)