Skip to content

[AutoDiff] Diagnose non-differentiable original function arguments/results. #26407

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 4 commits into from
Jul 30, 2019

Conversation

dan-zheng
Copy link
Contributor

Diagnose non-differentiable original function arguments/results in
VJPEmitter::visitApplyInst. Perform the check again if original function
is specialized via partial application.

Resolves TF-687.

Note: this PR depends on #26404 - update and test after it is merged.


struct TF_687<T> : Differentiable {
  @noDerivative var indirectDummy: T
  var base: Float

  init(_ base: Float, dummy: T) {
    self.base = base
    self.indirectDummy = dummy
  }
}
let _: @differentiable (Float) -> TF_687<Any> = { x in TF_687<Any>(x, dummy: x) }

Before:

$ swift tf-687.swift
Assertion failed: (paramTan && "Parameter type does not have a tangent space?"), function getAutoDiffAssociatedFunctionType, file /usr/local/src/swift-build/swift/lib/SIL/SILFunctionType.cpp, line 266.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-tensorflow-DEVELOPMENT-2019-07-25-a.xctoolchain/usr/bin/swift -frontend -interpret tf-687.swift -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name main
1.	Swift version 5.1-dev (LLVM 200186e28b, Swift 3416770d73)
2.	While running pass #668 SILModuleTransform "Differentiation".
0  swift                    0x0000000105154ad5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x0000000105153b18 llvm::sys::RunSignalHandlers() + 248
2  swift                    0x00000001051550c8 SignalHandler(int) + 264
3  libsystem_platform.dylib 0x00007fff65936b5d _sigtramp + 29
4  libsystem_platform.dylib 0x0000000111e2f938 _sigtramp + 2890894840
5  libsystem_c.dylib        0x00007fff657f06a6 abort + 127
6  libsystem_c.dylib        0x00007fff657b920d basename_r + 0
7  swift                    0x000000010541b1d3 swift::SILFunctionType::getAutoDiffAssociatedFunctionType(swift::AutoDiffIndexSubset*, unsigned int, unsigned int, swift::AutoDiffAssociatedFunctionKind, swift::SILModule&, llvm::function_ref<llvm::Optional<swift::ProtocolConformanceRef> (swift::CanType, swift::Type, swift::ProtocolDecl*)>, swift::CanGenericSignature) (.cold.22) + 35
8  swift                    0x0000000101d42f15 swift::SILFunctionType::getAutoDiffAssociatedFunctionType(swift::AutoDiffIndexSubset*, unsigned int, unsigned int, swift::AutoDiffAssociatedFunctionKind, swift::SILModule&, llvm::function_ref<llvm::Optional<swift::ProtocolConformanceRef> (swift::CanType, swift::Type, swift::ProtocolDecl*)>, swift::CanGenericSignature) + 3477
9  swift                    0x0000000101d6f5a2 swift::AutoDiffFunctionExtractInst::getExtracteeType(swift::SILValue, swift::AutoDiffFunctionExtractInst::Extractee, unsigned int, swift::SILModule&) + 162
10 swift                    0x0000000101d6f64c swift::AutoDiffFunctionExtractInst::AutoDiffFunctionExtractInst(swift::SILModule&, swift::SILDebugLocation, swift::AutoDiffFunctionExtractInst::Extractee, unsigned int, swift::SILValue) + 44
11 swift                    0x0000000101a920a4 (anonymous namespace)::VJPEmitter::visitApplyInst(swift::ApplyInst*) + 7764
12 swift                    0x0000000101a6c449 (anonymous namespace)::ADContext::processDifferentiableAttribute(swift::SILFunction*, swift::SILDifferentiableAttr*, (anonymous namespace)::DifferentiationInvoker) + 10233
13 swift                    0x0000000101abb1ca (anonymous namespace)::ADContext::promoteToDifferentiableFunction(swift::AutoDiffFunctionInst*, swift::SILBuilder&, swift::SILLocation, (anonymous namespace)::DifferentiationInvoker) + 4954
14 swift                    0x0000000101a6f63a (anonymous namespace)::ADContext::processAutoDiffFunctionInst(swift::AutoDiffFunctionInst*) + 378
15 swift                    0x0000000101a69501 (anonymous namespace)::Differentiation::run() + 2913

After:

$ swift tf-687.swift
tf-687.swift:10:49: error: function is not differentiable
let _: @differentiable (Float) -> TF_687<Any> = { x in TF_687<Any>(x, dummy: x) }
                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tf-687.swift:10:78: note: cannot differentiate through a non-differentiable argument; do you want to use 'withoutDerivative(at:)'?
let _: @differentiable (Float) -> TF_687<Any> = { x in TF_687<Any>(x, dummy: x) }
                                                                             ^

Create simple `BasicTypeSubstCloner` inheriting from `TypeSubstCloner`.
Use `BasicTypeSubstCloner` to clone generic curry thunks, remapping types.

Resolves TF-688.
@dan-zheng dan-zheng added the tensorflow This is for "tensorflow" branch PRs. label Jul 30, 2019
original, invoker, diag::autodiff_nondifferentiable_result);
errorOccurred = true;
// Check and diagnose non-differentiable original function type.
auto diagnoseNonDifferentiableOriginalFunctionType =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: this logic should be ported to JVPEmitter::visitApply in #26057.
cc @bartchr808

Copy link
Contributor

Choose a reason for hiding this comment

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

Made a note of this to be a TODO item for myself! 😄

@dan-zheng dan-zheng requested review from rxwei and bartchr808 July 30, 2019 08:39
…sults.

Diagnose non-differentiable original function arguments/results in
`VJPEmitter::visitApplyInst`. Perform the check again if original function
is specialized via partial application.

Resolves TF-687.
@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow

@dan-zheng dan-zheng merged commit 5a73cc1 into swiftlang:tensorflow Jul 30, 2019
@dan-zheng dan-zheng deleted the TF-687 branch July 30, 2019 17:35
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.

3 participants