Skip to content

Skeleton code for annotation processor. - #12828

Merged
gunan merged 1 commit into
tensorflow:masterfrom
kbsriram:java-ops-3a-processor-bazel
Oct 15, 2017
Merged

gunan merged 1 commit into
tensorflow:masterfrom
kbsriram:java-ops-3a-processor-bazel

Conversation

@kbsriram

@kbsriram kbsriram commented Sep 5, 2017

Copy link
Copy Markdown
Contributor

Add build changes and skeleton code (with test harness) for
the Operator annotation processor. This change focuses on build
and test-related changes, and generates an empty Ops class.

Please see #7149 for the master tracking issue.

@tensorflow-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@mention-bot

Copy link
Copy Markdown

@kbsriram, thanks for your PR! By analyzing the history of the files in this pull request, we identified @asimshankar, @karllessard and @jart to be potential reviewers.

@yifeif
yifeif requested a review from asimshankar September 5, 2017 21:47
@yifeif yifeif added the awaiting review Pull request awaiting review label Sep 5, 2017
@drpngx

drpngx commented Sep 17, 2017

Copy link
Copy Markdown
Contributor

@asimshankar what do you think?

Jenkins, test this please.

@sb2nov

sb2nov commented Sep 26, 2017

Copy link
Copy Markdown
Contributor

@asimshankar ping

@kbsriram
kbsriram force-pushed the java-ops-3a-processor-bazel branch from 72015b2 to da2e5bf Compare October 2, 2017 17:15

@asimshankar asimshankar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for doing this and apologies for the delay. Some comments.

import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;

public final class OperatorProcessor extends AbstractProcessor {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A docstring explaining what this is and how it's doing it would be nice :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done


# Helper function to copy a srcjar after moving any source files
# directly under the root to the "maven-style" src/main/java layout
# Arguments:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm a bit lost on why we end up in this situation (that the src_jar contains anything outside the "maven-style" hierarchy. Perhaps that will dawn on my as I see the rest of the CL, just wondering out aloud for now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The problem is that the annotation processor doesn't control where its generated sources are written. The processor only provides a package name - the rest of it is determined by the environment.

The environment is first determined by the way bazel calls javac, and then by how bazel packages such generated files into its -srcs.jar. bazel generates files into a bazel-out directory, and then jars them directly in the root of its -srcs.jar file. Is there an option I missed to control all this? I tried adding things like compiler options, but this ends up just confusing bazel.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe mention this in a comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review Asim!

Have added a comment here.


/** Basic tests for {@link org.tensorflow.processor.operator.OperatorProcessor}. */
@RunWith(JUnit4.class)
public final class BasicProcessorTest {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be OperatorProcessorTest?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done - if the tests cases gets large large, I'll split it into BasicOperatorProcessorTest if needed.

Comment thread tensorflow/java/BUILD Outdated

java_library(
name = "operator_plugin_library",
srcs = [":operator_plugin_sources"],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why bother with the filegroup here instead of say srcs = glob(["src/gen/java/org/tensorflow/processor/**/*.java"])?

Comment thread tensorflow/java/BUILD Outdated
name = "operator_plugin_library",
srcs = [":operator_plugin_sources"],
javacopts = JAVACOPTS,
resources = [":operator_plugin_config"],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here - inline the resource instead of indirecting through a filegroup?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done


// Collect all classes tagged with our annotation.
Set<TypeElement> opClasses = new HashSet<TypeElement>();
for (TypeElement annotation : annotations) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't there be precisely one annotations? If so, perhaps we can assert that annotations.size() is 1 and then call collectOpClasses once?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It can be zero or 1 depending on which phase this runs in. I'll combine this with the rest of the constraint reformulation you suggested. Ptal, and if overall it reads better to you, sgtm.

// API class which cannot be modified once generated. If we find an annotation after we've
// generated our code, flag the location of each such class.
if (hasRun) {
error(e, "Cannot handle generated @Operators");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps elaborate on the error message - explaining that we already processed Operators and wrote out a file and cannot process additional ones?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done


// @Operator can only apply to types, so e must be a TypeElement.
if (!(e instanceof TypeElement)) {
error(e, "@Operator can only be applied to classes");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we add more information that will help those that may encounter this error in debugging? For example, e.getKind() and e.getSimpleName() seem like they have something useful in them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Given this shows up as a compiler error message with file/line/character pointing to the incorrectly targeted element, do you think that might be sufficient to locate the problem element itself? I've added e.getKind() to the error message as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sounds great, thanks.

message(Kind.ERROR, e, message, args);
}

private void message(Kind kind, Element e, String msg, Object... args) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optional: If message is used only for error, perhaps inline it in error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

// This processor has to aggregate all op classes in one round, as it generates a single Ops
// API class which cannot be modified once generated. If we find an annotation after we've
// generated our code, flag the location of each such class.
if (hasRun) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of doing this in the loop, what about the following:

  • Make this method:
private boolean collectOpClasses(Set<? extends Element> elements, Set<TypeElement> opClasses) {
  for (Element e : elements) {
    ...
  }
}
  • Make the caller (process) do something like:
assert annotations.size() == 0;
Set<? extends Element> annotated = roundEnv.getElementsAnnotatedWith(annotation);
if (annotated.size() == 0) {
  return true;
}
if (annotated.size() > 0 && hasRun) {
  // print error
  return false;
}
if (!collectOpClasses(annotated, opClasses)) {
  return false;
}
...

It's essentially the same, but I think it makes the expected constraints clearer to follow. Though, it's possible you disagree :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, combined with the previous suggestion.

@asimshankar asimshankar added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Oct 5, 2017
@kbsriram
kbsriram force-pushed the java-ops-3a-processor-bazel branch 2 times, most recently from 7dc76be to 952d5ec Compare October 9, 2017 19:22

@asimshankar asimshankar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great, thanks. One minor comment

Comment thread tensorflow/java/BUILD
deps = [":operator_plugin_library"],
)

java_library(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optional: Call this annotation_processors or just processors?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Each java_plugin defines one processor, so I've renamed it to "processor" and "processor_library", wdyt? If we add another processor, we can disambiguate this name later as needed.


# Helper function to copy a srcjar after moving any source files
# directly under the root to the "maven-style" src/main/java layout
# Arguments:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe mention this in a comment?

Add build changes and skeleton code (with test harness) for
the Operator annotation processor. This change focuses on build
and test-related changes, and generates an empty Ops class.

Please see tensorflow#7149 for the master tracking issue.
@kbsriram
kbsriram force-pushed the java-ops-3a-processor-bazel branch from 952d5ec to 5200945 Compare October 10, 2017 14:02
@kbsriram

Copy link
Copy Markdown
Contributor Author

@tensorflow-jenkins test this please

@asimshankar

Copy link
Copy Markdown
Contributor

Jenkins, test this please

@asimshankar asimshankar added awaiting testing (then merge) and removed stat:awaiting response Status - Awaiting response from author labels Oct 10, 2017
@kbsriram

Copy link
Copy Markdown
Contributor Author

Could a kindly member look at/restart the testing? Looks like the "Ubuntu CC" test didn't run, or stalled. Thanks!

@asimshankar

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@gunan gunan added the kokoro:force-run Tests on submitted change label Oct 13, 2017
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Oct 13, 2017
@gunan
gunan merged commit b75a5c2 into tensorflow:master Oct 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants