Skip to content
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

[tflite] enable INT8 for Java binding #36397

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -30,7 +30,10 @@ public enum DataType {
INT64(4),

/** Strings. */
STRING(5);
STRING(5),

/** 8-bit signed integer. */
INT8(9);

private final int value;

Expand All @@ -45,6 +48,7 @@ public int byteSize() {
return 4;
case INT32:
return 4;
case INT8:
case UINT8:
return 1;
case INT64:
Expand Down Expand Up @@ -83,6 +87,7 @@ String toStringName() {
return "float";
case INT32:
return "int";
case INT8:
case UINT8:
return "byte";
case INT64:
Expand Down
Expand Up @@ -311,7 +311,13 @@ private void throwIfTypeIsIncompatible(Object o) {
return;
}
DataType oType = dataTypeOf(o);

if (oType != dtype) {
// INT8 and UINT8 have the same string name, "byte"
if (oType.toStringName().equals(dtype.toStringName())) {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Can you add a test case in DataTypeTest.java to verify this equality? You can basically just copy this exact logic and make sure it holds for int8/uint8.

return;
}

throw new IllegalArgumentException(
String.format(
"Cannot convert between a TensorFlowLite tensor with type %s and a Java "
Expand Down
2 changes: 2 additions & 0 deletions tensorflow/lite/java/src/main/native/tensor_jni.cc
Expand Up @@ -126,6 +126,7 @@ size_t WriteOneDimensionalArray(JNIEnv* env, jobject object, TfLiteType type,
env->GetLongArrayRegion(long_array, 0, num_elements, long_dst);
return to_copy;
}
case kTfLiteInt8:
case kTfLiteUInt8: {
jbyteArray byte_array = static_cast<jbyteArray>(array);
jbyte* byte_dst = static_cast<jbyte*>(dst);
Expand Down Expand Up @@ -174,6 +175,7 @@ size_t ReadOneDimensionalArray(JNIEnv* env, TfLiteType data_type,
static_cast<const jlong*>(src));
return size;
}
case kTfLiteInt8:
case kTfLiteUInt8: {
jbyteArray byte_array = static_cast<jbyteArray>(dst);
env->SetByteArrayRegion(byte_array, 0, len,
Expand Down
Expand Up @@ -39,4 +39,11 @@ public void testConversion() {
assertThat(DataType.fromC(dataType.c())).isEqualTo(dataType);
}
}

@Test
public void testINT8AndUINT8() {
assertThat(DataType.INT8.toStringName()).isEqualTo("byte");
assertThat(DataType.UINT8.toStringName()).isEqualTo("byte");
assertThat(DataType.INT8.toStringName()).isEqualTo(DataType.UINT8.toStringName());
}
}