-
Notifications
You must be signed in to change notification settings - Fork 722
Java Tensor and EValue serialization #6620
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -679,4 +679,105 @@ private static Tensor nativeNewTensor( | |
| tensor.mHybridData = hybridData; | ||
| return tensor; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes a {@code Tensor} into a byte array. | ||
| * | ||
| * @return The serialized byte array. | ||
| * @apiNote This method is experimental and subject to change without notice. This does NOT | ||
| * supoprt list type. | ||
| */ | ||
| public byte[] toByteArray() { | ||
| int dtypeSize = 0; | ||
| byte[] tensorAsByteArray = null; | ||
| if (dtype() == DType.FLOAT) { | ||
| dtypeSize = 4; | ||
|
||
| tensorAsByteArray = new byte[(int) numel() * dtypeSize]; | ||
| Tensor_float32 thiz = (Tensor_float32) this; | ||
| ByteBuffer.wrap(tensorAsByteArray).asFloatBuffer().put(thiz.getDataAsFloatArray()); | ||
| } else if (dtype() == DType.DOUBLE) { | ||
| dtypeSize = 8; | ||
| tensorAsByteArray = new byte[(int) numel() * dtypeSize]; | ||
| Tensor_float64 thiz = (Tensor_float64) this; | ||
| ByteBuffer.wrap(tensorAsByteArray).asDoubleBuffer().put(thiz.getDataAsDoubleArray()); | ||
| } else if (dtype() == DType.UINT8) { | ||
| dtypeSize = 1; | ||
| tensorAsByteArray = new byte[(int) numel()]; | ||
| Tensor_uint8 thiz = (Tensor_uint8) this; | ||
| ByteBuffer.wrap(tensorAsByteArray).put(thiz.getDataAsUnsignedByteArray()); | ||
| } else if (dtype() == DType.INT8) { | ||
| dtypeSize = 1; | ||
| tensorAsByteArray = new byte[(int) numel()]; | ||
| Tensor_int8 thiz = (Tensor_int8) this; | ||
| ByteBuffer.wrap(tensorAsByteArray).put(thiz.getDataAsByteArray()); | ||
| } else if (dtype() == DType.INT16) { | ||
| throw new IllegalArgumentException("DType.INT16 is not supported in Java so far"); | ||
| } else if (dtype() == DType.INT32) { | ||
| dtypeSize = 4; | ||
| tensorAsByteArray = new byte[(int) numel() * dtypeSize]; | ||
| Tensor_int32 thiz = (Tensor_int32) this; | ||
| ByteBuffer.wrap(tensorAsByteArray).asIntBuffer().put(thiz.getDataAsIntArray()); | ||
| } else if (dtype() == DType.INT64) { | ||
| dtypeSize = 8; | ||
| tensorAsByteArray = new byte[(int) numel() * dtypeSize]; | ||
| Tensor_int64 thiz = (Tensor_int64) this; | ||
| ByteBuffer.wrap(tensorAsByteArray).asLongBuffer().put(thiz.getDataAsLongArray()); | ||
| } else { | ||
| throw new IllegalArgumentException("Unknown Tensor dtype"); | ||
| } | ||
| ByteBuffer byteBuffer = | ||
| ByteBuffer.allocate(1 + 1 + 4 * shape.length + dtypeSize * (int) numel()); | ||
| byteBuffer.put((byte) dtype().jniCode); | ||
| byteBuffer.put((byte) shape.length); | ||
| for (long s : shape) { | ||
| byteBuffer.putInt((int) s); | ||
| } | ||
| byteBuffer.put(tensorAsByteArray); | ||
| return byteBuffer.array(); | ||
| } | ||
|
|
||
| /** | ||
| * Deserializes a {@code Tensor} from a byte[]. | ||
| * | ||
| * @param buffer The byte array to deserialize from. | ||
| * @return The deserialized {@code Tensor}. | ||
| * @apiNote This method is experimental and subject to change without notice. This does NOT | ||
| * supoprt list type. | ||
| */ | ||
| public static Tensor fromByteArray(byte[] bytes) { | ||
| if (bytes == null) { | ||
| throw new IllegalArgumentException("bytes cannot be null"); | ||
| } | ||
| ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||
| if (!buffer.hasRemaining()) { | ||
| throw new IllegalArgumentException("invalid buffer"); | ||
| } | ||
| byte scalarType = buffer.get(); | ||
| byte numberOfDimensions = buffer.get(); | ||
|
||
| long[] shape = new long[(int) numberOfDimensions]; | ||
| long numel = 1; | ||
| for (int i = 0; i < numberOfDimensions; i++) { | ||
| int dim = buffer.getInt(); | ||
| if (dim < 0) { | ||
| throw new IllegalArgumentException("invalid shape"); | ||
| } | ||
| shape[i] = dim; | ||
| numel *= dim; | ||
| } | ||
| if (scalarType == DType.FLOAT.jniCode) { | ||
| return new Tensor_float32(buffer.asFloatBuffer(), shape); | ||
| } else if (scalarType == DType.DOUBLE.jniCode) { | ||
| return new Tensor_float64(buffer.asDoubleBuffer(), shape); | ||
| } else if (scalarType == DType.UINT8.jniCode) { | ||
| return new Tensor_uint8(buffer, shape); | ||
| } else if (scalarType == DType.INT8.jniCode) { | ||
| return new Tensor_int8(buffer, shape); | ||
| } else if (scalarType == DType.INT16.jniCode) { | ||
| return new Tensor_int32(buffer.asIntBuffer(), shape); | ||
| } else if (scalarType == DType.INT64.jniCode) { | ||
| return new Tensor_int64(buffer.asLongBuffer(), shape); | ||
| } else { | ||
| throw new IllegalArgumentException("Unknown Tensor dtype"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will we add list support as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per Jacob, list is an internal dtype within ET runtime. Maybe we should totally get rid of list in java layer. I could double check with the team