Home | History | Annotate | Download | only in tensorflow
      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 package org.tensorflow;
     17 
     18 /** Static utility methods describing the TensorFlow runtime. */
     19 public final class TensorFlow {
     20   /** Returns the version of the underlying TensorFlow runtime. */
     21   public static native String version();
     22 
     23   /**
     24    * All the TensorFlow operations available in this address space.
     25    *
     26    * @return A serialized representation of an <a
     27    *     href="https://www.tensorflow.org/code/tensorflow/core/framework/op_def.proto">OpList</a>
     28    *     protocol buffer, which lists all the available TensorFlow operations.
     29    */
     30   public static native byte[] registeredOpList();
     31 
     32   /**
     33    * Load the dynamic library in filename and register the operations and kernels present in that
     34    * library.
     35    *
     36    * @param filename Path of the dynamic library containing operations and kernels to load.
     37    * @return Serialized bytes of the <a
     38    *     href="https://www.tensorflow.org/code/tensorflow/core/framework/op_def.proto">OpList</a>
     39    *     protocol buffer message defining the operations defined in the library.
     40    * @throws UnsatisfiedLinkError if filename cannot be loaded.
     41    */
     42   public static byte[] loadLibrary(String filename) {
     43     long h = 0;
     44     try {
     45       h = libraryLoad(filename);
     46     } catch (RuntimeException e) {
     47       throw new UnsatisfiedLinkError(e.getMessage());
     48     }
     49     try {
     50       return libraryOpList(h);
     51     } finally {
     52       libraryDelete(h);
     53     }
     54   }
     55 
     56   private static native long libraryLoad(String filename);
     57 
     58   private static native void libraryDelete(long handle);
     59 
     60   private static native byte[] libraryOpList(long handle);
     61 
     62   private TensorFlow() {}
     63 
     64   /** Load the TensorFlow runtime C library. */
     65   static void init() {
     66     NativeLibrary.load();
     67   }
     68 
     69   static {
     70     init();
     71   }
     72 }
     73