Skip to content

[AutoDiff] Fix @differentiable attribute derivative configurations. #31524

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
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7106,6 +7106,11 @@ void AbstractFunctionDecl::prepareDerivativeFunctionConfigurations() {
ArrayRef<AutoDiffConfig>
AbstractFunctionDecl::getDerivativeFunctionConfigurations() {
prepareDerivativeFunctionConfigurations();
// Resolve derivative function configurations from `@differentiable`
// attributes by type-checking them.
for (auto *diffAttr : getAttrs().getAttributes<DifferentiableAttr>())
(void)diffAttr->getParameterIndices();
// Load derivative configurations from imported modules.
auto &ctx = getASTContext();
if (ctx.getCurrentGeneration() > DerivativeFunctionConfigGeneration) {
unsigned previousGeneration = DerivativeFunctionConfigGeneration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import _Differentiation

protocol Protocol: Differentiable {
// Test cross-file `@differentiable` attribute.
@differentiable(wrt: self)
func identityDifferentiableAttr() -> Self
}

extension Protocol {
func identityDerivativeAttr() -> Self { self }

// Test cross-file `@derivative` attribute.
@derivative(of: identityDerivativeAttr)
func vjpIdentityDerivativeAttr() -> (
value: Self, pullback: (TangentVector) -> TangentVector
) {
fatalError()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %target-swift-frontend -emit-sil -verify -primary-file %s %S/Inputs/differentiation_diagnostics_other_file.swift -module-name main -o /dev/null

// Test differentiation transform cross-file diagnostics.

import _Differentiation

// TF-1271: Test `@differentiable` original function in other file.
@differentiable
func crossFileDifferentiableAttr<T: Protocol>(
_ input: T
) -> T {
return input.identityDifferentiableAttr()
}

// TF-1272: Test original function with registered derivatives in other files.
// FIXME(TF-1272): Find a way to type-check `@derivative` attributes in other
// files.
@differentiable
func crossFileDerivativeAttr<T: Protocol>(
_ input: T
) -> T {
// expected-error @+2 {{expression is not differentiable}}
// expected-note @+1 {{cannot differentiate functions that have not been marked '@differentiable' and that are defined in other files}}
return input.identityDerivativeAttr()
}