Skip to content

[AutoDiff upstream] Add stdlib Differentiable conformances and derivatives. #30875

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 11 commits into from
Apr 9, 2020

Conversation

dan-zheng
Copy link
Contributor

@dan-zheng dan-zheng commented Apr 8, 2020

Add Differentiable conformances and derivatives for stdlib types and functions to the _Differentiation module. Most upstreaming work done by @ematejska!


Differentiable conformances

  • FloatingPoint-conforming types: Float16, Float, Double, Float80
  • Array where Element: Differentiable
  • SIMD-conforming types where Scalar: Differentiable & BinaryFloatingPoint, Scalar.TangentVector: BinaryFloatingPoint

Derivatives

  • Floating-point operations: +, +=, -, -=, *, *=, /, /=
  • Array operations: Array.init(repeating:count:), Array.subscript.get, Array.+(_:_:)
  • SIMD operations: +, -, *, /
  • Most tgmath operations: exp, log, cos, sin, etc.

Differentiable collection higher-order functions:

  • Array.differentiableMap(_:), Array.differentiableReduce(_:_:)

Add end-to-end derivative tests.


Todos:

  • Differentiable.zeroTangentVector protocol requirement and implementations.
  • SIMD.sum differentiation.
    • TF-1103: @_alwaysEmitIntoClient derivative registration.

ematejska and others added 9 commits April 2, 2020 19:55
…ysEmitIntoClient since that's not supported yet.
- Standardize filenames: `XXXDifferentiation.swift`.
  - Use Pascal or snake case consistently.
- Formatting changes.
Add `@available` attributes for declarations using `Float16`.
…rmances.

Move `Differentiable` conformances out of Differentiable.swift.
All `Differentiable` conformances exist in separate files.
Temporarily disable not-yet-supported differentiation tests:

- Forward-mode differentiation.
  - TF-1237: to be upstreamed.
- `Differentiable.zeroTangentVector`.
  - TF-1238: to be upstreamed.
- `SIMD.sum` differentiation.
  - TF-1103: `@_alwaysEmitIntoClient` derivative registration.
…ions.

Test `Array.differentiableMap(_:)` and `Array.differentiableReduce(_:_:)`.
@dan-zheng
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 8, 2020

Build failed
Swift Test Linux Platform
Git Sha - b6d7653

@swift-ci
Copy link
Contributor

swift-ci commented Apr 8, 2020

Build failed
Swift Test OS X Platform
Git Sha - b6d7653

@dan-zheng
Copy link
Contributor Author

Two tests fail with -O due to SR-12548: SILCombiner::visitPartialApplyInst SIL verification failure. I'll submit a fix soon.

//===----------------------------------------------------------------------===//

import Swift
import SwiftShims
Copy link
Contributor

Choose a reason for hiding this comment

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

We might not need to import SwiftShims on master. What do you think? Aside from that looks good.

Copy link
Contributor

@ematejska ematejska left a comment

Choose a reason for hiding this comment

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

Looks like there are a couple of test failures still.

@dan-zheng
Copy link
Contributor Author

Test failures due to SR-12548 fixed in #30877.
@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 8, 2020

Build failed
Swift Test OS X Platform
Git Sha - 01db90d

@dan-zheng
Copy link
Contributor Author

Fixed test failures for 32-bit platforms.
@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 8, 2020

Build failed
Swift Test Linux Platform
Git Sha - 01db90d

@swift-ci
Copy link
Contributor

swift-ci commented Apr 9, 2020

Build failed
Swift Test OS X Platform
Git Sha - 01db90d

- Remove unnecessary imports from test/AutoDiff/stdlib/simd.swift.
- Use platform-correct `Builtin` integer type in
  `PullbackEmitter::getArrayAdjointElementBuffer`.
@dan-zheng dan-zheng force-pushed the autodiff-upstream-stdlib-differentiation branch from ffbe041 to 4495f6b Compare April 9, 2020 02:17
@dan-zheng
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 9, 2020

Build failed
Swift Test OS X Platform
Git Sha - ffbe041d8c4eafa4e463b71849d58251c183c713

@swift-ci
Copy link
Contributor

swift-ci commented Apr 9, 2020

Build failed
Swift Test Linux Platform
Git Sha - ffbe041d8c4eafa4e463b71849d58251c183c713

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test macOS

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test Windows

@dan-zheng dan-zheng merged commit 75548c3 into master Apr 9, 2020
@dan-zheng dan-zheng deleted the autodiff-upstream-stdlib-differentiation branch April 9, 2020 05:31
compnerd added a commit to compnerd/apple-swift that referenced this pull request Apr 9, 2020
The added test was not correctly indicated as a running test.
compnerd added a commit that referenced this pull request Apr 9, 2020
test: repair the Android CI after #30875
3405691582 added a commit to 3405691582/swift that referenced this pull request Apr 15, 2020
New files were added in swiftlang#30875 which did not include os(OpenBSD), so add
this.

add_swift_target_library in AddSwiftStdlib subsequently required
modification. _add_target_variant_link_flags likely needs modification as
well, but this is better suited to a separate PR.
MaxDesiatov added a commit to swiftwasm/swift that referenced this pull request Apr 20, 2020
* [build-script] Add the installation prefix to the toolchain path

In several places, it was assumed to be /usr and hard-coded to that. This makes sure
the installation prefix actually passed in is used.

* [stdlib] StaticString: extension per conformance

* Fix @available(macCatalyst N, iOS N+M) bug

Previously, availability checking computed a declaration’s availability as the intersection of all @available attributes with active platforms. This meant that Swift would not allow you to use an API that was available earlier in a child platform than in its parent until it was also available in the parent platform. That was incorrect.

This PR corrects availability checking to find the most specific @available attribute with an introduced version and use that instead.

Fixes rdar://60892534.

* [stdlib] Buildfix differentiation for OpenBSD.

New files were added in swiftlang#30875 which did not include os(OpenBSD), so add
this.

add_swift_target_library in AddSwiftStdlib subsequently required
modification. _add_target_variant_link_flags likely needs modification as
well, but this is better suited to a separate PR.

* Handle multiple @available attributes correctly

The previous commit broke a promise made by the implementation it replaced: if there were two or more @available(introduced:) attributes for the same platform, the greatest version wins. This commit restores that property while still completely overriding the parent platform’s availability with a child platform’s.

* [Diagnostics] Provide anchors on demand instead of storing in `FailureDiagnostic`

This decouples `FailureDiagnostic` from expression.

* [Diagnostics] Switch `get{Raw}Anchor` to return `TypedNode`

In preparation to anchor `ConstraintLocator` from `TypedNode`
let's refactor diagnostics (which is the biggest user of locators)
to support `TypedNode` instead of `Expr *`.

* [Diagnostics] Switch `getConstraintLocator` variants to accept `TypedNode`

* [Diagnostics] Switch `getContextualType*` to use `TypedNode`

* [Diagnostics] Switch `getType` to use `TypedNode`

* [Diagnostics] Resolve const-ness problems introduced by `TypedNode`

Since `TypedNode` elements are all marked as `const` diagnostics
need to get some of the APIs adjusted to support passing `const Expr *`.

* Add compiler option to *disable* warnings-as-errors
`-no-warnings-as-errors`

This functionality is required for build systems to be able to overload/disable a given Swift project's preference of treating warnings as errors.

Resolves rdar://problem/35699776

* [Diagnostics] Add helper functions to work with anchors - `{castTo, getAs, is}Expr`

* [Diagnostics] Adjust `getCallInfo` to accept `TypedNode` instead of expression

* Avoid unnecessary performTypeChecking() in REPL

* [SIL Diagnostics] Improve diagnostics for capture of inout values
within escaping autoclosures.

<rdar://problem/60551844>

* [NFC] Refactor validateTBDIfNeeded

Inline the conditions for the astGuaranteedToCorrespondToSIL parameter and clean up the logic here to be more const-friendly. Also try to centralize and document the reasons we skip TBD validation.

* Revert "[Darwin] Further restrict inference of the simulator environment"

* [Gardening] Const-qualify CompilerInstance::getInvocation()

* [NFC] Formalize isModuleExternallyConsumed

Yank this parameter and document the historical scope creep of this value.

* More StringRef -> std::string conversion fixes.

This is needed for the master-next branch.

* Devirtualizer: fix a miscompile due to handling of cast instructions.

getInstanceWithExactDynamicType returns a new instance and for this the class decl has to be updated.

https://bugs.swift.org/browse/SR-12538
rdar://problem/61911112

* [NFC] Fix typo in SerializedModuleLoader.cpp

Co-authored-by: Butta <[email protected]>
Co-authored-by: Ben Rimmington <[email protected]>
Co-authored-by: Brent Royal-Gordon <[email protected]>
Co-authored-by: 3405691582 <[email protected]>
Co-authored-by: Pavel Yaskevich <[email protected]>
Co-authored-by: Artem Chikin <[email protected]>
Co-authored-by: Rintaro Ishizaki <[email protected]>
Co-authored-by: Ravi Kandhadai <[email protected]>
Co-authored-by: Robert Widmann <[email protected]>
Co-authored-by: Saleem Abdulrasool <[email protected]>
Co-authored-by: Mishal Shah <[email protected]>
Co-authored-by: Michael Forster <[email protected]>
Co-authored-by: swift-ci <[email protected]>
Co-authored-by: Erik Eckstein <[email protected]>
Co-authored-by: Ravi Kandhadai <[email protected]>
Co-authored-by: Pavel Yaskevich <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants