Skip to content

[AutoDiff] [Sema] Infer Differentiable conformance for @differentiable function parameters and results. #24896

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 1 commit into from
May 19, 2019

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented May 19, 2019

Extend generic requirement inference to infer Differentiable protocol conformances from parameters and results that have a @differentiable function type. For each @differentiable function type, require its non-@nondiff parameter types and result to conform to Differentiable.

A common pattern in libraries like this

func foo<T: Differentiable, U: Differentiable, V: Differentiable>(
    _ f: @differentiable (T, U) -> U
)

... is now simplfied to just the following:

func foo<T, U, V>(_ f: @differentiable (T, U) -> V)
// Inferred requirements: `where T: Differentiable, U: Differentiable, V: Differentiable`

Resolves TF-512.

@rxwei rxwei added the tensorflow This is for "tensorflow" branch PRs. label May 19, 2019
@rxwei rxwei requested review from DougGregor and dan-zheng May 19, 2019 02:18
@rxwei rxwei changed the title [AutoDiff] [Sema] Infer Differentiable conformance for @differentiable function arguments and results. [AutoDiff] [Sema] Infer Differentiable conformance for @differentiable function parameters and results. May 19, 2019
…function parameters and results.

Extend generic requirement inference to infer `Differentiable` protocol conformances from parameters and results that have a `@differentiable` function type. For each `@differentiable` function type, require its non-`@nondiff` parameter types and result to conform to `Differentiable`.

A common pattern in libraries like this
```swift
func foo<T: Differentiable, U: Differentiable, V: Differentiable>(
    _ f: @differentiable (T, U) -> U
)
```

... is now simplfied to just the following:
```swift
func foo<T, U, V>(_ f: @differentiable (T, U) -> V)
// Inferred requirements: `where T: Differentiable, U: Differentiable, V: Differentiable`
```

Resolves [TF-512](https://bugs.swift.org/browse/TF-512).
@rxwei rxwei force-pushed the infer-diffable branch from 101bd36 to 3cf23ce Compare May 19, 2019 02:22
@rxwei
Copy link
Contributor Author

rxwei commented May 19, 2019

@swift-ci please test tensorflow

Copy link
Contributor

@dan-zheng dan-zheng left a comment

Choose a reason for hiding this comment

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

Simple!

@rxwei
Copy link
Contributor Author

rxwei commented May 19, 2019

The broken test is a dynamic callable regression from merge that's not reproducible on macOS, unrelated to this patch.

@rxwei rxwei merged commit 2efb6e1 into swiftlang:tensorflow May 19, 2019
@rxwei rxwei deleted the infer-diffable branch May 19, 2019 04:36
@saeta
Copy link
Contributor

saeta commented May 19, 2019

Hey @rxwei! This is an awesome simplification. I like it very much. Just wanted to check: can we have some tests ensure that everything works correctly if you do still specify T: Differentiable? (In case someone wants to write it out long ways for documentation purposes / etc.) If you have those tests already, then perfect! Thanks!

@rxwei
Copy link
Contributor Author

rxwei commented May 19, 2019

@saeta Those tests exist in the same file where I added new tests, and all other differentiation test files do still specify the conformances explicitly since they are unchanged.

rxwei added a commit to rxwei/swift that referenced this pull request Nov 25, 2019
…erentiable(linear)' parameters or results.

Update `GenericSignatureBuilder` to match the behavior specified in the manifesto:
https://github.com/apple/swift/blob/master/docs/DifferentiableProgramming.md#implied-generic-constraints

Previously, swiftlang#24896 added support for implied `Differentiable` constraints from `@differentiable` parameters or results.
rxwei added a commit that referenced this pull request Nov 25, 2019
…erentiable(linear)' parameters or results. (#28472)

Update `GenericSignatureBuilder` to match the behavior specified in the manifesto:
https://github.com/apple/swift/blob/master/docs/DifferentiableProgramming.md#implied-generic-constraints

Previously, #24896 added support for implied `Differentiable` constraints from `@differentiable` parameters or results.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tensorflow This is for "tensorflow" branch PRs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants