Skip to content

[AutoDiff] Support @differentiable class initializers. #29757

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 2 commits into from
Feb 11, 2020

Conversation

dan-zheng
Copy link
Contributor

Support @differentiable class initializers in final classes.

  • Allow @differentiable and @derivative attributes for original initializers
    in final classes. Reject original initializers in non-final classes.
  • Add special logic in SILGenModule::getOrCreateCustomDerivativeThunk to
    thunk user-defined class initializer derivative functions to the expected
    derivative type for class initializers.

Also support differentiation for class-related casting instructions:
unchecked_ref_cast and upcast.

These instructions are generated when calling super.init or referencing
inherited superclass members.

Resolves SR-12151 and SR-12153.


Example:

class Super: Differentiable {
  var base: Float

  // Error: `Super` is non-final.
  @differentiable
  init(base: Float) {
    self.base = base
  }
}

final class Sub: Super {
  // No error: `Sub` is final.
  @differentiable
  override init(base: Float) {
    super.init(base: base)
  }
}
class.swift:5:4: error: '@differentiable' attribute cannot be declared on 'init' in a non-final class; consider making 'Super' final
  @differentiable
   ^

Support `@differentiable` class initializers in final classes.

- Allow `@differentiable` and `@derivative` attributes for original initializers
  in final classes. Reject original initializers in non-final classes.
- Add special logic in `SILGenModule::getOrCreateCustomDerivativeThunk` to
  thunk user-defined class initializer derivative functions to the expected
  derivative type for class initializers.

Also support differentiation for class-related casting instructions:
`unchecked_ref_cast` and `upcast`.

These instructions are generated when calling `super.init` or referencing
inherited superclass members.

Resolves SR-12151 and SR-12153.
@dan-zheng dan-zheng added the tensorflow This is for "tensorflow" branch PRs. label Feb 11, 2020
@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow

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