|
| 1 | +/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | +package org.tensorflow; |
| 16 | + |
| 17 | +import org.bytedeco.javacpp.PointerScope; |
| 18 | +import org.tensorflow.types.TInt64; |
| 19 | +import org.tensorflow.types.family.TType; |
| 20 | + |
| 21 | +/** |
| 22 | + * A virtual type of {@link Tensor} composed of three dense tensors (indices, values and dimensions) |
| 23 | + * used to represent the sparse data into a multi-dimensional dense space. |
| 24 | + * |
| 25 | + * <p>Any tensor returned by a sparse tensor factory (e.g. {@link TInt64#sparseTensorOf(TInt64, |
| 26 | + * TInt64, TInt64)}) can be casted back to this interface to access directly the dense tensors it is |
| 27 | + * composed of. |
| 28 | + * |
| 29 | + * <p>A sparse tensor will keep strong references to its dense tensors to prevent them to be |
| 30 | + * released before it is closed itself. Likewise, closing a sparse tensor won't release the memory |
| 31 | + * of its dense tensors until they in turn are closed. It is then important to protect not only the |
| 32 | + * dense tensors within a <i>try-with-resource</i> block but the sparse tensor itself. |
| 33 | + * |
| 34 | + * <p>For example, this code is perfectly safe: |
| 35 | + * |
| 36 | + * <pre>{@code |
| 37 | + * TFloat64 createSparseTensor() { |
| 38 | + * try (TInt64 indices = TInt64.tensorOf(...); |
| 39 | + * TFloat64 values = TFloat64.vectorOf(...); |
| 40 | + * TInt64 denseShape = TInt64.vectorOf(...)) { |
| 41 | + * return TFloat64.sparseTensorOf(indices, values, denseShape); |
| 42 | + * } |
| 43 | + * } |
| 44 | + * try (TFloat64 sparseTensor = createSparseTensor()) { |
| 45 | + * ... |
| 46 | + * } |
| 47 | + * }</pre> |
| 48 | + * |
| 49 | + * @param <T> type of data stored in the tensor |
| 50 | + */ |
| 51 | +public interface SparseTensor<T extends TType> extends Tensor { |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a sparse tensor from {@code indices}, {@code values} and {@code denseShape} dense |
| 55 | + * tensors. |
| 56 | + * |
| 57 | + * @param indices A 2-D tensor of shape {@code [N, ndims]}, that specifies the indices of the |
| 58 | + * elements in the sparse tensor that contain non-default values (elements are zero-indexed). |
| 59 | + * For example, {@code indices=[[1,3,1], [2,4,0]]} specifies that the elements with indexes of |
| 60 | + * {@code [1,3,1]} and {@code [2,4,0]} have non-default values. |
| 61 | + * @param values A 1-D tensor of shape {@code [N]}, which supplies the values for each element in |
| 62 | + * indices. For example, given {@code indices=[[1,3,1], [2,4,0]]}, the parameter {@code |
| 63 | + * values=[18, 3.8]} specifies that element {@code [1,3,1]} of the sparse tensor has a value |
| 64 | + * of {@code 18}, and element {@code [2,4,0]} of the tensor has a value of {@code 3.8}. |
| 65 | + * @param denseShape A 1-D tensor of shape {@code [ndims]} where each the value at index {@code i} |
| 66 | + * represents the size of dimension {@code i} in a dense version of that tensor. |
| 67 | + * @return the new sparse tensor |
| 68 | + * @throws IllegalArgumentException if shapes of the dense tensors are not compatible |
| 69 | + */ |
| 70 | + static <T extends TType> SparseTensor<T> of(TInt64 indices, T values, TInt64 denseShape) { |
| 71 | + if (indices.rank() != 2) { |
| 72 | + throw new IllegalArgumentException("Sparse indices must be a rank-2 tensor"); |
| 73 | + } |
| 74 | + if (values.rank() != 1) { |
| 75 | + throw new IllegalArgumentException("Sparse values must be a rank-1 tensor"); |
| 76 | + } |
| 77 | + if (denseShape.rank() != 1) { |
| 78 | + throw new IllegalArgumentException("Sparse shape must be a rank-1 tensor"); |
| 79 | + } |
| 80 | + if (indices.shape().get(0) != values.shape().get(0)) { |
| 81 | + throw new IllegalArgumentException( |
| 82 | + "Number of indices must be equal to the number of values [" |
| 83 | + + indices.shape().get(0) |
| 84 | + + " != " |
| 85 | + + values.shape().get(0) |
| 86 | + + "]"); |
| 87 | + } |
| 88 | + if (indices.shape().get(1) != denseShape.shape().get(0)) { |
| 89 | + throw new IllegalArgumentException( |
| 90 | + "Indices must have a coordinate for each dimensions of the tensor [" |
| 91 | + + indices.shape().get(1) |
| 92 | + + " != " |
| 93 | + + denseShape.shape().get(0) |
| 94 | + + "]"); |
| 95 | + } |
| 96 | + // Use mapper of the values tensor as this is the one giving the type of the sparse tensor as |
| 97 | + // well |
| 98 | + TensorMapper<T> mapper = (TensorMapper<T>) values.asRawTensor().typeInfo().mapper(); |
| 99 | + |
| 100 | + // Attach all tensors to a new pointer scope (this will increment their reference count) and |
| 101 | + // preserve a strong reference to that scope inside the sparse tensor. This is done by |
| 102 | + // extending this scope in the sparse tensor constructors, via mapSparse() |
| 103 | + try (PointerScope scope = new PointerScope()) { |
| 104 | + scope.attach(indices.asRawTensor().nativeHandle()); |
| 105 | + scope.attach(values.asRawTensor().nativeHandle()); |
| 106 | + scope.attach(denseShape.asRawTensor().nativeHandle()); |
| 107 | + return mapper.mapSparse(indices, values, denseShape, scope); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + default RawTensor asRawTensor() { |
| 113 | + throw new UnsupportedOperationException( |
| 114 | + "Sparse tensors cannot be converted to a single raw tensor"); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns this instance as a typed tensor. |
| 119 | + * |
| 120 | + * <p>This method is equivalent to cast directly the {@code SparseTensor<T>} instance to {@code |
| 121 | + * T}. |
| 122 | + * |
| 123 | + * @return the typed tensor |
| 124 | + */ |
| 125 | + default T asTypedTensor() { |
| 126 | + return (T) this; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Gets the indices of the sparsed values. |
| 131 | + * |
| 132 | + * <p>Indices are a 2-D long array of shape {@code [N, ndims]}, that specifies the indices of the |
| 133 | + * elements in the sparse tensor that contain nonzero values (elements are zero-indexed). |
| 134 | + * |
| 135 | + * <p>For example, {@code indices=[[1,3], [2,4]]} specifies that the elements with indexes of |
| 136 | + * coordinates {@code [1,3]} and {@code [2,4]} have nonzero values. |
| 137 | + * |
| 138 | + * @return the indices |
| 139 | + */ |
| 140 | + TInt64 indices(); |
| 141 | + |
| 142 | + /** |
| 143 | + * Gets the sparse values. |
| 144 | + * |
| 145 | + * <p>Values are a 1-D array of type {@code T} and shape {@code [N]}, that supplies the values for |
| 146 | + * each element in indices. |
| 147 | + * |
| 148 | + * <p>For example, given {@code indices=[[1,3], [2,4]]}, and {@code values=[18, 3.6]} specifies |
| 149 | + * that element {@code [1,3]} of the sparse tensor has a value of {@code 18}, and element {@code |
| 150 | + * [2,4]} of the sparse tensor has a value of {@code 3.6}. |
| 151 | + * |
| 152 | + * @return the values |
| 153 | + */ |
| 154 | + T values(); |
| 155 | + |
| 156 | + /** |
| 157 | + * Gets the sparse tensor dimensions defining the shape in that tensor in a dense space. |
| 158 | + * |
| 159 | + * <p>Dimensions A 1-D tensor of shape {@code [ndims]} where each the value at index {@code i} |
| 160 | + * represents to total number of element in dimension {@code i} in a dense version of that tensor. |
| 161 | + * |
| 162 | + * @return the dense shape |
| 163 | + */ |
| 164 | + TInt64 denseShape(); |
| 165 | +} |
0 commit comments