Skip to content
This repository was archived by the owner on Jul 15, 2025. It is now read-only.

Conversation

@JimClarke5
Copy link
Contributor

Add Support for Sparse Arrays

@JimClarke5 JimClarke5 changed the title Sparse ternsor Sparse tensor Jul 30, 2021
@JimClarke5
Copy link
Contributor Author

@karllessard I think I need you to approve running the workflows for this PR.

@Craigacp
Copy link
Contributor

Craigacp commented Aug 1, 2021

I approved the CI run. GitHub changed actions so all first time contributions to a repo require approval so after this is integrated you'll be fine.

@JimClarke5
Copy link
Contributor Author

@karllessard @Craigacp I am getting many JavaDoc errors on the old code. Did we want to fix them now?

Also, I submitted a fix to one class in sparse with a JavaDoc error, but the same 1 workflow awaiting approval came back up.

* @param shape the shape of the dense array represented by this sparse array.
* @return the byte sparse array.
*/
public static ByteSparseNdArray sparseTensorOf(
Copy link
Contributor

Choose a reason for hiding this comment

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

In the Java NdArray space, we don't talk about "tensors" but simply about "arrays". So I would suggest to rename this method and the following by simply sparseOf

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, renamed all to sparseOf

* <pre>{@code
* FloatSparseNdArray st = new FloatSparseNdArray(
* StdArrays.of(new long[][] {{0, 0}, {1, 2}},
* StdArrays.of(new float[] {1f, 2f},
Copy link
Contributor

Choose a reason for hiding this comment

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

Whenever possible in your example, I would suggest to use NdArrays instead of passing by StdArrays, which is slower. Something like:

new FloatSparseNdArray(
    StdArrays.of(new long[][] {{0, 0}, {1, 2}}), <----- missing closing parentheses by the way...
    NdArrays.ofFloats(1f, 2f),
    Shape.of(3, 4)
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually NdArrays.ofFloats(...) is wrong, it should be NdArrays.vectorOf(...), for the values. I have changed the values argument to NdArrays.vectorOf(...), and fixed closing parenthesis. Also, changed
setValues(StdArrays.ndCopyOf(valuesArray)) to setValues(NdArrays.vectorOf(valuesArray)) in the write(ByteDataBuffer) methods.

}

/**
* Performs a binary search on the indices array to locate the the index of the specified
Copy link
Contributor

Choose a reason for hiding this comment

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

"the the" -> "the"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok found in 3 places and fixed.

import java.nio.ReadOnlyBufferException;
import java.util.concurrent.atomic.AtomicInteger;

public class ByteSparseWindow extends SparseWindow<Byte, ByteNdArray> implements ByteNdArray {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we rename this package and its classes to slice instead? (i.e. org.tensorflow.ndarray.impl.sparse.slice.ByteSparseSlice`)

Because window is already being used by DataBufferWindow, which is a different concept and act differently (allowing to "slide" a view inside a buffer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

@karllessard
Copy link
Contributor

Amazing job @JimClarke5 ! Please see my comments, mostly minor stuff

Change sparseTensorOf method names to sparseOf
Add more javadoc descriptions od Indices and Values in SparseNdArray interface.
'org.tensorflow.ndarray.impl.sparse.SparseNdArray' and
'org.tensorflow.ndarray.impl.sparse.slice.ObjectSparseSlice'.
Added test
'org.tensorflow.ndarray.impl.sparse.StringSparseNdArrayTest' to test Object sparse array using String.
I had to modify slowEquals in 'AbstractNdArray' to handle null values in equals

Also change javadoc from "nonzero" to "non-default" to be more accurate when zero is not the default.
* }</pre>
*/
public class SparseNdArray<T, U extends NdArray<T>> extends AbstractSparseNdArray<T, U>
implements org.tensorflow.ndarray.SparseNdArray<T, U> {
Copy link
Contributor

Choose a reason for hiding this comment

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

The name conflict between the interface and the "object" implementation is a bit annoying but I'm ok keeping it like that for now and readjust later if needed, since that SparseNdArray is a "private" implementation.

Copy link
Contributor

@karllessard karllessard left a comment

Choose a reason for hiding this comment

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

Thanks @JimClarke5 , you have done an amazing job!

@karllessard karllessard merged commit 887fc19 into tensorflow:main Aug 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants