Repository navigation
IREE Java TFLite Bindings - #5361
Conversation
|
Wow, this looks freaking fantastic! I'll do a full review later tonight! One early comment is to remove the IREE_CHECKs and replace them with throwing JNI exceptions or IREE_DCHECK if they are debug-only. CHECK will be going away soon: #5238. |
|
@benvanik to clarify your earlier comment about IREE_CHECK. Essentially these runtime checks to ensure the Tensor/Interpreter have been initialized properly before interacting with them. Even outside of debug-only, the code will still fail without the check. According to #5238, we can't generate any crashes in native code? That leaves me with the only alternative to throw Java errors through JNI. It would look like jni_utils from tensorflow |
(a little rambling here mostly because I realized I was a too terse with that statement - sorry about that :) Correct - you shouldn't generate crashes in native code (you of course can, but you can't use crashes as flow control in production code :). A CHECK (which is always enabled regardless of debug/release) will cause the entire app to just blink out of existence and leave no information for the user. Some apps will have native backtrace hooks (like crashpad) and may be able to get the unsymbolized stack trace, but it's really not a great way to report an error especially if it's one that the user can encounter. You can return status codes or throw exceptions - I always use status codes, but exceptions are more java normal, and in some cases if you need to handle exceptions on the JNI side anyway it's nice to have a consistent handling mechanism. You can use DCHECK (which is disabled in release builds) but note that DCHECK is only to be used for precondition checks and such where you are asserting your behavior (as the developer of the code) is correct. For anything coming from a user of the API you don't want DCHECKs as the user won't see them unless they are in a debug build and even if they do they'll get the same app-mysteriously-disappears-and-now-spend-8-hours-trying-to-get-a-symbolicated-stack-trace issue. I've also seen lots of code where people try to get clever and assert/DCHECK/etc right before they then gracefully handle the error and return but instead of helping it just means you can never test your failure handling in debug builds. For generating exceptions you can either do it via JNI on the native side or return a status code of some kind (bool, int, etc) and then throw the exception from your java code. Ideally you validate calls as much as you can on the java side and avoid calling down into native code at all if there would be an error, but that's not always reasonable as sometimes you'd have to have a non-trivial amount of duplication to detect an error may occur instead of just letting it happen. For much of the usage in this PR you can simply change your As an example, returning a 0 for nativeCreateOutput can indicate an error to the java side which can then throw an exception: Exception handling can be tricky - there are times when exceptions may be thrown by calls you make from within JNI. For example things like NewDirectByteBuffer can throw OutOfMemoryError. FindClass will actually throw NoClassDefFoundError - in which case your CHECK is going to prevent that nice useful message from ever being seen as you'll crash before control returns back to Java for it to print the message. This has some good examples: https://www.developer.com/open-source/exception-handling-in-jni/. For example on the GetFieldID/etc stuff as in https://github.com/google/iree/pull/5361/files#diff-275e2db9a14df8ebb307e9b017a1f76f0bc005a9c00808415303e086e5bbad4cR30-R38 you'll want to be checking the JNIEnv for whether an exception was thrown (GetFieldID can throw NoSuchFieldError, etc) and returning NULL to try to get back up to Java ASAP. You can see a list of what JNI stuff throws here in quite possibly the ugliest website ever: https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html HTH and happy to chat more - sorry for not giving more background :) |
|
oh and most of that was just general stuff - for the specific instances in here where you are CHECKing that the pointer passed in was right those are perfect DCHECKs: there are no cases where a user can call the private native methods - only you can - and you should have checked in Java before coming in. The best way to handle errors like that though is to prevent them: if it's impossible for a user to ever get an instance of your java object that has a null/uninitialized native handle then you never have to check. Not possible if you're matching tflite that uses constructors for things - in those cases you pretty much have to just throw exceptions. |
af32794 to
a67f32d
Compare
a67f32d to
1241b64
Compare
benvanik
left a comment
There was a problem hiding this comment.
Looking good! Just some minor bits about a few lines of code you can remove and an extra check or two.
ScottTodd
left a comment
There was a problem hiding this comment.
LGTM from me, pending Ben's recent comments being resolved
Nice work!
0080801 to
119a551
Compare
IREE Java TFLite Bindings
Implementation of the IREE Java TFLIte API. This API is designed to provide a similar interface and as much compatibility as possible with Tensorflow's Interpreter.java, but there are a few key unsupported features:
In addition, some features aren't fully fleshed out yet or tested:
This compatibility layer will only accept java.nio.Buffer type classes as input. Javadoc details how to convert from non buffer based inputs into buffers and vice versa.
For API brevity, all features/methods IREE cannot support aren't included in this shim. Comments are updated to reflect differences in the behavior of the underlying IREE runtime.
Includes Java classes for the API as well as an Android implementation test. Built by Gradle.