Skip to content

Implements both Tensor and NdArray interfaces from same instance #92

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,7 @@ public Constant<TString> constant(Charset charset, Shape shape, DataBuffer<Strin
* @throws IllegalArgumentException If the tensor datatype or shape is not compatible with the
* buffer
*/
public <T extends TType> Constant<T> constant(DataType<T> type, Shape shape,
public <T extends TType & Tensor> Constant<T> constant(DataType<T> type, Shape shape,
ByteDataBuffer data) {
return Constant.tensorOf(scope, type, shape, data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public final class DataType<T extends TType> {

@FunctionalInterface
public interface TensorMapper<T> {
public interface TensorInstantiator<T> {

/**
* Maps tensor memory to a data structure for manipulating elements of this type.
Expand All @@ -41,10 +41,10 @@ public interface TensorMapper<T> {
* @param name readable-name for this type
* @param value must match the corresponding TF_* value in the TensorFlow C API.
* @param byteSize size of an element of this type, in bytes, -1 if unknown
* @param tensorMapper method for mapping tensor memory to elements of this type
* @param tensorMapper method for instantiating tensor from a native reference
*/
public static <T extends TType> DataType<T> create(String name, int value, int byteSize, TensorMapper<T> tensorMapper) {
return new DataType<>(name, value, byteSize, tensorMapper);
public static <T extends TType> DataType<T> create(String name, int value, int byteSize, TensorInstantiator<T> instantiator) {
return new DataType<>(name, value, byteSize, instantiator);
}

/**
Expand Down Expand Up @@ -76,29 +76,29 @@ public String toString() {
/**
* Returns the numeric code for this datatype, as recognized by the native library (C API)
*/
int nativeCode() {
public int nativeCode() {
return nativeCode;
}

/**
* Maps a tensor to a data structure for manipulating elements of this type.
* Instantiate a tensor of this datatype from the provided native handle
*
* @param tensor tensor to map
* @return data structure of elements of this type
* @param handle tensor native handle
* @return a tensor of this datatype
*/
T map(Tensor<T> tensor) {
return tensorMapper.apply(tensor.nativeHandle(), tensor.shape());
T instantiateTensor(TF_Tensor handle, Shape shape) {
return tensorInstantiator.apply(handle, shape);
}

private final int nativeCode;
private final int byteSize;
private final String name;
private final TensorMapper<T> tensorMapper;
private final TensorInstantiator<T> tensorInstantiator;

private DataType(String name, int nativeCode, int byteSize, TensorMapper<T> tensorMapper) {
private DataType(String name, int nativeCode, int byteSize, TensorInstantiator<T> tensorInstantiator) {
this.name = name;
this.nativeCode = nativeCode;
this.byteSize = byteSize;
this.tensorMapper = tensorMapper;
this.tensorInstantiator = tensorInstantiator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ final class DataTypes {
* @return data type for this code
* @throws IllegalArgumentException if the code matches no registered data type
*/
static DataType<?> fromNativeCode(int nativeCode) {
DataType<?> dataType = DATA_TYPE_REGISTRY.get(nativeCode);
static DataType<? extends Tensor> fromNativeCode(int nativeCode) {
DataType<? extends Tensor> dataType = DATA_TYPE_REGISTRY.get(nativeCode);
if (dataType == null) {
throw new IllegalArgumentException(
"DataType " + nativeCode + " is not recognized in Java (version " + TensorFlow.version() + ")");
}
return dataType;
}

private static final Map<Integer, DataType<?>> DATA_TYPE_REGISTRY = new HashMap<>();
private static final Map<Integer, DataType<? extends Tensor>> DATA_TYPE_REGISTRY = new HashMap<>();

static {
register(TBool.DTYPE);
Expand All @@ -68,7 +68,7 @@ static DataType<?> fromNativeCode(int nativeCode) {

// TODO (karllessard): Right now this method is private but we might want to expose it
// to allow user to register custom data types?
private static void register(DataType<?> dataType) {
private static void register(DataType<? extends Tensor> dataType) {
DATA_TYPE_REGISTRY.put(dataType.nativeCode(), dataType);
DATA_TYPE_REGISTRY.put(dataType.nativeCode() + 100, dataType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ private static Tensor<?> resolveTensorHandle(TFE_TensorHandle handle, EagerSessi
TF_Status status = TF_Status.newStatus();
TF_Tensor tensor = TFE_TensorHandleResolve(handle, status).withDeallocator();
status.throwExceptionIfNotOK();
return Tensor.fromHandle(tensor, session);
Tensor<?> t = Tensors.fromHandle(tensor);
session.attach(t.nativeHandle());
// Now that the tensor life is attached to the eager session scope, closing it will
// implicitly detach it from its previous internal scope
// FIXME TENSOR REFACT : is that right and ok?
t.close();
return t;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public interface Operand<T extends TType> extends Op {
* @return the tensor
* @throws IllegalStateException if this is an operand of a graph
*/
default Tensor<T> asTensor() {
default T asTensor() {
return asOutput().tensor();
}

Expand All @@ -72,8 +72,10 @@ default Tensor<T> asTensor() {
*
* @return the tensor data
* @throws IllegalStateException if this is an operand of a graph
* @deprecated use {@link #asTensor()} instead
*/
@Deprecated
default T data() {
return asOutput().tensor().data();
return asTensor();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public <U extends TType> Output<U> expect(DataType<U> dt) {
* @see EagerSession
*/
@SuppressWarnings("unchecked")
public Tensor<T> tensor() {
return (Tensor<T>) operation.tensor(index);
public T tensor() {
return (T)operation.tensor(index);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ private static RunMetadata run(

for (int i = 0; i < noutputs; ++i) {
TF_Tensor h = outputValues.get(TF_Tensor.class, i).withDeallocator();
outputTensors.add(Tensor.fromHandle(h));
outputTensors.add(Tensors.fromHandle(h));
}
try {
return runMetadata != null ? RunMetadata.parseFrom(runMetadata.dataAsByteBuffer()) : null;
Expand Down
Loading