Skip to content

[AutoDiff] Add @differentiable fixit for protocols/classes. #29332

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
Jan 22, 2020

Conversation

dan-zheng
Copy link
Contributor

For protocol requirements and class members with @differentiable attribute,
conforming types and subclasses must have the same @differentiable attribute
(or one with a superset of differentiability parameters) on implementing/
overriding declarations.

For implementing/overriding declarations that are missing a @differentiable
attribute, emit a fixit that adds the missing attribute.

Resolves TF-1118.

This @differentiable attribute usability improvement was discussed at the
1/17/2020 Swift for TensorFlow open design meeting.


Example:

import _Differentiation
public protocol Layer {
  @differentiable
  func callAsFunction(_ input: Float) -> Float
}
public struct DummyLayer: Layer {
  public func callAsFunction(_ input: Float) -> Float { input }
}

Before:

$ swift -Xfrontend -enable-experimental-differentiable-programming toy.swift
toy.swift:6:15: error: type 'DummyLayer' does not conform to protocol 'Layer'
public struct DummyLayer: Layer {
              ^
toy.swift:7:8: note: candidate is missing attribute '@differentiable'
  func callAsFunction(_ input: Float) -> Float { input }
       ^
toy.swift:4:8: note: protocol requires function 'callAsFunction' with type '(Float) -> Float'; do you want to add a stub?
  func callAsFunction(_ input: Float) -> Float
       ^

After:

$ swift -Xfrontend -enable-experimental-differentiable-programming toy.swift
toy.swift:6:15: error: type 'DummyLayer' does not conform to protocol 'Layer'
public struct DummyLayer: Layer {
              ^
toy.swift:7:15: note: candidate is missing attribute '@differentiable'
  public func callAsFunction(_ input: Float) -> Float { input }
              ^
         @differentiable
toy.swift:4:8: note: protocol requires function 'callAsFunction' with type '(Float) -> Float'; do you want to add a stub?
  func callAsFunction(_ input: Float) -> Float
       ^

The second note protocol requires function 'callAsFunction' with type '(Float) -> Float'; do you want to add a stub? is a bit confusing. Disabling it would be ideal.

For protocol requirements and class members with `@differentiable` attribute,
conforming types and subclasses must have the same `@differentiable` attribute
(or one with a superset of differentiability parameters) on implementing/
overriding declarations.

For implementing/overriding declarations that are missing a `@differentiable`
attribute, emit a fixit that adds the missing attribute.

Resolves TF-1118.
Copy link
Contributor

@rxwei rxwei left a comment

Choose a reason for hiding this comment

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

LGTM

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test

@rxwei
Copy link
Contributor

rxwei commented Jan 21, 2020

I'd suggest verifying this in Xcode before checking it in.

@dan-zheng
Copy link
Contributor Author

In addition to fixit tests, verified that fixits behave as expected using c-arcmt-test and in Xcode.

@dan-zheng dan-zheng merged commit aabc849 into swiftlang:master Jan 22, 2020
@dan-zheng dan-zheng deleted the autodiff-diff-attr-usability branch January 22, 2020 04:01
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.

2 participants