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

[java] Allows the creation and extraction of zero length tensors #15116

Merged
merged 3 commits into from
Apr 5, 2023

Conversation

Craigacp
Copy link
Contributor

@Craigacp Craigacp commented Mar 20, 2023

Description

Allows the creation of zero length tensors via the buffer path (the array path with zero length arrays still throws as the validation logic to check it's not ragged would require more intrusive revision), and allows the tensor.getValue() method to return a Java multidimensional array with a zero dimension. Also added a test for the creation and extraction behaviour.

Motivation and Context

The Python interface can return zero length tensors (e.g. if object detection doesn't find any objects), and before this PR in Java calling tensor.getValue() throws an exception with a confusing error message. Fixes #7270 & #15107.

@Craigacp
Copy link
Contributor Author

Craigacp commented Apr 3, 2023

@yuslepukhin would you mind taking a look at this? It fixes a behaviour difference between Java & Python which people run into when deploying object recognition systems that don't find any objects in the supplied image.

@yuslepukhin
Copy link
Member

yuslepukhin commented Apr 3, 2023

@yuslepukhin would you mind taking a look at this? It fixes a behaviour difference between Java & Python which people run into when deploying object recognition systems that don't find any objects in the supplied image.

I think you mean scalar tensors. They are not zero length. They do have empty shapes, however, the length is 1. Correct me if I am wrong.

@Craigacp
Copy link
Contributor Author

Craigacp commented Apr 3, 2023

@yuslepukhin would you mind taking a look at this? It fixes a behaviour difference between Java & Python which people run into when deploying object recognition systems that don't find any objects in the supplied image.

I think you mean scalar tensors. They are not zero length. They do have empty shapes, however, the length is 1. Correct me if I am wrong.

No, those are already supported in the Java API. This specifically deals with the case where a tensor is used with a variable dimension that happens to be zero. For example if the output shape is[num_objects, 4] but the specific output tensor is of size [0, 4] because no objects were detected. The current behaviour in the Java API is to throw an exception with a confusing error message when tensor.getValue() is called, the new behaviour is to return a float[0][4] (or similar), which is closer to what Python does, which is return a numpy array with shape [0,4].

@yuslepukhin
Copy link
Member

/azp run MacOS CI Pipeline, Windows CPU CI Pipeline, Windows GPU CI Pipeline, Windows GPU TensorRT CI Pipeline, ONNX Runtime Web CI Pipeline, onnxruntime-python-checks-ci-pipeline

@yuslepukhin
Copy link
Member

/azp run Linux CPU CI Pipeline, Linux CPU Minimal Build E2E CI Pipeline, Linux GPU CI Pipeline, Linux GPU TensorRT CI Pipeline, Linux Nuphar CI Pipeline, Linux OpenVINO CI Pipeline, Linux QNN CI Pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 5 pipeline(s).

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@yuslepukhin
Copy link
Member

/azp run orttraining-amd-gpu-ci-pipeline, orttraining-linux-ci-pipeline, orttraining-linux-gpu-ci-pipeline, orttraining-ortmodule-distributed, onnxruntime-binary-size-checks-ci-pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 5 pipeline(s).

@yuslepukhin
Copy link
Member

/azp run Windows ARM64 QNN CI Pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

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

Ship

@yuslepukhin yuslepukhin merged commit ef11032 into microsoft:main Apr 5, 2023
@Craigacp Craigacp deleted the java-zero-tensor branch April 5, 2023 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ai.onnxruntime.OrtException: This tensor is not representable in Java, it's too big
2 participants