Java API DataBuffers.of uses buffer incorrectly
fyang996 opened this issue · 1 comments
fyang996 commented
Hi,
I am seeing this behavior when creating a Tensor using a buffer that is created by wrapping an array.
The example below can reproduce the issue:
// create an buffer by wrapping an array but buffer start from position 3 and length is 4
final IntBuffer src = IntBuffer.wrap(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8}, 3, 4).asReadOnlyBuffer();
final org.tensorflow.Tensor tensor = TInt32.tensorOf(Shape.of(4), DataBuffers.of(src));
final int[] value = new int[4];
tensor.asRawTensor().data().asInts().read(value);
assertArrayEquals(new int[]{0, 1, 2, 3}, value); // This is not really a correct behavior, it is not honoring the buffer offset when wrapping the array, it just takes the first 4 elements
//assertArrayEquals(new int[]{3, 4, 5, 6}, value); // This should be the desired result
System info:
Java 8
tensorflow-java (tensorflow-core-api) 0.4.1
karllessard commented
Thank you for reporting this issue @fanyang-dotcom , I've transferred it to the java-ndarray
repository since it has more to do with I/O buffers than TensorFlow.
I would have expect this to work as well, I'll take a look. Meanwhile, if that may unblock you, it seems that slicing it before passing it to the tensor works:
// create an buffer by wrapping an array but buffer start from position 3 and length is 4
final IntBuffer src = IntBuffer.wrap(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8}, 3, 4).asReadOnlyBuffer();
final org.tensorflow.Tensor tensor = TInt32.tensorOf(Shape.of(4), DataBuffers.of(src.slice()));