Home | History | Annotate | Download | only in util
      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 """Utilities for loading op libraries.
     16 
     17 @@load_op_library
     18 """
     19 from __future__ import absolute_import
     20 from __future__ import division
     21 from __future__ import print_function
     22 
     23 import os
     24 import re
     25 
     26 from tensorflow.python.framework import load_library
     27 from tensorflow.python.platform import resource_loader
     28 
     29 
     30 def load_op_library(path):
     31   """Loads a contrib op library from the given path.
     32 
     33   NOTE(mrry): On Windows, we currently assume that some contrib op
     34   libraries are statically linked into the main TensorFlow Python
     35   extension DLL - use dynamically linked ops if the .so is present.
     36 
     37   Args:
     38     path: An absolute path to a shared object file.
     39 
     40   Returns:
     41     A Python module containing the Python wrappers for Ops defined in the
     42     plugin.
     43   """
     44   if os.name == 'nt':
     45     # To avoid makeing every user_ops aware of windows, re-write
     46     # the file extension from .so to .dll.
     47     path = re.sub(r'\.so$', '.dll', path)
     48 
     49     # Currently we have only some user_ops as dlls on windows - don't try
     50     # to load them if the dll is not found.
     51     # TODO(mrry): Once we have all of them this check should be removed.
     52     if not os.path.exists(path):
     53       return None
     54   path = resource_loader.get_path_to_datafile(path)
     55   ret = load_library.load_op_library(path)
     56   assert ret, 'Could not load %s' % path
     57   return ret
     58