Skip to content

[AutoDiff] Remove deprecated method-style differential operators. #30108

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 2 commits into from
Feb 28, 2020
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
101 changes: 0 additions & 101 deletions stdlib/public/Differentiation/DifferentiationSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,107 +294,6 @@ public extension Differentiable {
}
}

//===----------------------------------------------------------------------===//
// Method-style differential operators
//===----------------------------------------------------------------------===//

public extension Differentiable {
@available(*, deprecated, message: """
Method-style differential operators are deprecated and will be removed; \
use top-level function 'Swift.valueWithPullback(at:in:)' instead
""")
@inlinable
func valueWithPullback<R>(
in f: @differentiable (Self) -> R
) -> (value: R, pullback: (R.TangentVector) -> TangentVector) {
return Builtin.applyDerivative_vjp_arity1(f, self)
}

@available(*, deprecated, message: """
Method-style differential operators are deprecated and will be removed; \
use top-level function 'Swift.pullback(at:in:)' instead
""")
@inlinable
func pullback<R>(
in f: @differentiable (Self) -> R
) -> (R.TangentVector) -> TangentVector {
return Builtin.applyDerivative_vjp_arity1(f, self).1
}

@available(*, deprecated, message: """
Method-style differential operators are deprecated and will be removed; \
use top-level function 'Swift.gradient(at:in:)' instead
""")
@inlinable
func gradient<R>(
in f: @differentiable (Self) -> R
) -> TangentVector
where R : FloatingPoint, R.TangentVector == R {
return Swift.pullback(at: self, in: f)(R(1))
}

@available(*, deprecated, message: """
Method-style differential operators are deprecated and will be removed; \
use top-level function 'Swift.valueWithGradient(at:in:)' instead
""")
@inlinable
func valueWithGradient<R>(
in f: @differentiable (Self) -> R
) -> (value: R, gradient: TangentVector)
where R : FloatingPoint, R.TangentVector == R {
let (y, pb) = Swift.valueWithPullback(at: self, in: f)
return (y, pb(R(1)))
}

@available(*, deprecated, message: """
Method-style differential operators are deprecated and will be removed; \
use top-level function 'Swift.valueWithPullback(at:_:in:)' instead
""")
@inlinable
func valueWithPullback<T, R>(
at x: T, in f: @differentiable (Self, T) -> R
) -> (value: R,
pullback: (R.TangentVector) -> (TangentVector, T.TangentVector)) {
return Builtin.applyDerivative_vjp_arity2(f, self, x)
}

@available(*, deprecated, message: """
Method-style differential operators are deprecated and will be removed; \
use top-level function 'Swift.pullback(at:_:in:)' instead
""")
@inlinable
func pullback<T, R>(
at x: T, in f: @differentiable (Self, T) -> R
) -> (R.TangentVector) -> (TangentVector, T.TangentVector) {
return Builtin.applyDerivative_vjp_arity2(f, self, x).1
}

@available(*, deprecated, message: """
Method-style differential operators are deprecated and will be removed; \
use top-level function 'Swift.gradient(at:_:in:)' instead
""")
@inlinable
func gradient<T, R>(
at x: T, in f: @differentiable (Self, T) -> R
) -> (TangentVector, T.TangentVector)
where R : FloatingPoint, R.TangentVector == R {
return Swift.pullback(at: self, x, in: f)(R(1))
}

@available(*, deprecated, message: """
Method-style differential operators are deprecated and will be removed; \
use top-level function 'Swift.valueWithGradient(at:_:in:)' instead
""")
@inlinable
func valueWithGradient<T, R>(
at x: T, in f: @differentiable (Self, T) -> R
) -> (value: R, gradient: (TangentVector, T.TangentVector))
where R : FloatingPoint, R.TangentVector == R {
let (y, pb) = Swift.valueWithPullback(at: self, x, in: f)
return (y, pb(R(1)))
}
}

//===----------------------------------------------------------------------===//
// Free-function-style differential operators
//===----------------------------------------------------------------------===//
Expand Down
4 changes: 2 additions & 2 deletions test/AutoDiff/downstream/control_flow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,9 @@ ControlFlowTests.test("Enums") {
}
}
expectEqual((Dense.TangentVector(w1: 10), 20),
Dense(w1: 4, w2: 5).gradient(at: 2, in: { dense, x in dense(x) }))
gradient(at: Dense(w1: 4, w2: 5), 2) { dense, x in dense(x) })
expectEqual((Dense.TangentVector(w1: 2), 4),
Dense(w1: 4, w2: nil).gradient(at: 2, in: { dense, x in dense(x) }))
gradient(at: Dense(w1: 4, w2: nil), 2) { dense, x in dense(x) })

indirect enum Indirect {
case e(Float, Enum)
Expand Down
2 changes: 1 addition & 1 deletion test/AutoDiff/downstream/leakchecking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extension DummyLayer {
-> (Output,
(Self.Output.TangentVector)
-> (Self.TangentVector, Self.Input.TangentVector)) {
return Swift.valueWithPullback(at: self, input) { $0.requirement($1) }
return valueWithPullback(at: self, input) { $0.requirement($1) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protocol DiffReq : Differentiable {
extension DiffReq where TangentVector : AdditiveArithmetic {
@inline(never) // Prevent specialization, to test all witness code.
func gradF(at x: Tracked<Float>) -> (Self.TangentVector, Tracked<Float>) {
return (valueWithPullback(at: x) { s, x in s.f(x) }).1(1)
return (valueWithPullback(at: self, x) { s, x in s.f(x) }).1(1)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/AutoDiff/downstream/refcounting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ struct UsesMethodOfNoDerivativeMember : Differentiable {
}
}

_ = UsesMethodOfNoDerivativeMember().pullback(at: .zero) { $0.applied(to: $1) }
_ = pullback(at: UsesMethodOfNoDerivativeMember(), .zero) { $0.applied(to: $1) }
2 changes: 1 addition & 1 deletion test/AutoDiff/downstream/simple_real_vector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public struct TF189: Differentiable {
@noDerivative public let nonTrivial: NonTrivial

func foo(input: Vector) -> Vector {
return self.pullback(at: input) { m, x in
return pullback(at: self, input) { m, x in
m.applied(to: x)
}(.zero).1
}
Expand Down
2 changes: 1 addition & 1 deletion utils/update_checkout/update-checkout-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@
"PythonKit": "master",
"swift-format": "master",
"tensorflow": "v2.1.0-rc1",
"tensorflow-swift-apis": "6a67b4ef646222af1dc345d631019cc3602eb16b",
"tensorflow-swift-apis": "cc2122763611f1be0c6cf8c4339547c3cf155a41",
"tensorflow-swift-quote": "46c3d2996541d0ea902630eda11be5a9ccdeaba3"
}
}
Expand Down