Skip to content

Refactor JNI code in C++ into Java code with JavaCPP - #18

Merged
karllessard merged 6 commits into
tensorflow:masterfrom
saudet:remove-jni-code
Jan 29, 2020
Merged

karllessard merged 6 commits into
tensorflow:masterfrom
saudet:remove-jni-code

Conversation

@saudet

@saudet saudet commented Jan 20, 2020

Copy link
Copy Markdown
Contributor

This is a WIP to remove the manually written JNI code from tensorflow-core-api. In the first commit for SavedModelBundle.java, I replaced about 100 lines of JNI code in C++ with about 40 lines of code in Java, and the unit tests still pass. We need to do the same with the rest of the JNI code, which shouldn't take me that long to do by myself, but if anyone is interested in helping, please let me know and I will give you write access to my fork!

There is also a lot of code in the Java section of the wrappers that is redundant with JavaCPP and that we will be able to remove in a second phase.

/cc @tzolov

@saudet
saudet requested a review from karllessard January 20, 2020 14:31
@saudet saudet changed the title Refactor saved_model_bundle_jni into SavedModelBundle with JavaCPP Refactor JNI code in C++ into Java code with JavaCPP Jan 20, 2020
throw new IndexOutOfBoundsException("MetaGraphDef is too large to serialize into a byte[] array");
} else {
byte[] jmetagraph_def = new byte[(int)metagraph_def.length()];
new BytePointer(metagraph_def.data()).get(jmetagraph_def);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I suggest we move this array copy stunt in AbstractTF_Buffer as a utility method like toBytes(), since TF_Buffer is meant to be generic.

Alternatively, we could retain a reference to metaGraphDef and return its data as a ByteBuffer to the user instead of an array, as it is only used for deserializing the proto message... But I'm wondering why we just don't import those proto classes in the core API and return typed objects to the user like MetaGraphDef instead of leaving him the burden to serialize/deserialize the proto messages.

What do you think @sjamesr ? Is the original reason of leaving the protos outside the Java client was only to avoid an extra dependency to grpc?

@karllessard

Copy link
Copy Markdown
Collaborator

@saudet , this PR is still marked as WIP, looks like it's ready to be merged now?

@saudet

saudet commented Jan 22, 2020

Copy link
Copy Markdown
Contributor Author

I was thinking we could do all of it together before merging? It shouldn't take me more than a few days to do...

@karllessard

Copy link
Copy Markdown
Collaborator

As you wish, we can do it iteratively with smaller PRs as well

@saudet

saudet commented Jan 23, 2020

Copy link
Copy Markdown
Contributor Author

Let's talk about it at the meeting tomorrow :)

@saudet
saudet marked this pull request as ready for review January 27, 2020 22:27

@karllessard karllessard left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good @saudet , I just left a few questions and remarks here and there.

I didn't compared yet the old JNI code with the new Java implementations but I think we can also rely on the Junit coverage for this.