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/graph_jni.h" 17 18 #include <limits> 19 #include "tensorflow/c/c_api.h" 20 #include "tensorflow/java/src/main/native/exception_jni.h" 21 22 namespace { 23 TF_Graph* requireHandle(JNIEnv* env, jlong handle) { 24 static_assert(sizeof(jlong) >= sizeof(TF_Graph*), 25 "Cannot package C object pointers as a Java long"); 26 if (handle == 0) { 27 throwException(env, kIllegalStateException, 28 "close() has been called on the Graph"); 29 return nullptr; 30 } 31 return reinterpret_cast<TF_Graph*>(handle); 32 } 33 } // namespace 34 35 JNIEXPORT jlong JNICALL Java_org_tensorflow_Graph_allocate(JNIEnv*, jclass) { 36 return reinterpret_cast<jlong>(TF_NewGraph()); 37 } 38 39 JNIEXPORT void JNICALL Java_org_tensorflow_Graph_delete(JNIEnv*, jclass, 40 jlong handle) { 41 if (handle == 0) return; 42 TF_DeleteGraph(reinterpret_cast<TF_Graph*>(handle)); 43 } 44 45 JNIEXPORT jlong JNICALL Java_org_tensorflow_Graph_operation(JNIEnv* env, 46 jclass clazz, 47 jlong handle, 48 jstring name) { 49 TF_Graph* g = requireHandle(env, handle); 50 if (g == nullptr) return 0; 51 const char* cname = env->GetStringUTFChars(name, nullptr); 52 TF_Operation* op = TF_GraphOperationByName(g, cname); 53 env->ReleaseStringUTFChars(name, cname); 54 return reinterpret_cast<jlong>(op); 55 } 56 57 JNIEXPORT jlongArray JNICALL Java_org_tensorflow_Graph_nextOperation(JNIEnv* env, 58 jclass clazz, 59 jlong handle, 60 jint position) { 61 TF_Graph* g = requireHandle(env, handle); 62 if (g == nullptr) return nullptr; 63 64 size_t pos = static_cast<size_t>(position); 65 TF_Operation* operation = TF_GraphNextOperation(g, &pos); 66 if (operation == nullptr) return nullptr; 67 68 jlong handle_and_position[2]; 69 handle_and_position[0] = reinterpret_cast<jlong>(operation); 70 handle_and_position[1] = static_cast<jlong>(pos); 71 72 jlongArray rhett = env->NewLongArray(2); 73 env->SetLongArrayRegion(rhett, 0, 2, handle_and_position); 74 return rhett; 75 } 76 77 JNIEXPORT void JNICALL Java_org_tensorflow_Graph_importGraphDef( 78 JNIEnv* env, jclass clazz, jlong handle, jbyteArray graph_def, 79 jstring prefix) { 80 TF_Graph* g = requireHandle(env, handle); 81 if (g == nullptr) return; 82 83 TF_ImportGraphDefOptions* opts = TF_NewImportGraphDefOptions(); 84 85 jboolean is_copy; 86 const char* cprefix = env->GetStringUTFChars(prefix, &is_copy); 87 TF_ImportGraphDefOptionsSetPrefix(opts, cprefix); 88 env->ReleaseStringUTFChars(prefix, cprefix); 89 90 static_assert(sizeof(jbyte) == 1, "unexpected size of the jbyte type"); 91 jbyte* bytes = env->GetByteArrayElements(graph_def, &is_copy); 92 TF_Buffer* buf = 93 TF_NewBufferFromString(bytes, env->GetArrayLength(graph_def)); 94 TF_Status* status = TF_NewStatus(); 95 96 TF_GraphImportGraphDef(g, buf, opts, status); 97 throwExceptionIfNotOK(env, status); 98 // Continue cleaning up resources even if an exception was thrown. 99 100 TF_DeleteStatus(status); 101 TF_DeleteBuffer(buf); 102 env->ReleaseByteArrayElements(graph_def, bytes, JNI_ABORT); 103 104 TF_DeleteImportGraphDefOptions(opts); 105 } 106 107 JNIEXPORT jbyteArray JNICALL 108 Java_org_tensorflow_Graph_toGraphDef(JNIEnv* env, jclass clazz, jlong handle) { 109 jbyteArray ret = nullptr; 110 TF_Graph* g = requireHandle(env, handle); 111 if (g == nullptr) return ret; 112 113 TF_Buffer* buf = TF_NewBuffer(); 114 TF_Status* status = TF_NewStatus(); 115 TF_GraphToGraphDef(g, buf, status); 116 if (throwExceptionIfNotOK(env, status)) { 117 // sizeof(jsize) is less than sizeof(size_t) on some platforms. 118 if (buf->length > std::numeric_limits<jint>::max()) { 119 throwException(env, kIndexOutOfBoundsException, 120 "GraphDef is too large to serialize into a byte[] array"); 121 } else { 122 static_assert(sizeof(jbyte) == 1, "unexpected size of the jbyte type"); 123 jint ret_len = static_cast<jint>(buf->length); 124 ret = env->NewByteArray(ret_len); 125 env->SetByteArrayRegion(ret, 0, ret_len, 126 static_cast<const jbyte*>(buf->data)); 127 } 128 } 129 TF_DeleteStatus(status); 130 TF_DeleteBuffer(buf); 131 return ret; 132 } 133