-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] Use @derivative
attribute to register tgmath derivatives.
#28933
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
Conversation
@swift-ci Please test tensorflow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat!
c77417e
to
0ed102d
Compare
@swift-ci Please test tensorflow |
// CHECK-LABEL: sil [serializable] [readnone] [clang tan] @tan : $@convention(c) (Double) -> Double | ||
// CHECK-NOT: sil shared [serializable] [readnone] @$sSo3tanyS2dFTO : $@convention(thin) (Double) -> Double | ||
|
||
@derivative(of: tan) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: it'd be nicer to register a derivative for a foreign function that doesn't already have a registered derivative in the stdlib (e.g. tan
). Let's revisit this test if issues arise with @derivative(of: tan)
.
Some context below. Swift tgmath functions are interesting because they call and alias Clang-imported functions. For example,
One idea is to use module-qualified names in @derivative(Darwin.tan)
func _vjpTan(_ x: Float) -> (value: Float, pullback: (Float) -> Float) { ... } However, this doesn't work because some tgmath function references are SILGen'd as import Darwin
// Foreign: sil [serializable] [readnone] [clang tan] @tan : $@convention(c) (Double) -> Double
_ = tan(Double(3))
// Non-foreign: sil [transparent] [serialized] @$s6Darwin3tanyS2fF : $@convention(thin) (Float) -> Float
_ = tan(Float(3)) In any case, registering derivatives for the actual underlying function seems more robust/future-proof than registering derivatives for any given wrapper function, because:
I refactored derivative registration support for imported functions to a separate PR #29016. I then rebased this PR on top of #29016: differentiability witnesses are emitted for all underlying tgmath functions. |
Looks like clang-imported symbols are in the |
Thanks for the idea! I actually found Example
import Darwin
_ = __C.tan test.swift:2:5: error: use of unresolved identifier '__C'
_ = __C.tan
^~~ I'm not sure how to trigger SILGen for
--- a/lib/SILGen/SILGen.cpp
+++ b/lib/SILGen/SILGen.cpp
@@ -802,6 +802,12 @@ void SILGenModule::postEmitFunction(SILDeclRef constant,
derivativeGenSig);
emitDifferentiabilityWitness(origAFD, origFn, config, jvp, vjp,
derivAttr);
+ if (requiresForeignEntryPoint(origAFD)) {
+ auto origDeclRef = SILDeclRef(origAFD);
+ auto *origFn = getFunction(origDeclRef, NotForDefinition);
+ llvm::errs() << "Test non-foreign function (in the `__C` module):\n";
+ origFn->dump();
+ }
}
};
if (auto *accessor = dyn_cast<AccessorDecl>(AFD)) $ ninja swift-stdlib
Test non-foreign function (in the `__C` module):
// @nonobjc tan(_:)
sil shared [serializable] [readnone] @$sSo3tanyS2dFTO : $@convention(thin) (Double) -> Double
...
SIL verification failed: external declaration of internal SILFunction not allowed: F->isAvailableExternally()In function:
// @nonobjc tan(_:)
sil shared [serializable] [readnone] @$sSo3tanyS2dFTO : $@convention(thin) (Double) -> Double I believe the body of a |
- Compile stdlib with `-enable-experimental-cross-file-derivative-registration`. - This is necessary for using `@derivative` with tgmath functions, since some original functions (e.g. the `tan(_: Double) -> Double`) are imported from Clang. - Replace `@differentiable(jvp:vjp:)` with `@derivative` for tgmath functions. Progress towards TF-1085: using `@derivative` attribute in the stdlib.
0ed102d
to
05f199b
Compare
@swift-ci Please test tensorflow |
-enable-experimental-cross-file-derivative-registration
.@derivative
with tgmath functions, sincesome original functions (e.g.
tan(_: Double) -> Double
) are importedfrom Clang.
@differentiable(jvp:vjp)
with@derivative
for tgmath functions.Progress towards TF-1085: using
@derivative
attribute in the stdlib.