Skip to content

[AutoDiff upstream] Add derivative function SILDeclRefs. #30564

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

Conversation

dan-zheng
Copy link
Contributor

@differentiable attribute on protocol requirements and non-final class members
will produce derivative function entries in witness tables and vtables.

This patch adds an optional derivative function configuration
(AutoDiffDerivativeFunctionIdentifier) to SILDeclRef to represent these
derivative function entries.

Derivative function configurations consist of:

  • A derivative function kind (JVP or VJP).
  • Differentiability parameter indices.
sil-decl-ref ::= '#' sil-identifier ('.' sil-identifier)* sil-decl-subref?
sil-decl-subref ::= '!' sil-decl-subref-part ('.' sil-decl-lang)? ('.' sil-decl-autodiff)?
sil-decl-autodiff ::= sil-decl-autodiff-kind '.' sil-decl-autodiff-indices
ail-decl-autodiff-kind ::= 'jvp' | 'vjp'
sil-decl-autodiff-indices ::= [SU]+
                               ^~ 'S' is set, 'U' is unset

Resolves TF-1209.
Enables TF-1212: upstream derivative function entries in witness tables/vtables.


Derivative function SILDeclRef examples:

protocol Protocol {
  @differentiable(wrt: (x, y))
  func f(_ x: Float, _ y: Float) -> Float
}

sil hidden @witness_method : $@convention(thin) <T where T : Protocol> (@in T) -> () {
bb0(%0 : $*T):
  // Original function reference.
  %1 = witness_method $T, #Protocol.f : <Self where Self : Protocol> (Self) -> (Float, Float) -> Float : $@convention(witness_method: Protocol) <τ_0_0 where τ_0_0 : Protocol> (@in_guaranteed τ_0_0) -> (Float, Float) -> Float

  // Derivative function references.
  %2 = witness_method $T, #Protocol.f!jvp.SSS : <Self where Self : Protocol> (Self) -> (Float, Float) -> Float : $@convention(witness_method: Protocol) <τ_0_0 where τ_0_0 : Protocol> (@in_guaranteed τ_0_0) -> (Float, Float) -> Float
  %3 = witness_method $T, #Protocol.f!jvp.UUS : <Self where Self : Protocol> (Self) -> (Float, Float) -> Float : $@convention(witness_method: Protocol) <τ_0_0 where τ_0_0 : Protocol> (@in_guaranteed τ_0_0) -> (Float, Float) -> Float
  %4 = witness_method $T, #Protocol.f!vjp.SSS : <Self where Self : Protocol> (Self) -> (Float, Float) -> Float : $@convention(witness_method: Protocol) <τ_0_0 where τ_0_0 : Protocol> (@in_guaranteed τ_0_0) -> (Float, Float) -> Float
  %5 = witness_method $T, #Protocol.f!vjp.UUS : <Self where Self : Protocol> (Self) -> (Float, Float) -> Float : $@convention(witness_method: Protocol) <τ_0_0 where τ_0_0 : Protocol> (@in_guaranteed τ_0_0) -> (Float, Float) -> Float

  %6 = tuple ()
  return %6 : $()
}

`@differentiable` attribute on protocol requirements and non-final class members
will produce derivative function entries in witness tables and vtables.

This patch adds an optional derivative function configuration
(`AutoDiffDerivativeFunctionIdentifier`) to `SILDeclRef` to represent these
derivative function entries.

Derivative function configurations consist of:
- A derivative function kind (JVP or VJP).
- Differentiability parameter indices.

Resolves TF-1209.
Enables TF-1212: upstream derivative function entries in witness tables/vtables.
@dan-zheng
Copy link
Contributor Author

@slavapestov: would you like to review this patch, as a SIL code owner?
You previously approved this patch in 2018 on tensorflow branch: #21224

@dan-zheng
Copy link
Contributor Author

@swift-ci Please smoke test

Comment on lines +107 to +109
CanGenericSignature derivativeCanGenSig;
if (derivativeGenericSignature)
derivativeCanGenSig = derivativeGenericSignature->getCanonicalSignature();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This can be simplified after #29105:

Suggested change
CanGenericSignature derivativeCanGenSig;
if (derivativeGenericSignature)
derivativeCanGenSig = derivativeGenericSignature->getCanonicalSignature();
auto derivativeCanGenSig = derivativeGenericSignature.getCanonicalSignature();

I'll land this in a follow-up.

@dan-zheng
Copy link
Contributor Author

@swift-ci Please smoke test macOS platform

@dan-zheng
Copy link
Contributor Author

Both macOS and Linux CI failed mysteriously. Retriggering.
@swift-ci Please smoke test Linux

@dan-zheng
Copy link
Contributor Author

@swift-ci Please smoke test Linux

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test

@dan-zheng
Copy link
Contributor Author

dan-zheng commented Mar 22, 2020

SwiftPM tests on Linux seem to have flaked again (for the fourth time):

:0: error: module file '/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/tmp_swiftpm/ModuleCache/34DIH411VVHZV/CDispatch-E28X8IBKETOX.pcm' is out of date and needs to be rebuilt: signature mismatch
10:31:25 :0: note: imported by module 'CoreFoundation' in '/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/tmp_swiftpm/ModuleCache/34DIH411VVHZV/CoreFoundation-1HECCF3FGVFVS.pcm'
10:31:25 :0: error: missing required modules: 'CDispatch', '_SwiftDispatchOverlayShims'
10:31:25 /home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/swiftpm/Tests/PackageLoadingTests/PDLoadingTests.swift:50: error: PackageDescription4_2LoadingTests.testBasics : failed - 
10:31:25 Test Case 'PackageDescription4_2LoadingTests.testBasics' failed (0.778 seconds)
10:31:25 Test Suite 'PackageDescription4_2LoadingTests' failed at 2020-03-22 17:31:06.456
10:31:25 	 Executed 1 test, with 1 failure (0 unexpected) in 0.778 (0.778) seconds
10:31:25 Test Suite 'Selected tests' failed at 2020-03-22 17:31:06.456
10:31:25 	 Executed 1 test, with 1 failure (0 unexpected) in 0.778 (0.778) seconds
@swift-ci Please smoke test Linux

@dan-zheng
Copy link
Contributor Author

Merging to unblock progress. Happy to address feedback later!

@dan-zheng dan-zheng merged commit aab622e into swiftlang:master Mar 22, 2020
@dan-zheng dan-zheng deleted the autodiff-upstream-sil branch March 22, 2020 18:50
dan-zheng added a commit to dan-zheng/swift that referenced this pull request Mar 23, 2020
Run `clang-format` on changes in swiftlang#30564.
dan-zheng added a commit that referenced this pull request Mar 23, 2020
Run `clang-format` on changes in #30564.
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