Skip to content

[AutoDiff] Disallow differentiation of opaque-result-typed functions. #32714

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
Jul 6, 2020

Conversation

dan-zheng
Copy link
Contributor

Reject @differentiable and @derivative attribute for original functions with opaque result types.

It is not possible to support derivative registration nor the differentiation transform for such functions.

There's no way to express the proper derivative type for functions returning some Differentiable:

func foo(_ x: Float) -> some Differentiable { x }

@derivative(of: foo)
// The expected JVP/VJP type isn't currently expressible:
func vjpFoo(_ x: Float) -> <T: Differentiable> (value: T, pullback: (T.TangentVector) -> TangentVector) { ... }

Aside from derivative registration, there's no principled way to "unpack" values of type (some Differentiable).TangentVector to get the underlying value, which would be necessary in the differentiation transform.

sil private [ossa] @AD__foo__pullback_src_0_wrt_0 : $@convention(thin) (@in_guaranteed @_opaqueReturnTypeOf("foo", 0) 🦸.TangentVector, @owned _AD__foo_bb0__PB__src_0_wrt_0) -> Float {
bb0(%0 : $*@_opaqueReturnTypeOf("foo", 0) 🦸.TangentVector, %1 : $_AD__foo_bb0__PB__src_0_wrt_0):
  // How to unpack `$*@_opaqueReturnTypeOf("foo", 0) 🦸.TangentVector` to the actual
  // underlying non-opaque type (e.g. `$*Float`)?
  //
  // We could perform some unsafe type coercion (`unchecked_addr_cast`), not sure about the full implications.
}

Resolves SR-12656: differentiation transform crash.

Reject `@differentiable` and `@derivative` attribute for original
functions with opaque result types.

It is not possible to support derivative registration nor the
differentiation transform for such functions.

Resolves SR-12656.
@dan-zheng dan-zheng requested review from rxwei and marcrasi July 5, 2020 23:30
@dan-zheng
Copy link
Contributor Author

@swift-ci Please smoke test

@dan-zheng
Copy link
Contributor Author

@swift-ci Please smoke test

@dan-zheng dan-zheng merged commit 9c20198 into swiftlang:master Jul 6, 2020
@dan-zheng dan-zheng deleted the SR-12656 branch July 6, 2020 04:15
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