Skip to content

[AutoDiff] Support '@differentiable(linear)' function conversion. #27687

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
Oct 15, 2019

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Oct 15, 2019

  • Support the following implicit conversions:
    • Non-differentiable to @differentiable(linear)
      • Sema: Emit LinearFunctionExpr.
      • SILGen: Lower LinearFunctionExpr to linear_function.
    • @differentiable(linear) to non-differentiable
      • Sema: Emit LinearFunctionExtractOriginalExpr.
      • SILGen: Lower LinearFunctionExtractOriginalExpr to linear_function_extract [original].
  • Reject the following implicit conversions:
    • @differentiable to @differentiable(linear)
      • This conversion is not rejected because a @differentiable function can never come directly from a closure expression or from a declaration/member reference.
    • @differentiable(linear) to @differentiable (TF-908)
      • This is supported by design, but is not yet implemented due to its complexity. This requires thunking @differentiable(linear) to a derivative (JVP) function where the derivative function returns the original result and the same linear map with @nondiff parameters partial-applied away.
  • Properly handle linear_function, differentiable_function_extract, and linear_function_extract in swift::isGuaranteedForwardingValueKind and swift::isOwnershipForwardingValueKind. This fixed a previously uncaught assertion, which was because differentiable_function_extract was in swift::isOwnershipForwardingValueKind while it should really be in swift::isGuaranteedForwardingValueKind.

For complete conversion rules among @differentiable functions, see Differentiable Programming Mega-Proposal - Type conversion.

Resolves TF-900.

- Support the following implicit conversions:
  - Non-differentiable to `@differentiable(linear)`
    - Sema: Emit `LinearFunctionExpr`.
    - SILGen: Lower `LinearFunctionExpr` to `linear_function`.
  - `@differentiable(linear)` to non-differentiable
    - Sema: Emit `LinearFunctionExtractOriginalExpr`.
    - SILGen: Lower `LinearFunctionExtractOriginalExpr` to `linear_function_extract [original]`.
- Reject the following implicit conversions:
  - `@differentiable` to `@differentiable(linear)`
    - This conversion is not rejected because a `@differentiable` function can never come directly from a closure expression or from a declaration/member reference.
  - `@differentiable(linear)` to `@differentiable` ([TF-908](https://bugs.swift.org/browse/TF-908))
    - This is supported by design, but is not yet implemented due to its complexity. This requires thunking `@differentiable(linear)` to a derivative (JVP) function where the derivative function returns the original result and the same linear map with `@nondiff` parameters partial-applied away.
- Properly handle `linear_function`, `differentiable_function_extract`, and `linear_function_extract` in `swift::isGuaranteedForwardingValueKind` and `swift::isOwnershipForwardingValueKind`. This fixed a previously uncaught assertion, which was because `differentiable_function_extract` was in `swift::isOwnershipForwardingValueKind` while it should really be in `swift::isGuaranteedForwardingValueKind`.

For complete conversion rules among `@differentiable` functions, see [Differentiable Programming Mega-Proposal - Type conversion](https://github.com/dan-zheng/swift/blob/differentiable-programming/docs/DifferentiableProgramming.md#type-conversion).

Resolves [TF-900](https://bugs.swift.org/browse/TF-900).
@rxwei rxwei added the tensorflow This is for "tensorflow" branch PRs. label Oct 15, 2019
@rxwei rxwei requested a review from dan-zheng October 15, 2019 07:12
@@ -1,6 +1,5 @@
// RUN: %target-swift-frontend -dump-ast %s | %FileCheck %s -check-prefix=CHECK-AST
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s -check-prefix=CHECK-SILGEN
// RUN: %target-swift-frontend -emit-sil %s | %FileCheck %s -check-prefix=CHECK-SIL
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 RUN line was removed because differentiable_function_silgen.swift is supposed to be a SILGen test, not a SIL canonicalization test.

@rxwei
Copy link
Contributor Author

rxwei commented Oct 15, 2019

@swift-ci please test tensorflow

@rxwei rxwei merged commit 504b794 into swiftlang:tensorflow Oct 15, 2019
@rxwei rxwei deleted the differentiable-linear-conversion branch October 15, 2019 07:58
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