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 1 commit
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,6 +311,11 @@ private void throwIfTypeIsIncompatible(Object o) {
return;
}
DataType oType = dataTypeOf(o);

// INT8 and UINT8 have the same string name, "byte"
if (oType.toStringName() == dtype.toStringName()) {
freedomtan marked this conversation as resolved.
Show resolved Hide resolved
return;
}
if (oType != dtype) {
throw new IllegalArgumentException(
String.format(
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