Skip to content

Java API DataBuffers.of uses buffer incorrectly #9

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

Open
fyang996 opened this issue Aug 31, 2022 · 1 comment
Open

Java API DataBuffers.of uses buffer incorrectly #9

fyang996 opened this issue Aug 31, 2022 · 1 comment

Comments

@fyang996
Copy link

fyang996 commented Aug 31, 2022

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 karllessard transferred this issue from tensorflow/java Sep 2, 2022
@karllessard
Copy link
Collaborator

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()));

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

No branches or pull requests

2 participants