Skip to content

[AutoDiff] Deprecate Differentiable.AllDifferentiableVariables. #26527

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
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
2 changes: 0 additions & 2 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ IDENTIFIER(by)
IDENTIFIER(scale)
IDENTIFIER(x)
// Differentiable
IDENTIFIER(AllDifferentiableVariables)
IDENTIFIER(TangentVector)
IDENTIFIER(allDifferentiableVariables)
IDENTIFIER(move)

// Kinds of layout constraints
Expand Down
501 changes: 103 additions & 398 deletions lib/Sema/DerivedConformanceDifferentiable.cpp

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions lib/Sema/DerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@ ValueDecl *DerivedConformance::getDerivableRequirement(NominalTypeDecl *nominal,
if (name.isSimpleName(ctx.Id_typeList))
return getRequirement(KnownProtocolKind::TensorGroup);

// SWIFT_ENABLE_TENSORFLOW
// Differentiable.allDifferentiableVariables
if (name.isSimpleName(ctx.Id_allDifferentiableVariables))
return getRequirement(KnownProtocolKind::Differentiable);

return nullptr;
}

Expand Down Expand Up @@ -454,9 +449,7 @@ ValueDecl *DerivedConformance::getDerivableRequirement(NominalTypeDecl *nominal,

// SWIFT_ENABLE_TENSORFLOW
// Differentiable.TangentVector
// Differentiable.AllDifferentiableVariables
if (name.isSimpleName(ctx.Id_TangentVector) ||
name.isSimpleName(ctx.Id_AllDifferentiableVariables))
if (name.isSimpleName(ctx.Id_TangentVector))
return getRequirement(KnownProtocolKind::Differentiable);

// SWIFT_ENABLE_TENSORFLOW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,12 @@ public struct Tracked<T> {
}
private var handle: Box

@differentiable(
vjp: _vjpInit
where T : Differentiable, T == T.AllDifferentiableVariables,
T == T.TangentVector
)
@differentiable(vjp: _vjpInit where T : Differentiable, T == T.TangentVector)
public init(_ value: T) {
self.handle = Box(value)
}

@differentiable(
vjp: _vjpValue
where T : Differentiable, T == T.AllDifferentiableVariables,
T == T.TangentVector
)
@differentiable(vjp: _vjpValue where T : Differentiable, T == T.TangentVector)
public var value: T {
get { handle.value }
set { handle.value = newValue }
Expand Down Expand Up @@ -174,17 +166,11 @@ extension Tracked : Strideable where T : Strideable, T.Stride == T.Stride.Magnit
}

// For now, `T` must be restricted to trivial types (like `Float` or `Tensor`).
extension Tracked : Differentiable
where T : Differentiable, T == T.AllDifferentiableVariables,
T == T.TangentVector
{
public typealias AllDifferentiableVariables = Tracked<T.AllDifferentiableVariables>
extension Tracked : Differentiable where T : Differentiable, T == T.TangentVector {
public typealias TangentVector = Tracked<T.TangentVector>
}

extension Tracked where T : Differentiable, T == T.AllDifferentiableVariables,
T == T.TangentVector
{
extension Tracked where T : Differentiable, T == T.TangentVector {
@usableFromInline
internal static func _vjpInit(_ value: T)
-> (value: Self, pullback: (Self.TangentVector) -> (T.TangentVector)) {
Expand All @@ -197,9 +183,7 @@ extension Tracked where T : Differentiable, T == T.AllDifferentiableVariables,
}
}

extension Tracked where T : Differentiable, T == T.AllDifferentiableVariables,
T == T.TangentVector
{
extension Tracked where T : Differentiable, T == T.TangentVector {
@usableFromInline
@differentiating(+)
internal static func _vjpAdd(lhs: Self, rhs: Self)
Expand All @@ -216,7 +200,7 @@ extension Tracked where T : Differentiable, T == T.AllDifferentiableVariables,
}

extension Tracked where T : Differentiable & SignedNumeric, T == T.Magnitude,
T == T.AllDifferentiableVariables, T == T.TangentVector {
T == T.TangentVector {
@usableFromInline
@differentiating(*)
internal static func _vjpMultiply(lhs: Self, rhs: Self)
Expand All @@ -225,8 +209,7 @@ extension Tracked where T : Differentiable & SignedNumeric, T == T.Magnitude,
}
}

extension Tracked where T : Differentiable & FloatingPoint,
T == T.AllDifferentiableVariables, T == T.TangentVector {
extension Tracked where T : Differentiable & FloatingPoint, T == T.TangentVector {
@usableFromInline
@differentiating(/)
internal static func _vjpDivide(lhs: Self, rhs: Self)
Expand Down
42 changes: 4 additions & 38 deletions stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1952,26 +1952,6 @@ extension Array where Element : Differentiable {

public typealias TangentVector =
Array<Element.TangentVector>.DifferentiableView
public typealias AllDifferentiableVariables =
Copy link
Contributor

Choose a reason for hiding this comment

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

In a separate patch, Array.DifferentiableView should be replaced with a struct TangentVector as described in the design overview.

Array<Element.AllDifferentiableVariables>.DifferentiableView

public var allDifferentiableVariables: AllDifferentiableVariables {
get {
return AllDifferentiableVariables(
base.map { $0.allDifferentiableVariables })
}
set {
precondition(
base.count == newValue.base.count,
"cannot set Array.DifferentiableView.AllDifferentiableVariables " +
"with count \(base.count) to " +
"Array.DifferentiableView.AllDifferentiableVariables with " +
"different count \(newValue.base.count)")
for i in base.indices {
base[i].allDifferentiableVariables = newValue.base[i]
}
}
}

public mutating func move(along direction: TangentVector) {
precondition(
Expand Down Expand Up @@ -2066,27 +2046,13 @@ extension Array.DifferentiableView : AdditiveArithmetic
/// Makes `Array` differentiable as the product manifold of `Element`
/// multiplied with itself `count` times.
extension Array : Differentiable where Element : Differentiable {
// In an ideal world, `TangentVector`, `TangentVector`, and
// `AllDifferentiableVariables` would all be `Array`s. Unfortunately, we
// can't conform `Array` to `AdditiveArithmetic` for `TangentVector` and
// `TangentVector`, because `Array` already has a static `+` method with
// different semantics from `AdditiveArithmetic` `+`. So we use
// In an ideal world, `TangentVector` would be `[Element.TangentVector]`.
// Unfortunately, we cannot conform `Array` to `AdditiveArithmetic` for
// `TangentVector` because `Array` already has a static `+` method with
// different semantics from `AdditiveArithmetic.+`. So we use
// `Array.DifferentiableView` for all these associated types.
public typealias TangentVector =
Array<Element.TangentVector>.DifferentiableView
public typealias AllDifferentiableVariables =
Array<Element.AllDifferentiableVariables>.DifferentiableView

public var allDifferentiableVariables: AllDifferentiableVariables {
get {
return DifferentiableView(self).allDifferentiableVariables
}
set {
var view = DifferentiableView(self)
view.allDifferentiableVariables = newValue
self = view.base
}
}

public mutating func move(along direction: TangentVector) {
var view = DifferentiableView(self)
Expand Down
45 changes: 13 additions & 32 deletions stdlib/public/core/AutoDiff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,24 @@ public extension VectorProtocol where VectorSpaceScalar : SignedNumeric {
/// tangent spaces are finite-dimensional.
public protocol Differentiable {
associatedtype TangentVector: Differentiable & AdditiveArithmetic
where TangentVector.TangentVector == TangentVector,
AllDifferentiableVariables.AllDifferentiableVariables ==
AllDifferentiableVariables,
AllDifferentiableVariables.TangentVector == TangentVector
/// The type of all differentiable variables in this type.
associatedtype AllDifferentiableVariables : Differentiable

/// All differentiable variables of this value.
var allDifferentiableVariables: AllDifferentiableVariables { get set }
where TangentVector.TangentVector == TangentVector

/// Moves `self` along the value space towards the given tangent vector. In
/// Riemannian geometry (mathematics), this represents an exponential map.
mutating func move(along direction: TangentVector)

@available(*, deprecated,
message: "'AllDifferentiableVariables' is now equal to 'Self' and will be removed")
typealias AllDifferentiableVariables = Self

@available(*, deprecated,
message: "'CotangentVector' is now equal to 'TangentVector' and will be removed")
typealias CotangentVector = TangentVector
}

public extension Differentiable where AllDifferentiableVariables == Self {
public extension Differentiable {
@available(*, deprecated,
message: "'allDifferentiableVariables' is now equal to 'self' and will be removed")
var allDifferentiableVariables: AllDifferentiableVariables {
get { return self }
set { self = newValue }
Expand Down Expand Up @@ -723,16 +721,14 @@ internal protocol _AnyDerivativeBox {
func _subtracting(_ x: _AnyDerivativeBox) -> _AnyDerivativeBox

// `Differentiable` requirements.
var _allDifferentiableVariables: _AnyDerivativeBox { get }
mutating func _move(along direction: _AnyDerivativeBox)

/// The underlying base value, type-erased to `Any`.
var _typeErasedBase: Any { get }

/// Returns the underlying value unboxed to the given type, if possible.
func _unboxed<U>(to type: U.Type) -> U?
where U : Differentiable, U.TangentVector == U,
U.AllDifferentiableVariables == U
where U : Differentiable, U.TangentVector == U
}

extension _AnyDerivativeBox {
Expand All @@ -754,8 +750,7 @@ internal func _derivativeTypeMismatch(
}

internal struct _ConcreteDerivativeBox<T> : _AnyDerivativeBox
where T : Differentiable, T.TangentVector == T,
T.AllDifferentiableVariables == T
where T : Differentiable, T.TangentVector == T
{
/// The underlying base value.
var _base: T
Expand All @@ -770,8 +765,7 @@ internal struct _ConcreteDerivativeBox<T> : _AnyDerivativeBox
}

func _unboxed<U>(to type: U.Type) -> U?
where U : Differentiable, U.TangentVector == U,
U.AllDifferentiableVariables == U
where U : Differentiable, U.TangentVector == U
{
return (self as? _ConcreteDerivativeBox<U>)?._base
}
Expand Down Expand Up @@ -824,10 +818,6 @@ internal struct _ConcreteDerivativeBox<T> : _AnyDerivativeBox

// `Differentiable` requirements.

var _allDifferentiableVariables: _AnyDerivativeBox {
return _ConcreteDerivativeBox(_base.allDifferentiableVariables)
}

mutating func _move(along direction: _AnyDerivativeBox) {
if direction._isOpaqueZero() {
return
Expand Down Expand Up @@ -861,24 +851,19 @@ public struct AnyDerivative : Differentiable & AdditiveArithmetic {

/// Creates a type-erased derivative from the given derivative.
@differentiable(vjp: _vjpInit(_:))
public init<T>(_ base: T)
where T : Differentiable, T.TangentVector == T,
T.AllDifferentiableVariables == T
{
public init<T>(_ base: T) where T : Differentiable, T.TangentVector == T {
self._box = _ConcreteDerivativeBox<T>(base)
}

@usableFromInline internal static func _vjpInit<T>(
_ base: T
) -> (AnyDerivative, (AnyDerivative) -> T.TangentVector)
where T : Differentiable, T.TangentVector == T,
T.AllDifferentiableVariables == T
where T : Differentiable, T.TangentVector == T
{
return (AnyDerivative(base), { v in v.base as! T.TangentVector })
}

public typealias TangentVector = AnyDerivative
public typealias AllDifferentiableVariables = AnyDerivative

// `Equatable` requirements (implied by `AdditiveArithmetic`).
public static func == (lhs: AnyDerivative, rhs: AnyDerivative) -> Bool {
Expand Down Expand Up @@ -929,10 +914,6 @@ public struct AnyDerivative : Differentiable & AdditiveArithmetic {
}

// `Differentiable` requirements.
public var allDifferentiableVariables: AllDifferentiableVariables {
get { return AnyDerivative(_box: _box._allDifferentiableVariables) }
// set { _box._allDifferentiableVariables = newValue._box }
}
public mutating func move(along direction: TangentVector) {
if _box._isOpaqueZero() {
_box = direction._box
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/core/FloatingPointTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,6 @@ extension ${Self} : VectorProtocol {

extension ${Self} : Differentiable {
public typealias TangentVector = ${Self}
public typealias AllDifferentiableVariables = ${Self}

public mutating func move(along direction: TangentVector) {
self += direction
Expand Down
4 changes: 0 additions & 4 deletions stdlib/public/core/SIMDVectorTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,6 @@ extension SIMD${n} : Differentiable
where Scalar : Differentiable & BinaryFloatingPoint,
Scalar.TangentVector : BinaryFloatingPoint {
public typealias TangentVector = SIMD${n}
public typealias AllDifferentiableVariables = SIMD${n}
public func tangentVector(from cotangent: TangentVector) -> TangentVector {
return cotangent
}
}

extension SIMD${n}
Expand Down
4 changes: 2 additions & 2 deletions test/AutoDiff/control_flow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,9 @@ ControlFlowTests.test("Enums") {
return input * w1
}
}
expectEqual((Dense.AllDifferentiableVariables(w1: 10), 20),
expectEqual((Dense.TangentVector(w1: 10), 20),
Dense(w1: 4, w2: 5).gradient(at: 2, in: { dense, x in dense(x) }))
expectEqual((Dense.AllDifferentiableVariables(w1: 2), 4),
expectEqual((Dense.TangentVector(w1: 2), 4),
Dense(w1: 4, w2: nil).gradient(at: 2, in: { dense, x in dense(x) }))

indirect enum Indirect {
Expand Down
2 changes: 0 additions & 2 deletions test/AutoDiff/derived_conformances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ DerivedConformanceTests.test("MemberwiseInitializers") {
@noDerivative let constant2 = Double(1)
var x = Float(1)
}
expectEqual(HasNoDerivativeConstant.AllDifferentiableVariables(x: 0),
HasNoDerivativeConstant.AllDifferentiableVariables.zero)
expectEqual(HasNoDerivativeConstant.TangentVector(x: 0),
HasNoDerivativeConstant.TangentVector.zero)
}
Expand Down
26 changes: 8 additions & 18 deletions test/AutoDiff/derived_differentiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ public struct Foo : Differentiable {
// CHECK-AST: @differentiable
// CHECK-AST: public var a: Float
// CHECK-AST: internal init(a: Float)
// CHECK-AST: public struct AllDifferentiableVariables
// CHECK-AST: public typealias AllDifferentiableVariables = Foo.AllDifferentiableVariables
// CHECK-AST: public typealias TangentVector = Foo.AllDifferentiableVariables
// CHECK-AST: public typealias TangentVector = Foo.AllDifferentiableVariables
// CHECK-AST: public struct TangentVector
// CHECK-AST: public typealias TangentVector = Foo.TangentVector

// CHECK-SILGEN-LABEL: // Foo.a.getter
// CHECK-SILGEN-NEXT: sil [transparent] [serialized] [differentiable source 0 wrt 0] [ossa] @$s22derived_differentiable3FooV1aSfvg : $@convention(method) (Foo) -> Float
Expand All @@ -33,7 +31,6 @@ let _: @differentiable (AdditiveTangentIsSelf) -> Float = { x in
// CHECK-AST: internal var dummy: PointwiseMultiplicativeDummy
// CHECK-AST: internal init(a: Float, dummy: PointwiseMultiplicativeDummy)
// CHECK-AST: internal typealias TangentVector = AdditiveTangentIsSelf
// CHECK-AST: internal typealias AllDifferentiableVariables = AdditiveTangentIsSelf

struct TestNoDerivative : Differentiable {
var w: Float
Expand All @@ -44,10 +41,8 @@ struct TestNoDerivative : Differentiable {
// CHECK-AST: var w: Float
// CHECK-AST: @noDerivative internal var technicallyDifferentiable: Float
// CHECK-AST: internal init(w: Float, technicallyDifferentiable: Float)
// CHECK-AST: internal struct AllDifferentiableVariables : Differentiable, AdditiveArithmetic, ElementaryFunctions, VectorProtocol
// CHECK-AST: internal typealias AllDifferentiableVariables = TestNoDerivative.AllDifferentiableVariables
// CHECK-AST: internal typealias TangentVector = TestNoDerivative.AllDifferentiableVariables
// CHECK-AST: internal typealias TangentVector = TestNoDerivative.AllDifferentiableVariables
// CHECK-AST: internal struct TangentVector : Differentiable, AdditiveArithmetic, ElementaryFunctions, VectorProtocol
// CHECK-AST: internal typealias TangentVector = TestNoDerivative.TangentVector

struct TestPointwiseMultiplicative : Differentiable {
var w: PointwiseMultiplicativeDummy
Expand All @@ -58,10 +53,8 @@ struct TestPointwiseMultiplicative : Differentiable {
// CHECK-AST: var w: PointwiseMultiplicativeDummy
// CHECK-AST: @noDerivative internal var technicallyDifferentiable: PointwiseMultiplicativeDummy
// CHECK-AST: internal init(w: PointwiseMultiplicativeDummy, technicallyDifferentiable: PointwiseMultiplicativeDummy)
// CHECK-AST: internal struct AllDifferentiableVariables : Differentiable, AdditiveArithmetic, PointwiseMultiplicative
// CHECK-AST: internal typealias AllDifferentiableVariables = TestPointwiseMultiplicative.AllDifferentiableVariables
// CHECK-AST: internal typealias TangentVector = TestPointwiseMultiplicative.AllDifferentiableVariables
// CHECK-AST: internal typealias TangentVector = TestPointwiseMultiplicative.AllDifferentiableVariables
// CHECK-AST: internal struct TangentVector : Differentiable, AdditiveArithmetic, PointwiseMultiplicative
// CHECK-AST: internal typealias TangentVector = TestPointwiseMultiplicative.TangentVector


struct TestKeyPathIterable : Differentiable, KeyPathIterable {
Expand All @@ -73,10 +66,8 @@ struct TestKeyPathIterable : Differentiable, KeyPathIterable {
// CHECK-AST: var w: Float
// CHECK-AST: @noDerivative internal var technicallyDifferentiable: Float
// CHECK-AST: internal init(w: Float, technicallyDifferentiable: Float)
// CHECK-AST: internal struct AllDifferentiableVariables : Differentiable, AdditiveArithmetic, KeyPathIterable, ElementaryFunctions, VectorProtocol
// CHECK-AST: internal typealias AllDifferentiableVariables = TestKeyPathIterable.AllDifferentiableVariables
// CHECK-AST: internal typealias TangentVector = TestKeyPathIterable.AllDifferentiableVariables
// CHECK-AST: internal typealias TangentVector = TestKeyPathIterable.AllDifferentiableVariables
// CHECK-AST: internal struct TangentVector : Differentiable, AdditiveArithmetic, ElementaryFunctions, VectorProtocol, KeyPathIterable
// CHECK-AST: internal typealias TangentVector = TestKeyPathIterable.TangentVector

struct GenericTanMember<T : Differentiable> : Differentiable, AdditiveArithmetic {
var x: T.TangentVector
Expand All @@ -86,7 +77,6 @@ struct GenericTanMember<T : Differentiable> : Differentiable, AdditiveArithmetic
// CHECK-AST: internal var x: T.TangentVector
// CHECK-AST: internal init(x: T.TangentVector)
// CHECK-AST: internal typealias TangentVector = GenericTanMember<T>
// CHECK-AST: internal typealias AllDifferentiableVariables = GenericTanMember<T>
// CHECK-AST: internal static var zero: GenericTanMember<T> { get }
// CHECK-AST: internal static func + (lhs: GenericTanMember<T>, rhs: GenericTanMember<T>) -> GenericTanMember<T>
// CHECK-AST: internal static func - (lhs: GenericTanMember<T>, rhs: GenericTanMember<T>) -> GenericTanMember<T>
Expand Down
Loading