Skip to content

[AutoDiff upstream] Upstream @derivative attribute type-checking. #28738

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 5 commits into from
Dec 13, 2019

Conversation

dan-zheng
Copy link
Contributor

@dan-zheng dan-zheng commented Dec 12, 2019

The @derivative attribute registers a function as a derivative of another
function-like declaration: a func, init, subscript, or var computed
property declaration.

The @derivative attribute also has an optional wrt: clause specifying the
parameters that are differentiated "with respect to", i.e. the differentiation
parameters. The differentiation parameters must conform to the Differentiable
protocol.

If the wrt: clause is unspecified, the differentiation parameters are inferred
to be all parameters that conform to Differentiable.

@derivative attribute type-checking verifies that the type of the derivative
function declaration is consistent with the type of the referenced original
declaration and the differentiation parameters.

Resolves TF-829.


Examples:

func sin(_ x: Float) -> Float { ... }

@derivative(of: sin)
func derivativeSin(_ x: Float) -> (value: Float, differential: (Float) -> Float) {
    (sin(x), { dx in dx * cos(x) })
}

public protocol ElementaryFunctions {
    static func log(_ x: Self) -> Self
    ...
}

public extension ElementaryFunctions
where Self: Differentiable & FloatingPoint, Self == Self.TangentVector {
    @inlinable
    @derivative(of: log)
    static func derivativeLog(_ x: Self) -> (value: Self, differential: (Self) -> Self) {
        (log(x), { dx in (1 / x) * dx })
    }
}

The differentiable programming manifesto has more information and examples.


Note: this code is upstreamed from tensorflow branch. Some functions are intentionally not static (fileprivate) because they have users in other files on tensorflow branch.

Known issues are referenced in comments:

  • TF-874: Simplify type calculation helpers.
  • TF-1042: Remove DerivativeAttrs cache from ASTContext.

We would like to upstream code and continue development on master, if that's acceptable!

cc @rxwei @marcrasi @bgogul

The `@derivative` attribute registers a function as a derivative of another
function-like declaration: a 'func', 'init', 'subscript', or 'var' computed
property declaration.

The `@derivative` attribute also has an optional `wrt:` clause specifying the
parameters that are differentiated "with respect to", i.e. the differentiation
parameters. The differentiation parameters must conform to the `Differentiable`
protocol.

If the `wrt:` clause is unspecified, the differentiation parameters are inferred
to be all parameters that conform to `Differentiable`.

`@derivative` attribute type-checking verifies that the type of the derivative
function declaration is consistent with the type of the referenced original
declaration and the differentiation parameters.

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

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 9e09105

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 9e09105

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 9e09105

Copy link
Contributor

@lattner lattner left a comment

Choose a reason for hiding this comment

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

Nice, I love the testcase, great job.

@dan-zheng dan-zheng force-pushed the derivative-attr-type-checking branch from 02515c9 to 2ce6b05 Compare December 12, 2019 22:25
@dan-zheng
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 2a63ab7

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 2a63ab7

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test macOS

@dan-zheng
Copy link
Contributor Author

Merging to unblock progress on upstreaming AutoDiff. Happy to address additional feedback later!

@dan-zheng dan-zheng merged commit bb1052c into swiftlang:master Dec 13, 2019
@dan-zheng dan-zheng deleted the derivative-attr-type-checking branch December 13, 2019 02:18
@compnerd
Copy link
Member

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.

5 participants