|
| 1 | +/* Copyright 2019 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 | + |
| 16 | +package org.tensorflow; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicReferenceArray; |
| 19 | + |
| 20 | +/** |
| 21 | + * Implementation of an {@link Operation} executed eagerly. |
| 22 | + * |
| 23 | + * <p>EagerOperation instances are valid only as long as the {@link EagerSession} they are a part of |
| 24 | + * is valid. Thus, if {@link EagerSession#close()} has been invoked, then methods on the |
| 25 | + * EagerOperation instance may fail with an {@code IllegalStateException}. |
| 26 | + * |
| 27 | + * <p>EagerOperation instances are thread-safe. |
| 28 | + */ |
| 29 | +class EagerOperation extends AbstractOperation { |
| 30 | + |
| 31 | + EagerOperation( |
| 32 | + EagerSession session, |
| 33 | + long opNativeHandle, |
| 34 | + long[] outputNativeHandles, |
| 35 | + String type, |
| 36 | + String name) { |
| 37 | + this.session = session; |
| 38 | + this.type = type; |
| 39 | + this.name = name; |
| 40 | + this.nativeRef = new NativeReference(session, this, opNativeHandle, outputNativeHandles); |
| 41 | + this.outputTensors = new AtomicReferenceArray<Tensor<?>>(outputNativeHandles.length); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public String name() { |
| 46 | + return name; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String type() { |
| 51 | + return type; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public int numOutputs() { |
| 56 | + return nativeRef.outputHandles.length; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int outputListLength(final String name) { |
| 61 | + return outputListLength(nativeRef.opHandle, name); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public int inputListLength(final String name) { |
| 66 | + return inputListLength(nativeRef.opHandle, name); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public long getUnsafeNativeHandle(int outputIndex) { |
| 71 | + return nativeRef.outputHandles[outputIndex]; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public long[] shape(int outputIndex) { |
| 76 | + // If the tensor of this output has already been resolved, return its shape. |
| 77 | + // Otherwise, retrieve the tensor shape from the native library. |
| 78 | + Tensor<?> tensor = outputTensors.get(outputIndex); |
| 79 | + if (tensor != null) { |
| 80 | + return tensor.shape(); |
| 81 | + } |
| 82 | + long outputNativeHandle = getUnsafeNativeHandle(outputIndex); |
| 83 | + long[] shape = new long[numDims(outputNativeHandle)]; |
| 84 | + for (int i = 0; i < shape.length; ++i) { |
| 85 | + shape[i] = dim(outputNativeHandle, i); |
| 86 | + } |
| 87 | + return shape; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public DataType dtype(int outputIndex) { |
| 92 | + // If the tensor of this output has already been resolved, return its datatype. |
| 93 | + // Otherwise, retrieve the tensor datatype from the native library. |
| 94 | + Tensor<?> tensor = outputTensors.get(outputIndex); |
| 95 | + if (tensor != null) { |
| 96 | + return tensor.dataType(); |
| 97 | + } |
| 98 | + long outputNativeHandle = getUnsafeNativeHandle(outputIndex); |
| 99 | + return DataType.fromC(dataType(outputNativeHandle)); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Tensor<?> tensor(int outputIndex) { |
| 104 | + Tensor<?> tensor = outputTensors.get(outputIndex); |
| 105 | + if (tensor == null) { |
| 106 | + tensor = resolveTensor(outputIndex); |
| 107 | + } |
| 108 | + return tensor; |
| 109 | + } |
| 110 | + |
| 111 | + private final EagerSession session; |
| 112 | + private final NativeReference nativeRef; |
| 113 | + private final String type; |
| 114 | + private final String name; |
| 115 | + private final AtomicReferenceArray<Tensor<?>> outputTensors; |
| 116 | + |
| 117 | + private Tensor<?> resolveTensor(int outputIndex) { |
| 118 | + // Take an optimistic approach, where we attempt to resolve the output tensor without locking. |
| 119 | + // If another thread has resolved it meanwhile, release our copy and reuse the existing one |
| 120 | + // instead. |
| 121 | + long tensorNativeHandle = resolveTensorHandle(getUnsafeNativeHandle(outputIndex)); |
| 122 | + Tensor<?> tensor = Tensor.fromHandle(tensorNativeHandle, session); |
| 123 | + if (!outputTensors.compareAndSet(outputIndex, null, tensor)) { |
| 124 | + tensor.close(); |
| 125 | + tensor = outputTensors.get(outputIndex); |
| 126 | + } |
| 127 | + return tensor; |
| 128 | + } |
| 129 | + |
| 130 | + private static class NativeReference extends EagerSession.NativeReference { |
| 131 | + |
| 132 | + NativeReference( |
| 133 | + EagerSession session, EagerOperation operation, long opHandle, long[] outputHandles) { |
| 134 | + super(session, operation); |
| 135 | + this.opHandle = opHandle; |
| 136 | + this.outputHandles = outputHandles; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + void delete() { |
| 141 | + if (opHandle != 0L) { |
| 142 | + for (int i = 0; i < outputHandles.length; ++i) { |
| 143 | + if (outputHandles[i] != 0L) { |
| 144 | + EagerOperation.deleteTensorHandle(outputHandles[i]); |
| 145 | + outputHandles[i] = 0L; |
| 146 | + } |
| 147 | + } |
| 148 | + EagerOperation.delete(opHandle); |
| 149 | + opHandle = 0L; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private long opHandle; |
| 154 | + private final long[] outputHandles; |
| 155 | + } |
| 156 | + |
| 157 | + private static native void delete(long handle); |
| 158 | + |
| 159 | + private static native void deleteTensorHandle(long handle); |
| 160 | + |
| 161 | + private static native long resolveTensorHandle(long handle); |
| 162 | + |
| 163 | + private static native int outputListLength(long handle, String name); |
| 164 | + |
| 165 | + private static native int inputListLength(long handle, String name); |
| 166 | + |
| 167 | + private static native int dataType(long handle); |
| 168 | + |
| 169 | + private static native int numDims(long handle); |
| 170 | + |
| 171 | + private static native long dim(long handle, int index); |
| 172 | +} |
0 commit comments