Skip to content

[Android] Add Tensor_unsupoprted return type instead of crash #10414

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

Merged
merged 5 commits into from
Apr 24, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@ public enum DType {
DType(int jniCode) {
this.jniCode = jniCode;
}

public static DType fromJniCode(int jniCode) {
for (DType dtype : values()) {
if (dtype.jniCode == jniCode) {
return dtype;
}
}
throw new IllegalArgumentException("No DType found for jniCode " + jniCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.pytorch.executorch;

import android.util.Log;
import com.facebook.jni.HybridData;
import com.facebook.jni.annotations.DoNotStrip;
import java.nio.Buffer;
Expand Down Expand Up @@ -630,6 +631,30 @@ public String toString() {
}
}

static class Tensor_unsupported extends Tensor {
private final ByteBuffer data;
private final DType mDtype;

private Tensor_unsupported(ByteBuffer data, long[] shape, DType dtype) {
super(shape);
this.data = data;
this.mDtype = dtype;
Log.e(
"ExecuTorch",
toString() + " in Java. Please consider re-export the model with proper return type");
}

@Override
public DType dtype() {
return mDtype;
}

@Override
public String toString() {
return String.format("Unsupported tensor(%s, dtype=%d)", Arrays.toString(shape), this.mDtype);
}
}

// region checks
private static void checkArgument(boolean expression, String errorMessage, Object... args) {
if (!expression) {
Expand Down Expand Up @@ -675,7 +700,7 @@ private static Tensor nativeNewTensor(
} else if (DType.INT8.jniCode == dtype) {
tensor = new Tensor_int8(data, shape);
} else {
throw new IllegalArgumentException("Unknown Tensor dtype");
tensor = new Tensor_unsupported(data, shape, DType.fromJniCode(dtype));
}
tensor.mHybridData = hybridData;
return tensor;
Expand Down
Loading