-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] Conform Optional
to Differentiable
.
#32948
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
dan-zheng
merged 2 commits into
swiftlang:master
from
dan-zheng:differentiable-optional
Jul 17, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
stdlib/public/Differentiation/OptionalDifferentiation.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//===--- OptionalDifferentiation.swift ------------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2020 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Swift | ||
|
||
extension Optional: Differentiable where Wrapped: Differentiable { | ||
public struct TangentVector: Differentiable, AdditiveArithmetic { | ||
public typealias TangentVector = Self | ||
|
||
public var value: Wrapped.TangentVector? | ||
|
||
public init(_ value: Wrapped.TangentVector?) { | ||
self.value = value | ||
} | ||
|
||
public static var zero: Self { | ||
return Self(.zero) | ||
} | ||
|
||
public static func + (lhs: Self, rhs: Self) -> Self { | ||
switch (lhs.value, rhs.value) { | ||
case (nil, nil): return Self(nil) | ||
case let (x?, nil): return Self(x) | ||
case let (nil, y?): return Self(y) | ||
case let (x?, y?): return Self(x + y) | ||
} | ||
} | ||
|
||
public static func - (lhs: Self, rhs: Self) -> Self { | ||
switch (lhs.value, rhs.value) { | ||
case (nil, nil): return Self(nil) | ||
case let (x?, nil): return Self(x) | ||
case let (nil, y?): return Self(.zero - y) | ||
case let (x?, y?): return Self(x - y) | ||
} | ||
} | ||
|
||
public mutating func move(along direction: TangentVector) { | ||
if let value = direction.value { | ||
self.value?.move(along: value) | ||
} | ||
} | ||
|
||
@noDerivative | ||
public var zeroTangentVectorInitializer: () -> TangentVector { | ||
switch value { | ||
case nil: | ||
return { Self(nil) } | ||
case let x?: | ||
return { [zeroTanInit = x.zeroTangentVectorInitializer] in | ||
Self(zeroTanInit()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
public mutating func move(along direction: TangentVector) { | ||
if let value = direction.value { | ||
self?.move(along: value) | ||
} | ||
} | ||
|
||
@noDerivative | ||
public var zeroTangentVectorInitializer: () -> TangentVector { | ||
switch self { | ||
case nil: | ||
return { TangentVector(nil) } | ||
case let x?: | ||
return { [zeroTanInit = x.zeroTangentVectorInitializer] in | ||
TangentVector(zeroTanInit()) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// RUN: %target-run-simple-swift | ||
// REQUIRES: executable_test | ||
|
||
import _Differentiation | ||
import StdlibUnittest | ||
|
||
var OptionalDifferentiationTests = TestSuite("OptionalDifferentiation") | ||
|
||
OptionalDifferentiationTests.test("Optional operations") { | ||
// Differentiable.move(along:) | ||
do { | ||
var some: Float? = 2 | ||
some.move(along: .init(3)) | ||
expectEqual(5, some) | ||
|
||
var none: Float? = nil | ||
none.move(along: .init(3)) | ||
expectEqual(nil, none) | ||
} | ||
|
||
// Differentiable.zeroTangentVectorInitializer | ||
do { | ||
let some: [Float]? = [1, 2, 3] | ||
expectEqual(.init([0, 0, 0]), some.zeroTangentVectorInitializer()) | ||
|
||
let none: [Float]? = nil | ||
expectEqual(.init(nil), none.zeroTangentVectorInitializer()) | ||
} | ||
} | ||
|
||
OptionalDifferentiationTests.test("Optional.TangentVector operations") { | ||
// Differentiable.move(along:) | ||
do { | ||
var some: Optional<Float>.TangentVector = .init(2) | ||
some.move(along: .init(3)) | ||
expectEqual(5, some.value) | ||
|
||
var none: Optional<Float>.TangentVector = .init(nil) | ||
none.move(along: .init(3)) | ||
expectEqual(nil, none.value) | ||
|
||
var nestedSome: Optional<Optional<Float>>.TangentVector = .init(.init(2)) | ||
nestedSome.move(along: .init(.init(3))) | ||
expectEqual(.init(5), nestedSome.value) | ||
|
||
var nestedNone: Optional<Optional<Float>>.TangentVector = .init(.init(nil)) | ||
nestedNone.move(along: .init(.init(3))) | ||
expectEqual(.init(nil), nestedNone.value) | ||
} | ||
|
||
// Differentiable.zeroTangentVectorInitializer | ||
do { | ||
let some: [Float]? = [1, 2, 3] | ||
expectEqual(.init([0, 0, 0]), some.zeroTangentVectorInitializer()) | ||
|
||
let none: [Float]? = nil | ||
expectEqual(.init(nil), none.zeroTangentVectorInitializer()) | ||
|
||
let nestedSome: [Float]?? = [1, 2, 3] | ||
expectEqual(.init(.init([0, 0, 0])), nestedSome.zeroTangentVectorInitializer()) | ||
|
||
let nestedNone: [Float]?? = nil | ||
expectEqual(.init(nil), nestedNone.zeroTangentVectorInitializer()) | ||
} | ||
|
||
// AdditiveArithmetic.zero | ||
expectEqual(.init(Float.zero), Float?.TangentVector.zero) | ||
expectEqual(.init([Float].TangentVector.zero), [Float]?.TangentVector.zero) | ||
|
||
expectEqual(.init(.init(Float.zero)), Float??.TangentVector.zero) | ||
expectEqual(.init(.init([Float].TangentVector.zero)), [Float]??.TangentVector.zero) | ||
|
||
// AdditiveArithmetic.+, AdditiveArithmetic.- | ||
do { | ||
let some: Optional<Float>.TangentVector = .init(2) | ||
let none: Optional<Float>.TangentVector = .init(nil) | ||
|
||
expectEqual(.init(4), some + some) | ||
expectEqual(.init(2), some + none) | ||
expectEqual(.init(2), none + some) | ||
expectEqual(.init(nil), none + none) | ||
|
||
expectEqual(.init(0), some - some) | ||
expectEqual(.init(2), some - none) | ||
expectEqual(.init(-2), none - some) | ||
expectEqual(.init(nil), none - none) | ||
|
||
let nestedSome: Optional<Optional<Float>>.TangentVector = .init(.init(2)) | ||
let nestedNone: Optional<Optional<Float>>.TangentVector = .init(.init(nil)) | ||
|
||
expectEqual(.init(.init(4)), nestedSome + nestedSome) | ||
expectEqual(.init(.init(2)), nestedSome + nestedNone) | ||
expectEqual(.init(.init(2)), nestedNone + nestedSome) | ||
expectEqual(.init(.init(nil)), nestedNone + nestedNone) | ||
|
||
expectEqual(.init(.init(0)), nestedSome - nestedSome) | ||
expectEqual(.init(.init(2)), nestedSome - nestedNone) | ||
expectEqual(.init(.init(-2)), nestedNone - nestedSome) | ||
expectEqual(.init(.init(nil)), nestedNone - nestedNone) | ||
} | ||
dan-zheng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
runAllTests() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: it seems more efficient to use
Optional.TangentVector(nil)
asOptional.TangentVector.zero
. We can explore the implications when working on differentiation support forOptional
.