• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 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 #include "tensorflow/java/src/main/native/tensor_jni.h"
17 
18 #include <assert.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <algorithm>
22 #include <memory>
23 
24 #include "tensorflow/c/c_api.h"
25 #include "tensorflow/java/src/main/native/exception_jni.h"
26 
27 namespace {
28 
requireHandle(JNIEnv * env,jlong handle)29 TF_Tensor* requireHandle(JNIEnv* env, jlong handle) {
30   if (handle == 0) {
31     throwException(env, kNullPointerException,
32                    "close() was called on the Tensor");
33     return nullptr;
34   }
35   return reinterpret_cast<TF_Tensor*>(handle);
36 }
37 
elemByteSize(TF_DataType dtype)38 size_t elemByteSize(TF_DataType dtype) {
39   // The code in this file makes the assumption that the
40   // TensorFlow TF_DataTypes and the Java primitive types
41   // have the same byte sizes. Validate that:
42   switch (dtype) {
43     case TF_BOOL:
44     case TF_UINT8:
45       static_assert(sizeof(jboolean) == 1,
46                     "Java boolean not compatible with TF_BOOL");
47       static_assert(sizeof(jbyte) == 1,
48                     "Java byte not compatible with TF_UINT8");
49       return 1;
50     case TF_FLOAT:
51     case TF_INT32:
52       static_assert(sizeof(jfloat) == 4,
53                     "Java float not compatible with TF_FLOAT");
54       static_assert(sizeof(jint) == 4, "Java int not compatible with TF_INT32");
55       return 4;
56     case TF_DOUBLE:
57     case TF_INT64:
58       static_assert(sizeof(jdouble) == 8,
59                     "Java double not compatible with TF_DOUBLE");
60       static_assert(sizeof(jlong) == 8,
61                     "Java long not compatible with TF_INT64");
62       return 8;
63     default:
64       return 0;
65   }
66 }
67 
68 // Write a Java scalar object (java.lang.Integer etc.) to a TF_Tensor.
writeScalar(JNIEnv * env,jobject src,TF_DataType dtype,void * dst,size_t dst_size)69 void writeScalar(JNIEnv* env, jobject src, TF_DataType dtype, void* dst,
70                  size_t dst_size) {
71   size_t sz = elemByteSize(dtype);
72   if (sz != dst_size) {
73     throwException(
74         env, kIllegalStateException,
75         "scalar (%d bytes) not compatible with allocated tensor (%d bytes)", sz,
76         dst_size);
77     return;
78   }
79   switch (dtype) {
80 // env->FindClass and env->GetMethodID are expensive and JNI best practices
81 // suggest that they should be cached. However, until the creation of scalar
82 // valued tensors seems to become a noticeable fraction of program execution,
83 // ignore that cost.
84 #define CASE(dtype, jtype, method_name, method_signature, call_type)           \
85   case dtype: {                                                                \
86     jclass clazz = env->FindClass("java/lang/Number");                         \
87     jmethodID method = env->GetMethodID(clazz, method_name, method_signature); \
88     jtype v = env->Call##call_type##Method(src, method);                       \
89     memcpy(dst, &v, sz);                                                       \
90     return;                                                                    \
91   }
92     CASE(TF_FLOAT, jfloat, "floatValue", "()F", Float);
93     CASE(TF_DOUBLE, jdouble, "doubleValue", "()D", Double);
94     CASE(TF_INT32, jint, "intValue", "()I", Int);
95     CASE(TF_INT64, jlong, "longValue", "()J", Long);
96     CASE(TF_UINT8, jbyte, "byteValue", "()B", Byte);
97 #undef CASE
98     case TF_BOOL: {
99       jclass clazz = env->FindClass("java/lang/Boolean");
100       jmethodID method = env->GetMethodID(clazz, "booleanValue", "()Z");
101       jboolean v = env->CallBooleanMethod(src, method);
102       *(static_cast<unsigned char*>(dst)) = v ? 1 : 0;
103       return;
104     }
105     default:
106       throwException(env, kIllegalStateException, "invalid DataType(%d)",
107                      dtype);
108       return;
109   }
110 }
111 
112 // Copy a 1-D array of Java primitive types to the tensor buffer dst.
113 // Returns the number of bytes written to dst.
write1DArray(JNIEnv * env,jarray array,TF_DataType dtype,void * dst,size_t dst_size)114 size_t write1DArray(JNIEnv* env, jarray array, TF_DataType dtype, void* dst,
115                     size_t dst_size) {
116   const int nelems = env->GetArrayLength(array);
117   jboolean is_copy;
118   switch (dtype) {
119 #define CASE(dtype, jtype, get_type)                                   \
120   case dtype: {                                                        \
121     jtype##Array a = static_cast<jtype##Array>(array);                 \
122     jtype* values = env->Get##get_type##ArrayElements(a, &is_copy);    \
123     size_t to_copy = nelems * elemByteSize(dtype);                     \
124     if (to_copy > dst_size) {                                          \
125       throwException(                                                  \
126           env, kIllegalStateException,                                 \
127           "cannot write Java array of %d bytes to Tensor of %d bytes", \
128           to_copy, dst_size);                                          \
129       to_copy = 0;                                                     \
130     } else {                                                           \
131       memcpy(dst, values, to_copy);                                    \
132     }                                                                  \
133     env->Release##get_type##ArrayElements(a, values, JNI_ABORT);       \
134     return to_copy;                                                    \
135   }
136     CASE(TF_FLOAT, jfloat, Float);
137     CASE(TF_DOUBLE, jdouble, Double);
138     CASE(TF_INT32, jint, Int);
139     CASE(TF_INT64, jlong, Long);
140     CASE(TF_BOOL, jboolean, Boolean);
141     CASE(TF_UINT8, jbyte, Byte);
142 #undef CASE
143     default:
144       throwException(env, kIllegalStateException, "invalid DataType(%d)",
145                      dtype);
146       return 0;
147   }
148 }
149 
150 // Copy the elements of a 1-D array from the tensor buffer src to a 1-D array of
151 // Java primitive types. Returns the number of bytes read from src.
read1DArray(JNIEnv * env,TF_DataType dtype,const void * src,size_t src_size,jarray dst)152 size_t read1DArray(JNIEnv* env, TF_DataType dtype, const void* src,
153                    size_t src_size, jarray dst) {
154   const int len = env->GetArrayLength(dst);
155   const size_t sz = len * elemByteSize(dtype);
156   if (sz > src_size) {
157     throwException(
158         env, kIllegalStateException,
159         "cannot fill a Java array of %d bytes with a Tensor of %d bytes", sz,
160         src_size);
161     return 0;
162   }
163   switch (dtype) {
164 #define CASE(dtype, jtype, primitive_type)                                 \
165   case dtype: {                                                            \
166     jtype##Array arr = static_cast<jtype##Array>(dst);                     \
167     env->Set##primitive_type##ArrayRegion(arr, 0, len,                     \
168                                           static_cast<const jtype*>(src)); \
169     return sz;                                                             \
170   }
171     CASE(TF_FLOAT, jfloat, Float);
172     CASE(TF_DOUBLE, jdouble, Double);
173     CASE(TF_INT32, jint, Int);
174     CASE(TF_INT64, jlong, Long);
175     CASE(TF_BOOL, jboolean, Boolean);
176     CASE(TF_UINT8, jbyte, Byte);
177 #undef CASE
178     default:
179       throwException(env, kIllegalStateException, "invalid DataType(%d)",
180                      dtype);
181   }
182   return 0;
183 }
184 
writeNDArray(JNIEnv * env,jarray src,TF_DataType dtype,int dims_left,char * dst,size_t dst_size)185 size_t writeNDArray(JNIEnv* env, jarray src, TF_DataType dtype, int dims_left,
186                     char* dst, size_t dst_size) {
187   if (dims_left == 1) {
188     return write1DArray(env, src, dtype, dst, dst_size);
189   } else {
190     jobjectArray ndarray = static_cast<jobjectArray>(src);
191     int len = env->GetArrayLength(ndarray);
192     size_t sz = 0;
193     for (int i = 0; i < len; ++i) {
194       jarray row = static_cast<jarray>(env->GetObjectArrayElement(ndarray, i));
195       sz +=
196           writeNDArray(env, row, dtype, dims_left - 1, dst + sz, dst_size - sz);
197       env->DeleteLocalRef(row);
198       if (env->ExceptionCheck()) return sz;
199     }
200     return sz;
201   }
202 }
203 
readNDArray(JNIEnv * env,TF_DataType dtype,const char * src,size_t src_size,int dims_left,jarray dst)204 size_t readNDArray(JNIEnv* env, TF_DataType dtype, const char* src,
205                    size_t src_size, int dims_left, jarray dst) {
206   if (dims_left == 1) {
207     return read1DArray(env, dtype, src, src_size, dst);
208   } else {
209     jobjectArray ndarray = static_cast<jobjectArray>(dst);
210     int len = env->GetArrayLength(ndarray);
211     size_t sz = 0;
212     for (int i = 0; i < len; ++i) {
213       jarray row = static_cast<jarray>(env->GetObjectArrayElement(ndarray, i));
214       sz +=
215           readNDArray(env, dtype, src + sz, src_size - sz, dims_left - 1, row);
216       env->DeleteLocalRef(row);
217       if (env->ExceptionCheck()) return sz;
218     }
219     return sz;
220   }
221 }
222 
TF_StringDecodeTojbyteArray(JNIEnv * env,const TF_TString * src)223 jbyteArray TF_StringDecodeTojbyteArray(JNIEnv* env, const TF_TString* src) {
224   const char* dst = TF_TString_GetDataPointer(src);
225   size_t dst_len = TF_TString_GetSize(src);
226 
227   jbyteArray ret = env->NewByteArray(dst_len);
228   jbyte* cpy = env->GetByteArrayElements(ret, nullptr);
229 
230   memcpy(cpy, dst, dst_len);
231   env->ReleaseByteArrayElements(ret, cpy, 0);
232   return ret;
233 }
234 
235 class StringTensorWriter {
236  public:
StringTensorWriter(TF_Tensor * t,int num_elements)237   StringTensorWriter(TF_Tensor* t, int num_elements)
238       : index_(0), data_(static_cast<TF_TString*>(TF_TensorData(t))) {}
239 
Add(const char * src,size_t len,TF_Status * status)240   void Add(const char* src, size_t len, TF_Status* status) {
241     if (TF_GetCode(status) != TF_OK) return;
242     TF_TString_Init(&data_[index_]);
243     TF_TString_Copy(&data_[index_++], src, len);
244   }
245 
246  private:
247   int index_;
248   TF_TString* data_;
249 };
250 
251 class StringTensorReader {
252  public:
StringTensorReader(const TF_Tensor * t,int num_elements)253   StringTensorReader(const TF_Tensor* t, int num_elements)
254       : index_(0), data_(static_cast<const TF_TString*>(TF_TensorData(t))) {}
255 
Next(JNIEnv * env,TF_Status * status)256   jbyteArray Next(JNIEnv* env, TF_Status* status) {
257     if (TF_GetCode(status) != TF_OK) return nullptr;
258     return TF_StringDecodeTojbyteArray(env, &data_[index_++]);
259   }
260 
261  private:
262   int index_;
263   const TF_TString* data_;
264 };
265 
readNDStringArray(JNIEnv * env,StringTensorReader * reader,int dims_left,jobjectArray dst,TF_Status * status)266 void readNDStringArray(JNIEnv* env, StringTensorReader* reader, int dims_left,
267                        jobjectArray dst, TF_Status* status) {
268   jsize len = env->GetArrayLength(dst);
269   if (dims_left == 1) {
270     for (jsize i = 0; i < len; ++i) {
271       jbyteArray elem = reader->Next(env, status);
272       if (TF_GetCode(status) != TF_OK) return;
273       env->SetObjectArrayElement(dst, i, elem);
274     }
275     return;
276   }
277   for (jsize i = 0; i < len; ++i) {
278     jobjectArray arr =
279         static_cast<jobjectArray>(env->GetObjectArrayElement(dst, i));
280     readNDStringArray(env, reader, dims_left - 1, arr, status);
281     if (TF_GetCode(status) != TF_OK) return;
282   }
283 }
284 }  // namespace
285 
Java_org_tensorflow_Tensor_allocate(JNIEnv * env,jclass clazz,jint dtype,jlongArray shape,jlong sizeInBytes)286 JNIEXPORT jlong JNICALL Java_org_tensorflow_Tensor_allocate(JNIEnv* env,
287                                                             jclass clazz,
288                                                             jint dtype,
289                                                             jlongArray shape,
290                                                             jlong sizeInBytes) {
291   int num_dims = static_cast<int>(env->GetArrayLength(shape));
292   jlong* dims = nullptr;
293   if (num_dims > 0) {
294     jboolean is_copy;
295     dims = env->GetLongArrayElements(shape, &is_copy);
296   }
297   static_assert(sizeof(jlong) == sizeof(int64_t),
298                 "Java long is not compatible with the TensorFlow C API");
299   // On some platforms "jlong" is a "long" while "int64_t" is a "long long".
300   //
301   // Thus, static_cast<int64_t*>(dims) will trigger a compiler error:
302   // static_cast from 'jlong *' (aka 'long *') to 'int64_t *' (aka 'long long
303   // *') is not allowed
304   //
305   // Since this array is typically very small, use the guaranteed safe scheme of
306   // creating a copy.
307   int64_t* dims_copy = new int64_t[num_dims];
308   for (int i = 0; i < num_dims; ++i) {
309     dims_copy[i] = static_cast<int64_t>(dims[i]);
310   }
311   TF_Tensor* t = TF_AllocateTensor(static_cast<TF_DataType>(dtype), dims_copy,
312                                    num_dims, static_cast<size_t>(sizeInBytes));
313   delete[] dims_copy;
314   if (dims != nullptr) {
315     env->ReleaseLongArrayElements(shape, dims, JNI_ABORT);
316   }
317   if (t == nullptr) {
318     throwException(env, kNullPointerException,
319                    "unable to allocate memory for the Tensor");
320     return 0;
321   }
322   return reinterpret_cast<jlong>(t);
323 }
324 
Java_org_tensorflow_Tensor_allocateScalarBytes(JNIEnv * env,jclass clazz,jbyteArray value)325 JNIEXPORT jlong JNICALL Java_org_tensorflow_Tensor_allocateScalarBytes(
326     JNIEnv* env, jclass clazz, jbyteArray value) {
327   // TF_STRING tensors are encoded with a table of 8-byte offsets followed by
328   // TF_StringEncode-encoded bytes.
329   size_t src_len = static_cast<int>(env->GetArrayLength(value));
330   TF_Tensor* t = TF_AllocateTensor(TF_STRING, nullptr, 0, sizeof(TF_TString));
331   TF_TString* dst = static_cast<TF_TString*>(TF_TensorData(t));
332 
333   TF_Status* status = TF_NewStatus();
334   jbyte* jsrc = env->GetByteArrayElements(value, nullptr);
335   // jsrc is an unsigned byte*, TF_StringEncode requires a char*.
336   // reinterpret_cast<> for this conversion should be safe.
337   TF_TString_Init(&dst[0]);
338   TF_TString_Copy(&dst[0], reinterpret_cast<const char*>(jsrc), src_len);
339 
340   env->ReleaseByteArrayElements(value, jsrc, JNI_ABORT);
341   if (!throwExceptionIfNotOK(env, status)) {
342     TF_DeleteStatus(status);
343     return 0;
344   }
345   TF_DeleteStatus(status);
346   return reinterpret_cast<jlong>(t);
347 }
348 
349 namespace {
checkForNullEntries(JNIEnv * env,jarray value,int num_dims)350 void checkForNullEntries(JNIEnv* env, jarray value, int num_dims) {
351   jsize len = env->GetArrayLength(value);
352   for (jsize i = 0; i < len; ++i) {
353     jarray elem = static_cast<jarray>(
354         env->GetObjectArrayElement(static_cast<jobjectArray>(value), i));
355     if (elem == nullptr) {
356       throwException(env, kNullPointerException,
357                      "null entries in provided array");
358       return;
359     }
360     if (env->ExceptionCheck()) return;
361   }
362 }
363 
fillNonScalarTF_STRINGTensorData(JNIEnv * env,jarray value,int num_dims,StringTensorWriter * writer,TF_Status * status)364 void fillNonScalarTF_STRINGTensorData(JNIEnv* env, jarray value, int num_dims,
365                                       StringTensorWriter* writer,
366                                       TF_Status* status) {
367   if (num_dims == 0) {
368     jbyte* jsrc =
369         env->GetByteArrayElements(static_cast<jbyteArray>(value), nullptr);
370     writer->Add(reinterpret_cast<const char*>(jsrc), env->GetArrayLength(value),
371                 status);
372     env->ReleaseByteArrayElements(static_cast<jbyteArray>(value), jsrc,
373                                   JNI_ABORT);
374     return;
375   }
376   jsize len = env->GetArrayLength(value);
377   for (jsize i = 0; i < len; ++i) {
378     jarray elem = static_cast<jarray>(
379         env->GetObjectArrayElement(static_cast<jobjectArray>(value), i));
380     fillNonScalarTF_STRINGTensorData(env, elem, num_dims - 1, writer, status);
381     if (TF_GetCode(status) != TF_OK) return;
382   }
383 }
384 }  // namespace
385 
Java_org_tensorflow_Tensor_allocateNonScalarBytes(JNIEnv * env,jclass clazz,jlongArray shape,jobjectArray value)386 JNIEXPORT jlong JNICALL Java_org_tensorflow_Tensor_allocateNonScalarBytes(
387     JNIEnv* env, jclass clazz, jlongArray shape, jobjectArray value) {
388   // TF_STRING tensors are encoded with a table of 8-byte offsets following by
389   // TF_StringEncode-encoded bytes.
390   const int num_dims = static_cast<int>(env->GetArrayLength(shape));
391   int64_t* dims = new int64_t[num_dims];
392   int64_t num_elements = 1;
393   {
394     jlong* jdims = env->GetLongArrayElements(shape, nullptr);
395     for (int i = 0; i < num_dims; ++i) {
396       dims[i] = static_cast<int64_t>(jdims[i]);
397       num_elements *= dims[i];
398     }
399     env->ReleaseLongArrayElements(shape, jdims, JNI_ABORT);
400   }
401   checkForNullEntries(env, value, num_dims);
402   if (env->ExceptionCheck()) return 0;
403   TF_Tensor* t = TF_AllocateTensor(TF_STRING, dims, num_dims,
404                                    sizeof(TF_TString) * num_elements);
405   if (t == nullptr) {
406     delete[] dims;
407     throwException(env, kNullPointerException,
408                    "unable to allocate memory for the Tensor");
409     return 0;
410   }
411   TF_Status* status = TF_NewStatus();
412   StringTensorWriter writer(t, num_elements);
413   fillNonScalarTF_STRINGTensorData(env, value, num_dims, &writer, status);
414   delete[] dims;
415   jlong ret = 0;
416   if (!throwExceptionIfNotOK(env, status)) {
417     TF_DeleteTensor(t);
418   } else {
419     ret = reinterpret_cast<jlong>(t);
420   }
421   TF_DeleteStatus(status);
422   return ret;
423 }
424 
Java_org_tensorflow_Tensor_delete(JNIEnv * env,jclass clazz,jlong handle)425 JNIEXPORT void JNICALL Java_org_tensorflow_Tensor_delete(JNIEnv* env,
426                                                          jclass clazz,
427                                                          jlong handle) {
428   if (handle == 0) return;
429   TF_DeleteTensor(reinterpret_cast<TF_Tensor*>(handle));
430 }
431 
Java_org_tensorflow_Tensor_buffer(JNIEnv * env,jclass clazz,jlong handle)432 JNIEXPORT jobject JNICALL Java_org_tensorflow_Tensor_buffer(JNIEnv* env,
433                                                             jclass clazz,
434                                                             jlong handle) {
435   TF_Tensor* t = requireHandle(env, handle);
436   if (t == nullptr) return nullptr;
437   void* data = TF_TensorData(t);
438   const size_t sz = TF_TensorByteSize(t);
439 
440   return env->NewDirectByteBuffer(data, static_cast<jlong>(sz));
441 }
442 
Java_org_tensorflow_Tensor_dtype(JNIEnv * env,jclass clazz,jlong handle)443 JNIEXPORT jint JNICALL Java_org_tensorflow_Tensor_dtype(JNIEnv* env,
444                                                         jclass clazz,
445                                                         jlong handle) {
446   static_assert(sizeof(jint) >= sizeof(TF_DataType),
447                 "TF_DataType in C cannot be represented as an int in Java");
448   TF_Tensor* t = requireHandle(env, handle);
449   if (t == nullptr) return 0;
450   return static_cast<jint>(TF_TensorType(t));
451 }
452 
Java_org_tensorflow_Tensor_shape(JNIEnv * env,jclass clazz,jlong handle)453 JNIEXPORT jlongArray JNICALL Java_org_tensorflow_Tensor_shape(JNIEnv* env,
454                                                               jclass clazz,
455                                                               jlong handle) {
456   TF_Tensor* t = requireHandle(env, handle);
457   if (t == nullptr) return nullptr;
458   static_assert(sizeof(jlong) == sizeof(int64_t),
459                 "Java long is not compatible with the TensorFlow C API");
460   const jsize num_dims = TF_NumDims(t);
461   jlongArray ret = env->NewLongArray(num_dims);
462   jlong* dims = env->GetLongArrayElements(ret, nullptr);
463   for (int i = 0; i < num_dims; ++i) {
464     dims[i] = static_cast<jlong>(TF_Dim(t, i));
465   }
466   env->ReleaseLongArrayElements(ret, dims, 0);
467   return ret;
468 }
469 
Java_org_tensorflow_Tensor_setValue(JNIEnv * env,jclass clazz,jlong handle,jobject value)470 JNIEXPORT void JNICALL Java_org_tensorflow_Tensor_setValue(JNIEnv* env,
471                                                            jclass clazz,
472                                                            jlong handle,
473                                                            jobject value) {
474   TF_Tensor* t = requireHandle(env, handle);
475   if (t == nullptr) return;
476   int num_dims = TF_NumDims(t);
477   TF_DataType dtype = TF_TensorType(t);
478   void* data = TF_TensorData(t);
479   const size_t sz = TF_TensorByteSize(t);
480   if (num_dims == 0) {
481     writeScalar(env, value, dtype, data, sz);
482   } else {
483     writeNDArray(env, static_cast<jarray>(value), dtype, num_dims,
484                  static_cast<char*>(data), sz);
485   }
486 }
487 
488 #define DEFINE_GET_SCALAR_METHOD(jtype, dtype, method_suffix)                  \
489   JNIEXPORT jtype JNICALL Java_org_tensorflow_Tensor_scalar##method_suffix(    \
490       JNIEnv* env, jclass clazz, jlong handle) {                               \
491     jtype ret = 0;                                                             \
492     TF_Tensor* t = requireHandle(env, handle);                                 \
493     if (t == nullptr) return ret;                                              \
494     if (TF_NumDims(t) != 0) {                                                  \
495       throwException(env, kIllegalStateException, "Tensor is not a scalar");   \
496     } else if (TF_TensorType(t) != dtype) {                                    \
497       throwException(env, kIllegalStateException, "Tensor is not a %s scalar", \
498                      #method_suffix);                                          \
499     } else {                                                                   \
500       memcpy(&ret, TF_TensorData(t), elemByteSize(dtype));                     \
501     }                                                                          \
502     return ret;                                                                \
503   }
504 DEFINE_GET_SCALAR_METHOD(jfloat, TF_FLOAT, Float);
505 DEFINE_GET_SCALAR_METHOD(jdouble, TF_DOUBLE, Double);
506 DEFINE_GET_SCALAR_METHOD(jint, TF_INT32, Int);
507 DEFINE_GET_SCALAR_METHOD(jlong, TF_INT64, Long);
508 DEFINE_GET_SCALAR_METHOD(jboolean, TF_BOOL, Boolean);
509 #undef DEFINE_GET_SCALAR_METHOD
510 
Java_org_tensorflow_Tensor_scalarBytes(JNIEnv * env,jclass clazz,jlong handle)511 JNIEXPORT jbyteArray JNICALL Java_org_tensorflow_Tensor_scalarBytes(
512     JNIEnv* env, jclass clazz, jlong handle) {
513   TF_Tensor* t = requireHandle(env, handle);
514   if (t == nullptr) return nullptr;
515   if (TF_NumDims(t) != 0) {
516     throwException(env, kIllegalStateException, "Tensor is not a scalar");
517     return nullptr;
518   }
519   if (TF_TensorType(t) != TF_STRING) {
520     throwException(env, kIllegalArgumentException,
521                    "Tensor is not a string/bytes scalar");
522     return nullptr;
523   }
524   const TF_TString* data = static_cast<const TF_TString*>(TF_TensorData(t));
525   jbyteArray ret = TF_StringDecodeTojbyteArray(env, &data[0]);
526   return ret;
527 }
528 
Java_org_tensorflow_Tensor_readNDArray(JNIEnv * env,jclass clazz,jlong handle,jobject value)529 JNIEXPORT void JNICALL Java_org_tensorflow_Tensor_readNDArray(JNIEnv* env,
530                                                               jclass clazz,
531                                                               jlong handle,
532                                                               jobject value) {
533   TF_Tensor* t = requireHandle(env, handle);
534   if (t == nullptr) return;
535   int num_dims = TF_NumDims(t);
536   TF_DataType dtype = TF_TensorType(t);
537   const void* data = TF_TensorData(t);
538   const size_t sz = TF_TensorByteSize(t);
539   if (num_dims == 0) {
540     throwException(env, kIllegalArgumentException,
541                    "copyTo() is not meant for scalar Tensors, use the scalar "
542                    "accessor (floatValue(), intValue() etc.) instead");
543     return;
544   }
545   if (dtype == TF_STRING) {
546     int64_t num_elements = 1;
547     for (int i = 0; i < num_dims; ++i) {
548       num_elements *= TF_Dim(t, i);
549     }
550     StringTensorReader reader(t, num_elements);
551     TF_Status* status = TF_NewStatus();
552     readNDStringArray(env, &reader, num_dims, static_cast<jobjectArray>(value),
553                       status);
554     throwExceptionIfNotOK(env, status);
555     TF_DeleteStatus(status);
556     return;
557   }
558   readNDArray(env, dtype, static_cast<const char*>(data), sz, num_dims,
559               static_cast<jarray>(value));
560 }
561