Skip to content

Commit 0b15060

Browse files
authored
Merge pull request #36124 from rxwei/remove-withderivative
2 parents 6097742 + 17cf272 commit 0b15060

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

stdlib/private/DifferentiationUnittest/DifferentiationUnittest.swift.gyb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,38 @@ public func withLeakChecking(
3434
file: file, line: line)
3535
}
3636

37+
@inlinable
38+
@_semantics("autodiff.nonvarying")
39+
public func withoutDerivative<T, R>(at x: T, in body: (T) -> R) -> R
40+
{
41+
body(x)
42+
}
43+
44+
public extension Differentiable {
45+
/// Applies the given closure to the derivative of `self`.
46+
///
47+
/// Returns `self` like an identity function. When the return value is used in
48+
/// a context where it is differentiated with respect to, applies the given
49+
/// closure to the derivative of the return value.
50+
@inlinable
51+
@differentiable(reverse, wrt: self)
52+
func withDerivative(_ body: @escaping (inout TangentVector) -> Void) -> Self {
53+
return self
54+
}
55+
56+
@inlinable
57+
@derivative(of: withDerivative)
58+
internal func _vjpWithDerivative(
59+
_ body: @escaping (inout TangentVector) -> Void
60+
) -> (value: Self, pullback: (TangentVector) -> TangentVector) {
61+
return (self, { grad in
62+
var grad = grad
63+
body(&grad)
64+
return grad
65+
})
66+
}
67+
}
68+
3769
public extension TestSuite {
3870
/// Execute test function and check expected leak count.
3971
func testWithLeakChecking(

stdlib/public/Differentiation/DifferentiationUtilities.swift

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,42 +31,6 @@ public func withoutDerivative<T>(at x: T) -> T {
3131
x
3232
}
3333

34-
/// Applies the given closure `body` to `x`. When used in a context where `x` is
35-
/// being differentiated with respect to, this function will not produce any
36-
/// derivative at `x`.
37-
// FIXME: Support throws-rethrows.
38-
@inlinable
39-
@inline(__always)
40-
@_semantics("autodiff.nonvarying")
41-
public func withoutDerivative<T, R>(at x: T, in body: (T) -> R) -> R {
42-
body(x)
43-
}
44-
45-
public extension Differentiable {
46-
/// Applies the given closure to the derivative of `self`.
47-
///
48-
/// Returns `self` like an identity function. When the return value is used in
49-
/// a context where it is differentiated with respect to, applies the given
50-
/// closure to the derivative of the return value.
51-
@inlinable
52-
@differentiable(reverse, wrt: self)
53-
func withDerivative(_ body: @escaping (inout TangentVector) -> Void) -> Self {
54-
return self
55-
}
56-
57-
@inlinable
58-
@derivative(of: withDerivative)
59-
internal func _vjpWithDerivative(
60-
_ body: @escaping (inout TangentVector) -> Void
61-
) -> (value: Self, pullback: (TangentVector) -> TangentVector) {
62-
return (self, { grad in
63-
var grad = grad
64-
body(&grad)
65-
return grad
66-
})
67-
}
68-
}
69-
7034
//===----------------------------------------------------------------------===//
7135
// Diagnostics
7236
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)