Skip to content

Conversation

@rxwei
Copy link
Contributor

@rxwei rxwei commented Feb 25, 2019

Semantic change

Since method differentiating w.r.t. self is more common than not w.r.t. self in an instance method, we make @differentiable's implied parameter list include self.

protocol P : Differentiable {
  @differentiable // equivalent to `@differentiable(wrt: (self, a, b))`
  func foo(a: Float, b: Float) -> Float
}

No changes to static methods since metatypes are never differentiable.

Compiler changes

  • Imply self when type-checking a @differentiable that does not have a wrt: parameter list on an instance method.
  • Fix the @differentiable protocol requirement matching logic to handle multiple attributes.
  • Make DifferentiableAttr::print print parsed parameters instead of canonical parameter indices. This is important because protocol witness diagnostics are making use of this printout.
    • Without a lot of special logic, canonical parameter indices would make things look like @differentiable(), @differentiable(wrt: (x)), etc, which are really not what we want in a diagnostic to encourage users to write. Parsed parameters are a lot simpler and intuitive: What you see in the protocol declaration is what you get in a diagnostic.
  • Add more parameter-setting utilities to AutoDiffParameterIndicesBuilder.

Resolves TF-295 and TF-285.

@rxwei rxwei added the tensorflow This is for "tensorflow" branch PRs. label Feb 25, 2019
@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

…clude 'self' on instance methods.

Since method differentiating w.r.t. `self` is more common than not w.r.t. `self` in an instance method, we make `@differentiable`'s implied parameter list include `self`.

```swift
protocol P : Differentiable {
  @differentiable // equivalent to `@differentiable(wrt: (self, a, b))`
  func foo(a: Float, b: Float) -> Float
}
```

No changes to static methods since metatypes are never differentiable.

* Imply `self` when type-checking a `@differentiable` that does not have a `wrt:` parameter list on an instance method.
* Fix the `@differentiable` protocol requirement matching logic to handle multiple attributes.
* Make `DifferentiableAttr::print` print parsed parameters instead of canonical parameter indices. This is important because protocol witness diagnostics are making use of this printout.
  * Without a lot of special logic, canonical parameter indices would make things look like `@differentiable()`, `@differentiable(wrt: (x))`, etc, which are really not what we want in a diagnostic to encourage users to write. Parsed parameters are a lot simpler and intuitive: What you see in the protocol declaration is what you get in a diagnostic.
* Add more parameter-setting utilities to `AutoDiffParameterIndicesBuilder`.

Resolves [TF-295](https://bugs.swift.org/browse/TF-295) and [TF-285](https://bugs.swift.org/browse/TF-285).
@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

7 similar comments
@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

@rxwei
Copy link
Contributor Author

rxwei commented Feb 25, 2019

@swift-ci please test tensorflow

@rxwei rxwei merged commit 6822658 into swiftlang:tensorflow Feb 26, 2019
@rxwei rxwei deleted the TF-295 branch February 26, 2019 00:31
rxwei added a commit to tensorflow/swift-apis that referenced this pull request Feb 26, 2019
…ed! (#33)

We've recently changed the type checker to improve the ergonomics of the `@differentiable` attribute.

* [swiftlang/swift#22915](swiftlang/swift#22915): Explicit differentiation parameters are no longer required in a `@differentiable` attribute when the function has some arguments that do not conform to `Differentiable`. Those non-differentiable parameters will be skipped and the rest will be differentiated with respect to.
* [swiftlang/swift#22877](swiftlang/swift#22877): On an instance method, when a `wrt:` is not specified, `self` is being implicitly included as a differentiation parameter.
* [swiftlang/swift#22877](swiftlang/swift#22877): When a `@differentiable` requirement is not met, the `@differentiable` attribute fix-it will appear exactly as written in the original declaration instead of the most complex, canonical form. For instance, `@differentiable` instead of `@differentiable(wrt: (x))`.

This greatly simplifies libraries and applications that use automatic differentiation. The protocol requirement `Layer.applied(to:in:)` becomes as simple as this:

```swift
@differentiable
func applied(to input: Input, in context: Context) -> Output
```

This PR updates deep learning APIs to use the simplest form of `@differentiable` possible. Hooray!
rxwei added a commit to rxwei/swift that referenced this pull request May 11, 2019
…clude 'self' on instance methods. (swiftlang#22877)

* [AutoDiff] TF-{295,285}: Make '@differentiable' default parameters include 'self' on instance methods.

Since method differentiating w.r.t. `self` is more common than not w.r.t. `self` in an instance method, we make `@differentiable`'s implied parameter list include `self`.

```swift
protocol P : Differentiable {
  @differentiable // equivalent to `@differentiable(wrt: (self, a, b))`
  func foo(a: Float, b: Float) -> Float
}
```

No changes to static methods since metatypes are never differentiable.

* Imply `self` when type-checking a `@differentiable` that does not have a `wrt:` parameter list on an instance method.
* Fix the `@differentiable` protocol requirement matching logic to handle multiple attributes.
* Make `DifferentiableAttr::print` print parsed parameters instead of canonical parameter indices. This is important because protocol witness diagnostics are making use of this printout.
  * Without a lot of special logic, canonical parameter indices would make things look like `@differentiable()`, `@differentiable(wrt: (x))`, etc, which are really not what we want in a diagnostic to encourage users to write. Parsed parameters are a lot simpler and intuitive: What you see in the protocol declaration is what you get in a diagnostic.
* Add more parameter-setting utilities to `AutoDiffParameterIndicesBuilder`.

Resolves [TF-295](https://bugs.swift.org/browse/TF-295) and [TF-285](https://bugs.swift.org/browse/TF-285).
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.

2 participants