Skip to content

Commit fc3d960

Browse files
authored
[TypeRefact] Added Shaped interface shared across different containers (#153)
* Added `Shaped` interface shared across different containers * Override default Javadoc for `shape()`
1 parent 8c67a5a commit fc3d960

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+226
-142
lines changed

ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,7 @@
5555
*
5656
* @param <T> the type of values to be mapped
5757
*/
58-
public interface NdArray<T> {
59-
60-
/**
61-
* @return the shape of this N-dimensional array
62-
*/
63-
Shape shape();
64-
65-
/**
66-
* @return the rank of this N-dimensional array
67-
*/
68-
default int rank() {
69-
return shape().numDimensions();
70-
}
71-
72-
/**
73-
* Computes and returns the total size of this N-dimensional array, in number of values.
74-
*
75-
* <p>For example, given a 3x3x2 matrix, the return value will be 18.
76-
* @return total size of this nd array
77-
*/
78-
default long size() {
79-
return shape().size();
80-
}
58+
public interface NdArray<T> extends Shaped {
8159

8260
/**
8361
* Returns a sequence of all elements at a given dimension.

ndarray/src/main/java/org/tensorflow/ndarray/NdArrays.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static ByteNdArray vectorOf(byte... values) {
6565
if (values == null) {
6666
throw new IllegalArgumentException("Values cannot be null");
6767
}
68-
return wrap(DataBuffers.of(values, false, false), Shape.of(values.length));
68+
return wrap(Shape.of(values.length), DataBuffers.of(values, false, false));
6969
}
7070

7171
/**
@@ -81,19 +81,19 @@ public static ByteNdArray ofBytes(Shape shape) {
8181
if (shape == null) {
8282
throw new IllegalArgumentException("Shape cannot be null");
8383
}
84-
return wrap(DataBuffers.ofBytes(shape.size()), shape);
84+
return wrap(shape, DataBuffers.ofBytes(shape.size()));
8585
}
8686

8787
/**
8888
* Wraps a buffer in a byte N-dimensional array of a given shape.
8989
*
90-
* @param buffer buffer to wrap
9190
* @param shape shape of the array
91+
* @param buffer buffer to wrap
9292
* @return new byte N-dimensional array
9393
* @throws IllegalArgumentException if shape is null, has unknown dimensions or has size bigger
9494
* in the buffer size
9595
*/
96-
public static ByteNdArray wrap(ByteDataBuffer buffer, Shape shape) {
96+
public static ByteNdArray wrap(Shape shape, ByteDataBuffer buffer) {
9797
return ByteDenseNdArray.create(buffer, shape);
9898
}
9999

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2020 The TensorFlow Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
=======================================================================
16+
*/
17+
package org.tensorflow.ndarray;
18+
19+
import java.util.function.BiConsumer;
20+
import java.util.function.Consumer;
21+
import org.tensorflow.ndarray.buffer.DataBuffer;
22+
import org.tensorflow.ndarray.index.Index;
23+
24+
/**
25+
* Any data container with a given {@link Shape}.
26+
*/
27+
public interface Shaped {
28+
29+
/**
30+
* @return the shape of this container
31+
*/
32+
Shape shape();
33+
34+
/**
35+
* @return the rank of this container
36+
*/
37+
default int rank() {
38+
return shape().numDimensions();
39+
}
40+
41+
/**
42+
* Computes and returns the total size of this container, in number of values.
43+
*
44+
* <p>For example, given a 3x3x2 matrix, the return value will be 18.
45+
*
46+
* @return number of values in this element
47+
*/
48+
default long size() {
49+
return shape().size();
50+
}
51+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
op {
22
graph_op_name: "BarrierIncompleteSize"
3+
out_arg {
4+
name: "size"
5+
rename_to: "output"
6+
}
37
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
op {
22
graph_op_name: "BarrierReadySize"
3+
out_arg {
4+
name: "size"
5+
rename_to: "output"
6+
}
37
}

tensorflow-core/tensorflow-core-api/src/bazel/api_def/api_def_LookupTableSizeV2.pbtxt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ op {
33
endpoint {
44
name: "LookupTableSize"
55
}
6+
out_arg {
7+
name: "size"
8+
rename_to: "output"
9+
}
610
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
op {
22
graph_op_name: "MapIncompleteSize"
3+
out_arg {
4+
name: "size"
5+
rename_to: "output"
6+
}
37
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
op {
22
graph_op_name: "MapSize"
3+
out_arg {
4+
name: "size"
5+
rename_to: "output"
6+
}
37
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
op {
22
graph_op_name: "OrderedMapIncompleteSize"
3+
out_arg {
4+
name: "size"
5+
rename_to: "output"
6+
}
37
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
op {
22
graph_op_name: "OrderedMapSize"
3+
out_arg {
4+
name: "size"
5+
rename_to: "output"
6+
}
37
}

tensorflow-core/tensorflow-core-api/src/bazel/api_def/api_def_QueueSizeV2.pbtxt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ op {
33
endpoint {
44
name: "io.QueueSize"
55
}
6+
out_arg {
7+
name: "size"
8+
rename_to: "output"
9+
}
610
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
op {
22
graph_op_name: "SetSize"
3+
out_arg {
4+
name: "size"
5+
rename_to: "output"
6+
}
37
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
op {
22
graph_op_name: "StageSize"
3+
out_arg {
4+
name: "size"
5+
rename_to: "output"
6+
}
37
}

tensorflow-core/tensorflow-core-api/src/bazel/api_def/api_def_TensorArraySizeV3.pbtxt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ op {
33
endpoint {
44
name: "TensorArraySize"
55
}
6+
out_arg {
7+
name: "size"
8+
rename_to: "output"
9+
}
610
}

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/op/core/BarrierIncompleteSize.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,23 @@ public static BarrierIncompleteSize create(Scope scope, Operand<TString> handle)
5353
* The number of incomplete elements (i.e. those with some of their value
5454
* components not set) in the barrier.
5555
*/
56-
public Output<TInt32> size() {
57-
return size;
56+
public Output<TInt32> output() {
57+
return output;
5858
}
5959

6060
@Override
6161
public Output<TInt32> asOutput() {
62-
return size;
62+
return output;
6363
}
6464

6565
/** The name of this op, as known by TensorFlow core engine */
6666
public static final String OP_NAME = "BarrierIncompleteSize";
6767

68-
private Output<TInt32> size;
68+
private Output<TInt32> output;
6969

7070
private BarrierIncompleteSize(Operation operation) {
7171
super(operation);
7272
int outputIdx = 0;
73-
size = operation.output(outputIdx++);
73+
output = operation.output(outputIdx++);
7474
}
7575
}

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/op/core/BarrierReadySize.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,23 @@ public static BarrierReadySize create(Scope scope, Operand<TString> handle) {
5353
* The number of complete elements (i.e. those with all of their value
5454
* components set) in the barrier.
5555
*/
56-
public Output<TInt32> size() {
57-
return size;
56+
public Output<TInt32> output() {
57+
return output;
5858
}
5959

6060
@Override
6161
public Output<TInt32> asOutput() {
62-
return size;
62+
return output;
6363
}
6464

6565
/** The name of this op, as known by TensorFlow core engine */
6666
public static final String OP_NAME = "BarrierReadySize";
6767

68-
private Output<TInt32> size;
68+
private Output<TInt32> output;
6969

7070
private BarrierReadySize(Operation operation) {
7171
super(operation);
7272
int outputIdx = 0;
73-
size = operation.output(outputIdx++);
73+
output = operation.output(outputIdx++);
7474
}
7575
}

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/op/core/LookupTableSize.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,23 @@ public static LookupTableSize create(Scope scope, Operand<?> tableHandle) {
5151
/**
5252
* Scalar that contains number of elements in the table.
5353
*/
54-
public Output<TInt64> size() {
55-
return size;
54+
public Output<TInt64> output() {
55+
return output;
5656
}
5757

5858
@Override
5959
public Output<TInt64> asOutput() {
60-
return size;
60+
return output;
6161
}
6262

6363
/** The name of this op, as known by TensorFlow core engine */
6464
public static final String OP_NAME = "LookupTableSizeV2";
6565

66-
private Output<TInt64> size;
66+
private Output<TInt64> output;
6767

6868
private LookupTableSize(Operation operation) {
6969
super(operation);
7070
int outputIdx = 0;
71-
size = operation.output(outputIdx++);
71+
output = operation.output(outputIdx++);
7272
}
7373
}

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/op/core/MapIncompleteSize.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,23 @@ public static Options sharedName(String sharedName) {
147147

148148
/**
149149
*/
150-
public Output<TInt32> size() {
151-
return size;
150+
public Output<TInt32> output() {
151+
return output;
152152
}
153153

154154
@Override
155155
public Output<TInt32> asOutput() {
156-
return size;
156+
return output;
157157
}
158158

159159
/** The name of this op, as known by TensorFlow core engine */
160160
public static final String OP_NAME = "MapIncompleteSize";
161161

162-
private Output<TInt32> size;
162+
private Output<TInt32> output;
163163

164164
private MapIncompleteSize(Operation operation) {
165165
super(operation);
166166
int outputIdx = 0;
167-
size = operation.output(outputIdx++);
167+
output = operation.output(outputIdx++);
168168
}
169169
}

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/op/core/MapSize.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,23 @@ public static Options sharedName(String sharedName) {
147147

148148
/**
149149
*/
150-
public Output<TInt32> size() {
151-
return size;
150+
public Output<TInt32> output() {
151+
return output;
152152
}
153153

154154
@Override
155155
public Output<TInt32> asOutput() {
156-
return size;
156+
return output;
157157
}
158158

159159
/** The name of this op, as known by TensorFlow core engine */
160160
public static final String OP_NAME = "MapSize";
161161

162-
private Output<TInt32> size;
162+
private Output<TInt32> output;
163163

164164
private MapSize(Operation operation) {
165165
super(operation);
166166
int outputIdx = 0;
167-
size = operation.output(outputIdx++);
167+
output = operation.output(outputIdx++);
168168
}
169169
}

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/op/core/OrderedMapIncompleteSize.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,23 @@ public static Options sharedName(String sharedName) {
147147

148148
/**
149149
*/
150-
public Output<TInt32> size() {
151-
return size;
150+
public Output<TInt32> output() {
151+
return output;
152152
}
153153

154154
@Override
155155
public Output<TInt32> asOutput() {
156-
return size;
156+
return output;
157157
}
158158

159159
/** The name of this op, as known by TensorFlow core engine */
160160
public static final String OP_NAME = "OrderedMapIncompleteSize";
161161

162-
private Output<TInt32> size;
162+
private Output<TInt32> output;
163163

164164
private OrderedMapIncompleteSize(Operation operation) {
165165
super(operation);
166166
int outputIdx = 0;
167-
size = operation.output(outputIdx++);
167+
output = operation.output(outputIdx++);
168168
}
169169
}

0 commit comments

Comments
 (0)