-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tensor.java
338 lines (283 loc) · 11.8 KB
/
Tensor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package org.tensorflow;
import java.lang.reflect.Array;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.util.Arrays;
public final class Tensor implements AutoCloseable {
private DataType dtype;
private long nativeHandle;
private long[] shapeCopy = null;
private static native long allocate(int i, long[] jArr, long j);
private static native long allocateScalarBytes(byte[] bArr);
private static native ByteBuffer buffer(long j);
private static native void delete(long j);
private static native int dtype(long j);
private static native void readNDArray(long j, Object obj);
private static native boolean scalarBoolean(long j);
private static native byte[] scalarBytes(long j);
private static native double scalarDouble(long j);
private static native float scalarFloat(long j);
private static native int scalarInt(long j);
private static native long scalarLong(long j);
private static native void setValue(long j, Object obj);
private static native long[] shape(long j);
public static Tensor create(Object obj) {
Tensor tensor = new Tensor();
tensor.dtype = dataTypeOf(obj);
tensor.shapeCopy = new long[numDimensions(obj)];
fillShape(obj, 0, tensor.shapeCopy);
if (tensor.dtype != DataType.STRING) {
tensor.nativeHandle = allocate(tensor.dtype.m19c(), tensor.shapeCopy, (long) (elemByteSize(tensor.dtype) * numElements(tensor.shapeCopy)));
setValue(tensor.nativeHandle, obj);
} else if (tensor.shapeCopy.length != 0) {
throw new UnsupportedOperationException(String.format("non-scalar DataType.STRING tensors are not supported yet (version %s). Please file a feature request at https://github.com/tensorflow/tensorflow/issues/new", new Object[]{TensorFlow.version()}));
} else {
tensor.nativeHandle = allocateScalarBytes((byte[]) obj);
}
return tensor;
}
public static Tensor create(long[] jArr, IntBuffer intBuffer) {
Tensor allocateForBuffer = allocateForBuffer(DataType.INT32, jArr, intBuffer.remaining());
allocateForBuffer.buffer().asIntBuffer().put(intBuffer);
return allocateForBuffer;
}
public static Tensor create(long[] jArr, FloatBuffer floatBuffer) {
Tensor allocateForBuffer = allocateForBuffer(DataType.FLOAT, jArr, floatBuffer.remaining());
allocateForBuffer.buffer().asFloatBuffer().put(floatBuffer);
return allocateForBuffer;
}
public static Tensor create(long[] jArr, DoubleBuffer doubleBuffer) {
Tensor allocateForBuffer = allocateForBuffer(DataType.DOUBLE, jArr, doubleBuffer.remaining());
allocateForBuffer.buffer().asDoubleBuffer().put(doubleBuffer);
return allocateForBuffer;
}
public static Tensor create(long[] jArr, LongBuffer longBuffer) {
Tensor allocateForBuffer = allocateForBuffer(DataType.INT64, jArr, longBuffer.remaining());
allocateForBuffer.buffer().asLongBuffer().put(longBuffer);
return allocateForBuffer;
}
public static Tensor create(DataType dataType, long[] jArr, ByteBuffer byteBuffer) {
int elemByteSize;
if (dataType != DataType.STRING) {
elemByteSize = elemByteSize(dataType);
if (byteBuffer.remaining() % elemByteSize != 0) {
throw new IllegalArgumentException(String.format("ByteBuffer with %d bytes is not compatible with a %s Tensor (%d bytes/element)", new Object[]{Integer.valueOf(byteBuffer.remaining()), dataType.toString(), Integer.valueOf(elemByteSize)}));
}
elemByteSize = byteBuffer.remaining() / elemByteSize;
} else {
elemByteSize = byteBuffer.remaining();
}
Tensor allocateForBuffer = allocateForBuffer(dataType, jArr, elemByteSize);
allocateForBuffer.buffer().put(byteBuffer);
return allocateForBuffer;
}
private static Tensor allocateForBuffer(DataType dataType, long[] jArr, int i) {
int numElements = numElements(jArr);
if (dataType != DataType.STRING) {
if (i != numElements) {
throw incompatibleBuffer(i, jArr);
}
i = numElements * elemByteSize(dataType);
}
Tensor tensor = new Tensor();
tensor.dtype = dataType;
tensor.shapeCopy = Arrays.copyOf(jArr, jArr.length);
tensor.nativeHandle = allocate(tensor.dtype.m19c(), tensor.shapeCopy, (long) i);
return tensor;
}
public void close() {
if (this.nativeHandle != 0) {
delete(this.nativeHandle);
this.nativeHandle = 0;
}
}
public DataType dataType() {
return this.dtype;
}
public int numDimensions() {
return this.shapeCopy.length;
}
public int numBytes() {
return buffer().remaining();
}
public int numElements() {
return numElements(this.shapeCopy);
}
public long[] shape() {
return this.shapeCopy;
}
public float floatValue() {
return scalarFloat(this.nativeHandle);
}
public double doubleValue() {
return scalarDouble(this.nativeHandle);
}
public int intValue() {
return scalarInt(this.nativeHandle);
}
public long longValue() {
return scalarLong(this.nativeHandle);
}
public boolean booleanValue() {
return scalarBoolean(this.nativeHandle);
}
public byte[] bytesValue() {
return scalarBytes(this.nativeHandle);
}
public <T> T copyTo(T t) {
throwExceptionIfTypeIsIncompatible(t);
readNDArray(this.nativeHandle, t);
return t;
}
public void writeTo(IntBuffer intBuffer) {
if (this.dtype != DataType.INT32) {
throw incompatibleBuffer((Buffer) intBuffer, this.dtype);
}
intBuffer.put(buffer().asIntBuffer());
}
public void writeTo(FloatBuffer floatBuffer) {
if (this.dtype != DataType.FLOAT) {
throw incompatibleBuffer((Buffer) floatBuffer, this.dtype);
}
floatBuffer.put(buffer().asFloatBuffer());
}
public void writeTo(DoubleBuffer doubleBuffer) {
if (this.dtype != DataType.DOUBLE) {
throw incompatibleBuffer((Buffer) doubleBuffer, this.dtype);
}
doubleBuffer.put(buffer().asDoubleBuffer());
}
public void writeTo(LongBuffer longBuffer) {
if (this.dtype != DataType.INT64) {
throw incompatibleBuffer((Buffer) longBuffer, this.dtype);
}
longBuffer.put(buffer().asLongBuffer());
}
public void writeTo(ByteBuffer byteBuffer) {
byteBuffer.put(buffer());
}
public String toString() {
return String.format("%s tensor with shape %s", new Object[]{this.dtype.toString(), Arrays.toString(shape())});
}
static Tensor fromHandle(long j) {
Tensor tensor = new Tensor();
tensor.dtype = DataType.fromC(dtype(j));
tensor.shapeCopy = shape(j);
tensor.nativeHandle = j;
return tensor;
}
long getNativeHandle() {
return this.nativeHandle;
}
private Tensor() {
}
private ByteBuffer buffer() {
return buffer(this.nativeHandle).order(ByteOrder.nativeOrder());
}
private static IllegalArgumentException incompatibleBuffer(Buffer buffer, DataType dataType) {
return new IllegalArgumentException(String.format("cannot use %s with Tensor of type %s", new Object[]{buffer.getClass().getName(), dataType}));
}
private static IllegalArgumentException incompatibleBuffer(int i, long[] jArr) {
return new IllegalArgumentException(String.format("buffer with %d elements is not compatible with a Tensor with shape %s", new Object[]{Integer.valueOf(i), Arrays.toString(jArr)}));
}
private static int numElements(long[] jArr) {
int i = 1;
for (long j : jArr) {
i = (int) (((long) i) * j);
}
return i;
}
private static int elemByteSize(DataType dataType) {
switch (dataType) {
case UINT8:
case BOOL:
return 1;
case FLOAT:
case INT32:
return 4;
case DOUBLE:
case INT64:
return 8;
case STRING:
throw new IllegalArgumentException("STRING tensors do not have a fixed element size");
default:
throw new IllegalArgumentException("DataType " + dataType + " is not supported yet");
}
}
private static DataType dataTypeOf(Object obj) {
if (obj.getClass().isArray()) {
if (Array.getLength(obj) == 0) {
throw new IllegalArgumentException("cannot create Tensors with a 0 dimension");
}
Object obj2 = Array.get(obj, 0);
if (Byte.class.isInstance(obj2) || Byte.TYPE.isInstance(obj2)) {
return DataType.STRING;
}
return dataTypeOf(obj2);
} else if (Float.class.isInstance(obj) || Float.TYPE.isInstance(obj)) {
return DataType.FLOAT;
} else {
if (Double.class.isInstance(obj) || Double.TYPE.isInstance(obj)) {
return DataType.DOUBLE;
}
if (Integer.class.isInstance(obj) || Integer.TYPE.isInstance(obj)) {
return DataType.INT32;
}
if (Long.class.isInstance(obj) || Long.TYPE.isInstance(obj)) {
return DataType.INT64;
}
if (Boolean.class.isInstance(obj) || Boolean.TYPE.isInstance(obj)) {
return DataType.BOOL;
}
throw new IllegalArgumentException("cannot create Tensors of " + obj.getClass().getName());
}
}
private static int numDimensions(Object obj) {
if (!obj.getClass().isArray()) {
return 0;
}
Object obj2 = Array.get(obj, 0);
if (Byte.class.isInstance(obj2) || Byte.TYPE.isInstance(obj2)) {
return 0;
}
return numDimensions(obj2) + 1;
}
private static void fillShape(Object obj, int i, long[] jArr) {
int i2 = 0;
if (jArr != null && i != jArr.length) {
int length = Array.getLength(obj);
if (jArr[i] == 0) {
jArr[i] = (long) length;
} else if (jArr[i] != ((long) length)) {
throw new IllegalArgumentException(String.format("mismatched lengths (%d and %d) in dimension %d", new Object[]{Long.valueOf(jArr[i]), Integer.valueOf(length), Integer.valueOf(i)}));
}
while (i2 < length) {
fillShape(Array.get(obj, i2), i + 1, jArr);
i2++;
}
}
}
private void throwExceptionIfTypeIsIncompatible(Object obj) {
if (numDimensions(obj) != numDimensions()) {
throw new IllegalArgumentException(String.format("cannot copy Tensor with %d dimensions into an object with %d", new Object[]{Integer.valueOf(numDimensions()), Integer.valueOf(numDimensions(obj))}));
} else if (dataTypeOf(obj) != this.dtype) {
throw new IllegalArgumentException(String.format("cannot copy Tensor with DataType %s into an object of type %s", new Object[]{this.dtype.toString(), obj.getClass().getName()}));
} else {
long[] jArr = new long[numDimensions()];
fillShape(obj, 0, jArr);
for (int i = 0; i < jArr.length; i++) {
if (jArr[i] != shape()[i]) {
throw new IllegalArgumentException(String.format("cannot copy Tensor with shape %s into object with shape %s", new Object[]{Arrays.toString(shape()), Arrays.toString(jArr)}));
}
}
}
}
static {
TensorFlow.init();
}
}