Skip to content

[AutoDiff] Be able to parse linear argument in @differentiable attribute #25228

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
Jun 4, 2019

Conversation

bartchr808
Copy link
Contributor

This PR adds the ability to parse and handle a linear argument in the @differentiable attribute in parseDifferentiableAttributeArguments

This PR does not handle modifying the C++ attribute class such that it handles whether the linear attribute is being used.

- Adds a 'linear` argument to '@differentiable' attribute
- 'linear' argument is optional, but when added, needs to be the first argument in the attribute
@bartchr808 bartchr808 added the tensorflow This is for "tensorflow" branch PRs. label Jun 4, 2019
@bartchr808 bartchr808 requested review from rxwei and dan-zheng June 4, 2019 00:32
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.

Could you please also create and add tests to test/AutoDiff/differentiating_attr_parse.swift?
You can:

  • Copy differentiating_attr_type_checking.swift
  • Change the RUN line to %target-swift-frontend -parse -verify %s
  • Preserve just a few @differentiating examples - no need to keep "original" functions since Sema isn't tested.
  • Add @differentiating(linear, ...) tests.

Thank you!

// Parse 'linear' label (optional).
if (Tok.is(tok::identifier) && Tok.getText() == "linear") {
linear = true;
consumeIdentifier();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
consumeIdentifier();
consumeIdentifier(tok::identifier);

This makes an assertion about the identifier being consumed, preventing errors in future changes just in case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

consumeIdentifier() does the following assertion inside to check if it's identifier, but also does a couple more:

assert(Tok.isAny(tok::identifier, tok::kw_self, tok::kw_Self));

However, I think calling consumeToken(tok::identifier) could be a better option as it internally does the following assertion to specifically only check if it's an identifier:

SourceLoc consumeToken(tok K) {
  assert(Tok.is(K) && "Consuming wrong token kind");
  return consumeToken();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it should be consumeToken(). I didn't notice that and thought you wrote consumeToken already :)

@bartchr808
Copy link
Contributor Author

Could you please also create and add tests to test/AutoDiff/differentiating_attr_parse.swift?

@dan-zheng I'll do it in my next PR for the @differentiating attr 😄

@rxwei
Copy link
Contributor

rxwei commented Jun 4, 2019

Could you please also create and add tests to test/AutoDiff/differentiating_attr_parse.swift?

@dan-zheng I'll do it in my next PR for the @differentiating attr 😄

For @differentiating, the linear symbol should appear after the original function name and a comma, e.g. @differentiating(orig(_:_:), linear), and @differentiating(orig(_:_:), linear, wrt: x).

@bartchr808
Copy link
Contributor Author

For @differentiating, the linear symbol should appear after the original function name and a comma, e.g. @differentiating(orig(::), linear), and @differentiating(orig(::), linear, wrt: x).

Sounds good!

@bartchr808
Copy link
Contributor Author

@swift-ci please test tensorflow

1 similar comment
@bartchr808
Copy link
Contributor Author

@swift-ci please test tensorflow

@bartchr808
Copy link
Contributor Author

Hmm seems like CI got some weird issue:

'Build finished. No test results found.'

Will try again

@bartchr808
Copy link
Contributor Author

@swift-ci please test tensorflow

@bartchr808 bartchr808 merged commit 07ea212 into swiftlang:tensorflow Jun 4, 2019
@bartchr808 bartchr808 deleted the diff-linear-attr branch June 4, 2019 18:42
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.

3 participants