Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion stdlib/public/core/AutoDiff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,39 @@ public func differentiableFunction<T, U, R>(
return original
}

public extension Differentiable {
@differentiable(wrt: self, vjp: _vjpWithGrad)
func withGradient(_ body: @escaping (inout CotangentVector) -> Void) -> Self {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withGradient(_ body: @escaping (CotangentVector) -> CotangentVector) seems like a more natural API at first glance. Do you have a reason to do it this way?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed in person

Copy link
Contributor Author

@rxwei rxwei Mar 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my core rationale is that withXXX APIs in Swift usually return whatever the closure returns, e.g. withUnsafePointer(_:) and withoutAcutallyEscaping(_:), so x.withGradient { $0 + 1 } would syntactically indicate to a Swift programmer that this is "returning the gradient plus one in the forward pass".

Forbidding a return value makes it slightly less misleading. We can totally change this later.

return self
}

@inlinable
internal func _vjpWithGrad(
_ body: @escaping (inout CotangentVector) -> Void
) -> (Self, (CotangentVector) -> CotangentVector) {
return (self, { grad in
var grad = grad
body(&grad)
return grad
})
}

@differentiable(wrt: self, vjp: _vjpWithGrad)
func withGradient(_ body: @escaping (CotangentVector) -> Void) -> Self {
return self
}

@inlinable
internal func _vjpWithGrad(
_ body: @escaping (CotangentVector) -> Void
) -> (Self, (CotangentVector) -> CotangentVector) {
return (self, { grad in
body(grad)
return grad
})
}
}

/// Make a function be recomputed in its pullback, known as "checkpointing" in
/// traditional automatic differentiation.
@inlinable
Expand All @@ -190,7 +223,7 @@ public extension Differentiable {
return body(self)
}

@usableFromInline
@inlinable
internal func _vjp_withRecomputationInPullbacks<Result : Differentiable>(
_ body: @escaping @differentiable (Self) -> Result
) -> (Result, (Result.CotangentVector) -> CotangentVector) {
Expand Down
17 changes: 17 additions & 0 deletions test/AutoDiff/custom_derivatives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,21 @@ CustomDerivativesTests.test("Retroactive") {
expectEqual(100, gradient(at: 3, in: functionWithRetroDeriv))
}

CustomDerivativesTests.test("SumOfGradPieces") {
var grad: Float = 0
let addToGrad = { grad += $0 }
_ = gradient(at: 4) { (x: Float) in
x.withGradient(addToGrad)
* x.withGradient(addToGrad)
* x.withGradient(addToGrad)
}
expectEqual(48, grad)
}

CustomDerivativesTests.test("ModifyGradientOfSum") {
expectEqual(30, gradient(at: 4) { (x: Float) in
x.withGradient { $0 *= 10 } + x.withGradient { $0 *= 20 }
})
}

runAllTests()