From d10262ebd79fd32f16035f25145bd1ac60a20fe4 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Tue, 11 Jan 2022 10:34:36 -0500 Subject: [PATCH 01/26] Allow SparseNdArray impls to be inherited (#5) --- .../impl/sparse/BooleanSparseNdArray.java | 22 +++++++++---------- .../impl/sparse/ByteSparseNdArray.java | 16 +++++++------- .../impl/sparse/DoubleSparseNdArray.java | 16 +++++++------- .../impl/sparse/FloatSparseNdArray.java | 16 +++++++------- .../ndarray/impl/sparse/IntSparseNdArray.java | 16 +++++++------- .../impl/sparse/LongSparseNdArray.java | 16 +++++++------- .../impl/sparse/ShortSparseNdArray.java | 16 +++++++------- .../ndarray/impl/sparse/SparseNdArray.java | 18 +++++++-------- 8 files changed, 68 insertions(+), 68 deletions(-) diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java index f9e41bf..f5b984f 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java @@ -63,7 +63,7 @@ public class BooleanSparseNdArray extends AbstractSparseNdArray implements ByteNdArray { /** - * Creates a ByteSparseNdArray + * Creates a ByteSparseNdArray with a default value of zero. * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). @@ -73,14 +73,16 @@ public class ByteSparseNdArray extends AbstractSparseNdArray * each element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter * {@code values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a * value of {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}. + * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()} * @param dimensions the dimensional space for the dense object represented by this sparse array, */ - ByteSparseNdArray(LongNdArray indices, ByteNdArray values, DimensionalSpace dimensions) { - this(indices, values, (byte) 0, dimensions); + protected ByteSparseNdArray( + LongNdArray indices, ByteNdArray values, byte defaultValue, DimensionalSpace dimensions) { + super(indices, values, defaultValue, dimensions); } /** - * Creates a ByteSparseNdArray with a default value of zero. + * Creates a ByteSparseNdArray * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). @@ -90,12 +92,10 @@ public class ByteSparseNdArray extends AbstractSparseNdArray * each element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter * {@code values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a * value of {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}. - * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()} * @param dimensions the dimensional space for the dense object represented by this sparse array, */ - ByteSparseNdArray( - LongNdArray indices, ByteNdArray values, byte defaultValue, DimensionalSpace dimensions) { - super(indices, values, defaultValue, dimensions); + ByteSparseNdArray(LongNdArray indices, ByteNdArray values, DimensionalSpace dimensions) { + this(indices, values, (byte) 0, dimensions); } /** diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java index 27a0832..07a6d2a 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java @@ -63,7 +63,7 @@ public class DoubleSparseNdArray extends AbstractSparseNdArray implements IntNdArray { /** - * Creates a IntSparseNdArray with a default value of zero. + * Creates a IntSparseNdArray * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). @@ -73,14 +73,16 @@ public class IntSparseNdArray extends AbstractSparseNdArray * in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code * values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a value of * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}. + * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()} * @param dimensions the dimensional space for the dense object represented by this sparse array, */ - IntSparseNdArray(LongNdArray indices, IntNdArray values, DimensionalSpace dimensions) { - this(indices, values, 0, dimensions); + protected IntSparseNdArray( + LongNdArray indices, IntNdArray values, int defaultValue, DimensionalSpace dimensions) { + super(indices, values, defaultValue, dimensions); } /** - * Creates a IntSparseNdArray + * Creates a IntSparseNdArray with a default value of zero. * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). @@ -90,12 +92,10 @@ public class IntSparseNdArray extends AbstractSparseNdArray * in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code * values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a value of * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}. - * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()} * @param dimensions the dimensional space for the dense object represented by this sparse array, */ - IntSparseNdArray( - LongNdArray indices, IntNdArray values, int defaultValue, DimensionalSpace dimensions) { - super(indices, values, defaultValue, dimensions); + IntSparseNdArray(LongNdArray indices, IntNdArray values, DimensionalSpace dimensions) { + this(indices, values, 0, dimensions); } /** diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java index 3385022..242ba50 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java @@ -62,7 +62,7 @@ public class LongSparseNdArray extends AbstractSparseNdArray implements LongNdArray { /** - * Creates a LongSparseNdArray with a default value of zero. + * Creates a LongSparseNdArray * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). @@ -72,14 +72,16 @@ public class LongSparseNdArray extends AbstractSparseNdArray * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code * values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a value of * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}. + * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()} * @param dimensions the dimensional space for the dense object represented by this sparse array, */ - LongSparseNdArray(LongNdArray indices, LongNdArray values, DimensionalSpace dimensions) { - this(indices, values, 0L, dimensions); + protected LongSparseNdArray( + LongNdArray indices, LongNdArray values, long defaultValue, DimensionalSpace dimensions) { + super(indices, values, defaultValue, dimensions); } /** - * Creates a LongSparseNdArray + * Creates a LongSparseNdArray with a default value of zero. * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). @@ -89,12 +91,10 @@ public class LongSparseNdArray extends AbstractSparseNdArray * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code * values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse NdArray has a value of * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.6}. - * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()} * @param dimensions the dimensional space for the dense object represented by this sparse array, */ - LongSparseNdArray( - LongNdArray indices, LongNdArray values, long defaultValue, DimensionalSpace dimensions) { - super(indices, values, defaultValue, dimensions); + LongSparseNdArray(LongNdArray indices, LongNdArray values, DimensionalSpace dimensions) { + this(indices, values, 0L, dimensions); } /** diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java index 50793b9..63dde22 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java @@ -63,7 +63,7 @@ public class ShortSparseNdArray extends AbstractSparseNdArray> extends AbstractSparseNdArra private final Class type; /** - * Creates a SparseNdArray with a default value of null. + * Creates a SparseNdArray * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). @@ -74,14 +74,17 @@ public class SparseNdArray> extends AbstractSparseNdArra * parameter {@code values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse * NdArray has a value of {@code 18}, and element {@code [2,4]} of the NdArray has a value of * {@code 3.6}. + * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()} * @param dimensions the dimensional space for the dense object represented by this sparse array, */ - SparseNdArray(Class type, LongNdArray indices, U values, DimensionalSpace dimensions) { - this(type, indices, values, null, dimensions); + protected SparseNdArray( + Class type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions) { + super(indices, values, defaultValue, dimensions); + this.type = type; } /** - * Creates a SparseNdArray + * Creates a SparseNdArray with a default value of null. * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). @@ -92,13 +95,10 @@ public class SparseNdArray> extends AbstractSparseNdArra * parameter {@code values=[18, 3.6]} specifies that element {@code [1,3]} of the sparse * NdArray has a value of {@code 18}, and element {@code [2,4]} of the NdArray has a value of * {@code 3.6}. - * @param defaultValue Scalar value to set for indices not specified in {@link #getIndices()} * @param dimensions the dimensional space for the dense object represented by this sparse array, */ - SparseNdArray( - Class type, LongNdArray indices, U values, T defaultValue, DimensionalSpace dimensions) { - super(indices, values, defaultValue, dimensions); - this.type = type; + SparseNdArray(Class type, LongNdArray indices, U values, DimensionalSpace dimensions) { + this(type, indices, values, null, dimensions); } /** From 98b34a3d9bbef1af72d351466eb2d98b4d41a230 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Tue, 8 Feb 2022 22:50:04 -0500 Subject: [PATCH 02/26] Better examples in Sparse array documentation (#6) --- .../java/org/tensorflow/ndarray/NdArrays.java | 160 +++++++++--------- 1 file changed, 80 insertions(+), 80 deletions(-) diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/NdArrays.java b/ndarray/src/main/java/org/tensorflow/ndarray/NdArrays.java index 64ab7eb..d79b781 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/NdArrays.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/NdArrays.java @@ -108,12 +108,12 @@ public static ByteNdArray wrap(Shape shape, ByteDataBuffer buffer) { * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D ByteNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}. * @param shape the shape of the dense array represented by this sparse array. * @return the byte sparse array. */ @@ -126,12 +126,12 @@ public static ByteSparseNdArray sparseOf(LongNdArray indices, ByteNdArray values * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non default values. * @param values A 1-D ByteNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}. * @param defaultValue Scalar value to set for indices not specified in 'indices' * @param shape the shape of the dense array represented by this sparse array. * @return the byte sparse array. @@ -201,12 +201,12 @@ public static LongNdArray wrap(Shape shape, LongDataBuffer buffer) { * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D LongNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18L, 3L]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18L}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3L}. * @param shape the shape of the dense array represented by this sparse array. * @return the long sparse array. */ @@ -219,12 +219,12 @@ public static LongSparseNdArray sparseOf(LongNdArray indices, LongNdArray values * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D LongNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18L, 3L]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18L}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3L}. * @param defaultValue Scalar value to set for indices not specified in 'indices' * @param shape the shape of the dense array represented by this sparse array. * @return the long sparse array. @@ -294,12 +294,12 @@ public static IntNdArray wrap(Shape shape, IntDataBuffer buffer) { * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D IntNdArray of shape {@code [N]}, which supplies the values for each element - * in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}. + * in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}. * @param shape the shape of the dense array represented by this sparse array. * @return the int sparse array. */ @@ -312,12 +312,12 @@ public static IntSparseNdArray sparseOf(LongNdArray indices, IntNdArray values, * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D IntNdArray of shape {@code [N]}, which supplies the values for each element - * in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}. + * in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}. * @param defaultValue Scalar value to set for indices not specified in 'indices' * @param shape the shape of the dense array represented by this sparse array. * @return the int sparse array. @@ -387,12 +387,12 @@ public static ShortNdArray wrap(Shape shape, ShortDataBuffer buffer) { * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D ShortNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}. * @param shape the shape of the dense array represented by this sparse array. * @return the short sparse array. */ @@ -405,12 +405,12 @@ public static ShortSparseNdArray sparseOf(LongNdArray indices, ShortNdArray valu * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D ShortNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18, 3]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3}. * @param defaultValue Scalar value to set for indices not specified in 'indices' * @param shape the shape of the dense array represented by this sparse array. * @return the short sparse array. @@ -480,12 +480,12 @@ public static FloatNdArray wrap(Shape shape, FloatDataBuffer buffer) { * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D FloatNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3.8]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.8}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18f, 3.8f]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18f}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3.8f}. * @param shape the shape of the dense array represented by this sparse array. * @return the float sparse array. */ @@ -498,12 +498,12 @@ public static FloatSparseNdArray sparseOf(LongNdArray indices, FloatNdArray valu * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D FloatNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3.8]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.8}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18f, 3.8f]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18f}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3.8f}. * @param defaultValue Scalar value to set for indices not specified in 'indices' * @param shape the shape of the dense array represented by this sparse array. * @return the float sparse array. @@ -573,12 +573,12 @@ public static DoubleNdArray wrap(Shape shape, DoubleDataBuffer buffer) { * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D DoubleNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3.8]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.8}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18, 3.8]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3.8}. * @param shape the shape of the dense array represented by this sparse array. * @return the float sparse array. */ @@ -592,12 +592,12 @@ public static DoubleSparseNdArray sparseOf( * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D DoubleNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[18, 3.8]} specifies that element {@code [1,3]} of the sparse NdArray has a value of - * {@code 18}, and element {@code [2,4]} of the NdArray has a value of {@code 3.8}. + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[18, 3.8]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value of + * {@code 18}, and element {@code [2,4,0]} of the NdArray has a value of {@code 3.8}. * @param defaultValue Scalar value to set for indices not specified in 'indices' * @param shape the shape of the dense array represented by this sparse array. * @return the float sparse array. @@ -668,12 +668,12 @@ public static BooleanNdArray wrap(Shape shape, BooleanDataBuffer buffer) { * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D BooleanNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[true, true]} specifies that element {@code [1,3]} of the sparse NdArray has a value - * of true, and element {@code [2,4]} of the NdArray has a value of true. All other values are + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[true, true]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value + * of true, and element {@code [2,4,0]} of the NdArray has a value of true. All other values are * false. * @param shape the shape of the dense array represented by this sparse array. * @return the float sparse array. @@ -688,12 +688,12 @@ public static BooleanSparseNdArray sparseOf( * * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D BooleanNdArray of shape {@code [N]}, which supplies the values for each - * element in indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=[true, true]} specifies that element {@code [1,3]} of the sparse NdArray has a value - * of true, and element {@code [2,4]} of the NdArray has a value of true. All other values are + * element in indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=[true, true]} specifies that element {@code [1,3,1]} of the sparse NdArray has a value + * of true, and element {@code [2,4,0]} of the NdArray has a value of true. All other values are * false. * @param defaultValue Scalar value to set for indices not specified in 'indices' * @param shape the shape of the dense array represented by this sparse array. @@ -776,12 +776,12 @@ public static NdArray wrap(Shape shape, DataBuffer buffer) { * @param type the class type represented by this sparse array. * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D NdArray of shape {@code [N]}, which supplies the values for each element in - * indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=["one", "two"]} specifies that element {@code [1,3]} of the sparse NdArray has a - * value of "one", and element {@code [2,4]} of the NdArray has a value of "two"". All other + * indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=["one", "two"]} specifies that element {@code [1,3,1]} of the sparse NdArray has a + * value of "one", and element {@code [2,4,0]} of the NdArray has a value of "two"". All other * values are null. * @param shape the shape of the dense array represented by this sparse array. * @return the float sparse array. @@ -798,12 +798,12 @@ public static NdArray sparseOfObjects( * @param type the class type represented by this sparse array. * @param indices A 2-D LongNdArray of shape {@code [N, ndims]}, that specifies the indices of the * elements in the sparse array that contain non-default values (elements are zero-indexed). - * For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of - * {@code [1,3]} and {@code [2,4]} have non-default values. + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. * @param values A 1-D NdArray of shape {@code [N]}, which supplies the values for each element in - * indices. For example, given {@code indices=[[1,3], [2,4]]}, the parameter {@code - * values=["one", "two"]} specifies that element {@code [1,3]} of the sparse NdArray has a - * value of "one", and element {@code [2,4]} of the NdArray has a value of "two"". All other + * indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code + * values=["one", "two"]} specifies that element {@code [1,3,1]} of the sparse NdArray has a + * value of "one", and element {@code [2,4,0]} of the NdArray has a value of "two"". All other * values are null. * @param defaultValue Scalar value to set for indices not specified in 'indices' * @param shape the shape of the dense array represented by this sparse array. From 283be2b356d886b75947b2e5163718032448b598 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Tue, 15 Feb 2022 22:48:37 -0500 Subject: [PATCH 03/26] Build on JDK11 by default (#7) --- .github/workflows/ci.yml | 5 ++ ndarray/pom.xml | 19 +++++++- ndarray/src/main/java/module-info.java | 37 +++++++++++++++ .../impl/buffer/raw/UnsafeReference.java | 46 +++++++++++-------- pom.xml | 18 ++++---- 5 files changed, 95 insertions(+), 30 deletions(-) create mode 100644 ndarray/src/main/java/module-info.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 09f6cff..2d1378f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,11 @@ jobs: build: runs-on: ubuntu-latest steps: + - name: Configure Java + uses: actions/setup-java@v2 + with: + distribution: 'adopt' + java-version: '11' - name: Checkout repository uses: actions/checkout@v1 - name: Build project diff --git a/ndarray/pom.xml b/ndarray/pom.xml index 3a8ba71..f34150c 100644 --- a/ndarray/pom.xml +++ b/ndarray/pom.xml @@ -75,16 +75,31 @@ org.apache.maven.plugins maven-surefire-plugin - 2.22.2 + 3.0.0-M5 1 false - -Xmx2G -XX:MaxPermSize=256m + -Xmx2G **/*Test.java + false + + org.apache.maven.plugins + maven-compiler-plugin + + + default-testCompile + + + --add-modules=java.desktop + + + + + diff --git a/ndarray/src/main/java/module-info.java b/ndarray/src/main/java/module-info.java new file mode 100644 index 0000000..d8a899f --- /dev/null +++ b/ndarray/src/main/java/module-info.java @@ -0,0 +1,37 @@ +/* + Copyright 2022 The TensorFlow Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + ======================================================================= + */ +module org.tensorflow.ndarray { + exports org.tensorflow.ndarray; + exports org.tensorflow.ndarray.buffer; + exports org.tensorflow.ndarray.buffer.layout; + exports org.tensorflow.ndarray.index; + + // Expose all implementions of our interfaces, so consumers can write custom + // implementations easily by extending from them + exports org.tensorflow.ndarray.impl.buffer.adapter; + exports org.tensorflow.ndarray.impl.buffer.layout; + exports org.tensorflow.ndarray.impl.buffer.misc; + exports org.tensorflow.ndarray.impl.buffer.nio; + exports org.tensorflow.ndarray.impl.buffer.raw; + exports org.tensorflow.ndarray.impl.dense; + exports org.tensorflow.ndarray.impl.dimension; + exports org.tensorflow.ndarray.impl.sequence; + exports org.tensorflow.ndarray.impl.sparse; + exports org.tensorflow.ndarray.impl.sparse.slice; + + requires jdk.unsupported; // required by raw buffer implementations using Unsafe +} diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java index 7b95eac..a8cc16d 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/buffer/raw/UnsafeReference.java @@ -36,24 +36,24 @@ static boolean isAvailable() { theUnsafe.setAccessible(true); Object instance = theUnsafe.get(null); if (instance.getClass() == clazz) { - // Validate that this Unsafe instance exposes all methods we need - clazz.getDeclaredMethod("getByte", Object.class, long.class); - clazz.getDeclaredMethod("putByte", Object.class, long.class, byte.class); - clazz.getDeclaredMethod("getShort", Object.class, long.class); - clazz.getDeclaredMethod("putShort", Object.class, long.class, short.class); - clazz.getDeclaredMethod("getInt", Object.class, long.class); - clazz.getDeclaredMethod("putInt", Object.class, long.class, int.class); - clazz.getDeclaredMethod("getLong", Object.class, long.class); - clazz.getDeclaredMethod("putLong", Object.class, long.class, long.class); - clazz.getDeclaredMethod("getFloat", Object.class, long.class); - clazz.getDeclaredMethod("putFloat", Object.class, long.class, float.class); - clazz.getDeclaredMethod("getDouble", Object.class, long.class); - clazz.getDeclaredMethod("putDouble", Object.class, long.class, double.class); - clazz.getDeclaredMethod("getBoolean", Object.class, long.class); - clazz.getDeclaredMethod("putBoolean", Object.class, long.class, boolean.class); - clazz.getDeclaredMethod("copyMemory", Object.class, long.class, Object.class, long.class, long.class); - clazz.getDeclaredMethod("arrayBaseOffset", Class.class); - clazz.getDeclaredMethod("arrayIndexScale", Class.class); + checkMethod(clazz, "getByte", Object.class, long.class); + checkMethod(clazz, "putByte", Object.class, long.class, byte.class); + checkMethod(clazz, "getShort", Object.class, long.class); + checkMethod(clazz, "putShort", Object.class, long.class, short.class); + checkMethod(clazz, "getInt", Object.class, long.class); + checkMethod(clazz, "putInt", Object.class, long.class, int.class); + checkMethod(clazz, "getLong", Object.class, long.class); + checkMethod(clazz, "putLong", Object.class, long.class, long.class); + checkMethod(clazz, "getFloat", Object.class, long.class); + checkMethod(clazz, "putFloat", Object.class, long.class, float.class); + checkMethod(clazz, "getDouble", Object.class, long.class); + checkMethod(clazz, "putDouble", Object.class, long.class, double.class); + checkMethod(clazz, "getBoolean", Object.class, long.class); + checkMethod(clazz, "putBoolean", Object.class, long.class, boolean.class); + checkMethod(clazz, "copyMemory", Object.class, long.class, Object.class, long.class, long.class); + checkMethod(clazz, "arrayBaseOffset", Class.class); + checkMethod(clazz, "arrayIndexScale", Class.class); + unsafe = (Unsafe) instance; } } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | SecurityException | IllegalAccessException | ClassCastException ex) { @@ -61,4 +61,14 @@ static boolean isAvailable() { } UNSAFE = unsafe; } + + /** + * Validate that this Unsafe instance exposes this method + * + * ErrorProne does not like that we do nothing with the returned method... but there is nothing to do with it, so disable the check + */ + @SuppressWarnings("ReturnValueIgnored") + private static void checkMethod(Class unsafeClass, String methodName, Class... parameterTypes) throws NoSuchMethodException { + unsafeClass.getDeclaredMethod(methodName, parameterTypes); + } } diff --git a/pom.xml b/pom.xml index 867038c..d4d45e4 100644 --- a/pom.xml +++ b/pom.xml @@ -34,12 +34,12 @@ ASCII - 1.8 - 1.8 + 11 + 11 5.6.2 1.21 2.7 - 2.6.0 + 2.10.0 true @@ -141,11 +141,11 @@ - jdk11 + jdk17 - 11 - 11 - 11 + 17 + 17 + 17 @@ -156,8 +156,6 @@ lint - - (1.9,) !lint.skip @@ -169,7 +167,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.0 + 3.9.0 true true From 75df6d0b10c301cd5c04ea23680dde89f6e9e22b Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Tue, 15 Feb 2022 23:08:23 -0500 Subject: [PATCH 04/26] Add missing export --- ndarray/src/main/java/module-info.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ndarray/src/main/java/module-info.java b/ndarray/src/main/java/module-info.java index d8a899f..6b33ed6 100644 --- a/ndarray/src/main/java/module-info.java +++ b/ndarray/src/main/java/module-info.java @@ -15,6 +15,8 @@ ======================================================================= */ module org.tensorflow.ndarray { + requires jdk.unsupported; // required by raw buffer implementations using Unsafe + exports org.tensorflow.ndarray; exports org.tensorflow.ndarray.buffer; exports org.tensorflow.ndarray.buffer.layout; @@ -22,6 +24,7 @@ // Expose all implementions of our interfaces, so consumers can write custom // implementations easily by extending from them + exports org.tensorflow.ndarray.impl.buffer; exports org.tensorflow.ndarray.impl.buffer.adapter; exports org.tensorflow.ndarray.impl.buffer.layout; exports org.tensorflow.ndarray.impl.buffer.misc; @@ -32,6 +35,4 @@ exports org.tensorflow.ndarray.impl.sequence; exports org.tensorflow.ndarray.impl.sparse; exports org.tensorflow.ndarray.impl.sparse.slice; - - requires jdk.unsupported; // required by raw buffer implementations using Unsafe } From 51ac2c0ff667dd4e601efbbf1ed7e6c289fad7b7 Mon Sep 17 00:00:00 2001 From: Adam Pocock Date: Wed, 16 Mar 2022 21:18:22 -0400 Subject: [PATCH 05/26] Adding toString to AbstractDenseNdArray and AbstractSparseNdArray (#8) --- .../impl/dense/AbstractDenseNdArray.java | 9 ++++++++ .../impl/sparse/AbstractSparseNdArray.java | 20 ++++++++++++++++++ .../ndarray/impl/sparse/SparseNdArray.java | 21 +++++++++++++++++++ .../impl/dense/BooleanDenseNdArrayTest.java | 10 +++++++++ .../impl/dense/DoubleDenseNdArrayTest.java | 12 +++++++++++ .../impl/sparse/DoubleSparseNdArrayTest.java | 12 +++++++++++ .../impl/sparse/StringSparseNdArrayTest.java | 11 ++++++++++ 7 files changed, 95 insertions(+) diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java index 0497095..30af952 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java @@ -132,6 +132,15 @@ public boolean equals(Object obj) { return buffer().equals(other.buffer()); } + /** + * A String showing the type and shape of this dense ndarray. + * @return A string containing the type and shape. + */ + @Override + public String toString() { + return this.getClass().getSimpleName() + "(shape=" + this.shape() + ")"; + } + protected AbstractDenseNdArray(DimensionalSpace dimensions) { super(dimensions); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java index daffad9..8e3892d 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java @@ -402,6 +402,26 @@ public boolean equals(Object obj) { return values.equals(other.values); } + /** + * A String showing the type, default value, number of elements and + * the dense shape of this sparse ndarray. + * @return A string containing the type, default value, number of elements and shape. + */ + @Override + public String toString() { + long numElements = values == null ? 0 : values.size(); + String strDefault; + if (defaultValue == null) { + strDefault = ""; + } else if (defaultValue instanceof Number) { + strDefault = defaultValue.toString(); + } else { + strDefault = "'" + defaultValue + "'"; + } + return this.getClass().getSimpleName() + "(defaultValue=" + strDefault + + ", numElements=" + numElements + ", shape=" + this.shape() + ")"; + } + /** * Performs a binary search on the indices array to locate the index of the specified coordinates. * The indices array must be sorted by coordinates, row major. diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java index bdf3d51..52b3867 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java @@ -394,4 +394,25 @@ public NdArray fromDense(NdArray src) { public Class getType() { return type; } + + /** + * A String showing the type, default value, number of elements and + * the dense shape of this sparse ndarray. + * @return A string containing the type, default value, number of elements and shape. + */ + @Override + public String toString() { + long numElements = getValues() == null ? 0 : getValues().size(); + String strDefault; + T defaultVal = getDefaultValue(); + if (defaultVal == null) { + strDefault = ""; + } else if (defaultVal instanceof Number) { + strDefault = defaultVal.toString(); + } else { + strDefault = "'" + defaultVal + "'"; + } + return this.getClass().getSimpleName() + "(type="+type.getSimpleName()+", defaultValue=" + strDefault + + ", numElements=" + numElements + ", shape=" + this.shape() + ")"; + } } diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArrayTest.java index 3654010..d477668 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArrayTest.java @@ -16,6 +16,8 @@ */ package org.tensorflow.ndarray.impl.dense; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.tensorflow.ndarray.Shape; import org.tensorflow.ndarray.buffer.DataBuffer; import org.tensorflow.ndarray.buffer.DataBuffers; @@ -32,4 +34,12 @@ public class BooleanDenseNdArrayTest extends BooleanNdArrayTestBase { @Override protected DataBuffer allocateBuffer(long size) { return DataBuffers.ofBooleans(size); } + + @Test + public void testToString() { + BooleanNdArray matrix3d = allocate(Shape.of(5, 4, 5)); + Assertions.assertEquals("BooleanDenseNdArray(shape=[5, 4, 5])",matrix3d.toString()); + BooleanNdArray scalar = allocate(Shape.of()); + Assertions.assertEquals("BooleanDenseNdArray(shape=[])",scalar.toString()); + } } diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArrayTest.java index 9810a74..279c7b8 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArrayTest.java @@ -16,6 +16,8 @@ */ package org.tensorflow.ndarray.impl.dense; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.tensorflow.ndarray.Shape; import org.tensorflow.ndarray.buffer.DataBuffer; import org.tensorflow.ndarray.buffer.DataBuffers; @@ -32,4 +34,14 @@ public class DoubleDenseNdArrayTest extends DoubleNdArrayTestBase { @Override protected DataBuffer allocateBuffer(long size) { return DataBuffers.ofDoubles(size); } + + @Test + public void testToString() { + DoubleNdArray matrix3d = allocate(Shape.of(5, 4, 5)); + Assertions.assertEquals("DoubleDenseNdArray(shape=[5, 4, 5])",matrix3d.toString()); + DoubleNdArray vector = allocate(Shape.of(5)); + Assertions.assertEquals("DoubleDenseNdArray(shape=[5])",vector.toString()); + DoubleNdArray scalar = allocate(Shape.of()); + Assertions.assertEquals("DoubleDenseNdArray(shape=[])",scalar.toString()); + } } diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArrayTest.java index 437c9dc..616863e 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArrayTest.java @@ -1,5 +1,6 @@ package org.tensorflow.ndarray.impl.sparse; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.tensorflow.ndarray.DoubleNdArray; import org.tensorflow.ndarray.LongNdArray; @@ -302,4 +303,15 @@ public void testSlice() { .forEachIndexed( (lidx, f) -> assertEquals(expected[i.getAndIncrement()], f.getDouble()))); } + + @Test + public void testToString() { + DoubleNdArray ndArray = StdArrays.ndCopyOf(dense2DArray); + DoubleSparseNdArray instance = + DoubleSparseNdArray.create(DimensionalSpace.create(ndArray.shape())); + instance.fromDense(ndArray); + Assertions.assertEquals("DoubleSparseNdArray(defaultValue=0.0, numElements=2, shape=[3, 4])",instance.toString()); + DoubleSparseNdArray empty = DoubleSparseNdArray.create(DimensionalSpace.create(Shape.of(5))); + Assertions.assertEquals("DoubleSparseNdArray(defaultValue=0.0, numElements=0, shape=[5])",empty.toString()); + } } diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/StringSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/StringSparseNdArrayTest.java index 40d6597..152a13e 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/StringSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/StringSparseNdArrayTest.java @@ -14,6 +14,7 @@ =======================================================================*/ package org.tensorflow.ndarray.impl.sparse; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.tensorflow.ndarray.LongNdArray; import org.tensorflow.ndarray.NdArray; @@ -338,4 +339,14 @@ public void testNullDefault() { assertNotNull(dArray.getObject()); assertEquals("a default", dArray.getObject()); } + + @Test + public void testToString() { + SparseNdArray> instance = + new SparseNdArray<>(String.class, indices, values, DimensionalSpace.create(shape)); + Assertions.assertEquals("SparseNdArray(type=String, defaultValue=, numElements=2, shape=[3, 4])",instance.toString()); + instance = new SparseNdArray<>( + String.class, indices, values, "a default", DimensionalSpace.create(shape)); + Assertions.assertEquals("SparseNdArray(type=String, defaultValue='a default', numElements=2, shape=[3, 4])",instance.toString()); + } } From dd4243f68417514261c2aeee755b67eae42410e3 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Tue, 15 Nov 2022 22:14:56 -0500 Subject: [PATCH 06/26] Update version in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 991886f..1cb3ac6 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To import the NdArray library in your project, simply add the following dependen org.tensorflow ndarray - 0.3.2 + 0.3.3 ``` From ec67ffade18bfc769edc754ecb949b4f8e5368e1 Mon Sep 17 00:00:00 2001 From: hmf Date: Mon, 21 Nov 2022 17:27:22 +0000 Subject: [PATCH 07/26] Test Java copyFrom Ok --- ndarray/src/main/java/module-info.java | 1 + .../org/tensorflow/ndarray/IndexTest.java | 117 ++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/ndarray/src/main/java/module-info.java b/ndarray/src/main/java/module-info.java index 6b33ed6..bc09f11 100644 --- a/ndarray/src/main/java/module-info.java +++ b/ndarray/src/main/java/module-info.java @@ -16,6 +16,7 @@ */ module org.tensorflow.ndarray { requires jdk.unsupported; // required by raw buffer implementations using Unsafe + requires java.desktop; // required for java.awt.* exports org.tensorflow.ndarray; exports org.tensorflow.ndarray.buffer; diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java index 6f92dab..00bec94 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import org.junit.jupiter.api.Test; import org.tensorflow.ndarray.index.Indices; @@ -43,6 +44,122 @@ public void testNullConversions(){ assertTrue(Indices.slice(null, null).endMask(), "Passed null for slice end but didn't set end mask"); } + + @Test + public void testIndices(){ + + String[][] indexData = new String[5][4]; + for (int i=0 ; i < 5; i++) + for (int j=0 ; j < 4; j++) + indexData[i][j] = "("+j+", "+i+")"; + + NdArray matrix2d = StdArrays.ndCopyOf(indexData); + assertEquals(2, matrix2d.rank()); + + /* + |(0, 0), (1, 0), (2, 0), (3, 0)| + |(0, 1), (1, 1), (2, 1), (3, 1)| + |(0, 2), (1, 2), (2, 2), (3, 2)| + |(0, 3), (1, 3), (2, 3), (3, 3)| + |(0, 4), (1, 4), (2, 4), (3, 4)| + */ + + NdArray same1 = matrix2d.slice(Indices.all()); + String[][] same1j = StdArrays.array2dCopyOf(same1, String.class); + assertEquals(2, same1.rank()); + assertEquals(same1, matrix2d); + + NdArray same2 = matrix2d.slice(Indices.ellipsis()); + String[][] same2j = StdArrays.array2dCopyOf(same2, String.class); + assertEquals(2, same2.rank()); + assertEquals(matrix2d, same2); + + // All rows, column 1 + NdArray same3 = matrix2d.slice(Indices.all(), Indices.at(1)); + assertEquals(1, same3.rank()); + String[] same3j = StdArrays.array1dCopyOf(same3, String.class); + assertArrayEquals(new String[] { "(1, 0)", "(1, 1)", "(1, 2)", "(1, 3)", "(1, 4)" }, same3j); + + // row 2, all columns + NdArray same4 = matrix2d.slice(Indices.at(2), Indices.all()); + assertEquals(1, same4.rank()); + String[] same4j = StdArrays.array1dCopyOf(same4, String.class); + assertArrayEquals(new String[] {"(0, 2)", "(1, 2)", "(2, 2)", "(3, 2)"}, same4j); + assertEquals(NdArrays.vectorOfObjects("(0, 2)", "(1, 2)", "(2, 2)", "(3, 2)"), same4); + + // row 2, column 1 + NdArray same5 = matrix2d.slice(Indices.at(2), Indices.at(1)); + assertEquals(0, same5.rank()); + assertTrue(same5.shape().isScalar()); + // Don't use an index + String same5j = same5.getObject(); + assertEquals("(1, 2)", same5j); + + // rows 1 to 2, all columns + NdArray same6 = matrix2d.slice(Indices.slice(1,3)); + assertEquals(2, same6.rank()); + String[][] same6j = StdArrays.array2dCopyOf(same6, String.class); + assertArrayEquals( + new String[][] + { + {"(0, 1)", "(1, 1)", "(2, 1)", "(3, 1)"}, + {"(0, 2)", "(1, 2)", "(2, 2)", "(3, 2)"} + }, + same6j + ); + + // Exception in thread "main" java.nio.BufferOverflowException + // all rows, columns 1 to 2 + NdArray same7 = matrix2d.slice(Indices.all(), Indices.slice(1,3)); + assertEquals(2, same7.rank()); + assertEquals(Shape.of(5,2), same7.shape()); + assertEquals(10, same7.size()); + NdArray r7_0 = same7.get(0); + NdArray r7_1 = same7.get(1); + NdArray r7_2 = same7.get(2); + NdArray r7_3 = same7.get(3); + NdArray r7_4 = same7.get(4); + assertEquals(1, r7_0.rank()); + assertEquals(Shape.of(2), r7_0.shape()); + assertEquals(2, r7_0.size()); + // TODO: I get a (0,0) which is not what I expected + System.out.println(r7_0.getObject()); + //assertEquals("(1,0)", r7_0.getObject()); + assertEquals( "(1, 0)", r7_0.getObject(0)); + assertEquals( "(2, 0)", r7_0.getObject(1)); + assertEquals( "(1, 1)", r7_1.getObject(0)); + assertEquals( "(2, 1)", r7_1.getObject(1)); + assertEquals( "(1, 2)", r7_2.getObject(0)); + assertEquals( "(2, 2)", r7_2.getObject(1)); + assertEquals( "(1, 3)", r7_3.getObject(0)); + assertEquals( "(2, 3)", r7_3.getObject(1)); + assertEquals( "(1, 4)", r7_4.getObject(0)); + assertEquals( "(2, 4)", r7_4.getObject(1)); + String[][] expectedr7 = new String[][] + { + {"(1, 0)", "(2, 0)"}, + {"(1, 1)", "(2, 1)"}, + {"(1, 2)", "(2, 2)"}, + {"(1, 3)", "(2, 3)"}, + {"(1, 4)", "(2, 4)"} + }; + //String[][] lArray = new String[5][2]; + String[][] lArray = new String[5][]; + lArray[0] = new String[2]; + lArray[1] = new String[2]; + lArray[2] = new String[2]; + lArray[3] = new String[2]; + lArray[4] = new String[2]; + StdArrays.copyFrom(same7, lArray); + assertArrayEquals( expectedr7, lArray); + String[][] same7j = StdArrays.array2dCopyOf(same7, String.class); + assertArrayEquals( expectedr7, same7j); + +/* +*/ + + assertEquals(0, 0); + } @Test public void testNewaxis(){ From 0a06ed0bff988ed78e732cc7a1652d242ab27b0c Mon Sep 17 00:00:00 2001 From: hmf Date: Mon, 21 Nov 2022 19:08:08 +0000 Subject: [PATCH 08/26] Test Java copyFrom - trying to replicate Scala error --- .../java/org/tensorflow/ndarray/IndexTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java index 00bec94..a1d7ef1 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java @@ -143,13 +143,13 @@ public void testIndices(){ {"(1, 3)", "(2, 3)"}, {"(1, 4)", "(2, 4)"} }; - //String[][] lArray = new String[5][2]; - String[][] lArray = new String[5][]; - lArray[0] = new String[2]; - lArray[1] = new String[2]; - lArray[2] = new String[2]; - lArray[3] = new String[2]; - lArray[4] = new String[2]; + String[][] lArray = new String[5][2]; + // String[][] lArray = new String[5][]; + // lArray[0] = new String[2]; + // lArray[1] = new String[2]; + // lArray[2] = new String[2]; + // lArray[3] = new String[2]; + // lArray[4] = new String[2]; StdArrays.copyFrom(same7, lArray); assertArrayEquals( expectedr7, lArray); String[][] same7j = StdArrays.array2dCopyOf(same7, String.class); From 3e138d5f64f25fc7f6714fcea2bbebf65185943e Mon Sep 17 00:00:00 2001 From: hmf Date: Tue, 22 Nov 2022 08:14:56 +0000 Subject: [PATCH 09/26] Test Java copyFrom - trying to replicate Scala error v2 --- .../src/test/java/org/tensorflow/ndarray/IndexTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java index a1d7ef1..db44f56 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java @@ -48,10 +48,13 @@ public void testNullConversions(){ @Test public void testIndices(){ - String[][] indexData = new String[5][4]; - for (int i=0 ; i < 5; i++) + // String[][] indexData = new String[5][4]; + String[][] indexData = new String[5][]; + for (int i=0 ; i < 5; i++){ + indexData[i] = new String[4]; for (int j=0 ; j < 4; j++) indexData[i][j] = "("+j+", "+i+")"; + } NdArray matrix2d = StdArrays.ndCopyOf(indexData); assertEquals(2, matrix2d.rank()); @@ -63,6 +66,7 @@ public void testIndices(){ |(0, 3), (1, 3), (2, 3), (3, 3)| |(0, 4), (1, 4), (2, 4), (3, 4)| */ + assertArrayEquals(new String[]{"(0, 0)", "(1, 0)", "(2, 0)", "(3, 0)"}, indexData[0]); NdArray same1 = matrix2d.slice(Indices.all()); String[][] same1j = StdArrays.array2dCopyOf(same1, String.class); From 0c450cc2bb840e5fc8caf2c6ab4a6d332172aafb Mon Sep 17 00:00:00 2001 From: hmf Date: Tue, 22 Nov 2022 17:11:59 +0000 Subject: [PATCH 10/26] Test Java copyFrom - added JDK 17 to container v2 --- .devcontainer/devcontainer.json | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .devcontainer/devcontainer.json diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..1805a20 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,23 @@ +{ + "name": "Java", + "image": "mcr.microsoft.com/devcontainers/java:17-buster", + "features": { + "ghcr.io/devcontainers/features/java:1": { + "version": "none", + "installMaven": "true", + "installGradle": "false" + } + } + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "java -version", + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} From ff04d867352b969a071a20739620048a66b324c8 Mon Sep 17 00:00:00 2001 From: hmf Date: Sat, 26 Nov 2022 12:52:03 +0000 Subject: [PATCH 11/26] Added basic index tests (rank 2) --- .devcontainer/devcontainer.json | 23 -- .../org/tensorflow/ndarray/IndexTest.java | 273 +++++++++++++++++- 2 files changed, 259 insertions(+), 37 deletions(-) delete mode 100644 .devcontainer/devcontainer.json diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json deleted file mode 100644 index 1805a20..0000000 --- a/.devcontainer/devcontainer.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "name": "Java", - "image": "mcr.microsoft.com/devcontainers/java:17-buster", - "features": { - "ghcr.io/devcontainers/features/java:1": { - "version": "none", - "installMaven": "true", - "installGradle": "false" - } - } - - // Use 'forwardPorts' to make a list of ports inside the container available locally. - // "forwardPorts": [], - - // Use 'postCreateCommand' to run commands after the container is created. - // "postCreateCommand": "java -version", - - // Configure tool-specific properties. - // "customizations": {}, - - // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. - // "remoteUser": "root" -} diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java index db44f56..10c0806 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java @@ -48,10 +48,8 @@ public void testNullConversions(){ @Test public void testIndices(){ - // String[][] indexData = new String[5][4]; - String[][] indexData = new String[5][]; + String[][] indexData = new String[5][4]; for (int i=0 ; i < 5; i++){ - indexData[i] = new String[4]; for (int j=0 ; j < 4; j++) indexData[i][j] = "("+j+", "+i+")"; } @@ -72,11 +70,13 @@ public void testIndices(){ String[][] same1j = StdArrays.array2dCopyOf(same1, String.class); assertEquals(2, same1.rank()); assertEquals(same1, matrix2d); + assertEquals(matrix2d, StdArrays.ndCopyOf(same1j)); NdArray same2 = matrix2d.slice(Indices.ellipsis()); String[][] same2j = StdArrays.array2dCopyOf(same2, String.class); assertEquals(2, same2.rank()); assertEquals(matrix2d, same2); + assertEquals(matrix2d, StdArrays.ndCopyOf(same2j)); // All rows, column 1 NdArray same3 = matrix2d.slice(Indices.all(), Indices.at(1)); @@ -127,8 +127,8 @@ public void testIndices(){ assertEquals(Shape.of(2), r7_0.shape()); assertEquals(2, r7_0.size()); // TODO: I get a (0,0) which is not what I expected - System.out.println(r7_0.getObject()); - //assertEquals("(1,0)", r7_0.getObject()); + // System.out.println(r7_0.getObject()); + // assertEquals("(1,0)", r7_0.getObject()); assertEquals( "(1, 0)", r7_0.getObject(0)); assertEquals( "(2, 0)", r7_0.getObject(1)); assertEquals( "(1, 1)", r7_1.getObject(0)); @@ -148,21 +148,266 @@ public void testIndices(){ {"(1, 4)", "(2, 4)"} }; String[][] lArray = new String[5][2]; - // String[][] lArray = new String[5][]; - // lArray[0] = new String[2]; - // lArray[1] = new String[2]; - // lArray[2] = new String[2]; - // lArray[3] = new String[2]; - // lArray[4] = new String[2]; StdArrays.copyFrom(same7, lArray); assertArrayEquals( expectedr7, lArray); String[][] same7j = StdArrays.array2dCopyOf(same7, String.class); assertArrayEquals( expectedr7, same7j); -/* -*/ + + // rows 1 to 2, columns 1 to 2 + NdArray same8 = matrix2d.slice(Indices.slice(1,3), Indices.slice(1,3)); + assertEquals(2, same8.rank()); + assertEquals(Shape.of(2,2), same8.shape()); + assertEquals(4, same8.size()); + String[][] same8j = StdArrays.array2dCopyOf(same8, String.class); + // print2D(same8j) + String[][] expected_r8 = new String[][] + { + {"(1, 1)", "(2, 1)"}, + {"(1, 2)", "(2, 2)"} + }; + assertArrayEquals(expected_r8, same8j); + NdArray r8_0 = same8.get(0); + NdArray r8_1 = same8.get(1); + assertEquals(1, r8_0.rank()); + assertEquals(Shape.of(2), r8_0.shape()); + assertEquals(2, r8_0.size()); + assertEquals("(1, 1)", r8_0.getObject(0)); + assertEquals("(2, 1)", r8_0.getObject(1)); + assertEquals("(1, 2)", r8_1.getObject(0)); + assertEquals("(2, 2)", r8_1.getObject(1)); + + // rows 1 to 2, columns 1 to 2 + NdArray same9 = matrix2d.slice(Indices.range(1,3), Indices.range(1,3)); + assertEquals(2, same9.rank()); + assertEquals(Shape.of(2,2), same9.shape()); + assertEquals(4, same9.size()); + String[][] same9j = StdArrays.array2dCopyOf(same9, String.class); + String[][] expected_r9 = new String[][] + { + {"(1, 1)", "(2, 1)"}, + {"(1, 2)", "(2, 2)"} + }; + assertArrayEquals(expected_r9, same9j); + NdArray r9_0 = same9.get(0); + NdArray r9_1 = same9.get(1); + assertEquals(1, r9_0.rank()); + assertEquals(Shape.of(2), r9_0.shape()); + assertEquals(2, r9_0.size()); + assertEquals("(1, 1)", r9_0.getObject(0)); + assertEquals("(2, 1)", r9_0.getObject(1)); + assertEquals("(1, 2)", r9_1.getObject(0)); + assertEquals("(2, 2)", r9_1.getObject(1)); + + // rows 1, 3 and 4, columns 0 to 2 + NdArray same10 = matrix2d.slice(Indices.odd(), Indices.even()); + String[][] same10j = StdArrays.array2dCopyOf(same10, String.class); + assertEquals(2, same10.rank()); + assertEquals(Shape.of(2,2), same10.shape()); + assertEquals(4, same10.size()); + String[][] expected_r10 = new String[][] + { + {"(0, 1)", "(2, 1)"}, + {"(0, 3)", "(2, 3)"} + }; + assertArrayEquals(expected_r10, same10j); + NdArray r10_0 = same10.get(0); + NdArray r10_1 = same10.get(1); + assertEquals(1, r10_0.rank()); + assertEquals(Shape.of(2), r10_0.shape()); + assertEquals(2, r10_0.size()); + assertEquals("(0, 1)", r10_0.getObject(0)); + assertEquals("(2, 1)", r10_0.getObject(1)); + assertEquals("(0, 3)", r10_1.getObject(0)); + assertEquals("(2, 3)", r10_1.getObject(1)); + + // rows 3 and 4, columns 0 and 1. Second value is stride + NdArray same11 = matrix2d.slice(Indices.sliceFrom(3,1), Indices.sliceFrom(2,1)); + String[][] same11j = StdArrays.array2dCopyOf(same11, String.class); + assertEquals(2, same11.rank()); + assertEquals(Shape.of(2,2), same11.shape()); + assertEquals(4, same11.size()); + String[][] expected_r11 = new String[][] + { + {"(2, 3)", "(3, 3)"}, + {"(2, 4)", "(3, 4)"} + }; + assertArrayEquals(expected_r11, same11j); + NdArray r11_0 = same11.get(0); + NdArray r11_1 = same11.get(1); + assertEquals(1, r11_0.rank()); + assertEquals(Shape.of(2), r11_0.shape()); + assertEquals(2, r11_0.size()); + assertEquals("(2, 3)", r11_0.getObject(0)); + assertEquals("(3, 3)", r11_0.getObject(1)); + assertEquals("(2, 4)", r11_1.getObject(0)); + assertEquals("(3, 4)", r11_1.getObject(1)); + + + // rows 0 and 2, columns 0 and 1. Second value is stride. Index non inclusive + NdArray same12 = matrix2d.slice(Indices.sliceTo(3,2), Indices.sliceTo(2,1)); + String[][] same12j = StdArrays.array2dCopyOf(same12, String.class); + assertEquals(2, same12.rank()); + assertEquals(Shape.of(2,2), same12.shape()); + assertEquals(4, same12.size()); + String[][] expected_r12 = new String[][] + { + {"(0, 0)", "(1, 0)"}, + {"(0, 2)", "(1, 2)"} + }; + assertArrayEquals(expected_r12, same12j); + NdArray r12_0 = same12.get(0); + NdArray r12_1 = same12.get(1); + assertEquals(1, r12_0.rank()); + assertEquals(Shape.of(2), r12_0.shape()); + assertEquals(2, r12_0.size()); + assertEquals("(0, 0)", r12_0.getObject(0)); + assertEquals("(1, 0)", r12_0.getObject(1)); + assertEquals("(0, 2)", r12_1.getObject(0)); + assertEquals("(1, 2)", r12_1.getObject(1)); + + // rows 0 and 2, columns 0 and 1. Second value is stride. Index non inclusive + NdArray same13 = matrix2d.slice(Indices.step(2), Indices.step(2)); + String[][] same13j = StdArrays.array2dCopyOf(same13, String.class); + assertEquals(2, same13.rank()); + assertEquals(Shape.of(3,2), same13.shape()); + assertEquals(6, same13.size()); + String[][] expected_r13 = new String[][] + { + {"(0, 0)", "(2, 0)"}, + {"(0, 2)", "(2, 2)"}, + {"(0, 4)", "(2, 4)"} + }; + assertArrayEquals(expected_r13, same13j); + NdArray r13_0 = same13.get(0); + NdArray r13_1 = same13.get(1); + NdArray r13_2 = same13.get(2); + assertEquals(1, r13_0.rank()); + assertEquals(Shape.of(2), r13_0.shape()); + assertEquals(2, r13_0.size()); + assertEquals("(0, 0)", r13_0.getObject(0)); + assertEquals("(2, 0)", r13_0.getObject(1)); + assertEquals("(0, 2)", r13_1.getObject(0)); + assertEquals("(2, 2)", r13_1.getObject(1)); + assertEquals("(0, 4)", r13_2.getObject(0)); + assertEquals("(2, 4)", r13_2.getObject(1)); + + + NdArray same14 = same13.slice(Indices.flip(), Indices.flip()); + String[][] same14j = StdArrays.array2dCopyOf(same14, String.class); + assertEquals(2, same14.rank()); + assertEquals(Shape.of(3,2), same14.shape()); + assertEquals(6, same14.size()); + String[][] expected_r14 = new String[][] + { + {"(2, 4)", "(0, 4)"}, + {"(2, 2)", "(0, 2)"}, + {"(2, 0)", "(0, 0)"} + }; + assertArrayEquals(same14j, expected_r14); + NdArray r14_0 = same14.get(0); + NdArray r14_1 = same14.get(1); + NdArray r14_2 = same14.get(2); + assertEquals(1, r14_0.rank()); + assertEquals(Shape.of(2), r14_0.shape()); + assertEquals(2, r14_0.size()); + assertEquals("(0, 0)", r14_2.getObject(1)); + assertEquals("(2, 0)", r14_2.getObject(0)); + assertEquals("(0, 2)", r14_1.getObject(1)); + assertEquals("(2, 2)", r14_1.getObject(0)); + assertEquals("(0, 4)", r14_0.getObject(1)); + assertEquals("(2, 4)", r14_0.getObject(0)); + + + NdArray same15 = matrix2d.slice(Indices.slice(4,0,-2), Indices.slice(3L,null,-2)); + String[][] same15j = StdArrays.array2dCopyOf(same15, String.class); + assertEquals(2, same15.rank()); + assertEquals(Shape.of(2,2), same15.shape()); + assertEquals(4,same15.size()); + String[][] expected_r15 = new String[][] + { + {"(3, 4)", "(1, 4)"}, + {"(3, 2)", "(1, 2)"}, + }; + assertArrayEquals(expected_r15, same15j); + NdArray r15_0 = same15.get(0); + NdArray r15_1 = same15.get(1); + assertEquals(1, r15_0.rank()); + assertEquals(Shape.of(2), r15_0.shape()); + assertEquals(2, r15_0.size()); + assertEquals("(3, 4)", r15_0.getObject(0)); + assertEquals("(1, 4)", r15_0.getObject(1)); + assertEquals("(3, 2)", r15_1.getObject(0)); + assertEquals("(1, 2)", r15_1.getObject(1)); + + NdArray same16 = matrix2d.slice(Indices.seq(4,2), Indices.seq(3,1)); + String[][] same16j = StdArrays.array2dCopyOf(same16, String.class); + assertEquals(2, same16.rank()); + assertEquals(Shape.of(2,2), same16.shape()); + assertEquals(4, same16.size()); + String[][] expected_r16 = new String[][] + { + {"(3, 4)", "(1, 4)"}, + {"(3, 2)", "(1, 2)"} + }; + assertArrayEquals(expected_r16, same16j); + NdArray r16_0 = same16.get(0); + NdArray r16_1 = same16.get(1); + assertEquals(1, r16_0.rank()); + assertEquals(Shape.of(2), r16_0.shape()); + assertEquals(2, r16_0.size()); + assertEquals("(3, 4)", r16_0.getObject(0)); + assertEquals("(1, 4)", r16_0.getObject(1)); + assertEquals("(3, 2)", r16_1.getObject(0)); + assertEquals("(1, 2)", r16_1.getObject(1)); - assertEquals(0, 0); + + // New axis always has size 1 + NdArray same17 = matrix2d.slice(Indices.all(), Indices.all(), Indices.newAxis()); + String[][][] same17j = StdArrays.array3dCopyOf(same17, String.class); + assertEquals(3, same17.rank()); + assertEquals(Shape.of(5,4,1), same17.shape()); + assertEquals(20, same17.size()); + String[][][] expected_r17 = new String[][][] + { + {{"(0, 0)"}, {"(1, 0)"}, {"(2, 0)"}, {"(3, 0)"}}, + {{"(0, 1)"}, {"(1, 1)"}, {"(2, 1)"}, {"(3, 1)"}}, + {{"(0, 2)"}, {"(1, 2)"}, {"(2, 2)"}, {"(3, 2)"}}, + {{"(0, 3)"}, {"(1, 3)"}, {"(2, 3)"}, {"(3, 3)"}}, + {{"(0, 4)"}, {"(1, 4)"}, {"(2, 4)"}, {"(3, 4)"}} + }; + assertArrayEquals(expected_r17, same17j); + NdArray r17_0 = same17.get(0); + NdArray r17_1 = same17.get(1); + NdArray r17_2 = same17.get(2); + NdArray r17_3 = same17.get(3); + NdArray r17_4 = same17.get(4); + assertEquals(2, r17_0.rank()); + assertEquals(Shape.of(4,1), r17_0.shape()); + assertEquals(4, r17_0.size()); + // row 0 + // What use case can we have for a new index of size 1? + // row 1 + assertEquals("(0, 1)", r17_1.getObject(0,0)); + assertEquals("(1, 1)", r17_1.getObject(1,0)); + assertEquals("(2, 1)", r17_1.getObject(2,0)); + assertEquals("(3, 1)", r17_1.getObject(3,0)); + // row 2 + assertEquals("(0, 2)", r17_2.getObject(0,0)); + assertEquals("(1, 2)", r17_2.getObject(1,0)); + assertEquals("(2, 2)", r17_2.getObject(2,0)); + assertEquals("(3, 2)", r17_2.getObject(3,0)); + // row 3 + assertEquals("(0, 3)", r17_3.getObject(0,0)); + assertEquals("(1, 3)", r17_3.getObject(1,0)); + assertEquals("(2, 3)", r17_3.getObject(2,0)); + assertEquals("(3, 3)", r17_3.getObject(3,0)); + // row 4 + assertEquals("(0, 4)", r17_4.getObject(0,0)); + assertEquals("(1, 4)", r17_4.getObject(1,0)); + assertEquals("(2, 4)", r17_4.getObject(2,0)); + assertEquals("(3, 4)", r17_4.getObject(3,0)); + } @Test From 74dc202ed708dadb2839ccfdf5afe65e3e095bc4 Mon Sep 17 00:00:00 2001 From: hmf Date: Tue, 29 Nov 2022 15:47:14 +0000 Subject: [PATCH 12/26] Added module-info to tests --- ndarray/src/main/java/module-info.java | 1 - ndarray/src/test/java/module-info.test | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 ndarray/src/test/java/module-info.test diff --git a/ndarray/src/main/java/module-info.java b/ndarray/src/main/java/module-info.java index bc09f11..6b33ed6 100644 --- a/ndarray/src/main/java/module-info.java +++ b/ndarray/src/main/java/module-info.java @@ -16,7 +16,6 @@ */ module org.tensorflow.ndarray { requires jdk.unsupported; // required by raw buffer implementations using Unsafe - requires java.desktop; // required for java.awt.* exports org.tensorflow.ndarray; exports org.tensorflow.ndarray.buffer; diff --git a/ndarray/src/test/java/module-info.test b/ndarray/src/test/java/module-info.test new file mode 100644 index 0000000..24830da --- /dev/null +++ b/ndarray/src/test/java/module-info.test @@ -0,0 +1,19 @@ +/* + Copyright 2022 The TensorFlow Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + ======================================================================= + */ +module org.tensorflow.ndarray.test { + requires java.desktop; // required for java.awt.* +} From c16570447a93e67c909482295cfd572cb89f4670 Mon Sep 17 00:00:00 2001 From: hmf Date: Wed, 30 Nov 2022 08:20:51 +0000 Subject: [PATCH 13/26] Module-info for tests use the same module name of src --- ndarray/src/test/java/module-info.test | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ndarray/src/test/java/module-info.test b/ndarray/src/test/java/module-info.test index 24830da..310e500 100644 --- a/ndarray/src/test/java/module-info.test +++ b/ndarray/src/test/java/module-info.test @@ -14,6 +14,9 @@ limitations under the License. ======================================================================= */ -module org.tensorflow.ndarray.test { +module org.tensorflow.ndarray { requires java.desktop; // required for java.awt.* + + requires transitive org.junit.jupiter.engine; + requires transitive org.junit.jupiter.api; } From a9d3f251a6559d70af1d7ef17719bb441af33bbc Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Wed, 25 Jan 2023 13:22:25 -0500 Subject: [PATCH 14/26] Value streaming for NdArrays (#15) --- .../org/tensorflow/ndarray/DoubleNdArray.java | 17 +++++++++- .../org/tensorflow/ndarray/IntNdArray.java | 17 +++++++++- .../org/tensorflow/ndarray/LongNdArray.java | 17 +++++++++- .../java/org/tensorflow/ndarray/NdArray.java | 17 +++++++++- .../ndarray/IntNdArrayTestBase.java | 32 +++++++++++++++-- .../ndarray/LongNdArrayTestBase.java | 28 ++++++++++++++- .../tensorflow/ndarray/NdArrayTestBase.java | 34 ++++++++++++++++--- 7 files changed, 150 insertions(+), 12 deletions(-) diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java index 80e99b0..b0e6dab 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java @@ -1,5 +1,5 @@ /* - Copyright 2019 The TensorFlow Authors. All Rights Reserved. + Copyright 2019-2023 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,6 +20,9 @@ import org.tensorflow.ndarray.buffer.DoubleDataBuffer; import org.tensorflow.ndarray.index.Index; +import java.util.stream.DoubleStream; +import java.util.stream.StreamSupport; + /** * An {@link NdArray} of doubles. */ @@ -68,6 +71,18 @@ public interface DoubleNdArray extends NdArray { */ DoubleNdArray setDouble(double value, long... coordinates); + /** + * Retrieve all scalar values of this array as a stream of doubles. + * + *

For {@code rank() > 1} arrays, all vectors of the last dimension are collated so that the scalar values are + * returned in sequential order.

+ * + * @return scalar values as a stream + */ + default DoubleStream streamOfDoubles() { + return StreamSupport.stream(scalars().spliterator(), false).mapToDouble(DoubleNdArray::getDouble); + } + @Override DoubleNdArray slice(Index... indices); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java index aa2cc65..e6a5cf0 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java @@ -1,5 +1,5 @@ /* - Copyright 2019 The TensorFlow Authors. All Rights Reserved. + Copyright 2019-2023 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,6 +20,9 @@ import org.tensorflow.ndarray.buffer.IntDataBuffer; import org.tensorflow.ndarray.index.Index; +import java.util.stream.IntStream; +import java.util.stream.StreamSupport; + /** * An {@link NdArray} of integers. */ @@ -68,6 +71,18 @@ public interface IntNdArray extends NdArray { */ IntNdArray setInt(int value, long... coordinates); + /** + * Retrieve all scalar values of this array as a stream of integers. + * + *

For {@code rank() > 1} arrays, all vectors of the last dimension are collated so that the scalar values are + * returned in sequential order.

+ * + * @return scalar values as a stream + */ + default IntStream streamOfInts() { + return StreamSupport.stream(scalars().spliterator(), false).mapToInt(IntNdArray::getInt); + } + @Override IntNdArray slice(Index... indices); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java index 3e5be6d..e7bd266 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java @@ -1,5 +1,5 @@ /* - Copyright 2019 The TensorFlow Authors. All Rights Reserved. + Copyright 2019-2023 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,6 +20,9 @@ import org.tensorflow.ndarray.buffer.LongDataBuffer; import org.tensorflow.ndarray.index.Index; +import java.util.stream.LongStream; +import java.util.stream.StreamSupport; + /** * An {@link NdArray} of longs. */ @@ -68,6 +71,18 @@ public interface LongNdArray extends NdArray { */ LongNdArray setLong(long value, long... coordinates); + /** + * Retrieve all scalar values of this array as a stream of longs. + * + *

For {@code rank() > 1} arrays, all vectors of the last dimension are collated so that the scalar values are + * returned in sequential order.

+ * + * @return scalar values as a stream + */ + default LongStream streamOfLongs() { + return StreamSupport.stream(scalars().spliterator(), false).mapToLong(LongNdArray::getLong); + } + @Override LongNdArray slice(Index... indices); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java index 6686abd..f1e84d4 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java @@ -1,5 +1,5 @@ /* - Copyright 2019 The TensorFlow Authors. All Rights Reserved. + Copyright 2019-2023 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,6 +18,9 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + import org.tensorflow.ndarray.buffer.DataBuffer; import org.tensorflow.ndarray.index.Index; @@ -229,6 +232,18 @@ public interface NdArray extends Shaped { */ NdArray setObject(T value, long... coordinates); + /** + * Retrieve all scalar values of this array as a stream of objects. + * + *

For {@code rank() > 1} arrays, all vectors of the last dimension are collated so that the scalar values are + * returned in sequential order.

+ * + * @return scalar values as a stream + */ + default Stream streamOfObjects() { + return StreamSupport.stream(scalars().spliterator(), false).map(NdArray::getObject); + } + /** * Copy the content of this array to the destination array. * diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/IntNdArrayTestBase.java b/ndarray/src/test/java/org/tensorflow/ndarray/IntNdArrayTestBase.java index 1a3c7cb..0fba8fb 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/IntNdArrayTestBase.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/IntNdArrayTestBase.java @@ -1,5 +1,5 @@ /* - Copyright 2019 The TensorFlow Authors. All Rights Reserved. + Copyright 2019-2023 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,10 +16,11 @@ */ package org.tensorflow.ndarray; -import static org.junit.jupiter.api.Assertions.assertEquals; - import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + public abstract class IntNdArrayTestBase extends NdArrayTestBase { @Override @@ -52,4 +53,29 @@ public void iteratePrimitiveElements() { assertEquals(9, matrix3d.getInt(0, 0, 4)); assertEquals(7, matrix3d.getInt(0, 1, 2)); } + + @Test + public void streamingInts() { + IntNdArray scalar = allocate(Shape.scalar()); + scalar.setInt(1); + var values = scalar.streamOfInts().toArray(); + assertArrayEquals(new int[]{1}, values); + + IntNdArray vector = allocate(Shape.of(5)); + vector.setInt(1, 0); + vector.setInt(2, 1); + vector.setInt(3, 2); + vector.setInt(4, 3); + vector.setInt(5, 4); + values = vector.streamOfInts().toArray(); + assertArrayEquals(new int[]{1, 2, 3, 4, 5}, values); + + IntNdArray matrix = allocate(Shape.of(2, 2)); + matrix.setInt(1, 0, 0); + matrix.setInt(2, 0, 1); + matrix.setInt(3, 1, 0); + matrix.setInt(4, 1, 1); + values = matrix.streamOfInts().toArray(); + assertArrayEquals(new int[]{1, 2, 3, 4}, values); + } } diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/LongNdArrayTestBase.java b/ndarray/src/test/java/org/tensorflow/ndarray/LongNdArrayTestBase.java index b91c19d..520d9bc 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/LongNdArrayTestBase.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/LongNdArrayTestBase.java @@ -1,5 +1,5 @@ /* - Copyright 2019 The TensorFlow Authors. All Rights Reserved. + Copyright 2019-2023 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ */ package org.tensorflow.ndarray; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; @@ -52,4 +53,29 @@ public void iteratePrimitiveElements() { assertEquals(9, matrix3d.getLong(0, 0, 4)); assertEquals(7, matrix3d.getLong(0, 1, 2)); } + + @Test + public void streamingLongs() { + LongNdArray scalar = allocate(Shape.scalar()); + scalar.setLong(1L); + var values = scalar.streamOfLongs().toArray(); + assertArrayEquals(new long[]{1L}, values); + + LongNdArray vector = allocate(Shape.of(5)); + vector.setLong(1L, 0); + vector.setLong(2L, 1); + vector.setLong(3L, 2); + vector.setLong(4L, 3); + vector.setLong(5L, 4); + values = vector.streamOfLongs().toArray(); + assertArrayEquals(new long[]{1L, 2L, 3L, 4L, 5L}, values); + + LongNdArray matrix = allocate(Shape.of(2, 2)); + matrix.setLong(1L, 0, 0); + matrix.setLong(2L, 0, 1); + matrix.setLong(3L, 1, 0); + matrix.setLong(4L, 1, 1); + values = matrix.streamOfLongs().toArray(); + assertArrayEquals(new long[]{1L, 2L, 3L, 4L}, values); + } } diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java b/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java index c44db8d..8a09ec7 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java @@ -1,5 +1,5 @@ /* - Copyright 2019 The TensorFlow Authors. All Rights Reserved. + Copyright 2019-2023 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,9 +16,7 @@ */ package org.tensorflow.ndarray; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.*; import static org.tensorflow.ndarray.NdArrays.vectorOfObjects; import static org.tensorflow.ndarray.index.Indices.all; import static org.tensorflow.ndarray.index.Indices.at; @@ -32,6 +30,9 @@ import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException; +import java.util.List; +import java.util.stream.Collectors; + import org.junit.jupiter.api.Test; import org.tensorflow.ndarray.buffer.DataBuffer; import org.tensorflow.ndarray.index.Indices; @@ -358,4 +359,29 @@ public void iterateScalarsOnSegmentedElements() { }); }); } + + @Test + public void streamingObjects() { + NdArray scalar = allocate(Shape.scalar()); + scalar.setObject(valueOf(1L)); + var values = scalar.streamOfObjects().collect(Collectors.toList()); + assertIterableEquals(List.of(valueOf(1L)), values); + + NdArray vector = allocate(Shape.of(5)); + vector.setObject(valueOf(1L), 0); + vector.setObject(valueOf(2L), 1); + vector.setObject(valueOf(3L), 2); + vector.setObject(valueOf(4L), 3); + vector.setObject(valueOf(5L), 4); + values = vector.streamOfObjects().collect(Collectors.toList()); + assertIterableEquals(List.of(valueOf(1L), valueOf(2L), valueOf(3L), valueOf(4L), valueOf(5L)), values); + + NdArray matrix = allocate(Shape.of(2, 2)); + matrix.setObject(valueOf(1L), 0, 0); + matrix.setObject(valueOf(2L), 0, 1); + matrix.setObject(valueOf(3L), 1, 0); + matrix.setObject(valueOf(4L), 1, 1); + values = matrix.streamOfObjects().collect(Collectors.toList()); + assertIterableEquals(List.of(valueOf(1L), valueOf(2L), valueOf(3L), valueOf(4L)), values); + } } From 70b6a17557c1a6c09b333529f6c2780d130709aa Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Wed, 25 Jan 2023 13:55:38 -0500 Subject: [PATCH 15/26] Use semantic versioning for release --- pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pom.xml b/pom.xml index d4d45e4..275f0b4 100644 --- a/pom.xml +++ b/pom.xml @@ -271,7 +271,15 @@ true
+ + maven-release-plugin + 3.0.0-M5 + + SemVerVersionPolicy + + + From eb4478836ff521b81af128d4c9ad27194a063862 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Wed, 25 Jan 2023 14:32:37 -0500 Subject: [PATCH 16/26] Enable release plugin --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2d1378f..f957101 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,3 +30,9 @@ jobs: run: | echo "ossrh${{ secrets.CI_DEPLOY_USERNAME }}${{ secrets.CI_DEPLOY_PASSWORD }}" > $HOME/.m2/settings.xml mvn deploy -B -U -e -Dmaven.test.skip=true + - name: Release artifacts + if: github.event_name == 'push' && startsWith(github.ref, 'r') + run: | + echo "ossrh${{ secrets.CI_DEPLOY_USERNAME }}${{ secrets.CI_DEPLOY_PASSWORD }}" > $HOME/.m2/settings.xml + mvn release:prepare -B -U -e + mvn release:perform -B -U -e -Dmaven.test.skip=true From f0b344ab9d5e94d8b4c8a62fc0b07054a117a3c1 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Wed, 25 Jan 2023 14:33:39 -0500 Subject: [PATCH 17/26] Temporarily change SCM for not messing up with real repo --- pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 275f0b4..58d2840 100644 --- a/pom.xml +++ b/pom.xml @@ -23,9 +23,9 @@ - https://github.com/tensorflow/java-ndarray.git - git@github.com:tensorflow/java-ndarray.git - scm:git:https://github.com/tensorflow/java-ndarray.git + https://github.com/karllessard/tensorflow-java-ndarray.git + git@github.com:karllessard/tensorflow-java-ndarray.git + scm:git:https://github.com/karllessard/tensorflow-java-ndarray.git From 96e0fdb97d8819f8c4ef5cebce7393aed9d9e806 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Wed, 25 Jan 2023 21:19:35 -0500 Subject: [PATCH 18/26] Release 0.4.0 --- .github/workflows/ci.yml | 4 ++-- .gitignore | 2 ++ README.md | 2 +- ndarray/pom.xml | 2 +- pom.xml | 4 ++-- release.sh | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 42 insertions(+), 6 deletions(-) create mode 100755 release.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2d1378f..d82ed88 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,11 +3,11 @@ on: push: branches: - main - - r[0-9]+.* + # - r[0-9]+.* pull_request: branches: - main - - r[0-9]+.* + # - r[0-9]+.* types: [opened, reopened, synchronize] jobs: build: diff --git a/.gitignore b/.gitignore index 6240411..16d3539 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.iml .idea target + +settings.xml diff --git a/README.md b/README.md index 1cb3ac6..7b9076b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To import the NdArray library in your project, simply add the following dependen org.tensorflow ndarray - 0.3.3 + 0.4.0 ``` diff --git a/ndarray/pom.xml b/ndarray/pom.xml index f34150c..73e80fa 100644 --- a/ndarray/pom.xml +++ b/ndarray/pom.xml @@ -22,7 +22,7 @@ org.tensorflow tensorflow-java-ndarray - 0.4.0-SNAPSHOT + 0.4.0 ndarray jar diff --git a/pom.xml b/pom.xml index d4d45e4..66f491c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.tensorflow tensorflow-java-ndarray - 0.4.0-SNAPSHOT + 0.4.0 pom NdArray Parent @@ -82,7 +82,7 @@ ossrh - https://oss.sonatype.org/service/local/staging/deployByRepositoryId/${stagingRepositoryId}/ + https://oss.sonatype.org/service/local/staging/deploy/maven2/ diff --git a/release.sh b/release.sh new file mode 100755 index 0000000..cf8e05f --- /dev/null +++ b/release.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# Copyright 2023 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +# +# Script to upload release artifacts for the TensorFlow Java library to +# Maven Central. See RELEASE.md for an explanation. + +cd $(dirname "$0") +shift +shift + +export GPG_TTY=$(tty) +set -ex + +docker run \ + -e GPG_TTY="${GPG_TTY}" \ + -v ${PWD}:/tensorflow-java-ndarray \ + -v ${HOME}/.gnupg:/root/.gnupg \ + -w /tensorflow-java-ndarray \ + -it \ + maven:3.8.6-jdk-11 \ + mvn --settings settings.xml -Preleasing clean deploy -B -U -e From 3bd6562d7c0e8c9234e5a2bc4ab744f2729b36f8 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Thu, 26 Jan 2023 17:03:39 -0500 Subject: [PATCH 19/26] Prepare next iteration --- ndarray/pom.xml | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ndarray/pom.xml b/ndarray/pom.xml index 73e80fa..9966a35 100644 --- a/ndarray/pom.xml +++ b/ndarray/pom.xml @@ -22,7 +22,7 @@ org.tensorflow tensorflow-java-ndarray - 0.4.0 + 0.5.0-SNAPSHOT ndarray jar diff --git a/pom.xml b/pom.xml index 128a0d4..ce70d66 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.tensorflow tensorflow-java-ndarray - 0.4.0 + 0.5.0-SNAPSHOT pom NdArray Parent From 145062f2f0d36ec70b4ed8c4ca3ffc71581ea304 Mon Sep 17 00:00:00 2001 From: Jay Modi Date: Sat, 17 Feb 2024 08:53:05 -0800 Subject: [PATCH 20/26] Update README.md (#17) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7b9076b..5e860b6 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ assertEquals(4095L, bufferB.size()); assertEquals(2, bufferB.getInt(0)); // Resize a buffer to 10 elements -IntDataBuffer bufferC = bufferA.narrow(10); +IntDataBuffer bufferC = bufferB.narrow(10); assertEquals(10L, bufferB.size()); assertEquals(2, bufferB.getInt(0)); ``` From 05202d984f79b1e5aea7e04d5608e0d393551eb2 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Sat, 17 Feb 2024 19:22:35 -0500 Subject: [PATCH 21/26] Fixing CI build --- .github/workflows/ci.yml | 10 ++-------- release.sh | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d4a44c3..2d1378f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,11 +3,11 @@ on: push: branches: - main - # - r[0-9]+.* + - r[0-9]+.* pull_request: branches: - main - # - r[0-9]+.* + - r[0-9]+.* types: [opened, reopened, synchronize] jobs: build: @@ -30,9 +30,3 @@ jobs: run: | echo "ossrh${{ secrets.CI_DEPLOY_USERNAME }}${{ secrets.CI_DEPLOY_PASSWORD }}" > $HOME/.m2/settings.xml mvn deploy -B -U -e -Dmaven.test.skip=true - - name: Release artifacts - if: github.event_name == 'push' && startsWith(github.ref, 'r') - run: | - echo "ossrh${{ secrets.CI_DEPLOY_USERNAME }}${{ secrets.CI_DEPLOY_PASSWORD }}" > $HOME/.m2/settings.xml - mvn release:prepare -B -U -e - mvn release:perform -B -U -e -Dmaven.test.skip=true diff --git a/release.sh b/release.sh index cf8e05f..ce3aed8 100755 --- a/release.sh +++ b/release.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Copyright 2023 The TensorFlow Authors. All Rights Reserved. +# Copyright 2024 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,20 +15,38 @@ # ============================================================================== # # Script to upload release artifacts for the TensorFlow Java library to -# Maven Central. See RELEASE.md for an explanation. +# Maven Central. See RELEASE.md for explanation. cd $(dirname "$0") shift shift +CMD="$*" + +# If release fails, debug with +# ./release.sh bash +# To get a shell to poke around the maven artifacts with. +if [[ -z "${CMD}" ]] +then + CMD="mvn clean deploy -B -e --settings ./settings.xml -Pdeploying -Preleasing" +fi export GPG_TTY=$(tty) set -ex +if [[ ! -f settings.xml ]] +then + cp -f ~/.m2/settings.xml . +fi + docker run \ -e GPG_TTY="${GPG_TTY}" \ -v ${PWD}:/tensorflow-java-ndarray \ -v ${HOME}/.gnupg:/root/.gnupg \ -w /tensorflow-java-ndarray \ -it \ + --platform linux/amd64 \ maven:3.8.6-jdk-11 \ - mvn --settings settings.xml -Preleasing clean deploy -B -U -e + ${CMD} + +echo +echo "Release completed" From e9ff657e213e2b89410970c60a28c19c8994134d Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Sat, 17 Feb 2024 21:38:25 -0500 Subject: [PATCH 22/26] Viewing arrays with different shapes (#18) --- .../tensorflow/ndarray/BooleanNdArray.java | 3 ++ .../org/tensorflow/ndarray/ByteNdArray.java | 3 ++ .../org/tensorflow/ndarray/DoubleNdArray.java | 3 ++ .../org/tensorflow/ndarray/FloatNdArray.java | 3 ++ .../org/tensorflow/ndarray/IntNdArray.java | 3 ++ .../org/tensorflow/ndarray/LongNdArray.java | 3 ++ .../java/org/tensorflow/ndarray/NdArray.java | 32 +++++++++++++++++-- .../org/tensorflow/ndarray/ShortNdArray.java | 3 ++ .../impl/dense/AbstractDenseNdArray.java | 18 +++++++++-- .../impl/dense/BooleanDenseNdArray.java | 2 +- .../ndarray/impl/dense/ByteDenseNdArray.java | 2 +- .../ndarray/impl/dense/DenseNdArray.java | 2 +- .../impl/dense/DoubleDenseNdArray.java | 2 +- .../ndarray/impl/dense/FloatDenseNdArray.java | 2 +- .../ndarray/impl/dense/IntDenseNdArray.java | 2 +- .../ndarray/impl/dense/LongDenseNdArray.java | 2 +- .../ndarray/impl/dense/ShortDenseNdArray.java | 2 +- .../impl/sparse/AbstractSparseNdArray.java | 5 +++ .../tensorflow/ndarray/NdArrayTestBase.java | 27 ++++++++++++++++ .../tensorflow/ndarray/SparseNdArrayTest.java | 7 ++++ 20 files changed, 112 insertions(+), 14 deletions(-) diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java index 5b4bedb..bd16a9a 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java @@ -68,6 +68,9 @@ public interface BooleanNdArray extends NdArray { */ BooleanNdArray setBoolean(boolean value, long... coordinates); + @Override + BooleanNdArray withShape(Shape shape); + @Override BooleanNdArray slice(Index... indices); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/ByteNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/ByteNdArray.java index 0e6f118..47e5a0d 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/ByteNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/ByteNdArray.java @@ -68,6 +68,9 @@ public interface ByteNdArray extends NdArray { */ ByteNdArray setByte(byte value, long... coordinates); + @Override + ByteNdArray withShape(Shape shape); + @Override ByteNdArray slice(Index... indices); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java index b0e6dab..da42bab 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java @@ -83,6 +83,9 @@ default DoubleStream streamOfDoubles() { return StreamSupport.stream(scalars().spliterator(), false).mapToDouble(DoubleNdArray::getDouble); } + @Override + DoubleNdArray withShape(Shape shape); + @Override DoubleNdArray slice(Index... indices); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/FloatNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/FloatNdArray.java index 8d4fbf5..34e4201 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/FloatNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/FloatNdArray.java @@ -68,6 +68,9 @@ public interface FloatNdArray extends NdArray { */ FloatNdArray setFloat(float value, long... coordinates); + @Override + FloatNdArray withShape(Shape shape); + @Override FloatNdArray slice(Index... coordinates); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java index e6a5cf0..71f19b9 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java @@ -83,6 +83,9 @@ default IntStream streamOfInts() { return StreamSupport.stream(scalars().spliterator(), false).mapToInt(IntNdArray::getInt); } + @Override + IntNdArray withShape(Shape shape); + @Override IntNdArray slice(Index... indices); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java index e7bd266..a55b2ab 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java @@ -83,6 +83,9 @@ default LongStream streamOfLongs() { return StreamSupport.stream(scalars().spliterator(), false).mapToLong(LongNdArray::getLong); } + @Override + LongNdArray withShape(Shape shape); + @Override LongNdArray slice(Index... indices); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java index f1e84d4..a75da48 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java @@ -16,14 +16,14 @@ */ package org.tensorflow.ndarray; +import org.tensorflow.ndarray.buffer.DataBuffer; +import org.tensorflow.ndarray.index.Index; + import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.stream.Stream; import java.util.stream.StreamSupport; -import org.tensorflow.ndarray.buffer.DataBuffer; -import org.tensorflow.ndarray.index.Index; - /** * A data structure of N-dimensions. * @@ -101,6 +101,32 @@ public interface NdArray extends Shaped { */ NdArraySequence> scalars(); + /** + * Returns a new N-dimensional view of this array with the given {@code shape}. + * + *

The provided {@code shape} must comply to the following characteristics: + *

    + *
  • new shape is known (i.e. has no unknown dimension)
  • + *
  • new shape size is equal to the size of the current shape (i.e. same number of elements)
  • + *
+ * For example, + *
{@code
+   *    NdArrays.ofInts(Shape.scalar()).withShape(Shape.of(1, 1));  // ok
+   *    NdArrays.ofInts(Shape.of(2, 3).withShape(Shape.of(3, 2));   // ok
+   *    NdArrays.ofInts(Shape.scalar()).withShape(Shape.of(1, 2));  // not ok, sizes are different (1 != 2)
+   *    NdArrays.ofInts(Shape.of(2, 3)).withShape(Shape.unknown()); // not ok, new shape unknown
+   * }
+ * + *

Any changes applied to the returned view affect the data of this array as well, as there + * is no copy involved. + * + * @param shape the new shape to apply + * @return a new array viewing the data according to the new shape, or this array if shapes are the same + * @throws IllegalArgumentException if the provided {@code shape} is not compliant + * @throws UnsupportedOperationException if this array does not support this operation + */ + NdArray withShape(Shape shape); + /** * Creates a multi-dimensional view (or slice) of this array by mapping one or more dimensions * to the given index selectors. diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java index f9335b4..92b608f 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java @@ -68,6 +68,9 @@ public interface ShortNdArray extends NdArray { */ ShortNdArray setShort(short value, long... coordinates); + @Override + ShortNdArray withShape(Shape shape); + @Override ShortNdArray slice(Index... coordinates); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java index 30af952..baaf23e 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java @@ -18,6 +18,7 @@ import org.tensorflow.ndarray.NdArray; import org.tensorflow.ndarray.NdArraySequence; +import org.tensorflow.ndarray.Shape; import org.tensorflow.ndarray.impl.AbstractNdArray; import org.tensorflow.ndarray.impl.dimension.RelativeDimensionalSpace; import org.tensorflow.ndarray.impl.sequence.FastElementSequence; @@ -43,7 +44,7 @@ public NdArraySequence elements(int dimensionIdx) { DimensionalSpace elemDims = dimensions().from(dimensionIdx + 1); try { DataBufferWindow> elemWindow = buffer().window(elemDims.physicalSize()); - U element = instantiate(elemWindow.buffer(), elemDims); + U element = instantiateView(elemWindow.buffer(), elemDims); return new FastElementSequence(this, dimensionIdx, element, elemWindow); } catch (UnsupportedOperationException e) { // If buffer windows are not supported, fallback to slicing (and slower) sequence @@ -51,10 +52,21 @@ public NdArraySequence elements(int dimensionIdx) { } } + @Override + public U withShape(Shape shape) { + if (shape == null || shape.isUnknown() || shape.size() != this.shape().size()) { + throw new IllegalArgumentException("Shape " + shape + " cannot be used to reshape ndarray of shape " + this.shape()); + } + if (shape.equals(this.shape())) { + return (U)this; + } + return instantiateView(buffer(), DimensionalSpace.create(shape)); + } + @Override public U slice(long position, DimensionalSpace sliceDimensions) { DataBuffer sliceBuffer = buffer().slice(position, sliceDimensions.physicalSize()); - return instantiate(sliceBuffer, sliceDimensions); + return instantiateView(sliceBuffer, sliceDimensions); } @Override @@ -147,7 +159,7 @@ protected AbstractDenseNdArray(DimensionalSpace dimensions) { abstract protected DataBuffer buffer(); - abstract U instantiate(DataBuffer buffer, DimensionalSpace dimensions); + abstract U instantiateView(DataBuffer buffer, DimensionalSpace dimensions); long positionOf(long[] coords, boolean isValue) { if (coords == null || coords.length == 0) { diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java index 0764146..9c134b5 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java @@ -73,7 +73,7 @@ protected BooleanDenseNdArray(BooleanDataBuffer buffer, Shape shape) { } @Override - BooleanDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) { + BooleanDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) { return new BooleanDenseNdArray((BooleanDataBuffer)buffer, dimensions); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java index 172432b..a2525c6 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java @@ -73,7 +73,7 @@ protected ByteDenseNdArray(ByteDataBuffer buffer, Shape shape) { } @Override - ByteDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) { + ByteDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) { return new ByteDenseNdArray((ByteDataBuffer)buffer, dimensions); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DenseNdArray.java index 819d95d..18b3755 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DenseNdArray.java @@ -45,7 +45,7 @@ protected DenseNdArray(DataBuffer buffer, Shape shape) { } @Override - DenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) { + DenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) { return new DenseNdArray<>(buffer, dimensions); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java index f54b8d0..a967ce1 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java @@ -73,7 +73,7 @@ protected DoubleDenseNdArray(DoubleDataBuffer buffer, Shape shape) { } @Override - DoubleDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) { + DoubleDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) { return new DoubleDenseNdArray((DoubleDataBuffer)buffer, dimensions); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java index 196b5ef..a04c192 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java @@ -73,7 +73,7 @@ protected FloatDenseNdArray(FloatDataBuffer buffer, Shape shape) { } @Override - FloatDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) { + FloatDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) { return new FloatDenseNdArray((FloatDataBuffer) buffer, dimensions); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java index a7af498..e1a726f 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java @@ -73,7 +73,7 @@ protected IntDenseNdArray(IntDataBuffer buffer, Shape shape) { } @Override - IntDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) { + IntDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) { return new IntDenseNdArray((IntDataBuffer)buffer, dimensions); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java index cd56dad..802cbcd 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java @@ -73,7 +73,7 @@ protected LongDenseNdArray(LongDataBuffer buffer, Shape shape) { } @Override - LongDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) { + LongDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) { return new LongDenseNdArray((LongDataBuffer)buffer, dimensions); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java index 291c01a..434b260 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java @@ -73,7 +73,7 @@ protected ShortDenseNdArray(ShortDataBuffer buffer, Shape shape) { } @Override - ShortDenseNdArray instantiate(DataBuffer buffer, DimensionalSpace dimensions) { + ShortDenseNdArray instantiateView(DataBuffer buffer, DimensionalSpace dimensions) { return new ShortDenseNdArray((ShortDataBuffer)buffer, dimensions); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java index 8e3892d..e4a2fba 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/AbstractSparseNdArray.java @@ -212,6 +212,11 @@ protected long[] getIndicesCoordinates(LongNdArray l) { */ public abstract U toDense(); + @Override + public U withShape(Shape shape) { + throw new UnsupportedOperationException("Sparse NdArrays cannot be viewed with a different shape"); + } + /** {@inheritDoc} */ @Override public NdArray slice(Index... indices) { diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java b/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java index 8a09ec7..577c2b7 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java @@ -384,4 +384,31 @@ public void streamingObjects() { values = matrix.streamOfObjects().collect(Collectors.toList()); assertIterableEquals(List.of(valueOf(1L), valueOf(2L), valueOf(3L), valueOf(4L)), values); } + + @Test + public void withShape() { + Shape originalShape = Shape.scalar(); + Shape newShape = originalShape.prepend(1).prepend(1); // [1, 1] + + NdArray originalArray = allocate(originalShape); + originalArray.setObject(valueOf(10L)); + assertEquals(valueOf(10L), originalArray.getObject()); + + NdArray newArray = originalArray.withShape(newShape); + assertNotNull(newArray); + assertEquals(newShape, newArray.shape()); + assertEquals(originalShape, originalArray.shape()); + assertEquals(valueOf(10L), newArray.getObject(0, 0)); + + NdArray sameArray = originalArray.withShape(Shape.scalar()); + assertSame(originalArray, sameArray); + + assertThrows(IllegalArgumentException.class, () -> originalArray.withShape(Shape.of(2))); + assertThrows(IllegalArgumentException.class, () -> originalArray.withShape(Shape.unknown())); + + NdArray originalMatrix = allocate(Shape.of(2, 3)); + assertThrows(IllegalArgumentException.class, () -> originalMatrix.withShape(Shape.scalar())); + NdArray newMatrix = originalMatrix.withShape(Shape.of(3, 2)); + assertEquals(Shape.of(3, 2), newMatrix.shape()); + } } diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/SparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/SparseNdArrayTest.java index 43779b3..0c5d6b3 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/SparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/SparseNdArrayTest.java @@ -25,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; public class SparseNdArrayTest { @@ -188,4 +189,10 @@ public void testShort() { assertEquals((short) 0, instance.getShort(2, 2)); assertEquals((short) 0xff00, instance.getShort(2, 3)); } + + @Test + public void withShape() { + NdArray sparseArray = NdArrays.sparseOf(indices, NdArrays.vectorOf(1, 2, 3), shape); + assertThrows(UnsupportedOperationException.class, () -> sparseArray.withShape(shape.prepend(1))); + } } From 369b05a08200913283fc4780a76dae0082dd10f2 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Mon, 19 Feb 2024 21:46:55 -0500 Subject: [PATCH 23/26] Rename read/write to copyTo/From (#19) --- README.md | 2 +- .../tensorflow/ndarray/BooleanNdArray.java | 8 +++---- .../org/tensorflow/ndarray/ByteNdArray.java | 8 +++---- .../org/tensorflow/ndarray/DoubleNdArray.java | 8 +++---- .../org/tensorflow/ndarray/FloatNdArray.java | 8 +++---- .../org/tensorflow/ndarray/IntNdArray.java | 8 +++---- .../org/tensorflow/ndarray/LongNdArray.java | 8 +++---- .../java/org/tensorflow/ndarray/NdArray.java | 14 +++++++---- .../org/tensorflow/ndarray/ShortNdArray.java | 8 +++---- .../org/tensorflow/ndarray/StdArrays.java | 16 ++++++------- .../tensorflow/ndarray/impl/Validator.java | 4 ++-- .../impl/dense/AbstractDenseNdArray.java | 8 +++---- .../impl/dense/BooleanDenseNdArray.java | 8 +++---- .../ndarray/impl/dense/ByteDenseNdArray.java | 8 +++---- .../impl/dense/DoubleDenseNdArray.java | 8 +++---- .../ndarray/impl/dense/FloatDenseNdArray.java | 8 +++---- .../ndarray/impl/dense/IntDenseNdArray.java | 8 +++---- .../ndarray/impl/dense/LongDenseNdArray.java | 8 +++---- .../ndarray/impl/dense/ShortDenseNdArray.java | 8 +++---- .../impl/sparse/BooleanSparseNdArray.java | 24 +++++++++---------- .../impl/sparse/ByteSparseNdArray.java | 24 +++++++++---------- .../impl/sparse/DoubleSparseNdArray.java | 24 +++++++++---------- .../impl/sparse/FloatSparseNdArray.java | 24 +++++++++---------- .../ndarray/impl/sparse/IntSparseNdArray.java | 24 +++++++++---------- .../impl/sparse/LongSparseNdArray.java | 24 +++++++++---------- .../impl/sparse/ShortSparseNdArray.java | 24 +++++++++---------- .../ndarray/impl/sparse/SparseNdArray.java | 16 ++++++------- .../impl/sparse/slice/BooleanSparseSlice.java | 12 +++++----- .../impl/sparse/slice/ByteSparseSlice.java | 12 +++++----- .../impl/sparse/slice/DoubleSparseSlice.java | 12 +++++----- .../impl/sparse/slice/FloatSparseSlice.java | 12 +++++----- .../impl/sparse/slice/IntSparseSlice.java | 12 +++++----- .../impl/sparse/slice/LongSparseSlice.java | 12 +++++----- .../impl/sparse/slice/ObjectSparseSlice.java | 4 ++-- .../impl/sparse/slice/ShortSparseSlice.java | 12 +++++----- .../impl/sparse/slice/SparseSlice.java | 2 +- .../tensorflow/ndarray/NdArrayTestBase.java | 8 +++---- .../impl/sparse/BooleanSparseNdArrayTest.java | 12 +++++----- .../impl/sparse/ByteSparseNdArrayTest.java | 12 +++++----- .../impl/sparse/DoubleSparseNdArrayTest.java | 12 +++++----- .../impl/sparse/FloatSparseNdArrayTest.java | 12 +++++----- .../impl/sparse/IntSparseNdArrayTest.java | 12 +++++----- .../impl/sparse/LongSparseNdArrayTest.java | 12 +++++----- .../impl/sparse/ShortSparseNdArrayTest.java | 12 +++++----- .../impl/sparse/StringSparseNdArrayTest.java | 12 +++++----- 45 files changed, 270 insertions(+), 264 deletions(-) diff --git a/README.md b/README.md index 5e860b6..780d50c 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ IntNdArray vector = matrix3d.get(0, 1); assertEquals(1, vector.rank()); // Rewriting the values of the vector using a primitive array -vector.write(new int[] { 7, 8 }); +vector.copyFrom(DataBuffers.of(new int[] { 7, 8 })); assertEquals(7, matrix3d.getInt(0, 1, 0)); assertEquals(8, matrix3d.getInt(0, 1, 1)); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java index bd16a9a..a762896 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java @@ -100,12 +100,12 @@ default BooleanNdArray setObject(Boolean value, long... coordinates) { BooleanNdArray copyTo(NdArray dst); @Override - BooleanNdArray read(DataBuffer dst); + BooleanNdArray copyTo(DataBuffer dst); - BooleanNdArray read(BooleanDataBuffer dst); + BooleanNdArray copyTo(BooleanDataBuffer dst); @Override - BooleanNdArray write(DataBuffer src); + BooleanNdArray copyFrom(DataBuffer src); - BooleanNdArray write(BooleanDataBuffer src); + BooleanNdArray copyFrom(BooleanDataBuffer src); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/ByteNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/ByteNdArray.java index 47e5a0d..7a6f0cd 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/ByteNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/ByteNdArray.java @@ -100,12 +100,12 @@ default ByteNdArray setObject(Byte value, long... coordinates) { ByteNdArray copyTo(NdArray dst); @Override - ByteNdArray read(DataBuffer dst); + ByteNdArray copyTo(DataBuffer dst); - ByteNdArray read(ByteDataBuffer dst); + ByteNdArray copyTo(ByteDataBuffer dst); @Override - ByteNdArray write(DataBuffer src); + ByteNdArray copyFrom(DataBuffer src); - ByteNdArray write(ByteDataBuffer src); + ByteNdArray copyFrom(ByteDataBuffer src); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java index da42bab..d8d2312 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/DoubleNdArray.java @@ -115,12 +115,12 @@ default DoubleNdArray setObject(Double value, long... coordinates) { DoubleNdArray copyTo(NdArray dst); @Override - DoubleNdArray read(DataBuffer dst); + DoubleNdArray copyTo(DataBuffer dst); - DoubleNdArray read(DoubleDataBuffer dst); + DoubleNdArray copyTo(DoubleDataBuffer dst); @Override - DoubleNdArray write(DataBuffer src); + DoubleNdArray copyFrom(DataBuffer src); - DoubleNdArray write(DoubleDataBuffer src); + DoubleNdArray copyFrom(DoubleDataBuffer src); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/FloatNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/FloatNdArray.java index 34e4201..98ec87e 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/FloatNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/FloatNdArray.java @@ -100,12 +100,12 @@ default FloatNdArray setObject(Float value, long... coordinates) { FloatNdArray copyTo(NdArray dst); @Override - FloatNdArray read(DataBuffer dst); + FloatNdArray copyTo(DataBuffer dst); - FloatNdArray read(FloatDataBuffer dst); + FloatNdArray copyTo(FloatDataBuffer dst); @Override - FloatNdArray write(DataBuffer src); + FloatNdArray copyFrom(DataBuffer src); - FloatNdArray write(FloatDataBuffer src); + FloatNdArray copyFrom(FloatDataBuffer src); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java index 71f19b9..b165900 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/IntNdArray.java @@ -115,12 +115,12 @@ default IntNdArray setObject(Integer value, long... coordinates) { IntNdArray copyTo(NdArray dst); @Override - IntNdArray read(DataBuffer dst); + IntNdArray copyTo(DataBuffer dst); - IntNdArray read(IntDataBuffer dst); + IntNdArray copyTo(IntDataBuffer dst); @Override - IntNdArray write(DataBuffer src); + IntNdArray copyFrom(DataBuffer src); - IntNdArray write(IntDataBuffer src); + IntNdArray copyFrom(IntDataBuffer src); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java index a55b2ab..5d5766a 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/LongNdArray.java @@ -115,12 +115,12 @@ default LongNdArray setObject(Long value, long... coordinates) { LongNdArray copyTo(NdArray dst); @Override - LongNdArray read(DataBuffer dst); + LongNdArray copyTo(DataBuffer dst); - LongNdArray read(LongDataBuffer dst); + LongNdArray copyTo(LongDataBuffer dst); @Override - LongNdArray write(DataBuffer src); + LongNdArray copyFrom(DataBuffer src); - LongNdArray write(LongDataBuffer src); + LongNdArray copyFrom(LongDataBuffer src); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java index a75da48..da34fd3 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java @@ -285,33 +285,39 @@ default Stream streamOfObjects() { NdArray copyTo(NdArray dst); /** - * Read the content of this N-dimensional array into the destination buffer. + * Copy the content of this N-dimensional array into the destination buffer. * *

The size of the buffer must be equal or greater to the {@link #size()} of this * array, or an exception is thrown. After the copy, content of the buffer and of the array can be * altered independently, without affecting each other. * + *

Note: in version 0.4.0 and earlier, this method was named {@code read(DataBuffer)}. It has been renamed to + * explicitly indicate the direction of the data flow to avoid confusion. + * * @param dst the destination buffer * @return this array * @throws java.nio.BufferOverflowException if the buffer cannot hold the content of this array * @see DataBuffer#size() */ - NdArray read(DataBuffer dst); + NdArray copyTo(DataBuffer dst); /** - * Write the content of this N-dimensional array from the source buffer. + * Copy the content of the source buffer into this N-dimensional array. * *

The size of the buffer must be equal or greater to the {@link #size()} of this * array, or an exception is thrown. After the copy, content of the buffer and of the array can be * altered independently, without affecting each other. * + *

Note: in version 0.4.0 and earlier, this method was named {@code write(DataBuffer)}. It has been renamed to + * explicitly indicate the direction of the data flow to avoid confusion. + * * @param src the source buffer * @return this array * @throws java.nio.BufferUnderflowException if the buffer has not enough remaining data to write * into this array * @see DataBuffer#size() */ - NdArray write(DataBuffer src); + NdArray copyFrom(DataBuffer src); /** * Checks equality between n-dimensional arrays. diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java index 92b608f..022ccf7 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java @@ -100,12 +100,12 @@ default ShortNdArray setObject(Short value, long... coordinates) { ShortNdArray copyTo(NdArray dst); @Override - ShortNdArray read(DataBuffer dst); + ShortNdArray copyTo(DataBuffer dst); - ShortNdArray read(ShortDataBuffer dst); + ShortNdArray copyTo(ShortDataBuffer dst); @Override - ShortNdArray write(DataBuffer src); + ShortNdArray copyFrom(DataBuffer src); - ShortNdArray write(ShortDataBuffer src); + ShortNdArray copyFrom(ShortDataBuffer src); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/StdArrays.java b/ndarray/src/main/java/org/tensorflow/ndarray/StdArrays.java index 249e69a..7367165 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/StdArrays.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/StdArrays.java @@ -2010,7 +2010,7 @@ public static void copyFrom(IntNdArray src, int[] dst) { if (src.size() > dst.length) { throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length); } - src.read(DataBuffers.of(dst, false, false)); + src.copyTo(DataBuffers.of(dst, false, false)); } /** @@ -2113,7 +2113,7 @@ public static void copyFrom(LongNdArray src, long[] dst) { if (src.size() > dst.length) { throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length); } - src.read(DataBuffers.of(dst, false, false)); + src.copyTo(DataBuffers.of(dst, false, false)); } /** @@ -2216,7 +2216,7 @@ public static void copyFrom(FloatNdArray src, float[] dst) { if (src.size() > dst.length) { throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length); } - src.read(DataBuffers.of(dst, false, false)); + src.copyTo(DataBuffers.of(dst, false, false)); } /** @@ -2319,7 +2319,7 @@ public static void copyFrom(DoubleNdArray src, double[] dst) { if (src.size() > dst.length) { throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length); } - src.read(DataBuffers.of(dst, false, false)); + src.copyTo(DataBuffers.of(dst, false, false)); } /** @@ -2422,7 +2422,7 @@ public static void copyFrom(ByteNdArray src, byte[] dst) { if (src.size() > dst.length) { throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length); } - src.read(DataBuffers.of(dst, false, false)); + src.copyTo(DataBuffers.of(dst, false, false)); } /** @@ -2525,7 +2525,7 @@ public static void copyFrom(ShortNdArray src, short[] dst) { if (src.size() > dst.length) { throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length); } - src.read(DataBuffers.of(dst, false, false)); + src.copyTo(DataBuffers.of(dst, false, false)); } /** @@ -2628,7 +2628,7 @@ public static void copyFrom(BooleanNdArray src, boolean[] dst) { if (src.size() > dst.length) { throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length); } - src.read(DataBuffers.of(dst, false, false)); + src.copyTo(DataBuffers.of(dst, false, false)); } /** @@ -2732,7 +2732,7 @@ public static void copyFrom(NdArray src, T[] dst) { if (src.size() > dst.length) { throw new ArrayIndexOutOfBoundsException(String.valueOf(src.size()) + " > " + dst.length); } - src.read(DataBuffers.of(dst, false, false)); + src.copyTo(DataBuffers.of(dst, false, false)); } /** diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/Validator.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/Validator.java index 285d099..3e0ba20 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/Validator.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/Validator.java @@ -30,13 +30,13 @@ public static void copyToNdArrayArgs(NdArray ndArray, NdArray otherNdArray } } - public static void readToBufferArgs(NdArray ndArray, DataBuffer dst) { + public static void copyToBufferArgs(NdArray ndArray, DataBuffer dst) { if (dst.size() < ndArray.size()) { throw new BufferOverflowException(); } } - public static void writeFromBufferArgs(NdArray ndArray, DataBuffer src) { + public static void copyFromBufferArgs(NdArray ndArray, DataBuffer src) { if (src.size() < ndArray.size()) { throw new BufferUnderflowException(); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java index baaf23e..a22518d 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/AbstractDenseNdArray.java @@ -101,15 +101,15 @@ public U setObject(T value, long... coords) { } @Override - public U read(DataBuffer dst) { - Validator.readToBufferArgs(this, dst); + public U copyTo(DataBuffer dst) { + Validator.copyToBufferArgs(this, dst); DataTransfer.execute(buffer(), dimensions(), dst, DataTransfer::ofValue); return (U)this; } @Override - public U write(DataBuffer src) { - Validator.writeFromBufferArgs(this, src); + public U copyFrom(DataBuffer src) { + Validator.copyFromBufferArgs(this, src); DataTransfer.execute(src, buffer(), dimensions(), DataTransfer::ofValue); return (U)this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java index 9c134b5..c3df6e8 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/BooleanDenseNdArray.java @@ -55,15 +55,15 @@ public BooleanNdArray copyTo(NdArray dst) { } @Override - public BooleanNdArray read(BooleanDataBuffer dst) { - Validator.readToBufferArgs(this, dst); + public BooleanNdArray copyTo(BooleanDataBuffer dst) { + Validator.copyToBufferArgs(this, dst); DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofBoolean); return this; } @Override - public BooleanNdArray write(BooleanDataBuffer src) { - Validator.writeFromBufferArgs(this, src); + public BooleanNdArray copyFrom(BooleanDataBuffer src) { + Validator.copyFromBufferArgs(this, src); DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofBoolean); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java index a2525c6..1f01ce6 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ByteDenseNdArray.java @@ -55,15 +55,15 @@ public ByteNdArray copyTo(NdArray dst) { } @Override - public ByteNdArray read(ByteDataBuffer dst) { - Validator.readToBufferArgs(this, dst); + public ByteNdArray copyTo(ByteDataBuffer dst) { + Validator.copyToBufferArgs(this, dst); DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofByte); return this; } @Override - public ByteNdArray write(ByteDataBuffer src) { - Validator.writeFromBufferArgs(this, src); + public ByteNdArray copyFrom(ByteDataBuffer src) { + Validator.copyFromBufferArgs(this, src); DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofByte); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java index a967ce1..d983d16 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/DoubleDenseNdArray.java @@ -55,15 +55,15 @@ public DoubleNdArray copyTo(NdArray dst) { } @Override - public DoubleNdArray read(DoubleDataBuffer dst) { - Validator.readToBufferArgs(this, dst); + public DoubleNdArray copyTo(DoubleDataBuffer dst) { + Validator.copyToBufferArgs(this, dst); DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofDouble); return this; } @Override - public DoubleNdArray write(DoubleDataBuffer src) { - Validator.writeFromBufferArgs(this, src); + public DoubleNdArray copyFrom(DoubleDataBuffer src) { + Validator.copyFromBufferArgs(this, src); DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofDouble); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java index a04c192..779e047 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/FloatDenseNdArray.java @@ -55,15 +55,15 @@ public FloatNdArray copyTo(NdArray dst) { } @Override - public FloatNdArray read(FloatDataBuffer dst) { - Validator.readToBufferArgs(this, dst); + public FloatNdArray copyTo(FloatDataBuffer dst) { + Validator.copyToBufferArgs(this, dst); DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofFloat); return this; } @Override - public FloatNdArray write(FloatDataBuffer src) { - Validator.writeFromBufferArgs(this, src); + public FloatNdArray copyFrom(FloatDataBuffer src) { + Validator.copyFromBufferArgs(this, src); DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofFloat); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java index e1a726f..4e2183d 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/IntDenseNdArray.java @@ -55,15 +55,15 @@ public IntNdArray copyTo(NdArray dst) { } @Override - public IntNdArray read(IntDataBuffer dst) { - Validator.readToBufferArgs(this, dst); + public IntNdArray copyTo(IntDataBuffer dst) { + Validator.copyToBufferArgs(this, dst); DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofInt); return this; } @Override - public IntNdArray write(IntDataBuffer src) { - Validator.writeFromBufferArgs(this, src); + public IntNdArray copyFrom(IntDataBuffer src) { + Validator.copyFromBufferArgs(this, src); DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofInt); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java index 802cbcd..cde91a3 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/LongDenseNdArray.java @@ -55,15 +55,15 @@ public LongNdArray copyTo(NdArray dst) { } @Override - public LongNdArray read(LongDataBuffer dst) { - Validator.readToBufferArgs(this, dst); + public LongNdArray copyTo(LongDataBuffer dst) { + Validator.copyToBufferArgs(this, dst); DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofLong); return this; } @Override - public LongNdArray write(LongDataBuffer src) { - Validator.writeFromBufferArgs(this, src); + public LongNdArray copyFrom(LongDataBuffer src) { + Validator.copyFromBufferArgs(this, src); DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofLong); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java index 434b260..8d0dfc8 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/dense/ShortDenseNdArray.java @@ -55,15 +55,15 @@ public ShortNdArray copyTo(NdArray dst) { } @Override - public ShortNdArray read(ShortDataBuffer dst) { - Validator.readToBufferArgs(this, dst); + public ShortNdArray copyTo(ShortDataBuffer dst) { + Validator.copyToBufferArgs(this, dst); DataTransfer.execute(buffer, dimensions(), dst, DataTransfer::ofShort); return this; } @Override - public ShortNdArray write(ShortDataBuffer src) { - Validator.writeFromBufferArgs(this, src); + public ShortNdArray copyFrom(ShortDataBuffer src) { + Validator.copyFromBufferArgs(this, src); DataTransfer.execute(src, buffer, dimensions(), DataTransfer::ofShort); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java index f5b984f..d5d8d72 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java @@ -124,7 +124,7 @@ protected BooleanSparseNdArray( BooleanDataBuffer dataBuffer, boolean defaultValue, DimensionalSpace dimensions) { super(defaultValue, dimensions); // use write to set up the indices and values - write(dataBuffer); + copyFrom(dataBuffer); } /** @@ -266,7 +266,7 @@ public static BooleanSparseNdArray create( */ public static BooleanSparseNdArray create(BooleanNdArray src) { BooleanDataBuffer buffer = DataBuffers.ofBooleans(src.size()); - src.read(buffer); + src.copyTo(buffer); return new BooleanSparseNdArray(buffer, DimensionalSpace.create(src.shape())); } /** @@ -278,7 +278,7 @@ public static BooleanSparseNdArray create(BooleanNdArray src) { */ public static BooleanSparseNdArray create(BooleanNdArray src, boolean defaultValue) { BooleanDataBuffer buffer = DataBuffers.ofBooleans(src.size()); - src.read(buffer); + src.copyTo(buffer); return new BooleanSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape())); } @@ -318,13 +318,13 @@ public BooleanNdArray setBoolean(boolean value, long... coordinates) { /** {@inheritDoc} */ @Override - public BooleanNdArray read(DataBuffer dst) { - return read((BooleanDataBuffer) dst); + public BooleanNdArray copyTo(DataBuffer dst) { + return copyTo((BooleanDataBuffer) dst); } /** {@inheritDoc} */ @Override - public BooleanNdArray read(BooleanDataBuffer dst) { + public BooleanNdArray copyTo(BooleanDataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Boolean[] defaults = new Boolean[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -344,7 +344,7 @@ public BooleanNdArray read(BooleanDataBuffer dst) { /** {@inheritDoc} */ @Override - public BooleanNdArray write(BooleanDataBuffer src) { + public BooleanNdArray copyFrom(BooleanDataBuffer src) { List indices = new ArrayList<>(); List values = new ArrayList<>(); @@ -368,8 +368,8 @@ public BooleanNdArray write(BooleanDataBuffer src) { /** {@inheritDoc} */ @Override - public BooleanNdArray write(DataBuffer src) { - return write((BooleanDataBuffer) src); + public BooleanNdArray copyFrom(DataBuffer src) { + return copyFrom((BooleanDataBuffer) src); } /** @@ -379,7 +379,7 @@ public BooleanNdArray write(DataBuffer src) { */ public BooleanNdArray toDense() { BooleanDataBuffer dataBuffer = DataBuffers.ofBooleans(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -391,8 +391,8 @@ public BooleanNdArray toDense() { */ public BooleanNdArray fromDense(BooleanNdArray src) { BooleanDataBuffer buffer = DataBuffers.ofBooleans(src.size()); - src.read(buffer); - write(buffer); + src.copyTo(buffer); + copyFrom(buffer); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArray.java index 259aefe..633ca49 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArray.java @@ -118,7 +118,7 @@ protected ByteSparseNdArray( ByteSparseNdArray(ByteDataBuffer dataBuffer, byte defaultValue, DimensionalSpace dimensions) { super(defaultValue, dimensions); // use write to set up the indices and values - write(dataBuffer); + copyFrom(dataBuffer); } /** @@ -255,7 +255,7 @@ public static ByteSparseNdArray create(ByteDataBuffer buffer, byte defaultValue, */ public static ByteSparseNdArray create(ByteNdArray src) { ByteDataBuffer buffer = DataBuffers.ofBytes(src.size()); - src.read(buffer); + src.copyTo(buffer); return new ByteSparseNdArray(buffer, DimensionalSpace.create(src.shape())); } @@ -268,7 +268,7 @@ public static ByteSparseNdArray create(ByteNdArray src) { */ public static ByteSparseNdArray create(ByteNdArray src, byte defaultValue) { ByteDataBuffer buffer = DataBuffers.ofBytes(src.size()); - src.read(buffer); + src.copyTo(buffer); return new ByteSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape())); } @@ -302,13 +302,13 @@ public ByteNdArray setByte(byte value, long... coordinates) { /** {@inheritDoc} */ @Override - public ByteNdArray read(DataBuffer dst) { - return read((ByteDataBuffer) dst); + public ByteNdArray copyTo(DataBuffer dst) { + return copyTo((ByteDataBuffer) dst); } /** {@inheritDoc} */ @Override - public ByteNdArray read(ByteDataBuffer dst) { + public ByteNdArray copyTo(ByteDataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Byte[] defaults = new Byte[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -328,7 +328,7 @@ public ByteNdArray read(ByteDataBuffer dst) { /** {@inheritDoc} */ @Override - public ByteNdArray write(ByteDataBuffer src) { + public ByteNdArray copyFrom(ByteDataBuffer src) { List indices = new ArrayList<>(); List values = new ArrayList<>(); @@ -352,8 +352,8 @@ public ByteNdArray write(ByteDataBuffer src) { /** {@inheritDoc} */ @Override - public ByteNdArray write(DataBuffer src) { - return write((ByteDataBuffer) src); + public ByteNdArray copyFrom(DataBuffer src) { + return copyFrom((ByteDataBuffer) src); } /** @@ -363,7 +363,7 @@ public ByteNdArray write(DataBuffer src) { */ public ByteNdArray toDense() { ByteDataBuffer dataBuffer = DataBuffers.ofBytes(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -375,8 +375,8 @@ public ByteNdArray toDense() { */ public ByteNdArray fromDense(ByteNdArray src) { ByteDataBuffer buffer = DataBuffers.ofBytes(src.size()); - src.read(buffer); - write(buffer); + src.copyTo(buffer); + copyFrom(buffer); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java index 07a6d2a..35b3541 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArray.java @@ -118,7 +118,7 @@ protected DoubleSparseNdArray( DoubleDataBuffer dataBuffer, double defaultValue, DimensionalSpace dimensions) { super(defaultValue, dimensions); // use write to set up the indices and values - write(dataBuffer); + copyFrom(dataBuffer); } /** @@ -257,7 +257,7 @@ public static DoubleSparseNdArray create( */ public static DoubleSparseNdArray create(DoubleNdArray src) { DoubleDataBuffer buffer = DataBuffers.ofDoubles(src.size()); - src.read(buffer); + src.copyTo(buffer); return new DoubleSparseNdArray(buffer, DimensionalSpace.create(src.shape())); } /** @@ -269,7 +269,7 @@ public static DoubleSparseNdArray create(DoubleNdArray src) { */ public static DoubleSparseNdArray create(DoubleNdArray src, double defaultValue) { DoubleDataBuffer buffer = DataBuffers.ofDoubles(src.size()); - src.read(buffer); + src.copyTo(buffer); return new DoubleSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape())); } @@ -303,13 +303,13 @@ public DoubleNdArray setDouble(double value, long... coordinates) { /** {@inheritDoc} */ @Override - public DoubleNdArray read(DataBuffer dst) { - return read((DoubleDataBuffer) dst); + public DoubleNdArray copyTo(DataBuffer dst) { + return copyTo((DoubleDataBuffer) dst); } /** {@inheritDoc} */ @Override - public DoubleNdArray read(DoubleDataBuffer dst) { + public DoubleNdArray copyTo(DoubleDataBuffer dst) { // set buf to the default values, then overwrite with the indices/values. Double[] defaults = new Double[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -329,7 +329,7 @@ public DoubleNdArray read(DoubleDataBuffer dst) { /** {@inheritDoc} */ @Override - public DoubleNdArray write(DoubleDataBuffer src) { + public DoubleNdArray copyFrom(DoubleDataBuffer src) { List indices = new ArrayList<>(); List values = new ArrayList<>(); @@ -353,8 +353,8 @@ public DoubleNdArray write(DoubleDataBuffer src) { /** {@inheritDoc} */ @Override - public DoubleNdArray write(DataBuffer src) { - return write((DoubleDataBuffer) src); + public DoubleNdArray copyFrom(DataBuffer src) { + return copyFrom((DoubleDataBuffer) src); } /** @@ -364,7 +364,7 @@ public DoubleNdArray write(DataBuffer src) { */ public DoubleNdArray toDense() { DoubleDataBuffer dataBuffer = DataBuffers.ofDoubles(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -376,8 +376,8 @@ public DoubleNdArray toDense() { */ public DoubleNdArray fromDense(DoubleNdArray src) { DoubleDataBuffer buffer = DataBuffers.ofDoubles(src.size()); - src.read(buffer); - write(buffer); + src.copyTo(buffer); + copyFrom(buffer); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArray.java index 9b224a0..88f34b1 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArray.java @@ -118,7 +118,7 @@ protected FloatSparseNdArray( FloatSparseNdArray(FloatDataBuffer dataBuffer, float defaultValue, DimensionalSpace dimensions) { super(defaultValue, dimensions); // use write to set up the indices and values - write(dataBuffer); + copyFrom(dataBuffer); } /** @@ -255,7 +255,7 @@ public static FloatSparseNdArray create(FloatDataBuffer buffer, float defaultVal */ public static FloatSparseNdArray create(FloatNdArray src) { FloatDataBuffer buffer = DataBuffers.ofFloats(src.size()); - src.read(buffer); + src.copyTo(buffer); return new FloatSparseNdArray(buffer, DimensionalSpace.create(src.shape())); } /** @@ -267,7 +267,7 @@ public static FloatSparseNdArray create(FloatNdArray src) { */ public static FloatSparseNdArray create(FloatNdArray src, float defaultValue) { FloatDataBuffer buffer = DataBuffers.ofFloats(src.size()); - src.read(buffer); + src.copyTo(buffer); return new FloatSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape())); } @@ -301,13 +301,13 @@ public FloatNdArray setFloat(float value, long... coordinates) { /** {@inheritDoc} */ @Override - public FloatNdArray read(DataBuffer dst) { - return read((FloatDataBuffer) dst); + public FloatNdArray copyTo(DataBuffer dst) { + return copyTo((FloatDataBuffer) dst); } /** {@inheritDoc} */ @Override - public FloatNdArray read(FloatDataBuffer dst) { + public FloatNdArray copyTo(FloatDataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Float[] defaults = new Float[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -327,7 +327,7 @@ public FloatNdArray read(FloatDataBuffer dst) { /** {@inheritDoc} */ @Override - public FloatNdArray write(FloatDataBuffer src) { + public FloatNdArray copyFrom(FloatDataBuffer src) { List indices = new ArrayList<>(); List values = new ArrayList<>(); @@ -351,8 +351,8 @@ public FloatNdArray write(FloatDataBuffer src) { /** {@inheritDoc} */ @Override - public FloatNdArray write(DataBuffer src) { - return write((FloatDataBuffer) src); + public FloatNdArray copyFrom(DataBuffer src) { + return copyFrom((FloatDataBuffer) src); } /** @@ -362,7 +362,7 @@ public FloatNdArray write(DataBuffer src) { */ public FloatNdArray toDense() { FloatDataBuffer dataBuffer = DataBuffers.ofFloats(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -374,8 +374,8 @@ public FloatNdArray toDense() { */ public FloatNdArray fromDense(FloatNdArray src) { FloatDataBuffer buffer = DataBuffers.ofFloats(src.size()); - src.read(buffer); - write(buffer); + src.copyTo(buffer); + copyFrom(buffer); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArray.java index 757363f..d79c441 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArray.java @@ -116,7 +116,7 @@ protected IntSparseNdArray( IntSparseNdArray(IntDataBuffer dataBuffer, int defaultValue, DimensionalSpace dimensions) { super(defaultValue, dimensions); // use write to set up the indices and values - write(dataBuffer); + copyFrom(dataBuffer); } /** @@ -269,7 +269,7 @@ public static IntSparseNdArray create(IntDataBuffer buffer, int defaultValue, Sh */ public static IntSparseNdArray create(IntNdArray src) { IntDataBuffer buffer = DataBuffers.ofInts(src.size()); - src.read(buffer); + src.copyTo(buffer); return new IntSparseNdArray(buffer, DimensionalSpace.create(src.shape())); } @@ -282,7 +282,7 @@ public static IntSparseNdArray create(IntNdArray src) { */ public static IntSparseNdArray create(IntNdArray src, int defaultValue) { IntDataBuffer buffer = DataBuffers.ofInts(src.size()); - src.read(buffer); + src.copyTo(buffer); return new IntSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape())); } @@ -316,13 +316,13 @@ public IntNdArray setInt(int value, long... coordinates) { /** {@inheritDoc} */ @Override - public IntNdArray read(DataBuffer dst) { - return read((IntDataBuffer) dst); + public IntNdArray copyTo(DataBuffer dst) { + return copyTo((IntDataBuffer) dst); } /** {@inheritDoc} */ @Override - public IntNdArray read(IntDataBuffer dst) { + public IntNdArray copyTo(IntDataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Integer[] defaults = new Integer[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -342,7 +342,7 @@ public IntNdArray read(IntDataBuffer dst) { /** {@inheritDoc} */ @Override - public IntNdArray write(IntDataBuffer src) { + public IntNdArray copyFrom(IntDataBuffer src) { List indices = new ArrayList<>(); List values = new ArrayList<>(); @@ -366,8 +366,8 @@ public IntNdArray write(IntDataBuffer src) { /** {@inheritDoc} */ @Override - public IntNdArray write(DataBuffer src) { - return write((IntDataBuffer) src); + public IntNdArray copyFrom(DataBuffer src) { + return copyFrom((IntDataBuffer) src); } /** @@ -377,7 +377,7 @@ public IntNdArray write(DataBuffer src) { */ public IntNdArray toDense() { IntDataBuffer dataBuffer = DataBuffers.ofInts(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -389,8 +389,8 @@ public IntNdArray toDense() { */ public IntNdArray fromDense(IntNdArray src) { IntDataBuffer buffer = DataBuffers.ofInts(src.size()); - src.read(buffer); - write(buffer); + src.copyTo(buffer); + copyFrom(buffer); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java index 242ba50..1c01198 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArray.java @@ -117,7 +117,7 @@ protected LongSparseNdArray( LongSparseNdArray(LongDataBuffer dataBuffer, long defaultValue, DimensionalSpace dimensions) { super(defaultValue, dimensions); // use write to set up the indices and values - write(dataBuffer); + copyFrom(dataBuffer); } /** @@ -254,7 +254,7 @@ public static LongSparseNdArray create(LongDataBuffer buffer, long defaultValue, */ public static LongSparseNdArray create(LongNdArray src) { LongDataBuffer buffer = DataBuffers.ofLongs(src.size()); - src.read(buffer); + src.copyTo(buffer); return new LongSparseNdArray(buffer, DimensionalSpace.create(src.shape())); } @@ -267,7 +267,7 @@ public static LongSparseNdArray create(LongNdArray src) { */ public static LongSparseNdArray create(LongNdArray src, long defaultValue) { LongDataBuffer buffer = DataBuffers.ofLongs(src.size()); - src.read(buffer); + src.copyTo(buffer); return new LongSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape())); } @@ -301,13 +301,13 @@ public LongNdArray setLong(long value, long... coordinates) { /** {@inheritDoc} */ @Override - public LongNdArray read(DataBuffer dst) { - return read((LongDataBuffer) dst); + public LongNdArray copyTo(DataBuffer dst) { + return copyTo((LongDataBuffer) dst); } /** {@inheritDoc} */ @Override - public LongNdArray read(LongDataBuffer dst) { + public LongNdArray copyTo(LongDataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Long[] defaults = new Long[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -327,7 +327,7 @@ public LongNdArray read(LongDataBuffer dst) { /** {@inheritDoc} */ @Override - public LongNdArray write(LongDataBuffer src) { + public LongNdArray copyFrom(LongDataBuffer src) { List indices = new ArrayList<>(); List values = new ArrayList<>(); @@ -351,8 +351,8 @@ public LongNdArray write(LongDataBuffer src) { /** {@inheritDoc} */ @Override - public LongNdArray write(DataBuffer src) { - return write((LongDataBuffer) src); + public LongNdArray copyFrom(DataBuffer src) { + return copyFrom((LongDataBuffer) src); } /** @@ -362,7 +362,7 @@ public LongNdArray write(DataBuffer src) { */ public LongNdArray toDense() { LongDataBuffer dataBuffer = DataBuffers.ofLongs(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -374,8 +374,8 @@ public LongNdArray toDense() { */ public LongNdArray fromDense(LongNdArray src) { LongDataBuffer buffer = DataBuffers.ofLongs(src.size()); - src.read(buffer); - write(buffer); + src.copyTo(buffer); + copyFrom(buffer); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java index 63dde22..051a7cb 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArray.java @@ -118,7 +118,7 @@ protected ShortSparseNdArray( ShortSparseNdArray(ShortDataBuffer dataBuffer, short defaultValue, DimensionalSpace dimensions) { super(defaultValue, dimensions); // use write to set up the indices and values - write(dataBuffer); + copyFrom(dataBuffer); } /** @@ -255,7 +255,7 @@ public static ShortSparseNdArray create(ShortDataBuffer buffer, short defaultVal */ public static ShortSparseNdArray create(ShortNdArray src) { ShortDataBuffer buffer = DataBuffers.ofShorts(src.size()); - src.read(buffer); + src.copyTo(buffer); return new ShortSparseNdArray(buffer, DimensionalSpace.create(src.shape())); } @@ -268,7 +268,7 @@ public static ShortSparseNdArray create(ShortNdArray src) { */ public static ShortSparseNdArray create(ShortNdArray src, short defaultValue) { ShortDataBuffer buffer = DataBuffers.ofShorts(src.size()); - src.read(buffer); + src.copyTo(buffer); return new ShortSparseNdArray(buffer, defaultValue, DimensionalSpace.create(src.shape())); } @@ -302,13 +302,13 @@ public ShortNdArray setShort(short value, long... coordinates) { /** {@inheritDoc} */ @Override - public ShortNdArray read(DataBuffer dst) { - return read((ShortDataBuffer) dst); + public ShortNdArray copyTo(DataBuffer dst) { + return copyTo((ShortDataBuffer) dst); } /** {@inheritDoc} */ @Override - public ShortNdArray read(ShortDataBuffer dst) { + public ShortNdArray copyTo(ShortDataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Short[] defaults = new Short[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -328,7 +328,7 @@ public ShortNdArray read(ShortDataBuffer dst) { /** {@inheritDoc} */ @Override - public ShortNdArray write(ShortDataBuffer src) { + public ShortNdArray copyFrom(ShortDataBuffer src) { List indices = new ArrayList<>(); List values = new ArrayList<>(); @@ -352,8 +352,8 @@ public ShortNdArray write(ShortDataBuffer src) { /** {@inheritDoc} */ @Override - public ShortNdArray write(DataBuffer src) { - return write((ShortDataBuffer) src); + public ShortNdArray copyFrom(DataBuffer src) { + return copyFrom((ShortDataBuffer) src); } /** @@ -363,7 +363,7 @@ public ShortNdArray write(DataBuffer src) { */ public ShortNdArray toDense() { ShortDataBuffer dataBuffer = DataBuffers.ofShorts(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -375,8 +375,8 @@ public ShortNdArray toDense() { */ public ShortNdArray fromDense(ShortNdArray src) { ShortDataBuffer buffer = DataBuffers.ofShorts(src.size()); - src.read(buffer); - write(buffer); + src.copyTo(buffer); + copyFrom(buffer); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java index 52b3867..c6d93f2 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/SparseNdArray.java @@ -123,7 +123,7 @@ protected SparseNdArray( super(defaultValue, dimensions); this.type = type; // use write to set up the indices and values - write(dataBuffer); + copyFrom(dataBuffer); } /** @@ -266,7 +266,7 @@ public static > SparseNdArray create( */ public static > SparseNdArray create(Class type, U src) { DataBuffer buffer = DataBuffers.ofObjects(type, src.size()); - src.read(buffer); + src.copyTo(buffer); return new SparseNdArray<>(type, buffer, DimensionalSpace.create(src.shape())); } /** @@ -279,7 +279,7 @@ public static > SparseNdArray create(Class type public static > SparseNdArray create( Class type, U src, T defaultValue) { DataBuffer buffer = DataBuffers.ofObjects(type, src.size()); - src.read(buffer); + src.copyTo(buffer); return new SparseNdArray<>(type, buffer, defaultValue, DimensionalSpace.create(src.shape())); } @@ -312,7 +312,7 @@ public U slice(long position, DimensionalSpace sliceDimensions) { /** {@inheritDoc} */ @Override - public NdArray read(DataBuffer dst) { + public NdArray copyTo(DataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values @SuppressWarnings("unchecked") T[] defaults = (T[]) Array.newInstance(type, (int) dst.size()); @@ -336,7 +336,7 @@ public NdArray read(DataBuffer dst) { @SuppressWarnings({ "unchecked", }) - public NdArray write(DataBuffer src) { + public NdArray copyFrom(DataBuffer src) { List indices = new ArrayList<>(); List values = new ArrayList<>(); @@ -368,7 +368,7 @@ public NdArray write(DataBuffer src) { @SuppressWarnings("unchecked") public U toDense() { DataBuffer dataBuffer = DataBuffers.ofObjects(type, shape().size()); - read(dataBuffer); + copyTo(dataBuffer); // unchecked cast, suppressed. return (U) NdArrays.wrap(shape(), dataBuffer); } @@ -381,8 +381,8 @@ public U toDense() { */ public NdArray fromDense(NdArray src) { DataBuffer buffer = DataBuffers.ofObjects(type, src.size()); - src.read(buffer); - write(buffer); + src.copyTo(buffer); + copyFrom(buffer); return this; } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/BooleanSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/BooleanSparseSlice.java index 24283db..945ccdc 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/BooleanSparseSlice.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/BooleanSparseSlice.java @@ -49,7 +49,7 @@ public BooleanSparseSlice( @Override public BooleanNdArray toDense() { BooleanDataBuffer dataBuffer = DataBuffers.ofBooleans(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -75,7 +75,7 @@ public BooleanNdArray set(NdArray src, long... coordinates) { /** {@inheritDoc} */ @Override - public BooleanNdArray read(DataBuffer dst) { + public BooleanNdArray copyTo(DataBuffer dst) { // zero out buf. Boolean[] defaults = new Boolean[(int) shape().size()]; dst.write(defaults); @@ -93,17 +93,17 @@ public BooleanNdArray read(DataBuffer dst) { } @Override - public BooleanNdArray read(BooleanDataBuffer dst) { - return read((DataBuffer) dst); + public BooleanNdArray copyTo(BooleanDataBuffer dst) { + return copyTo((DataBuffer) dst); } @Override - public BooleanNdArray write(DataBuffer src) { + public BooleanNdArray copyFrom(DataBuffer src) { throw new ReadOnlyBufferException(); } @Override - public BooleanNdArray write(BooleanDataBuffer src) { + public BooleanNdArray copyFrom(BooleanDataBuffer src) { throw new ReadOnlyBufferException(); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ByteSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ByteSparseSlice.java index a8ced3b..35e33d9 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ByteSparseSlice.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ByteSparseSlice.java @@ -48,7 +48,7 @@ public ByteSparseSlice( @Override public ByteNdArray toDense() { ByteDataBuffer dataBuffer = DataBuffers.ofBytes(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -74,7 +74,7 @@ public ByteNdArray set(NdArray src, long... coordinates) { /** {@inheritDoc} */ @Override - public ByteNdArray read(DataBuffer dst) { + public ByteNdArray copyTo(DataBuffer dst) { // zero out buf. Byte[] defaults = new Byte[(int) shape().size()]; dst.write(defaults); @@ -92,17 +92,17 @@ public ByteNdArray read(DataBuffer dst) { } @Override - public ByteNdArray read(ByteDataBuffer dst) { - return read((DataBuffer) dst); + public ByteNdArray copyTo(ByteDataBuffer dst) { + return this.copyTo((DataBuffer) dst); } @Override - public ByteNdArray write(DataBuffer src) { + public ByteNdArray copyFrom(DataBuffer src) { throw new ReadOnlyBufferException(); } @Override - public ByteNdArray write(ByteDataBuffer src) { + public ByteNdArray copyFrom(ByteDataBuffer src) { throw new ReadOnlyBufferException(); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/DoubleSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/DoubleSparseSlice.java index 596be18..3f66308 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/DoubleSparseSlice.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/DoubleSparseSlice.java @@ -49,7 +49,7 @@ public DoubleSparseSlice( @Override public DoubleNdArray toDense() { DoubleDataBuffer dataBuffer = DataBuffers.ofDoubles(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -75,7 +75,7 @@ public DoubleNdArray set(NdArray src, long... coordinates) { /** {@inheritDoc} */ @Override - public DoubleNdArray read(DataBuffer dst) { + public DoubleNdArray copyTo(DataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Double[] defaults = new Double[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -94,17 +94,17 @@ public DoubleNdArray read(DataBuffer dst) { } @Override - public DoubleNdArray read(DoubleDataBuffer dst) { - return read((DataBuffer) dst); + public DoubleNdArray copyTo(DoubleDataBuffer dst) { + return this.copyTo((DataBuffer) dst); } @Override - public DoubleNdArray write(DataBuffer src) { + public DoubleNdArray copyFrom(DataBuffer src) { throw new ReadOnlyBufferException(); } @Override - public DoubleNdArray write(DoubleDataBuffer src) { + public DoubleNdArray copyFrom(DoubleDataBuffer src) { throw new ReadOnlyBufferException(); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/FloatSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/FloatSparseSlice.java index 5e6094d..8e2204d 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/FloatSparseSlice.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/FloatSparseSlice.java @@ -49,7 +49,7 @@ public FloatSparseSlice( @Override public FloatNdArray toDense() { FloatDataBuffer dataBuffer = DataBuffers.ofFloats(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -75,7 +75,7 @@ public FloatNdArray set(NdArray src, long... coordinates) { /** {@inheritDoc} */ @Override - public FloatNdArray read(DataBuffer dst) { + public FloatNdArray copyTo(DataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Float[] defaults = new Float[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -94,17 +94,17 @@ public FloatNdArray read(DataBuffer dst) { } @Override - public FloatNdArray read(FloatDataBuffer dst) { - return read((DataBuffer) dst); + public FloatNdArray copyTo(FloatDataBuffer dst) { + return this.copyTo((DataBuffer) dst); } @Override - public FloatNdArray write(DataBuffer src) { + public FloatNdArray copyFrom(DataBuffer src) { throw new ReadOnlyBufferException(); } @Override - public FloatNdArray write(FloatDataBuffer src) { + public FloatNdArray copyFrom(FloatDataBuffer src) { throw new ReadOnlyBufferException(); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/IntSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/IntSparseSlice.java index 067c64b..988551b 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/IntSparseSlice.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/IntSparseSlice.java @@ -49,7 +49,7 @@ public IntSparseSlice( @Override public IntNdArray toDense() { IntDataBuffer dataBuffer = DataBuffers.ofInts(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -75,7 +75,7 @@ public IntNdArray set(NdArray src, long... coordinates) { /** {@inheritDoc} */ @Override - public IntNdArray read(DataBuffer dst) { + public IntNdArray copyTo(DataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Integer[] defaults = new Integer[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -94,17 +94,17 @@ public IntNdArray read(DataBuffer dst) { } @Override - public IntNdArray read(IntDataBuffer dst) { - return read((DataBuffer) dst); + public IntNdArray copyTo(IntDataBuffer dst) { + return this.copyTo((DataBuffer) dst); } @Override - public IntNdArray write(DataBuffer src) { + public IntNdArray copyFrom(DataBuffer src) { throw new ReadOnlyBufferException(); } @Override - public IntNdArray write(IntDataBuffer src) { + public IntNdArray copyFrom(IntDataBuffer src) { throw new ReadOnlyBufferException(); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/LongSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/LongSparseSlice.java index ea182b7..0916293 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/LongSparseSlice.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/LongSparseSlice.java @@ -49,7 +49,7 @@ public LongSparseSlice( @Override public LongNdArray toDense() { LongDataBuffer dataBuffer = DataBuffers.ofLongs(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -75,7 +75,7 @@ public LongNdArray set(NdArray src, long... coordinates) { /** {@inheritDoc} */ @Override - public LongNdArray read(DataBuffer dst) { + public LongNdArray copyTo(DataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Long[] defaults = new Long[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -94,17 +94,17 @@ public LongNdArray read(DataBuffer dst) { } @Override - public LongNdArray read(LongDataBuffer dst) { - return read((DataBuffer) dst); + public LongNdArray copyTo(LongDataBuffer dst) { + return copyTo((DataBuffer) dst); } @Override - public LongNdArray write(DataBuffer src) { + public LongNdArray copyFrom(DataBuffer src) { throw new ReadOnlyBufferException(); } @Override - public LongNdArray write(LongDataBuffer src) { + public LongNdArray copyFrom(LongDataBuffer src) { throw new ReadOnlyBufferException(); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ObjectSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ObjectSparseSlice.java index be7f9f4..9f62bf8 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ObjectSparseSlice.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ObjectSparseSlice.java @@ -48,7 +48,7 @@ public ObjectSparseSlice( @SuppressWarnings("unchecked") public U toDense() { DataBuffer dataBuffer = DataBuffers.ofObjects(getType(), shape().size()); - read(dataBuffer); + copyTo(dataBuffer); // unchecked NdArray to U return (U) NdArrays.wrap(shape(), dataBuffer); } @@ -66,7 +66,7 @@ public U set(NdArray src, long... coordinates) { /** {@inheritDoc} */ @Override @SuppressWarnings("unchecked") - public U read(DataBuffer dst) { + public U copyTo(DataBuffer dst) { // unchecked Object to T[] T[] defaults = (T[]) Array.newInstance(getType(), (int) dst.size()); Arrays.fill(defaults, getDefaultValue()); diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ShortSparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ShortSparseSlice.java index 16aa561..2da0f8c 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ShortSparseSlice.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/ShortSparseSlice.java @@ -49,7 +49,7 @@ public ShortSparseSlice( @Override public ShortNdArray toDense() { ShortDataBuffer dataBuffer = DataBuffers.ofShorts(shape().size()); - read(dataBuffer); + copyTo(dataBuffer); return NdArrays.wrap(shape(), dataBuffer); } @@ -75,7 +75,7 @@ public ShortNdArray set(NdArray src, long... coordinates) { /** {@inheritDoc} */ @Override - public ShortNdArray read(DataBuffer dst) { + public ShortNdArray copyTo(DataBuffer dst) { // set the values in buf to the default, then overwrite with indices/values Short[] defaults = new Short[(int) shape().size()]; Arrays.fill(defaults, getDefaultValue()); @@ -94,17 +94,17 @@ public ShortNdArray read(DataBuffer dst) { } @Override - public ShortNdArray read(ShortDataBuffer dst) { - return read((DataBuffer) dst); + public ShortNdArray copyTo(ShortDataBuffer dst) { + return this.copyTo((DataBuffer) dst); } @Override - public ShortNdArray write(DataBuffer src) { + public ShortNdArray copyFrom(DataBuffer src) { throw new ReadOnlyBufferException(); } @Override - public ShortNdArray write(ShortDataBuffer src) { + public ShortNdArray copyFrom(ShortDataBuffer src) { throw new ReadOnlyBufferException(); } diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/SparseSlice.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/SparseSlice.java index e0db839..3e5be6f 100644 --- a/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/SparseSlice.java +++ b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/slice/SparseSlice.java @@ -133,7 +133,7 @@ public NdArraySequence elements(int dimensionIdx) { /** {@inheritDoc} */ @Override - public NdArray write(DataBuffer src) { + public NdArray copyFrom(DataBuffer src) { throw new ReadOnlyBufferException(); } diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java b/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java index 577c2b7..36064d2 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java @@ -258,7 +258,7 @@ public void writeAndReadWithBuffers() { buffer.setObject(valueOf(val), val); } NdArray matrix = allocate(Shape.of(3, 5)); - matrix.write(buffer); + matrix.copyFrom(buffer); assertEquals(valueOf(0L), matrix.getObject(0, 0)); assertEquals(valueOf(4L), matrix.getObject(0, 4)); assertEquals(valueOf(5L), matrix.getObject(1, 0)); @@ -266,7 +266,7 @@ public void writeAndReadWithBuffers() { assertEquals(valueOf(14L), matrix.getObject(2, 4)); matrix.setObject(valueOf(100L), 1, 0); - matrix.read(buffer); + matrix.copyTo(buffer); assertEquals(valueOf(0L), buffer.getObject(0)); assertEquals(valueOf(4L), buffer.getObject(4)); assertEquals(valueOf(100L), buffer.getObject(5)); @@ -274,13 +274,13 @@ public void writeAndReadWithBuffers() { assertEquals(valueOf(14L), buffer.getObject(14)); try { - matrix.write(buffer.narrow(10)); + matrix.copyFrom(buffer.narrow(10)); fail(); } catch (BufferUnderflowException e) { // as expected } try { - matrix.read(buffer.narrow(10)); + matrix.copyTo(buffer.narrow(10)); fail(); } catch (BufferOverflowException e) { // as expected diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArrayTest.java index bcb7235..ecd9e85 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArrayTest.java @@ -47,12 +47,12 @@ public void testBasic() { } @Test - public void testRead() { + public void testCopyToBuffer() { BooleanSparseNdArray instance = new BooleanSparseNdArray(indices, values, DimensionalSpace.create(shape)); BooleanDataBuffer dataBuffer = DataBuffers.ofBooleans(instance.shape().size()); - instance.read(dataBuffer); + instance.copyTo(dataBuffer); boolean[] array = new boolean[denseArray.length]; dataBuffer.read(array); @@ -60,12 +60,12 @@ public void testRead() { } @Test - public void testWrite() { + public void testCopyFromBuffer() { BooleanDataBuffer dataBuffer = RawDataBufferFactory.create(denseArray, false); // use a zero buffer BooleanSparseNdArray instance = BooleanSparseNdArray.create(DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -84,7 +84,7 @@ public void testWriteDefaultValue() { // use a zero buffer BooleanSparseNdArray instance = BooleanSparseNdArray.create(true, DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(valuesDefault, instance.getValues()); @@ -274,7 +274,7 @@ public void testCreate() { BooleanDataBuffer dataBuffer = RawDataBufferFactory.create(denseArray, false); // use a zero buffer BooleanSparseNdArray instanceB = BooleanSparseNdArray.create(DimensionalSpace.create(shape)); - instanceB.write(dataBuffer); + instanceB.copyFrom(dataBuffer); assertEquals(instance, instanceB); BooleanSparseNdArray instanceC = diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArrayTest.java index 7dd9df2..efa0572 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/ByteSparseNdArrayTest.java @@ -45,12 +45,12 @@ public void testBasic() { } @Test - public void testRead() { + public void testCopyToBuffer() { ByteSparseNdArray instance = new ByteSparseNdArray(indices, values, DimensionalSpace.create(shape)); ByteDataBuffer dataBuffer = DataBuffers.ofBytes(instance.shape().size()); - instance.read(dataBuffer); + instance.copyTo(dataBuffer); byte[] array = new byte[denseArray.length]; dataBuffer.read(array); @@ -58,12 +58,12 @@ public void testRead() { } @Test - public void testWrite() { + public void testCopyFromBuffer() { ByteDataBuffer dataBuffer = RawDataBufferFactory.create(denseArray, false); // use a zero buffer ByteSparseNdArray instance = ByteSparseNdArray.create(DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -80,7 +80,7 @@ public void testWriteDefaultValue() { // use a zero buffer ByteSparseNdArray instance = ByteSparseNdArray.create((byte) -1, DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -265,7 +265,7 @@ public void testCreate() { ByteDataBuffer dataBuffer = RawDataBufferFactory.create(denseArray, false); // use a zero buffer ByteSparseNdArray instanceB = ByteSparseNdArray.create(DimensionalSpace.create(shape)); - instanceB.write(dataBuffer); + instanceB.copyFrom(dataBuffer); assertEquals(instance, instanceB); ByteSparseNdArray instanceC = diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArrayTest.java index 616863e..f7ff65f 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/DoubleSparseNdArrayTest.java @@ -49,12 +49,12 @@ public void testBasic() { } @Test - public void testRead() { + public void testCopyToBuffer() { DoubleSparseNdArray instance = new DoubleSparseNdArray(indices, values, DimensionalSpace.create(shape)); DoubleDataBuffer dataBuffer = DataBuffers.ofDoubles(instance.shape().size()); - instance.read(dataBuffer); + instance.copyTo(dataBuffer); double[] array = new double[denseArray.length]; dataBuffer.read(array); @@ -62,12 +62,12 @@ public void testRead() { } @Test - public void testWrite() { + public void testCopyFromBuffer() { DoubleDataBuffer dataBuffer = NioDataBufferFactory.create(DoubleBuffer.wrap(denseArray)); // use a zero buffer DoubleSparseNdArray instance = DoubleSparseNdArray.create(DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -80,7 +80,7 @@ public void testWriteDefaultValue() { DoubleDataBuffer dataBuffer = RawDataBufferFactory.create(denseArrayDefaultValue, false); // use a zero buffer DoubleSparseNdArray instance = DoubleSparseNdArray.create(-1d, DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -266,7 +266,7 @@ public void testCreate() { DoubleDataBuffer dataBuffer = RawDataBufferFactory.create(denseArray, false); // use a zero buffer DoubleSparseNdArray instanceB = DoubleSparseNdArray.create(DimensionalSpace.create(shape)); - instanceB.write(dataBuffer); + instanceB.copyFrom(dataBuffer); assertEquals(instance, instanceB); DoubleSparseNdArray instanceC = diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArrayTest.java index f5f6ba4..d948d1e 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/FloatSparseNdArrayTest.java @@ -46,12 +46,12 @@ public void testBasic() { } @Test - public void testRead() { + public void testCopyToBuffer() { FloatSparseNdArray instance = new FloatSparseNdArray(indices, values, DimensionalSpace.create(shape)); FloatDataBuffer dataBuffer = DataBuffers.ofFloats(instance.shape().size()); - instance.read(dataBuffer); + instance.copyTo(dataBuffer); float[] array = new float[denseArray.length]; dataBuffer.read(array); @@ -59,12 +59,12 @@ public void testRead() { } @Test - public void testWrite() { + public void testCopyFromBuffer() { FloatDataBuffer dataBuffer = NioDataBufferFactory.create(FloatBuffer.wrap(denseArray)); // use a zero buffer FloatSparseNdArray instance = FloatSparseNdArray.create(DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -80,7 +80,7 @@ public void testWriteDefaultValue() { FloatDataBuffer dataBuffer = RawDataBufferFactory.create(denseArrayDefaultValue, false); // use a zero buffer FloatSparseNdArray instance = FloatSparseNdArray.create(-1f, DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -274,7 +274,7 @@ public void testCreate() { FloatDataBuffer dataBuffer = RawDataBufferFactory.create(denseArray, false); // use a zero buffer FloatSparseNdArray instanceB = FloatSparseNdArray.create(DimensionalSpace.create(shape)); - instanceB.write(dataBuffer); + instanceB.copyFrom(dataBuffer); assertEquals(instance, instanceB); FloatSparseNdArray instanceC = diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArrayTest.java index 8492493..b60b7db 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/IntSparseNdArrayTest.java @@ -47,12 +47,12 @@ public void testBasic() { } @Test - public void testRead() { + public void testCopyToBuffer() { IntSparseNdArray instance = new IntSparseNdArray(indices, values, DimensionalSpace.create(shape)); IntDataBuffer dataBuffer = DataBuffers.ofInts(instance.shape().size()); - instance.read(dataBuffer); + instance.copyTo(dataBuffer); int[] array = new int[denseArray.length]; dataBuffer.read(array); @@ -60,12 +60,12 @@ public void testRead() { } @Test - public void testWrite() { + public void testCopyFromBufferBuffer() { IntDataBuffer dataBuffer = NioDataBufferFactory.create(IntBuffer.wrap(denseArray)); // use a zero buffer IntSparseNdArray instance = IntSparseNdArray.create(DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -79,7 +79,7 @@ public void testWriteDefaultValue() { IntDataBuffer dataBuffer = RawDataBufferFactory.create(denseArrayDefaultValue, false); // use a zero buffer IntSparseNdArray instance = IntSparseNdArray.create(-1, DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -272,7 +272,7 @@ public void testCreate() { IntDataBuffer dataBuffer = RawDataBufferFactory.create(denseArray, false); // use a zero buffer IntSparseNdArray instanceB = IntSparseNdArray.create(DimensionalSpace.create(shape)); - instanceB.write(dataBuffer); + instanceB.copyFrom(dataBuffer); assertEquals(instance, instanceB); IntSparseNdArray instanceC = diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArrayTest.java index 0a4b6c6..7f9a136 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/LongSparseNdArrayTest.java @@ -46,12 +46,12 @@ public void testBasic() { } @Test - public void testRead() { + public void testCopyToBuffer() { LongSparseNdArray instance = new LongSparseNdArray(indices, values, DimensionalSpace.create(shape)); LongDataBuffer dataBuffer = DataBuffers.ofLongs(instance.shape().size()); - instance.read(dataBuffer); + instance.copyTo(dataBuffer); long[] array = new long[denseArray.length]; dataBuffer.read(array); @@ -59,12 +59,12 @@ public void testRead() { } @Test - public void testWrite() { + public void testCopyFromBuffer() { LongDataBuffer dataBuffer = NioDataBufferFactory.create(LongBuffer.wrap(denseArray)); // use a zero buffer LongSparseNdArray instance = LongSparseNdArray.create(DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -78,7 +78,7 @@ public void testWriteDefaultValue() { LongDataBuffer dataBuffer = RawDataBufferFactory.create(denseArrayDefaultValue, false); // use a zero buffer LongSparseNdArray instance = LongSparseNdArray.create(-1L, DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -271,7 +271,7 @@ public void testCreate() { LongDataBuffer dataBuffer = RawDataBufferFactory.create(denseArray, false); // use a zero buffer LongSparseNdArray instanceB = LongSparseNdArray.create(DimensionalSpace.create(shape)); - instanceB.write(dataBuffer); + instanceB.copyFrom(dataBuffer); assertEquals(instance, instanceB); LongSparseNdArray instanceC = diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArrayTest.java index cec1358..5f85413 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/ShortSparseNdArrayTest.java @@ -45,12 +45,12 @@ public void testBasic() { } @Test - public void testRead() { + public void testCopyToBuffer() { ShortSparseNdArray instance = new ShortSparseNdArray(indices, values, DimensionalSpace.create(shape)); ShortDataBuffer dataBuffer = DataBuffers.ofShorts(instance.shape().size()); - instance.read(dataBuffer); + instance.copyTo(dataBuffer); short[] array = new short[denseArray.length]; dataBuffer.read(array); @@ -58,12 +58,12 @@ public void testRead() { } @Test - public void testWrite() { + public void testCopyFromBuffer() { ShortDataBuffer dataBuffer = NioDataBufferFactory.create(ShortBuffer.wrap(denseArray)); // use a zero buffer ShortSparseNdArray instance = ShortSparseNdArray.create(DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -81,7 +81,7 @@ public void testWriteDefaultValue() { // use a zero buffer ShortSparseNdArray instance = ShortSparseNdArray.create((short) -1, DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -275,7 +275,7 @@ public void testCreate() { ShortDataBuffer dataBuffer = RawDataBufferFactory.create(denseArray, false); // use a zero buffer ShortSparseNdArray instanceB = ShortSparseNdArray.create(DimensionalSpace.create(shape)); - instanceB.write(dataBuffer); + instanceB.copyFrom(dataBuffer); assertEquals(instance, instanceB); ShortSparseNdArray instanceC = diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/StringSparseNdArrayTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/StringSparseNdArrayTest.java index 152a13e..a93cd45 100644 --- a/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/StringSparseNdArrayTest.java +++ b/ndarray/src/test/java/org/tensorflow/ndarray/impl/sparse/StringSparseNdArrayTest.java @@ -64,12 +64,12 @@ public void testBasic() { } @Test - public void testRead() { + public void testCopyToBuffer() { SparseNdArray> instance = new SparseNdArray<>(String.class, indices, values, DimensionalSpace.create(shape)); DataBuffer dataBuffer = DataBuffers.ofObjects(String.class, instance.shape().size()); - instance.read(dataBuffer); + instance.copyTo(dataBuffer); String[] array = new String[denseArray.length]; dataBuffer.read(array); @@ -77,13 +77,13 @@ public void testRead() { } @Test - public void testWrite() { + public void testCopyFromBuffer() { DataBuffer dataBuffer = DataBuffers.ofObjects(denseArray); // use a zero buffer SparseNdArray> instance = SparseNdArray.create(String.class, DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -103,7 +103,7 @@ public void testWriteDefaultValue() { // use a zero buffer SparseNdArray> instance = SparseNdArray.create(String.class, defaultValue, DimensionalSpace.create(shape)); - instance.write(dataBuffer); + instance.copyFrom(dataBuffer); assertEquals(indices, instance.getIndices()); assertEquals(values, instance.getValues()); @@ -282,7 +282,7 @@ public void testCreate() { // use a zero buffer SparseNdArray> instanceB = SparseNdArray.create(String.class, DimensionalSpace.create(shape)); - instanceB.write(dataBuffer); + instanceB.copyFrom(dataBuffer); assertEquals(instance, instanceB); SparseNdArray> instanceC = From 569f45aeda395748ad1068cd8ddf907c22f15975 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Tue, 20 Feb 2024 08:19:42 -0500 Subject: [PATCH 24/26] Releasing 1.0.0-rc.1 --- README.md | 2 +- RELEASE.md | 179 ++++++++++++++++++++++++++++++++++++++++++++++++ ndarray/pom.xml | 2 +- pom.xml | 31 +++++---- release.sh | 2 +- 5 files changed, 199 insertions(+), 17 deletions(-) create mode 100644 RELEASE.md diff --git a/README.md b/README.md index 780d50c..45b4956 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To import the NdArray library in your project, simply add the following dependen org.tensorflow ndarray - 0.4.0 + 1.0.0-rc.1 ``` diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..264933a --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,179 @@ +# Releasing NdArray Java Library + +The +[NdArray Java Library](https://github.com/tensorflow/java-ndarray) is available on Maven Central and JCenter +through artifacts uploaded to +[OSS Sonatype](https://oss.sonatype.org/content/repositories/releases/org/tensorflow/). This +document describes the process of updating the release artifacts. It does _not_ describe how to use +the artifacts, for which the reader is referred to the +[NdArray Java Library installation instructions](https://github.com/tensorflow/java-ndarray/blob/main/README.md). + +## Release process overview + +NdArray Java Library must be conducted locally in a [Docker](https://www.docker.com) container for a hermetic release process. + +It is important to note that any change pushed to a release branch (i.e. a branch prefixed +by `r`) will start a new release workflow. Therefore, these changes should always increment the +version number. + +### Pre-requisites + +- `docker` +- An account at [oss.sonatype.org](https://oss.sonatype.org/), that has + permissions to update artifacts in the `org.tensorflow` group. If your + account does not have permissions, then you'll need to ask someone who does + to [file a ticket](https://issues.sonatype.org/) to add to the permissions + ([sample ticket](https://issues.sonatype.org/browse/MVNCENTRAL-1637)). +- A GPG signing key, required + [to sign the release artifacts](http://central.sonatype.org/pages/apache-maven.html#gpg-signed-components). + +### Preparing a release + +#### Major or minor release + +1. Get a clean version of the source code by cloning the + [NdArray Java Library GitHub repository](https://github.com/tensorflow/java-ndarray) + ``` + git clone https://github.com/tensorflow/java-ndarray + ``` +2. Create a new branch for the release named `r.` + ``` + git checkout -b r1.0 + ``` +3. Update the version of the Maven artifacts to the full version of the release + ``` + mvn versions:set -DnewVersion=1.0.0 + ``` +4. Update the NdArray Java Library version to reflect the new release at the following locations: + - https://github.com/tensorflow/java-ndarray/blob/main/README.md#introduction + +5. Commit the changes and push the branch to the GitHub repository + ``` + git add . + git commit -m "Releasing 1.0.0" + git push --set-upstream origin r1.0 + ``` + +#### Patch release + +1. Get a clean version of the source code by cloning the + [NdArray Java Library GitHub repository](https://github.com/tensorflow/java-ndarray) + ``` + git clone https://github.com/tensorflow/java-ndarray + ``` +2. Switch to the release branch of the version to patch + ``` + git checkout r1.0 + ``` +3. Patch the code with your changes. For example, changes could be merged from another branch you + were working on or be applied directly to this branch when the required changes are minimal. + +4. Update the version of the Maven artifacts to the full version of the release + ``` + mvn versions:set -DnewVersion=1.0.1 + ``` +5. Update the NdArray Java Library version to reflect the new release at the following locations: + - https://github.com/tensorflow/java-ndarray/blob/main/README.md#introduction + +6. Commit the changes and push the branch to the GitHub repository + ``` + git add . + git commit -m "Releasing 1.0.1" + git push + ``` + +### Performing the release + +1. At the root of your repository copy, create a Maven settings.xml file with your OSSRH credentials and + your GPG key passphrase: + ```sh + SONATYPE_USERNAME="your_sonatype.org_username_here" + SONATYPE_PASSWORD="your_sonatype.org_password_here" + GPG_PASSPHRASE="your_gpg_passphrase_here" + cat > settings.xml < + + + ossrh + ${SONATYPE_USERNAME} + ${SONATYPE_PASSWORD} + + + ossrh-staging + ${SONATYPE_USERNAME} + ${SONATYPE_PASSWORD} + + + + + + true + + + gpg2 + ${GPG_PASSPHRASE} + + + + + EOF + ``` +2. Execute the `release.sh` script. This will sign and deploy artifacts on OSS Sonatype. + + On success, the released artifacts are uploaded to the private staging repository in OSS Sonatype (check at the Maven + build output to know the exact location of the staging repository). After inspecting the artifacts in OSS Sonatype, you + should release or drop them. + + Visit https://oss.sonatype.org/#stagingRepositories, find the `orgtensorflow-*` + of your release and click `Close` and `Release` to finalize the release. You always have the option + to `Drop` it to abort and restart if something went wrong. + +3. Go to GitHub and create a release tag on the release branch with a summary of what the version includes. + +Some things of note: + - For details, look at the [Sonatype guide](http://central.sonatype.org/pages/releasing-the-deployment.html). + - Syncing with [Maven Central](http://repo1.maven.org/maven2/org/tensorflow/) + can take 10 minutes to 2 hours (as per the [OSSRH guide](http://central.sonatype.org/pages/ossrh-guide.html#releasing-to-central)). + +### Finishing a release + +#### Major or minor release + +1. Checkout the main branch and merge back changes from the released branch + ``` + git checkout main + git merge r1.0 + ``` +2. In your local copy, checkout the main branch and increase the next snapshot version. + ``` + mvn versions:set -DnewVersion=1.1.0-SNAPSHOT + ``` +3. Update the NdArray Java Library version to reflect the new release at the following locations: + - https://github.com/tensorflow/java-ndarray/blob/main/README.md#introduction + +4. Commit your changes and push the main branch to the GitHub repository + ``` + git add . + git commit -m "Increase version for next iteration" + git push + ``` + +#### Patch release + +1. Checkout the main branch and merge back changes from the released branch, preserving current snapshot version + ``` + git checkout main + git merge r1.0 + ``` +2. Commit the main and push the main branch to the GitHub repository + ``` + git add . + git commit -m "Merge release 1.0.1" + git push + ``` + +## References + +- [Sonatype guide](http://central.sonatype.org/pages/ossrh-guide.html) for + hosting releases. +- [Ticket that created the `org/tensorflow` configuration](https://issues.sonatype.org/browse/OSSRH-28072) on OSSRH. diff --git a/ndarray/pom.xml b/ndarray/pom.xml index 9966a35..c7407f1 100644 --- a/ndarray/pom.xml +++ b/ndarray/pom.xml @@ -22,7 +22,7 @@ org.tensorflow tensorflow-java-ndarray - 0.5.0-SNAPSHOT + 1.0.0-rc.1 ndarray jar diff --git a/pom.xml b/pom.xml index ce70d66..ad8e4cb 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.tensorflow tensorflow-java-ndarray - 0.5.0-SNAPSHOT + 1.0.0-rc.1 pom NdArray Parent @@ -56,6 +56,7 @@ + ossrh-snapshots @@ -125,19 +126,21 @@ false - - - ossrh-staging - OSSRH Sonatype Staging - https://oss.sonatype.org/service/local/staging/deployByRepositoryId/${stagingRepositoryId}/ - - true - - - false - - - + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.8 + true + + ossrh + https://oss.sonatype.org/ + false + + + + diff --git a/release.sh b/release.sh index ce3aed8..2addcdd 100755 --- a/release.sh +++ b/release.sh @@ -27,7 +27,7 @@ CMD="$*" # To get a shell to poke around the maven artifacts with. if [[ -z "${CMD}" ]] then - CMD="mvn clean deploy -B -e --settings ./settings.xml -Pdeploying -Preleasing" + CMD="mvn clean deploy -B -e --settings ./settings.xml -Preleasing" fi export GPG_TTY=$(tty) From 1124b7337d2c079ab77985abcd2d9b620d7c2742 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Tue, 20 Feb 2024 21:28:17 -0500 Subject: [PATCH 25/26] Increase version for next iteration --- RELEASE.md | 4 +--- ndarray/pom.xml | 2 +- pom.xml | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index 264933a..30de621 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -148,10 +148,8 @@ Some things of note: ``` mvn versions:set -DnewVersion=1.1.0-SNAPSHOT ``` -3. Update the NdArray Java Library version to reflect the new release at the following locations: - - https://github.com/tensorflow/java-ndarray/blob/main/README.md#introduction -4. Commit your changes and push the main branch to the GitHub repository +3. Commit your changes and push the main branch to the GitHub repository ``` git add . git commit -m "Increase version for next iteration" diff --git a/ndarray/pom.xml b/ndarray/pom.xml index c7407f1..543cf70 100644 --- a/ndarray/pom.xml +++ b/ndarray/pom.xml @@ -22,7 +22,7 @@ org.tensorflow tensorflow-java-ndarray - 1.0.0-rc.1 + 1.1.0-SNAPSHOT ndarray jar diff --git a/pom.xml b/pom.xml index ad8e4cb..104f431 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.tensorflow tensorflow-java-ndarray - 1.0.0-rc.1 + 1.1.0-SNAPSHOT pom NdArray Parent From dc83b8254ff108a487f2e27d3c293425471bb6e0 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Fri, 10 May 2024 15:13:53 -0400 Subject: [PATCH 26/26] Update version in Maven example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 45b4956..dfe4822 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To import the NdArray library in your project, simply add the following dependen org.tensorflow ndarray - 1.0.0-rc.1 + 1.0.0 ```