Skip to content

[AutoDiff] Mangle derivative functions and linear maps #35259

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 1 commit into from
Jan 7, 2021

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Jan 5, 2021

Mangle derivative functions and linear maps.

  • Mangle::ASTMangler::mangleAutoDiffDerivativeFunction() and Mangle::ASTMangler::mangleAutoDiffLinearMap() accept original function declarations and return a mangled name for a derivative function or linear map. This is called during SILGen and TBDGen.
  • Mangle::DifferentiationMangler handles differentiation function mangling in the differentiation transform. This part is necessary because we need to perform demangling on the original function and remangle it as part of a differentiation function mangling tree in order to get the correct substitutions in the mangled derivative generic signature.

A mangled differentiation function name includes:

  • The original function.
  • The differentiation function kind.
  • The parameter indices for differentiation.
  • The result indices for differentiation.
  • The derivative generic signature.
global ::= global generic-signature? 'TJ' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // autodiff function

AUTODIFF-FUNCTION-KIND ::= 'f'        // JVP (forward-mode derivative)
AUTODIFF-FUNCTION-KIND ::= 'r'        // VJP (reverse-mode derivative)
AUTODIFF-FUNCTION-KIND ::= 'd'        // differential
AUTODIFF-FUNCTION-KIND ::= 'p'        // pullback

INDEX-SUBSET ::= ('S' | 'U')+

Example:

> swift-demangle --compact s13test_mangling4foo21xq_x_t16_Differentiation14DifferentiableR_AA1P13TangentVectorRp_r0_lFAdERzAdER_AafGRpzAafHRQr0_lTJrSpSr
reverse-mode derivative of test_mangling.foo2<A, B where B: _Differentiation.Differentiable, B.TangentVector: test_mangling.P>(x: A) -> B with respect to parameters {0} and results {0} with <A, B where A: _Differentiation.Differentiable, B: _Differentiation.Differentiable, A.TangentVector: test_mangling.P, B.TangentVector: test_mangling.P>

Also:

  • Make AD-generated linear map structures' fields have private access level so that TBDGen won't generate symbols for their accessors. (SR-14014)

Resolves SR-13507.

Copy link
Contributor

@dan-zheng dan-zheng left a comment

Choose a reason for hiding this comment

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

Nice!

@rxwei rxwei force-pushed the autodiff-mangling branch from 769d91c to d0ec736 Compare January 5, 2021 13:01
@rxwei rxwei marked this pull request as ready for review January 5, 2021 13:01
@rxwei
Copy link
Contributor Author

rxwei commented Jan 5, 2021

@swift-ci please test

@rxwei rxwei force-pushed the autodiff-mangling branch from d0ec736 to 3b65e15 Compare January 5, 2021 13:20
@rxwei
Copy link
Contributor Author

rxwei commented Jan 5, 2021

@swift-ci please smoke test

@swift-ci
Copy link
Contributor

swift-ci commented Jan 5, 2021

Build failed
Swift Test OS X Platform
Git Sha - d0ec73681f881d17420bb07eefaaffe58c29d138

@rxwei
Copy link
Contributor Author

rxwei commented Jan 5, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Jan 5, 2021

Build failed
Swift Test OS X Platform
Git Sha - 3b65e15826745991db49c8a8063feba5b8193907

@swift-ci
Copy link
Contributor

swift-ci commented Jan 5, 2021

Build failed
Swift Test Linux Platform
Git Sha - 3b65e15826745991db49c8a8063feba5b8193907

@rxwei
Copy link
Contributor Author

rxwei commented Jan 5, 2021

@swift-ci please smoke test

@rxwei
Copy link
Contributor Author

rxwei commented Jan 5, 2021

@swift-ci please test

@rxwei rxwei requested a review from CodaFi January 5, 2021 18:37
@swift-ci
Copy link
Contributor

swift-ci commented Jan 5, 2021

Build failed
Swift Test OS X Platform
Git Sha - 3b65e15826745991db49c8a8063feba5b8193907

@swift-ci
Copy link
Contributor

swift-ci commented Jan 5, 2021

Build failed
Swift Test Linux Platform
Git Sha - 3b65e15826745991db49c8a8063feba5b8193907

@rxwei
Copy link
Contributor Author

rxwei commented Jan 5, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Jan 6, 2021

Build failed
Swift Test Linux Platform
Git Sha - 3b65e15826745991db49c8a8063feba5b8193907

@rxwei
Copy link
Contributor Author

rxwei commented Jan 6, 2021

@swift-ci please test linux

@swift-ci
Copy link
Contributor

swift-ci commented Jan 6, 2021

Build failed
Swift Test OS X Platform
Git Sha - 3b65e15826745991db49c8a8063feba5b8193907

@swift-ci
Copy link
Contributor

swift-ci commented Jan 6, 2021

Build failed
Swift Test Linux Platform
Git Sha - 3b65e15826745991db49c8a8063feba5b8193907

@rxwei rxwei force-pushed the autodiff-mangling branch from 3b65e15 to 6e1df19 Compare January 6, 2021 07:47
@rxwei
Copy link
Contributor Author

rxwei commented Jan 6, 2021

@swift-ci please test

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

Basically LGTM. But I have a few comments.

Two general comments:

  • Can you add tests to test/Demangle/Inputs/manglings.txt?

  • It's good that you split such a large PR into multiple commits. But ideally each commit should be self-contained, i.e. each commit should contain a good commit message and the project should be compilable after each commit, and ideally the tests should pass. This makes it easier to understand "git log" and it enables "git bisect".
    Your commit messages "Make compile" and "Fix all tests" suggest that this is not the case here.

@rxwei rxwei force-pushed the autodiff-mangling branch 2 times, most recently from c1858a3 to 72b165a Compare January 7, 2021 10:12
- `Mangle::ASTMangler::mangleAutoDiffDerivativeFunction()` and `Mangle::ASTMangler::mangleAutoDiffLinearMap()` accept original function declarations and return a mangled name for a derivative function or linear map. This is called during SILGen and TBDGen.
- `Mangle::DifferentiationMangler` handles differentiation function mangling in the differentiation transform. This part is necessary because we need to perform demangling on the original function and remangle it as part of a differentiation function mangling tree in order to get the correct substitutions in the mangled derivative generic signature.

A mangled differentiation function name includes:
- The original function.
- The differentiation function kind.
- The parameter indices for differentiation.
- The result indices for differentiation.
- The derivative generic signature.
@rxwei rxwei force-pushed the autodiff-mangling branch from 72b165a to ffe6064 Compare January 7, 2021 10:21
@rxwei
Copy link
Contributor Author

rxwei commented Jan 7, 2021

It's good that you split such a large PR into multiple commits. But ideally each commit should be self-contained, i.e. each commit should contain a good commit message and the project should be compilable after each commit, and ideally the tests should pass. This makes it easier to understand "git log" and it enables "git bisect".
Your commit messages "Make compile" and "Fix all tests" suggest that this is not the case here.

Sorry, the commit log in the PR may appear confusing, but I did not intend to merge these commits as is. My intention is to keep the original WIP commit by @dan-zheng to acknowledge his original contribution in the PR. When I merge I always use squash-and-merge, so only one commit will go into the tree. Nonetheless, I have squashed those commits.

@rxwei
Copy link
Contributor Author

rxwei commented Jan 7, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Jan 7, 2021

Build failed
Swift Test Linux Platform
Git Sha - ffe6064

@rxwei
Copy link
Contributor Author

rxwei commented Jan 7, 2021

@swift-ci please test linux

@eeckstein eeckstein self-requested a review January 7, 2021 13:18
Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM!

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.

4 participants