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/.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 991886f..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 - 0.3.2 + 1.0.0 ``` @@ -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)); ``` @@ -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/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..30de621 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,177 @@ +# 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. 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 3a8ba71..543cf70 100644 --- a/ndarray/pom.xml +++ b/ndarray/pom.xml @@ -22,7 +22,7 @@ org.tensorflow tensorflow-java-ndarray - 0.4.0-SNAPSHOT + 1.1.0-SNAPSHOT ndarray jar @@ -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..6b33ed6 --- /dev/null +++ b/ndarray/src/main/java/module-info.java @@ -0,0 +1,38 @@ +/* + 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 { + 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; + 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; + 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; +} diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/BooleanNdArray.java index 5b4bedb..a762896 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); @@ -97,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 0e6f118..7a6f0cd 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); @@ -97,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 80e99b0..d8d2312 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,21 @@ 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 withShape(Shape shape); + @Override DoubleNdArray slice(Index... indices); @@ -97,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 8d4fbf5..98ec87e 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); @@ -97,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 aa2cc65..b165900 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,21 @@ 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 withShape(Shape shape); + @Override IntNdArray slice(Index... indices); @@ -97,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 3e5be6d..5d5766a 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,21 @@ 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 withShape(Shape shape); + @Override LongNdArray slice(Index... indices); @@ -97,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 6686abd..da34fd3 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. @@ -16,11 +16,14 @@ */ package org.tensorflow.ndarray; -import java.util.function.BiConsumer; -import java.util.function.Consumer; 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; + /** * A data structure of N-dimensions. * @@ -98,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. @@ -229,6 +258,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. * @@ -244,33 +285,39 @@ public interface NdArray extends Shaped { 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/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. diff --git a/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/ShortNdArray.java index f9335b4..022ccf7 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); @@ -97,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/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/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..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 @@ -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 @@ -89,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; } @@ -132,13 +144,22 @@ 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); } 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..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; } @@ -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..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; } @@ -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..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; } @@ -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..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; } @@ -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..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; } @@ -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..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; } @@ -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..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; } @@ -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 daffad9..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) { @@ -402,6 +407,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/BooleanSparseNdArray.java b/ndarray/src/main/java/org/tensorflow/ndarray/impl/sparse/BooleanSparseNdArray.java index f9e41bf..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 @@ -63,7 +63,7 @@ public class BooleanSparseNdArray extends AbstractSparseNdArray 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 540c79f..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 @@ -63,7 +63,7 @@ public class ByteSparseNdArray 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); } /** @@ -118,7 +118,7 @@ public class ByteSparseNdArray extends AbstractSparseNdArray 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 27a0832..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 @@ -63,7 +63,7 @@ public class DoubleSparseNdArray extends AbstractSparseNdArray 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 11a3b05..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 @@ -63,7 +63,7 @@ public class FloatSparseNdArray extends AbstractSparseNdArray 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 f822a42..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 @@ -63,7 +63,7 @@ public class IntSparseNdArray 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); } /** @@ -116,7 +116,7 @@ public class IntSparseNdArray extends AbstractSparseNdArray 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 3385022..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 @@ -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); } /** @@ -117,7 +117,7 @@ public class LongSparseNdArray extends AbstractSparseNdArray 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 50793b9..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 @@ -63,7 +63,7 @@ public class ShortSparseNdArray extends AbstractSparseNdArray 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 2c69fc9..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 @@ -63,7 +63,7 @@ public class SparseNdArray> 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); } /** @@ -123,7 +123,7 @@ public class SparseNdArray> extends AbstractSparseNdArra 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; } @@ -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/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/module-info.test b/ndarray/src/test/java/module-info.test new file mode 100644 index 0000000..310e500 --- /dev/null +++ b/ndarray/src/test/java/module-info.test @@ -0,0 +1,22 @@ +/* + 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 { + requires java.desktop; // required for java.awt.* + + requires transitive org.junit.jupiter.engine; + requires transitive org.junit.jupiter.api; +} diff --git a/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java b/ndarray/src/test/java/org/tensorflow/ndarray/IndexTest.java index 6f92dab..10c0806 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,371 @@ 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)| + */ + 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); + 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)); + 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]; + 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)); + + + // 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 public void testNewaxis(){ 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..36064d2 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; @@ -257,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)); @@ -265,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)); @@ -273,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 @@ -358,4 +359,56 @@ 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); + } + + @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))); + } } 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/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 437c9dc..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 @@ -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; @@ -48,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); @@ -61,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()); @@ -79,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()); @@ -265,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 = @@ -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/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 40d6597..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 @@ -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; @@ -63,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); @@ -76,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()); @@ -102,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()); @@ -281,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 = @@ -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()); + } } diff --git a/pom.xml b/pom.xml index 867038c..104f431 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.tensorflow tensorflow-java-ndarray - 0.4.0-SNAPSHOT + 1.1.0-SNAPSHOT pom NdArray Parent @@ -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 @@ -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 @@ -56,6 +56,7 @@ + ossrh-snapshots @@ -82,7 +83,7 @@ ossrh - https://oss.sonatype.org/service/local/staging/deployByRepositoryId/${stagingRepositoryId}/ + https://oss.sonatype.org/service/local/staging/deploy/maven2/ @@ -125,27 +126,29 @@ 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 + + + + - jdk11 + jdk17 - 11 - 11 - 11 + 17 + 17 + 17 @@ -156,8 +159,6 @@ lint - - (1.9,) !lint.skip @@ -169,7 +170,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.0 + 3.9.0 true true @@ -273,7 +274,15 @@ true + + maven-release-plugin + 3.0.0-M5 + + SemVerVersionPolicy + + + diff --git a/release.sh b/release.sh new file mode 100755 index 0000000..2addcdd --- /dev/null +++ b/release.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# 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. +# 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 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 -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 \ + ${CMD} + +echo +echo "Release completed"