Skip to content

Commit 864b30b

Browse files
lu-wang-gtflite-support-robot
authored andcommitted
Throw RuntimeExceptions instead of AssertionError in NLClassifier
PiperOrigin-RevId: 401316443
1 parent 87743ab commit 864b30b

File tree

2 files changed

+67
-52
lines changed

2 files changed

+67
-52
lines changed

tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/text/nlclassifier/NLClassifier.java

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -209,59 +209,61 @@ public abstract static class Builder {
209209
private static final String NL_CLASSIFIER_NATIVE_LIBNAME = "task_text_jni";
210210

211211
/**
212-
* Constructor to initialize the JNI with a pointer from C++.
212+
* Creates {@link NLClassifier} from default {@link NLClassifierOptions}.
213213
*
214-
* @param nativeHandle a pointer referencing memory allocated in C++.
215-
*/
216-
protected NLClassifier(long nativeHandle) {
217-
super(nativeHandle);
218-
}
219-
220-
/**
221-
* Create {@link NLClassifier} from default {@link NLClassifierOptions}.
222-
*
223-
* @param context Android context.
224-
* @param pathToModel Path to the classification model relative to asset dir.
225-
* @return {@link NLClassifier} instance.
226-
* @throws IOException If model file fails to load.
214+
* @param context Android context
215+
* @param modelPath path to the classification model relative to asset dir
216+
* @return an {@link NLClassifier} instance
217+
* @throws IOException if model file fails to load
218+
* @throws IllegalArgumentException if an argument is invalid
219+
* @throws IllegalStateException if there is an internal error
220+
* @throws RuntimeException if there is an otherwise unspecified error
227221
*/
228-
public static NLClassifier createFromFile(Context context, String pathToModel)
229-
throws IOException {
230-
return createFromFileAndOptions(context, pathToModel, NLClassifierOptions.builder().build());
222+
public static NLClassifier createFromFile(Context context, String modelPath) throws IOException {
223+
return createFromFileAndOptions(context, modelPath, NLClassifierOptions.builder().build());
231224
}
232225

233226
/**
234-
* Create {@link NLClassifier} from default {@link NLClassifierOptions}.
227+
* Creates {@link NLClassifier} from default {@link NLClassifierOptions}.
235228
*
236-
* @param modelFile The classification model {@link File} instance.
237-
* @return {@link NLClassifier} instance.
238-
* @throws IOException If model file fails to load.
229+
* @param modelFile the classification model {@link File} instance
230+
* @return an {@link NLClassifier} instance
231+
* @throws IOException if model file fails to load
232+
* @throws IllegalArgumentException if an argument is invalid
233+
* @throws IllegalStateException if there is an internal error
234+
* @throws RuntimeException if there is an otherwise unspecified error
239235
*/
240236
public static NLClassifier createFromFile(File modelFile) throws IOException {
241237
return createFromFileAndOptions(modelFile, NLClassifierOptions.builder().build());
242238
}
243239

244240
/**
245-
* Create {@link NLClassifier} from {@link NLClassifierOptions}.
241+
* Creates {@link NLClassifier} from {@link NLClassifierOptions}.
246242
*
247243
* @param context Android context
248-
* @param pathToModel Path to the classification model relative to asset dir.
249-
* @param options Configurations for the model.
250-
* @return {@link NLClassifier} instance.
251-
* @throws IOException If model file fails to load.
244+
* @param modelPath path to the classification model relative to asset dir
245+
* @param options configurations for the model.
246+
* @return an {@link NLClassifier} instance
247+
* @throws IOException if model file fails to load
248+
* @throws IllegalArgumentException if an argument is invalid
249+
* @throws IllegalStateException if there is an internal error
250+
* @throws RuntimeException if there is an otherwise unspecified error
252251
*/
253252
public static NLClassifier createFromFileAndOptions(
254-
Context context, String pathToModel, NLClassifierOptions options) throws IOException {
255-
return createFromBufferAndOptions(TaskJniUtils.loadMappedFile(context, pathToModel), options);
253+
Context context, String modelPath, NLClassifierOptions options) throws IOException {
254+
return createFromBufferAndOptions(TaskJniUtils.loadMappedFile(context, modelPath), options);
256255
}
257256

258257
/**
259-
* Create {@link NLClassifier} from {@link NLClassifierOptions}.
258+
* Creates {@link NLClassifier} from {@link NLClassifierOptions}.
260259
*
261-
* @param modelFile The classification model {@link File} instance.
262-
* @param options Configurations for the model.
263-
* @return {@link NLClassifier} instance.
264-
* @throws IOException If model file fails to load.
260+
* @param modelFile the classification model {@link File} instance
261+
* @param options configurations for the model
262+
* @return an {@link NLClassifier} instance
263+
* @throws IOException if model file fails to load
264+
* @throws IllegalArgumentException if an argument is invalid
265+
* @throws IllegalStateException if there is an internal error
266+
* @throws RuntimeException if there is an otherwise unspecified error
265267
*/
266268
public static NLClassifier createFromFileAndOptions(
267269
File modelFile, final NLClassifierOptions options) throws IOException {
@@ -284,12 +286,14 @@ public long createHandle() {
284286
}
285287

286288
/**
287-
* Create {@link NLClassifier} with a model {@link ByteBuffer} and {@link NLClassifierOptions}.
289+
* Creates {@link NLClassifier} with a model {@link ByteBuffer} and {@link NLClassifierOptions}.
288290
*
289291
* @param modelBuffer a direct {@link ByteBuffer} or a {@link MappedByteBuffer} of the
290292
* classification model
291-
* @param options Configurations for the model
293+
* @param options configurations for the model
292294
* @return {@link NLClassifier} instance
295+
* @throws IllegalStateException if there is an internal error
296+
* @throws RuntimeException if there is an otherwise unspecified error
293297
* @throws IllegalArgumentException if the model buffer is not a direct {@link ByteBuffer} or a
294298
* {@link MappedByteBuffer}
295299
*/
@@ -316,15 +320,29 @@ public long createHandle() {
316320
}
317321

318322
/**
319-
* Perform classification on a string input, returns classified {@link Category}s.
323+
* Performs classification on a string input, returns classified {@link Category}s.
320324
*
321-
* @param text input text to the model.
322-
* @return A list of Category results.
325+
* @param text input text to the model
326+
* @return a list of Category results
323327
*/
324328
public List<Category> classify(String text) {
325329
return classifyNative(getNativeHandle(), text);
326330
}
327331

332+
/**
333+
* Constructor to initialize the JNI with a pointer from C++.
334+
*
335+
* @param nativeHandle a pointer referencing memory allocated in C++.
336+
*/
337+
protected NLClassifier(long nativeHandle) {
338+
super(nativeHandle);
339+
}
340+
341+
@Override
342+
protected void deinit(long nativeHandle) {
343+
deinitJni(nativeHandle);
344+
}
345+
328346
private static native long initJniWithByteBuffer(
329347
NLClassifierOptions options, ByteBuffer modelBuffer, long baseOptionsHandle);
330348

@@ -333,11 +351,6 @@ private static native long initJniWithFileDescriptor(
333351

334352
private static native List<Category> classifyNative(long nativeHandle, String text);
335353

336-
@Override
337-
protected void deinit(long nativeHandle) {
338-
deinitJni(nativeHandle);
339-
}
340-
341354
/**
342355
* Native implementation to release memory pointed by the pointer.
343356
*

tensorflow_lite_support/java/src/native/task/text/nlclassifier/nl_classifier_jni.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ extern std::unique_ptr<OpResolver> CreateOpResolver();
3232

3333
namespace {
3434

35+
using ::tflite::support::utils::GetExceptionClassNameForStatusCode;
3536
using ::tflite::support::utils::GetMappedFileBuffer;
3637
using ::tflite::support::utils::JStringToString;
37-
using ::tflite::support::utils::kAssertionError;
3838
using ::tflite::support::utils::kInvalidPointer;
3939
using ::tflite::support::utils::ThrowException;
4040
using ::tflite::task::core::BaseOptions;
@@ -90,6 +90,8 @@ NLClassifierOptions ConvertToProtoOptions(JNIEnv* env,
9090
return proto_options;
9191
}
9292

93+
} // namespace
94+
9395
extern "C" JNIEXPORT void JNICALL
9496
Java_org_tensorflow_lite_task_text_nlclassifier_NLClassifier_deinitJni(
9597
JNIEnv* env, jobject thiz, jlong native_handle) {
@@ -113,9 +115,10 @@ Java_org_tensorflow_lite_task_text_nlclassifier_NLClassifier_initJniWithByteBuff
113115
if (classifier_or.ok()) {
114116
return reinterpret_cast<jlong>(classifier_or->release());
115117
} else {
116-
ThrowException(env, kAssertionError,
117-
"Error occurred when initializing NLClassifier: %s",
118-
classifier_or.status().message().data());
118+
ThrowException(
119+
env, GetExceptionClassNameForStatusCode(classifier_or.status().code()),
120+
"Error occurred when initializing NLClassifier: %s",
121+
classifier_or.status().message().data());
119122
return kInvalidPointer;
120123
}
121124
}
@@ -138,9 +141,10 @@ Java_org_tensorflow_lite_task_text_nlclassifier_NLClassifier_initJniWithFileDesc
138141
if (classifier_or.ok()) {
139142
return reinterpret_cast<jlong>(classifier_or->release());
140143
} else {
141-
ThrowException(env, kAssertionError,
142-
"Error occurred when initializing NLClassifier: %s",
143-
classifier_or.status().message().data());
144+
ThrowException(
145+
env, GetExceptionClassNameForStatusCode(classifier_or.status().code()),
146+
"Error occurred when initializing NLClassifier: %s",
147+
classifier_or.status().message().data());
144148
return kInvalidPointer;
145149
}
146150
}
@@ -150,5 +154,3 @@ Java_org_tensorflow_lite_task_text_nlclassifier_NLClassifier_classifyNative(
150154
JNIEnv* env, jclass thiz, jlong native_handle, jstring text) {
151155
return RunClassifier(env, native_handle, text);
152156
}
153-
154-
} // namespace

0 commit comments

Comments
 (0)