Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 226
[Type Refactor] Merge TType and Tensor instances as a single entity#160
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,7 +16,6 @@ | ||
| package org.tensorflow; | ||
| import org.tensorflow.internal.c_api.TF_Tensor; | ||
| import org.tensorflow.ndarray.Shape; | ||
| import org.tensorflow.types.TBfloat16; | ||
| import org.tensorflow.types.TBool; | ||
| import org.tensorflow.types.TFloat16; | ||
| @@ -35,13 +34,17 @@ public final class DataType<T extends TType> { | ||
| public interface TensorMapper<T> { | ||
| /** | ||
| * Maps tensor memory to a data structure for manipulating elements of this type. | ||
| * Maps the tensor memory to a n-dimensional typed data space. | ||
| * | ||
| * @param nativeTensor pointer to the native tensor | ||
| * @param shape the shape of the tensor | ||
| * @return data structure of elements of this type | ||
| * <p>This method is designed to be invoked internally by this library only, in order to pass the | ||
| * native handle of {@code tensor} as {@code nativeHandle} (and since only classes from the | ||
| * {@code org.tensorflow} package can retrieve such handle). | ||
| * | ||
| * @param tensor the tensor to map in its raw nature | ||
| * @param nativeHandle native handle of the tensor | ||
| * @return a typed tensor of type {@code T} | ||
| */ | ||
| T apply(TF_Tensor nativeTensor, Shape shape); | ||
| T apply(RawTensor tensor, TF_Tensor nativeHandle); | ||
karllessard marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /** | ||
| @@ -158,13 +161,13 @@ int nativeCode() { | ||
| } | ||
| /** | ||
| * Maps a tensor to a data structure for manipulating elements of this type. | ||
| * Maps a raw tensor to a typed tensor. | ||
| * | ||
| * @param tensor tensor to map | ||
| * @return data structure of elements of this type | ||
| */ | ||
| T map(Tensor<T> tensor) { | ||
| return tensorMapper.apply(tensor.nativeHandle(), tensor.shape()); | ||
| T map(RawTensor tensor) { | ||
| return tensorMapper.apply(tensor, tensor.nativeHandle()); | ||
| } | ||
| private final int nativeCode; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -91,7 +91,7 @@ public TFE_TensorHandle getUnsafeNativeHandle(int outputIndex) { | ||
| public Shape shape(int outputIndex) { | ||
| // If the tensor of this output has already been resolved, return its shape. | ||
| // Otherwise, retrieve the tensor shape from the native library. | ||
| Tensor<?> tensor = outputTensors.get(outputIndex); | ||
| Tensor tensor = outputTensors.get(outputIndex); | ||
| if (tensor != null) { | ||
| return tensor.shape(); | ||
| } | ||
| @@ -107,7 +107,7 @@ public Shape shape(int outputIndex) { | ||
| public DataType<?> dtype(int outputIndex) { | ||
| // If the tensor of this output has already been resolved, return its datatype. | ||
| // Otherwise, retrieve the tensor datatype from the native library. | ||
| Tensor<?> tensor = outputTensors.get(outputIndex); | ||
| Tensor tensor = outputTensors.get(outputIndex); | ||
| if (tensor != null) { | ||
| return tensor.dataType(); | ||
| } | ||
| @@ -116,8 +116,8 @@ public DataType<?> dtype(int outputIndex) { | ||
| } | ||
| @Override | ||
| public Tensor<?> tensor(int outputIndex) { | ||
| Tensor<?> tensor = outputTensors.get(outputIndex); | ||
| public Tensor tensor(int outputIndex) { | ||
| Tensor tensor = outputTensors.get(outputIndex); | ||
| if (tensor == null) { | ||
| tensor = resolveTensor(outputIndex); | ||
| } | ||
| @@ -127,21 +127,21 @@ public Tensor<?> tensor(int outputIndex) { | ||
| private final EagerSession session; | ||
| private final String type; | ||
| private final String name; | ||
| private final AtomicReferenceArray<Tensor<?>> outputTensors; | ||
| private final AtomicReferenceArray<Tensor> outputTensors; | ||
| private Tensor<?> resolveTensor(int outputIndex) { | ||
| private Tensor resolveTensor(int outputIndex) { | ||
| // Take an optimistic approach, where we attempt to resolve the output tensor without locking. | ||
| // If another thread has resolved it meanwhile, release our copy and reuse the existing one | ||
| // instead. | ||
| Tensor<?> tensor = resolveTensorHandle(getUnsafeNativeHandle(outputIndex), session); | ||
| Tensor tensor = resolveTensorHandle(getUnsafeNativeHandle(outputIndex), session); | ||
| if (!outputTensors.compareAndSet(outputIndex, null, tensor)) { | ||
| session.detach(tensor.nativeHandle()); | ||
| session.detach(tensor.asRawTensor().nativeHandle()); | ||
| tensor = outputTensors.get(outputIndex); | ||
| } | ||
| return tensor; | ||
| } | ||
| private TFE_Op opHandle; | ||
| private final TFE_Op opHandle; | ||
| private final TFE_TensorHandle[] outputHandles; | ||
| private static void requireOp(TFE_Op handle) { | ||
| @@ -156,13 +156,13 @@ private static void requireTensorHandle(TFE_TensorHandle handle) { | ||
| } | ||
| } | ||
| private static Tensor<?> resolveTensorHandle(TFE_TensorHandle handle, EagerSession session) { | ||
| private static Tensor resolveTensorHandle(TFE_TensorHandle handle, EagerSession session) { | ||
| requireTensorHandle(handle); | ||
| try (PointerScope scope = new PointerScope()) { | ||
| TF_Status status = TF_Status.newStatus(); | ||
| TF_Tensor tensor = TFE_TensorHandleResolve(handle, status).withDeallocator(); | ||
| status.throwExceptionIfNotOK(); | ||
| return Tensor.fromHandle(tensor, session); | ||
| return RawTensor.fromHandle(tensor, session).asTypedTensor(); | ||
karllessard marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.