Skip to content

[AutoDiff] Store original declaration in DifferentiableAttr. #27985

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

Conversation

dan-zheng
Copy link
Contributor

@dan-zheng dan-zheng commented Oct 31, 2019

Store original declaration Decl * in DifferentiableAttr.

This is important for requestifying
DifferentiableAttr->getParameterIndices(): we want the ability to
resolve parameter indices without needing to pass the original
AbstractFunctionDecl to getParameterIndices.

Add round-trip @differentiable attribute AST serialization test.
@differentiable attribute type-checking and serialization assert
that the original declaration is set.


A step towards unblocking the swift-DEVELOPMENT-SNAPSHOT-2019-10-24-a merge.

@dan-zheng dan-zheng added the tensorflow This is for "tensorflow" branch PRs. label Oct 31, 2019
@dan-zheng
Copy link
Contributor Author

This fails locally during stdlib compilation with a nondescript error:

[14/233] Generating /Users/danielzheng/swif...acosx/Darwin.swiftmodule/x86_64.swiftmodule
FAILED: lib/swift/macosx/Darwin.swiftmodule/x86_64.swiftmodule lib/swift/macosx/Darwin.swiftmodule/x86_64.swiftdoc lib/swift/macosx/Darwin.swiftmodule/x86_64.swiftinterface
cd /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/stdlib/public/Platform && /usr/local/Cellar/cmake/3.15.4/bin/cmake -E remove -f /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx/Darwin.swiftmodule/x86_64.swiftmodule /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx/Darwin.swiftmodule/x86_64.swiftdoc /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx/Darwin.swiftmodule/x86_64.swiftinterface && /usr/local/Cellar/cmake/3.15.4/bin/cmake -E make_directory /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx /Users/danielzheng/sw
ift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx/Darwin.swiftmodule /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx/Darwin.swiftmodule/Project && /usr/bin/python /Users/danielzheng/swift-tf/swift/utils/line-directive @/Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/stdlib/public/Platform/YhWMK.txt -- /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./bin/swiftc -emit-module -o /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx/Darwin.swiftmodule/x86_64.swiftmodule -emit-module-source-info-path /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx/Darwin.swiftmodule/Project/x86_64.swiftsourceinfo -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -target x86_64-apple-macosx10.9 -resource-dir /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/../../../Developer/Library/Frameworks -O -D SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING -I /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx -module-cache-path /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./module-cache -no-link-objc-runtime -enable-library-evolution -Xfrontend -enforce-exclusivity=unchecked -module-name Darwin -swift-version 5 -swift-version 5 -autolink-force-load -runtime-compatibility-version none -disable-autolinking-runtime-compatibility-dynamic-replacements -warn-swift3-objc-inference-complete -Xfrontend -disable-objc-attr-requires-foundation-module -Xfrontend -verify-syntax-tree -warn-implicit-overrides -Fsystem /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/PrivateFrameworks/ -module-link-name swiftDarwin -force-single-frontend-invocation -parse-as-library -emit-module-interface-path /Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/./lib/swift/macosx/Darwin.swiftmodule/x86_64.swiftinterface @/Users/danielzheng/swift-tf/build/Ninja-ReleaseAssert+stdlib-Release/swift-macosx-x86_64/stdlib/public/Platform/YhWMK.txt
<unknown>:0: error: unable to execute command: Illegal instruction: 4
<unknown>:0: error: compile command failed due to signal 4 (use -v to see invocation)

Triggering tests to see if the issue is reproducible.

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow


FuncDecl *originalDecl = cast<FuncDecl>(MF.getDecl(originalDeclId));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is this line causes an infinite loop: we cannot deserialize the original declaration here because @differentiable attributes are deserialized as part of deserializing the original declaration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 4df0307 via ad-hoc deserialization logic. I couldn't think of a better solution.

Store original `AbstractFunctionDecl` in `DifferentiableAttr`.

This is important for requestifying
`DifferentiableAttr->getParameterIndices()`: we want the ability to
resolve parameter indices without needing to pass the original
`AbstractFunctionDecl` to `getParameterIndices`.
Deserializing the original declaration DeclID in `@differentiable`
attributes does not work because it causes `@differentiable` attribute
deserialization to enter an infinite loop.

Instead, use ad-hoc deserialization logic to set the original declaration
in `@differentiable` attributes.

Add round-trip `@differentiable` attribute AST serialization test.
`@differentiable` attribute serialization asserts that the original
declaration is set.
@dan-zheng dan-zheng force-pushed the diff-attr-store-original-decl branch from 93ce245 to 4df0307 Compare October 31, 2019 03:51
@dan-zheng
Copy link
Contributor Author

Triggering extended test suite (tensorflow/swift-apis, tensorflow/swift-models, etc).

@dan-zheng
Copy link
Contributor Author

dan-zheng commented Oct 31, 2019

Extended test suite failed while building tensorflow/swift-apis:

Step 17/30 : RUN /swift-tensorflow-toolchain/usr/bin/swift test
 ---> Running in 1a511890753d
[1/57] Compiling TensorFlow Optimizer.swift
[2/57] Compiling TensorFlow TensorShape.swift
[3/57] Compiling TensorFlow Threading.swift
[4/57] Compiling TensorFlow TensorHandle.swift
[5/57] Compiling TensorFlow TensorProtocol.swift
[6/57] Compiling TensorFlow Execution.swift
[7/57] Compiling TensorFlow LazyTensorContext.swift
[8/57] Compiling TensorFlow StdlibExtensions.swift
[9/57] Compiling TensorFlow ShapedArray.swift
[10/57] Compiling TensorFlow StringTensor.swift
[11/57] Compiling TensorFlow Runtime.swift
[12/57] Compiling TensorFlow Serialization.swift
[13/57] Compiling TensorFlow SGD.swift
[14/57] Compiling TensorFlow LazyTensorTFFunctionBuilder.swift
[15/57] Compiling TensorFlow LazyTensorTrace.swift
[16/57] Compiling TensorFlow Context.swift
[17/57] Compiling TensorFlow ArrayOps.swift
[18/57] Compiling TensorFlow Random.swift
[19/57] Compiling TensorFlow Comparison.swift
[20/57] Compiling TensorFlow BackwardsCompatibility.swift
[21/58] Compiling TensorFlow Dataset.swift
[22/58] Merging module Experimental
[23/60] Wrapping AST for Experimental for debugging
[24/60] Compiling ExperimentalTests ComplexTests.swift
[25/60] Compiling ExperimentalTests XCTestManifests.swift
[26/60] Compiling TensorFlow DataTypes.swift
[27/60] Compiling TensorFlow DifferentialOperators.swift
[28/60] Compiling TensorFlow LazyTensorTraceCache.swift
[29/60] Compiling TensorFlow PythonConversion.swift
[30/60] Compiling TensorFlow Sequential.swift
[31/60] Compiling TensorFlow Tensor.swift
[32/60] Compiling TensorFlow TensorGroup.swift
[33/60] Compiling TensorFlow LazyTensorOperation.swift
[34/60] Compiling TensorFlow LazyTensorShapeInference.swift
[37/61] Merging module ExperimentalTests
[38/61] Wrapping AST for ExperimentalTests for debugging
[39/61] Compiling TensorFlow NN.swift
[40/61] Compiling TensorFlow Upsampling.swift
[41/61] Compiling TensorFlow MomentumBased.swift
[42/61] Compiling TensorFlow Utilities.swift
[43/61] Compiling TensorFlow Initializers.swift
[44/61] Compiling TensorFlow Initialization.swift
[45/61] Compiling TensorFlow Normalization.swift
[46/61] Compiling TensorFlow Recurrent.swift
[47/61] Compiling TensorFlow Basic.swift
[48/61] Compiling TensorFlow RawOpsGenerated.swift
[49/61] Compiling TensorFlow TFTensorOperation.swift
[50/61] Compiling TensorFlow Core.swift
[51/61] Compiling TensorFlow Embedding.swift
[52/61] Compiling TensorFlow Loss.swift
[53/61] Compiling TensorFlow Pooling.swift
[54/61] Compiling TensorFlow Layer.swift
[55/61] Compiling TensorFlow Convolutional.swift
[56/61] Compiling TensorFlow Math.swift
swift: /swift-base/swift/lib/Serialization/Serialization.cpp:2313: void swift::serialization::Serializer::DeclSerializer::writeDeclAttribute(const swift::DeclAttribute *): Assertion `attr->getOriginalFunction() && "@differentiable attribute must have original function resolved"' failed.
Stack dump:
0.	Program arguments: /swift-tensorflow-toolchain/usr/bin/swift -frontend -merge-modules -emit-module /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/BackwardsCompatibility~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Bindings/EagerExecution~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Bindings/RawOpsGenerated~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Bindings/TFTensorOperation~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Context~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/ArrayOps~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/DataTypes~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/DifferentialOperators~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/Execution~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/LazyTensorContext~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/LazyTensorOperation~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/LazyTensorShapeInference~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/LazyTensorTFFunctionBuilder~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/LazyTensorTrace~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/LazyTensorTraceCache~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/PythonConversion~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/Runtime~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/Serialization~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/ShapedArray~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/StringTensor~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/Tensor~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/TensorGroup~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/TensorHandle~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/TensorProtocol~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/TensorShape~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/Threading~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Core/Utilities~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Initializers~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layer~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layers/Convolutional~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layers/Core~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layers/Embedding~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layers/Initialization~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layers/Normalization~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layers/Pooling~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layers/Recurrent~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layers/Sequential~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Layers/Upsampling~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Loss~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Operators/Basic~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Operators/Comparison~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Operators/Dataset~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Operators/Math~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Operators/NN~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Optimizers/MomentumBased~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Optimizers/Optimizer~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Optimizers/SGD~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/Random~partial.swiftmodule /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.build/StdlibExtensions~partial.swiftmodule -parse-as-library -sil-merge-partial-modules -disable-diagnostic-passes -disable-sil-perf-optzns -target x86_64-unknown-linux-gnu -disable-objc-interop -I /swift-apis/.build/x86_64-unknown-linux-gnu/debug -enable-testing -g -module-cache-path /swift-apis/.build/x86_64-unknown-linux-gnu/debug/ModuleCache -swift-version 4.2 -Onone -D SWIFT_PACKAGE -D DEBUG -enable-anonymous-context-mangled-names -emit-module-doc-path /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.swiftdoc -emit-module-source-info-path /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.swiftsourceinfo -module-name TensorFlow -o /swift-apis/.build/x86_64-unknown-linux-gnu/debug/TensorFlow.swiftmodule 
1.	Swift version 5.1.1-dev (LLVM 6e04008c7f, Swift 4df03077b6)
2.	While running pass #6054 SILModuleTransform "SerializeSILPass".
3.	While serializing 'layer1' (in module 'TensorFlow')

Please hold off on merging; more changes are necessary to support requestifying @differentiable attribute parameter indices resolution.

Store original declaration `Decl *` in `DifferentiableAttr` instead of
casting to `AbstractFunctionDecl *`. This helps ensure that the original
declaration is set.
@dan-zheng dan-zheng force-pushed the diff-attr-store-original-decl branch from 0c62032 to e2be0a2 Compare October 31, 2019 23:58
- Add tight assertion `attr->getOriginalDeclaration` to TypeCheckAttr.cpp.
- Move `setOriginalFunctionInDifferentiableAttributes` call out of
  `if (declWasHandledAlready(D))` body.
  - This was found necessary while requestifying parameter indices resolution.
@dan-zheng dan-zheng force-pushed the diff-attr-store-original-decl branch from 4415f95 to 12bd568 Compare November 1, 2019 00:19
@dan-zheng dan-zheng requested a review from marcrasi November 1, 2019 01:00
@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow

@dan-zheng
Copy link
Contributor Author

Confirmed that extended test suite has passed.

@dan-zheng dan-zheng merged commit 38bc954 into swiftlang:tensorflow Nov 1, 2019
@dan-zheng dan-zheng deleted the diff-attr-store-original-decl branch November 1, 2019 06:22
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