Skip to content

[TypeRefact] Added Shaped interface shared across different containers #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions ndarray/src/main/java/org/tensorflow/ndarray/NdArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,7 @@
*
* @param <T> the type of values to be mapped
*/
public interface NdArray<T> {

/**
* @return the shape of this N-dimensional array
*/
Shape shape();

/**
* @return the rank of this N-dimensional array
*/
default int rank() {
return shape().numDimensions();
}

/**
* Computes and returns the total size of this N-dimensional array, in number of values.
*
* <p>For example, given a 3x3x2 matrix, the return value will be 18.
* @return total size of this nd array
*/
default long size() {
return shape().size();
}
public interface NdArray<T> extends Shaped {

/**
* Returns a sequence of all elements at a given dimension.
Expand Down
8 changes: 4 additions & 4 deletions ndarray/src/main/java/org/tensorflow/ndarray/NdArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static ByteNdArray vectorOf(byte... values) {
if (values == null) {
throw new IllegalArgumentException("Values cannot be null");
}
return wrap(DataBuffers.of(values, false, false), Shape.of(values.length));
return wrap(Shape.of(values.length), DataBuffers.of(values, false, false));
}

/**
Expand All @@ -81,19 +81,19 @@ public static ByteNdArray ofBytes(Shape shape) {
if (shape == null) {
throw new IllegalArgumentException("Shape cannot be null");
}
return wrap(DataBuffers.ofBytes(shape.size()), shape);
return wrap(shape, DataBuffers.ofBytes(shape.size()));
}

/**
* Wraps a buffer in a byte N-dimensional array of a given shape.
*
* @param buffer buffer to wrap
* @param shape shape of the array
* @param buffer buffer to wrap
* @return new byte N-dimensional array
* @throws IllegalArgumentException if shape is null, has unknown dimensions or has size bigger
* in the buffer size
*/
public static ByteNdArray wrap(ByteDataBuffer buffer, Shape shape) {
public static ByteNdArray wrap(Shape shape, ByteDataBuffer buffer) {
return ByteDenseNdArray.create(buffer, shape);
}

Expand Down
51 changes: 51 additions & 0 deletions ndarray/src/main/java/org/tensorflow/ndarray/Shaped.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2020 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.
=======================================================================
*/
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;

/**
* Any data container with a given {@link Shape}.
*/
public interface Shaped {

/**
* @return the shape of this container
*/
Shape shape();

/**
* @return the rank of this container
*/
default int rank() {
return shape().numDimensions();
}

/**
* Computes and returns the total size of this container, in number of values.
*
* <p>For example, given a 3x3x2 matrix, the return value will be 18.
*
* @return number of values in this element
*/
default long size() {
return shape().size();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
op {
graph_op_name: "BarrierIncompleteSize"
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
op {
graph_op_name: "BarrierReadySize"
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ op {
endpoint {
name: "LookupTableSize"
}
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
op {
graph_op_name: "MapIncompleteSize"
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
op {
graph_op_name: "MapSize"
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
op {
graph_op_name: "OrderedMapIncompleteSize"
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
op {
graph_op_name: "OrderedMapSize"
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ op {
endpoint {
name: "io.QueueSize"
}
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
op {
graph_op_name: "SetSize"
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
op {
graph_op_name: "StageSize"
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ op {
endpoint {
name: "TensorArraySize"
}
out_arg {
name: "size"
rename_to: "output"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ public static BarrierIncompleteSize create(Scope scope, Operand<TString> handle)
* The number of incomplete elements (i.e. those with some of their value
* components not set) in the barrier.
*/
public Output<TInt32> size() {
return size;
public Output<TInt32> output() {
return output;
}

@Override
public Output<TInt32> asOutput() {
return size;
return output;
}

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

private Output<TInt32> size;
private Output<TInt32> output;

private BarrierIncompleteSize(Operation operation) {
super(operation);
int outputIdx = 0;
size = operation.output(outputIdx++);
output = operation.output(outputIdx++);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ public static BarrierReadySize create(Scope scope, Operand<TString> handle) {
* The number of complete elements (i.e. those with all of their value
* components set) in the barrier.
*/
public Output<TInt32> size() {
return size;
public Output<TInt32> output() {
return output;
}

@Override
public Output<TInt32> asOutput() {
return size;
return output;
}

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

private Output<TInt32> size;
private Output<TInt32> output;

private BarrierReadySize(Operation operation) {
super(operation);
int outputIdx = 0;
size = operation.output(outputIdx++);
output = operation.output(outputIdx++);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ public static LookupTableSize create(Scope scope, Operand<?> tableHandle) {
/**
* Scalar that contains number of elements in the table.
*/
public Output<TInt64> size() {
return size;
public Output<TInt64> output() {
return output;
}

@Override
public Output<TInt64> asOutput() {
return size;
return output;
}

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

private Output<TInt64> size;
private Output<TInt64> output;

private LookupTableSize(Operation operation) {
super(operation);
int outputIdx = 0;
size = operation.output(outputIdx++);
output = operation.output(outputIdx++);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,23 @@ public static Options sharedName(String sharedName) {

/**
*/
public Output<TInt32> size() {
return size;
public Output<TInt32> output() {
return output;
}

@Override
public Output<TInt32> asOutput() {
return size;
return output;
}

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

private Output<TInt32> size;
private Output<TInt32> output;

private MapIncompleteSize(Operation operation) {
super(operation);
int outputIdx = 0;
size = operation.output(outputIdx++);
output = operation.output(outputIdx++);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,23 @@ public static Options sharedName(String sharedName) {

/**
*/
public Output<TInt32> size() {
return size;
public Output<TInt32> output() {
return output;
}

@Override
public Output<TInt32> asOutput() {
return size;
return output;
}

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

private Output<TInt32> size;
private Output<TInt32> output;

private MapSize(Operation operation) {
super(operation);
int outputIdx = 0;
size = operation.output(outputIdx++);
output = operation.output(outputIdx++);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,23 @@ public static Options sharedName(String sharedName) {

/**
*/
public Output<TInt32> size() {
return size;
public Output<TInt32> output() {
return output;
}

@Override
public Output<TInt32> asOutput() {
return size;
return output;
}

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

private Output<TInt32> size;
private Output<TInt32> output;

private OrderedMapIncompleteSize(Operation operation) {
super(operation);
int outputIdx = 0;
size = operation.output(outputIdx++);
output = operation.output(outputIdx++);
}
}
Loading