Esempio n. 1
0
    def load(self, graph: Graph):
        argv = graph.graph['cmd_params']
        if argv.tensorflow_custom_layer_libraries:
            libraries = argv.tensorflow_custom_layer_libraries.split(',')
            for library in libraries:
                log.info('Loading library "{}" with custom operations'.format(library))
                tf_v1.load_op_library(library)

        graph_def, variables_values = load_tf_graph_def(graph_file_name=argv.input_model,
                                                        is_binary=not argv.input_model_is_text,
                                                        checkpoint=argv.input_checkpoint,
                                                        user_output_node_names_list=argv.output,
                                                        model_dir=argv.saved_model_dir,
                                                        meta_graph_file=argv.input_meta_graph,
                                                        saved_model_tags=argv.saved_model_tags)

        try:
            tf_v1.import_graph_def(graph_def, name='')
        except:
            log.warning("TensorFlow post-processing of loaded model was unsuccessful. "
                        "This is an optional step that Model Optimizer performs for any input model but it is not usually "
                        "required for all models."
                        "It likely means that the original model is ill-formed. "
                        "Model Optimizer will continue converting this model.")

        log.debug("Number of nodes in graph_def: {}".format(len(graph_def.node)))  # pylint: disable=no-member

        if argv.tensorboard_logdir:
            tensorboard_util.dump_for_tensorboard(graph_def, argv.tensorboard_logdir)

        update_extractors_with_extensions(tf_op_extractors)

        try:
            protobuf2nx(graph, graph_def)
        except Exception as e:
            raise Error(
                'Cannot pre-process TensorFlow graph after reading from model file "{}". ' \
                'File is corrupt or has unsupported format. Details: {}. ' +
                refer_to_faq_msg(44),
                argv.model_name,
                str(e)
            ) from e

        graph.__setattr__('name', argv.model_name)
        # 'layout' parameter change may cause an issue in EltwiseInputReshape replacer
        # and convert_nhwc_to_nchw(graph)
        graph.graph['layout'] = 'NCHW' if argv.disable_nhwc_to_nchw else 'NHWC'
        graph.graph['fw'] = 'tf'

        graph.graph['variables_values'] = variables_values
        del variables_values

        restore_edges(graph, get_tf_edges)
        remove_control_dependency_inputs(graph)

        graph.check_empty_graph('protobuf2nx. It may happen due to problems with loaded model')
        extract_node_attrs(graph, lambda node: tf_op_extractor(node, check_for_duplicates(tf_op_extractors)))
Esempio n. 2
0
def _custom_cpp_op(op: CompilableOp, stateful, name):
    """ Compiles and registers a custom C++ Tensorflow operator """
    # Compile the .so file
    tf_path = os.path.abspath(os.path.dirname(tf.__file__))

    so_file = TFCompiler().compile_op(
        op.name,
        op.files,
        op.inputs,
        op.outputs,
        any([f.endswith('.cu') for f in op.files]),
        op.live_output,
        additional_cmake_options=['-DTENSORFLOW_PATH=' + tf_path] +
        op.cmake_options,
        additional_definitions=op.defs,
        output_folder=op.output_folder)

    # Load the compiled library into Tensorflow
    op_module = tf.load_op_library(so_file)
    op_func = getattr(op_module, 'tf_op' + op.name)
    op_grad_func = getattr(op_module, 'tf_op_grad' + op.name)

    # Create the deep500 custom op object
    lib = ctypes.CDLL(so_file)
    if not getattr(lib, 'create_new_op', False):
        raise ValueError('Invalid custom operator library file')
    lib.create_new_op.restype = ctypes.c_int64
    lib.is_cuda_supported.restype = ctypes.c_bool
    lib.report.restype = ctypes.c_int64

    return TFCompiledOp(op, op_func, op_grad_func, lib)
def testCustomOp(feedMat, corpus, chars, wordChars):
    "decode using word beam search. Result is tuple, first entry is label string, second entry is char string."

    # TF session
    sess = tfv1.Session()
    sess.run(tfv1.global_variables_initializer())

    # load custom TF op
    word_beam_search_module = tfv1.load_op_library(
        '../cpp/proj/TFWordBeamSearch.so')

    # input with shape TxBxC
    mat = tfv1.placeholder(tf.float32, shape=feedMat.shape)

    # decode using the "Words" mode of word beam search with beam width set to 25 and add-k smoothing to 0.0
    assert len(chars) + 1 == mat.shape[2]
    decode = word_beam_search_module.word_beam_search(mat, 25, 'Words', 0.0,
                                                      corpus.encode('utf8'),
                                                      chars.encode('utf8'),
                                                      wordChars.encode('utf8'))

    # feed matrix of shape TxBxC and evaluate TF graph
    res = sess.run(decode, {mat: feedMat})

    # result is string of labels terminated by blank (similar to C-strings) if shorter than T
    blank = len(chars)
    s = ''
    for label in res[0]:
        if label == blank:
            break
        s += chars[label]  # map label to char
    return (res[0], s)
Esempio n. 4
0
	def setupCTC(self):
		"create CTC loss and decoder and return them"
		# BxTxC -> TxBxC
		self.ctcIn3dTBC = tf.transpose(self.rnnOut3d, [1, 0, 2])
		# ground truth text as sparse tensor
		self.gtTexts = tf.SparseTensor(tf.placeholder(tf.int64, shape=[None, 2]) , tf.placeholder(tf.int32, [None]), tf.placeholder(tf.int64, [2]))

		# calc loss for batch
		self.seqLen = tf.placeholder(tf.int32, [None])
		self.loss = tf.reduce_mean(tf.nn.ctc_loss(labels=self.gtTexts, inputs=self.ctcIn3dTBC, sequence_length=self.seqLen, ctc_merge_repeated=True))

		# calc loss for each element to compute label probability
		self.savedCtcInput = tf.placeholder(tf.float32, shape=[Model.maxTextLen, None, len(self.charList) + 1])
		self.lossPerElement = tf.nn.ctc_loss(labels=self.gtTexts, inputs=self.savedCtcInput, sequence_length=self.seqLen, ctc_merge_repeated=True)

		# decoder: either best path decoding or beam search decoding
		if self.decoderType == DecoderType.BestPath:
			self.decoder = tf.nn.ctc_greedy_decoder(inputs=self.ctcIn3dTBC, sequence_length=self.seqLen)
		elif self.decoderType == DecoderType.BeamSearch:
			self.decoder = tf.nn.ctc_beam_search_decoder(inputs=self.ctcIn3dTBC, sequence_length=self.seqLen, beam_width=50, merge_repeated=False)
		elif self.decoderType == DecoderType.WordBeamSearch:
			# import compiled word beam search operation (see https://github.com/githubharald/CTCWordBeamSearch)
			word_beam_search_module = tf.load_op_library('TFWordBeamSearch.so')

			# prepare information about language (dictionary, characters in dataset, characters forming words)
			chars = str().join(self.charList)
			wordChars = open('./model/model/wordCharList.txt').read().splitlines()[0]
			corpus = open('./data/corpus.txt').read()

			# decode using the "Words" mode of word beam search
			self.decoder = word_beam_search_module.word_beam_search(tf.nn.softmax(self.ctcIn3dTBC, dim=2), 50, 'Words', 0.0, corpus.encode('utf8'), chars.encode('utf8'), wordChars.encode('utf8'))
    def setupCTC(self):
        """ Create CTC loss and decoder and return them """
        # BxTxC -> TxBxC
        self.ctcIn3dTBC = tf.transpose(a=self.rnnOut3d, perm=[1, 0, 2])

        # Ground truth text as sparse tensor
        with tf.compat.v1.name_scope('CTC_Loss'):
            self.gtTexts = tf.SparseTensor(
                tf.compat.v1.placeholder(tf.int64, shape=[None, 2]),
                tf.compat.v1.placeholder(tf.int32, [None]),
                tf.compat.v1.placeholder(tf.int64, [2]))
            # Calculate loss for batch
            self.seqLen = tf.compat.v1.placeholder(tf.int32, [None])
            self.loss = tf.reduce_mean(input_tensor=tf.compat.v1.nn.ctc_loss(
                labels=self.gtTexts,
                inputs=self.ctcIn3dTBC,
                sequence_length=self.seqLen,
                ctc_merge_repeated=True,
                ignore_longer_outputs_than_inputs=True))
        with tf.compat.v1.name_scope('CTC_Decoder'):
            # Decoder: Best path decoding or Word beam search decoding
            if self.decoderType == DecoderType.BestPath:
                self.decoder = tf.nn.ctc_greedy_decoder(
                    inputs=self.ctcIn3dTBC, sequence_length=self.seqLen)
            elif self.decoderType == DecoderType.BeamSearch:
                self.decoder = tf.compat.v1.nn.ctc_beam_search_decoder(
                    inputs=self.ctcIn3dTBC,
                    sequence_length=self.seqLen,
                    beam_width=50,
                    merge_repeated=True)
            elif self.decoderType == DecoderType.WordBeamSearch:
                # Import compiled word beam search operation (see https://github.com/githubharald/CTCWordBeamSearch)
                word_beam_search_module = tf.load_op_library(
                    './TFWordBeamSearch.so')

                # Prepare: dictionary, characters in dataset, characters forming words
                chars = codecs.open(FilePaths.wordCharList.txt, 'r').read()
                wordChars = codecs.open(FilePaths.fnWordCharList, 'r').read()
                corpus = codecs.open(FilePaths.corpus.txt, 'r').read()

                # # Decoder using the "NGramsForecastAndSample": restrict number of (possible) next words to at most 20 words: O(W) mode of word beam search
                # decoder = word_beam_search_module.word_beam_search(tf.nn.softmax(ctcIn3dTBC, dim=2), 25, 'NGramsForecastAndSample', 0.0, corpus.encode('utf8'), chars.encode('utf8'), wordChars.encode('utf8'))

                # Decoder using the "Words": only use dictionary, no scoring: O(1) mode of word beam search
                self.decoder = word_beam_search_module.word_beam_search(
                    tf.nn.softmax(self.ctcIn3dTBC, axis=2), 25, 'Words', 0.0,
                    corpus.encode('utf8'), chars.encode('utf8'),
                    wordChars.encode('utf8'))

        # Return a CTC operation to compute the loss and CTC operation to decode the RNN output
        return self.loss, self.decoder
Esempio n. 6
0
def load_fl_ops_lib():

    global _fl_ops_so
    # Use double-checked locking to avoid taking lock unnecessarily.
    if _fl_ops_so:
        return _fl_ops_so

    _fl_ops_so_lock.acquire()

    try:
        if _fl_ops_so:
            return _fl_ops_so

        _fl_ops_so = tf.load_op_library(dir_path + "/_fl_ops.so")
        return _fl_ops_so

    finally:
        _fl_ops_so_lock.release()
Esempio n. 7
0
def _loader_ops(so_path):
  """ Post-building custom ops loading operations.
  Args:
    so_path Shared object path
  """
  try:
    lib = tf.load_op_library(str(so_path))
    entries = lib.OP_LIST.ListFields()[0][1]
    try:
      while True:
        opname = entries.pop().ListFields()[0][1]
        opname = _camel_to_snake(opname)
        register_op(opname, getattr(lib, opname))
    except IndexError:
      pass
  except Exception as err:
    with tools.Context(so_path.stem, "warning"):
      print("Loading failed for custom op " + repr(str(so_path)) + ": " + str(err))
      with tools.Context("traceback", "trace"):
        traceback.print_exc()
Esempio n. 8
0
    def _processor(self, in_q, out_q, batch, rc):
        tf.reset_default_graph()
        processor_graph = tf.Graph()
        with processor_graph.as_default() as g1:
            with g1.name_scope("processor_graph") as scope:
                op_module = tf.load_op_library('libkernel.so')
                inp_batch = tf.placeholder(dtype=float,
                                           shape=(batch, 3, 416, 416),
                                           name="place_holder1")
                out_s, out_m, out_l = op_module.My_Yolo_OP(
                    yolo_input=inp_batch)
                init_op = tf.initialize_all_variables()

        with tf.Session(graph=processor_graph) as sess:
            sess.run(init_op)
            while True:
                curr_batch = in_q.get()
                if curr_batch == 'STOP':
                    out_q.put('STOP')
                    break
                yolo_out_s, yolo_out_m, yolo_out_l = sess.run(
                    [out_s, out_m, out_l], feed_dict={inp_batch: curr_batch})
                rc.tick()
                out_q.put((yolo_out_s, yolo_out_m, yolo_out_l))
Esempio n. 9
0
''' Furthest point sampling
Original author: Haoqiang Fan
Modified by Charles R. Qi
All Rights Reserved. 2017. 
'''
#import tensorflow as tf
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()
from tensorflow.python.framework import ops
import sys
import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sampling_module = tf.load_op_library(
    os.path.join(BASE_DIR, 'tf_sampling_so.so'))


def prob_sample(inp, inpr):
    '''
input:
    batch_size * ncategory float32
    batch_size * npoints   float32
returns:
    batch_size * npoints   int32
    '''
    return sampling_module.prob_sample(inp, inpr)


ops.NoGradient('ProbSample')
Esempio n. 10
0
import tensorflow.compat.v1 as tf
import os

#module_path = os.path.dirname(os.path.realpath(__file__))
#module_file = os.path.join(module_path, 'tf_mpi_ops.so')
#assert (os.path.isfile(module_file)), 'module tf_mpi_ops does not exist'
#mpi_ops = tf.load_op_library(module_file)
mpi_ops = tf.load_op_library('/home/zhaoxc/workspace/sw_hvd/sw_horovod/common/mpi_ops/mpi_ops/tf_mpi_ops.so')

def broadcast(in_tensor, root):
    return mpi_ops.tf_broadcast(in_tensor, root=root)

def allreduce(in_tensor):
    return mpi_ops.tf_allreduce(in_tensor)

def allgather(in_tensor, size):
    return mpi_ops.tf_allgather(in_tensor, size=size)

def gather(in_tensor, root, rank, size):
    return mpi_ops.tf_gather(in_tensor, root=root, rank=rank, size=size)

def allreduces(in_tensor_list, high_prec=0):
    print("****************************now in allreduces*********************************\n")
    print("about to get into mpi_ops.tf_allreduces\n")
    return mpi_ops.tf_allreduces(in_tensor_list, precision=high_prec)

def broadcasts(in_tensor_list, root):
    return mpi_ops.tf_broadcasts(in_tensor_list, root=root)
Esempio n. 11
0
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()
print(tf.__version__)

row_widths = tf.placeholder(dtype=tf.int32, shape=[None], name="width")
batch_size = tf.size(row_widths)
max_width = tf.reduce_max(row_widths)
indices = tf.to_int64(tf.where(tf.sequence_mask(row_widths, max_width)))
dense_shapes = tf.to_int64([batch_size, max_width])

with tf.Session() as sess:
    x, y = sess.run([indices, dense_shapes],
                    feed_dict={"width:0": [2, 3, 4, 5]})
    print(x)
    print(x.shape)
    print(y)

print("begin custom op")
sparse_module = tf.load_op_library('./sparse_transformer.so')
z = sparse_module.sparse_transformer(row_widths)
print("get the shape before run")
print(z.indices.get_shape())
print(z.shape.get_shape())
print("begin to run")
with tf.Session() as sess:
    x, y = sess.run(z, feed_dict={"width:0": [2, 3, 4, 5]})
    print(x.shape)
    print(x)
    print(y)
Esempio n. 12
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Main code for creating the tree ensemble layer."""
import os
import tensorflow.compat.v1 as tf
from tensorflow import keras
from tensorflow.python.framework import ops
from tensorflow.keras.initializers import RandomUniform

# Assumes that neural_trees_ops.so is in tf_trees.
dir_path = os.path.dirname(os.path.realpath(__file__))
tf_trees_module = tf.load_op_library(dir_path + '/neural_trees_ops.so')


# Register the custom gradient for NTComputeOutputOp.
@ops.RegisterGradient('NTComputeOutputOp')
def _nt_compute_input_and_internal_params_gradients_op_cc(
    op, grad_loss_wrt_tree_output, _):
  """Associated a custom gradient with an op."""
  output_logits_dim = op.get_attr('output_logits_dim')
  depth = op.get_attr('depth')
  parallelize_over_samples = op.get_attr('parallelize_over_samples')
  return [
      tf_trees_module.nt_compute_input_and_internal_params_gradients_op(
          grad_loss_wrt_tree_output, op.inputs[0], op.inputs[1], op.inputs[2],
          op.inputs[3], output_logits_dim, depth, parallelize_over_samples),
      None
Esempio n. 13
0
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import candidate_sampling_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import embedding_ops
from tensorflow.python.ops import gen_array_ops
from tensorflow.python.ops import gen_nn_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import sparse_ops
from tensorflow.python.ops import variables
from tensorflow.python.util.deprecation import deprecated_args
from tensorflow.python.util.deprecation import deprecated_argument_lookup
from tensorflow.python.util.tf_export import tf_export

mpu_sim_conv2d_lib = tf.load_op_library(
    '../../bin/build_mpusim_conv2d_release/mpusim-conv2d.so')


def mpusim_separable_conv2d_impl(input,
                                 depthwise_filter,
                                 pointwise_filter,
                                 strides,
                                 padding,
                                 rate=None,
                                 name=None,
                                 data_format=None,
                                 activations_datatype_size_byte=1,
                                 weights_datatype_size_byte=1,
                                 results_datatype_size_byte=4,
                                 systolic_array_height=256,
                                 systolic_array_width=256,
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""When building as a dynamic .so provide a hook to load it at runtime."""
import tensorflow.compat.v1 as tf

# TODO: Store the returned object so application code can use it.
tf.load_op_library(
    'multidim_image_augmentation/python/ops/_augmentation_ops.so')
Esempio n. 15
0
                    log_device_placement=True)) as sess:
                inputs_dict = {
                    self.input_tensor: self.input_data,
                    self.filter_tensor: self.filter_data
                }

                sess.run(tf.compat.v1.global_variables_initializer())

                result = sess.run(
                    tf.abs(self.output_tensor - self.output_ref_tensor) /
                    tf.reduce_max(tf.abs(self.output_ref_tensor)),
                    feed_dict=inputs_dict)
                print('{}: Linf Error: {}'.format(device, np.max(result)))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--device',
        type=str,
        help='Tensorflow device to be used (ie. \'cpu:0\', \'gpu:0\', ...).',
        default='cpu:0')

    args = parser.parse_args()

    test_op_module = tf.load_op_library('libApproxGPUOpsTF.so')

    test = TestApproxConv2D(test_op_module, [1, 256, 256, 1], [8, 8, 1, 1],
                            [1, 1, 1, 1])
    test.run(args.device)
Esempio n. 16
0
def get_module(module_name: str) -> "ModuleType":
    """Load force module.

    Returns
    -------
    ModuleType
        loaded force module

    Raises
    ------
    FileNotFoundError
        if module is not found in directory
    """
    if platform.system() == "Windows":
        ext = ".dll"
    #elif platform.system() == "Darwin":
    #    ext = ".dylib"
    else:
        ext = ".so"

    module_file = ((Path(__file__).parent / SHARED_LIB_MODULE /
                    module_name).with_suffix(ext).resolve())

    if not module_file.is_file():
        raise FileNotFoundError(f"module {module_name} does not exist")
    else:
        try:
            module = tf.load_op_library(str(module_file))
        except tf.errors.NotFoundError as e:
            # check CXX11_ABI_FLAG is compatiblity
            # see https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
            # ABI should be the same
            if 'CXX11_ABI_FLAG' in tf.__dict__:
                tf_cxx11_abi_flag = tf.CXX11_ABI_FLAG
            else:
                tf_cxx11_abi_flag = tf.sysconfig.CXX11_ABI_FLAG
            if TF_CXX11_ABI_FLAG != tf_cxx11_abi_flag:
                raise RuntimeError(
                    "This deepmd-kit package was compiled with "
                    "CXX11_ABI_FLAG=%d, but TensorFlow runtime was compiled "
                    "with CXX11_ABI_FLAG=%d. These two library ABIs are "
                    "incompatible and thus an error is raised when loading %s. "
                    "You need to rebuild deepmd-kit against this TensorFlow "
                    "runtime." % (
                        TF_CXX11_ABI_FLAG,
                        tf_cxx11_abi_flag,
                        module_name,
                    )) from e

            # different versions may cause incompatibility
            # see #406, #447, #557, #774, and #796 for example
            # throw a message if versions are different
            if TF_VERSION != tf_py_version:
                raise RuntimeError(
                    "The version of TensorFlow used to compile this "
                    "deepmd-kit package is %s, but the version of TensorFlow "
                    "runtime you are using is %s. These two versions are "
                    "incompatible and thus an error is raised when loading %s. "
                    "You need to install TensorFlow %s, or rebuild deepmd-kit "
                    "against TensorFlow %s.\nIf you are using a wheel from "
                    "pypi, you may consider to install deepmd-kit execuating "
                    "`pip install deepmd-kit --no-binary deepmd-kit` "
                    "instead." % (
                        TF_VERSION,
                        tf_py_version,
                        module_name,
                        TF_VERSION,
                        tf_py_version,
                    )) from e
            raise RuntimeError(
                "This deepmd-kit package is inconsitent with TensorFlow "
                "Runtime, thus an error is raised when loading %s. "
                "You need to rebuild deepmd-kit against this TensorFlow "
                "runtime." % (module_name, )) from e
        return module
Esempio n. 17
0
from tensorflow.python import pywrap_tensorflow
import pkg_resources

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

coref_op_library = tf.load_op_library(
    pkg_resources.resource_filename("e2edutch", "lib/coref_kernels.so"))

extract_spans = coref_op_library.extract_spans
tf.NotDifferentiable("ExtractSpans")
Esempio n. 18
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os.path
# This is a placeholder for a Google-internal import.
import tensorflow.compat.v1 as tf

from tensorflow_serving.experimental.tensorflow.ops.remote_predict.ops import gen_remote_predict_op
# pylint: disable=wildcard-import
from tensorflow_serving.experimental.tensorflow.ops.remote_predict.ops.gen_remote_predict_op import *
# pylint: enable=wildcard-import

_remote_predict_op_module = tf.load_op_library(
    os.path.join(tf.compat.v1.resource_loader.get_data_files_path(),
                 '_remote_predict_op.so'))


# Aliases
def run(input_tensor_alias,
        input_tensors,
        output_tensor_alias,
        target_address,
        model_name,
        model_version=-1,
        max_rpc_deadline_millis=3000,
        output_types=None,
        name=None,
        signature_name='serving_default'):
    """Runs a predict in remote process through rpc.
Esempio n. 19
0
import tensorflow.compat.v1 as tf
from tensorflow.python import pywrap_tensorflow

srl_op_library = tf.load_op_library("./srl_kernels.so")

extract_spans = srl_op_library.extract_spans
tf.no_gradient("ExtractSpans")

Esempio n. 20
0
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for ORQA ops."""
# from language.orqa import ops as orqa_ops
import tensorflow.compat.v1 as tf

orqa_ops = tf.load_op_library('exp/rlmfetops.so')


class OrqaOpsTest(tf.test.TestCase):
    def test_reader_inputs(self):
        concat_inputs = orqa_ops.reader_inputs(question_token_ids=[0, 1],
                                               block_token_ids=[[2, 3, 4],
                                                                [5, 6, 0]],
                                               block_lengths=[3, 2],
                                               block_token_map=[[1, 2, 5],
                                                                [1, 3, 4]],
                                               answer_token_ids=[[3, 4],
                                                                 [7, 0]],
                                               answer_lengths=[2, 1],
                                               cls_token_id=10,
                                               sep_token_id=11,
Esempio n. 21
0
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

zero_out_module = tf.load_op_library('./zero_out.so')
x = zero_out_module.zero_out([[1, 2], [3, 4]])
with tf.Session() as sess:
	y = sess.run(x)
	print(x.shape)
	print(y)
Esempio n. 22
0
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from tensorflow.python.framework import ops
import sys
import os
BASE_DIR = os.path.dirname(__file__)
sys.path.append(BASE_DIR)
interpolate_module=tf.load_op_library(os.path.join(BASE_DIR, 'tf_interpolate_so.so'))
def three_nn(xyz1, xyz2):
    '''
    Input:
        xyz1: (b,n,3) float32 array, unknown points
        xyz2: (b,m,3) float32 array, known points
    Output:
        dist: (b,n,3) float32 array, distances to known points
        idx: (b,n,3) int32 array, indices to known points
    '''
    return interpolate_module.three_nn(xyz1, xyz2)
ops.NoGradient('ThreeNN')
def three_interpolate(points, idx, weight):
    '''
    Input:
        points: (b,m,c) float32 array, known points
        idx: (b,n,3) int32 array, indices to known points
        weight: (b,n,3) float32 array, weights on known points
    Output:
        out: (b,n,c) float32 array, interpolated point values
    '''
    return interpolate_module.three_interpolate(points, idx, weight)
@tf.RegisterGradient('ThreeInterpolate')
Esempio n. 23
0
_HOME = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../'))

_CPU_LIB_PATH = os.path.join(_HOME, 'cc/embedding.so')


class custom_fedlearner_operators_failed_to_load(object):
    pass


lagrange_lite_ops = custom_fedlearner_operators_failed_to_load

path = _CPU_LIB_PATH

if os.path.exists(path):
    lagrange_lite_ops = tf.load_op_library(path)

if lagrange_lite_ops is custom_fedlearner_operators_failed_to_load:
    print("Failed to load fedlearner operators from %s" % path)


def _multidevice_preprocess_fids(fids, config, num_shards):
    name = config['name']
    with tf.name_scope("lagrange_multidevice_preprocess_fid/%s" % name):
        slot_weight_index = tf.constant(config['slot_weight_index'],
                                        dtype=tf.int64)
        slot_hash_size = tf.constant(config['slot_hash_size'], dtype=tf.int64)
        slot_weight_offset = tf.constant(config['slot_weight_offset'],
                                         dtype=tf.int64)

        ret = lagrange_lite_ops.lagrange_multi_device_preprocess_fid(
def driver(argv: argparse.Namespace):
    """
    Convert TF GraphDef object to NetworkX representation.
    The resulting graph is still TF-specific and needs normalization passes to be applied.
    The specific TF structure assumes each GraphDef node is converted to a single
    NetworkX node, node id is an original TF node name, and edges go directly from one op   to another op.
    """

    if argv.tensorflow_custom_layer_libraries:
        libraries = argv.tensorflow_custom_layer_libraries.split(',')
        for library in libraries:
            log.info(
                'Loading library "{}" with custom operations'.format(library))
            tf_v1.load_op_library(library)

    graph_def, variables_values = load_tf_graph_def(
        graph_file_name=argv.input_model,
        is_binary=not argv.input_model_is_text,
        checkpoint=argv.input_checkpoint,
        user_output_node_names_list=argv.output,
        model_dir=argv.saved_model_dir,
        meta_graph_file=argv.input_meta_graph,
        saved_model_tags=argv.saved_model_tags)

    try:
        tf_v1.import_graph_def(graph_def, name='')
    except:
        log.warning(
            "TensorFlow post-processing of loaded model was unsuccessful. "
            "This is an optional step that Model Optimizer performs for any input model but it is not usually "
            "required for all models."
            "It likely means that the original model is ill-formed. "
            "Model Optimizer will continue converting this model.")

    log.debug("Number of nodes in graph_def: {}".format(len(graph_def.node)))  # pylint: disable=no-member

    if argv.tensorboard_logdir:
        tensorboard_util.dump_for_tensorboard(graph_def,
                                              argv.tensorboard_logdir)

    update_extractors_with_extensions(tf_op_extractors)

    try:
        graph = protobuf2nx(graph_def)
        graph.__setattr__('name', argv.model_name)
        # 'layout' parameter change may cause an issue in EltwiseInputReshape replacer
        # and convert_nhwc_to_nchw(graph)
        graph.graph['layout'] = 'NCHW' if argv.disable_nhwc_to_nchw else 'NHWC'
        graph.graph['cmd_params'] = argv
        graph.graph['fw'] = 'tf'
        graph.graph['ir_version'] = get_ir_version(argv)

        graph.graph['variables_values'] = variables_values
        del variables_values

        graph = restore_edges(graph, get_tf_edges)
        graph = remove_control_dependency_inputs(graph)
    except Exception as e:
        raise Error(
            'Cannot pre-process TensorFlow graph after reading from model file "{}". ' \
            'File is corrupt or has unsupported format. Details: {}. ' +
            refer_to_faq_msg(44),
            argv.model_name,
            str(e)
        ) from e

    graph.check_empty_graph(
        'protobuf2nx. It may happen due to problems with loaded model')
    extract_node_attrs(
        graph, lambda node: tf_op_extractor(
            node, check_for_duplicates(tf_op_extractors)))

    # --------------------------------- LOAD END ------------------------------------------------------

    class_registration.apply_replacements(graph, [
        class_registration.ClassType.FRONT_REPLACER,
        class_registration.ClassType.MIDDLE_REPLACER,
        class_registration.ClassType.BACK_REPLACER
    ])
    return graph
Esempio n. 25
0
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
''' python custom ops '''
import tensorflow.compat.v1 as tf
from absl import logging

so_lib_file = tf.io.gfile.glob(tf.resource_loader.get_data_files_path() +
                               '/x_ops*.so')[0].split('/')[-1]
path = tf.resource_loader.get_path_to_datafile(so_lib_file)
logging.info('x_ops.so path:{}'.format(path))

gen_x_ops = tf.load_op_library(
    tf.resource_loader.get_path_to_datafile(so_lib_file))

spectrum = gen_x_ops.spectrum
fbank = gen_x_ops.fbank
delta_delta = gen_x_ops.delta_delta
pitch = gen_x_ops.pitch
mfcc = gen_x_ops.mfcc_dct
frame_pow = gen_x_ops.frame_pow
speed = gen_x_ops.speed
Esempio n. 26
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import tensorflow.compat.v1 as tf
from tensorflow.python.framework import ops

_op_library = tf.load_op_library(
    os.path.join(os.path.dirname(__file__), 'tf_nndistance2.so'))


def nn_distance2(xyz1, xyz2):
    """
    Computes the distance of nearest neighbors for a pair of point clouds.

    Args:
        xyz1: (batch_size, n1, 3) the first point cloud
        xyz2: (batch_size, n2, 3) the second point cloud

    Returns:
        (dist1, idx1, dist2, idx2)
        dist1: (batch_size, n1)  squared distance from first to second
        idx1:  (batch_size, n1)  nearest neighbor from first to second
        dist2: (batch_size, n2)  squared distance from second to first
        idx2:  (batch_size, n2)  nearest neighbor from second to first
    """
    return _op_library.nn_distance2(xyz1, xyz2)


@ops.RegisterGradient('NnDistance2')
Esempio n. 27
0
    def load(self, graph: Graph):
        argv = graph.graph['cmd_params']
        if argv.tensorflow_custom_layer_libraries:
            libraries = argv.tensorflow_custom_layer_libraries.split(',')
            for library in libraries:
                log.info('Loading library "{}" with custom operations'.format(
                    library))
                tf_v1.load_op_library(library)

        graph_def, variables_values, framework, inputs_outputs_order = load_tf_graph_def(
            graph_file_name=argv.input_model,
            is_binary=not argv.input_model_is_text,
            checkpoint=argv.input_checkpoint,
            user_output_node_names_list=argv.output,
            model_dir=argv.saved_model_dir,
            meta_graph_file=argv.input_meta_graph,
            saved_model_tags=argv.saved_model_tags)

        if inputs_outputs_order is not None and isinstance(
                inputs_outputs_order, tuple):
            graph.inputs_order = inputs_outputs_order[0]
            graph.outputs_order = inputs_outputs_order[1]

        send_framework_info(framework)

        try:
            tf_v1.import_graph_def(graph_def, name='')
        except:
            log.warning(
                "TensorFlow post-processing of loaded model was unsuccessful. "
                "This is an optional step that Model Optimizer performs for any input model but it is not usually "
                "required for all models. "
                "It likely means that the original model is ill-formed. "
                "Model Optimizer will continue converting this model.")

        log.debug("Number of nodes in graph_def: {}".format(len(
            graph_def.node)))  # pylint: disable=no-member

        if argv.tensorboard_logdir:
            tensorboard_util.dump_for_tensorboard(graph_def,
                                                  argv.tensorboard_logdir)

        update_extractors_with_extensions(tf_op_extractors)

        try:
            protobuf2nx(graph, graph_def)
        except Exception as e:
            raise Error(
                'Cannot pre-process TensorFlow graph after reading from model file "{}". ' \
                'File is corrupt or has unsupported format. Details: {}. ' +
                refer_to_faq_msg(44),
                argv.model_name,
                str(e)
            ) from e

        graph.__setattr__('name', argv.model_name)
        # 'layout' parameter change may cause an issue in EltwiseInputReshape replacer
        # and convert_nhwc_to_nchw(graph)
        graph.graph['layout'] = 'NCHW' if argv.disable_nhwc_to_nchw else 'NHWC'
        graph.graph['fw'] = 'tf'

        graph.graph['variables_values'] = variables_values
        del variables_values

        used_tensors = restore_edges(graph, get_tf_edges)

        # Tensor names information corresponding to a node is stored on outgoing edges.
        # As output nodes do not have outgoing edges, fake outputs are required. In the following code
        # for each output Identity node is added, and tensor name for the output is kept
        # on (output, fake output) edge. After Result nodes adding transformation fake outputs
        # are deleted from graph.
        add_outputs_identity(
            graph, graph.nodes - used_tensors,
            lambda g, output, fake_node_name: g.add_edges_from(
                [create_tf_edge(output, fake_node_name, 0)]))

        remove_control_dependency_inputs(graph)

        graph.check_empty_graph(
            'protobuf2nx. It may happen due to problems with loaded model')
        extract_node_attrs(
            graph, lambda node: tf_op_extractor(
                node, check_for_duplicates(tf_op_extractors)))

        # try to detect layout from the nodes of the graph. If there are no convolution nodes in N(D)HWC layout then we
        # consider that the graph is in NCHW layout and no layout conversion should be performed
        if not argv.disable_nhwc_to_nchw and not graph_or_sub_graph_has_nhwc_ops(
                graph):
            if not argv.silent:
                log.debug('disable_nhwc_to_nchw" was automatically enabled.')
            for_graph_and_each_sub_graph_recursively(
                graph, update_cmd_params_and_layout)

        send_op_names_info(framework, graph)
        send_shapes_info(framework, graph)
Esempio n. 28
0
# coding=utf-8
# Copyright 2022 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Exposes C++ operations in Python."""
import tensorflow.compat.v1 as tf

try:
    from sgk.sparse.ops.cc import gen_sparse_ops  # pylint: disable=g-import-not-at-top
    kernels = gen_sparse_ops
except ImportError:
    # TODO(tgale): Avoid harcoding the library path.
    kernels = tf.load_op_library("/usr/local/sgk/lib/libsgk.so")
Esempio n. 29
0
def get_plugin(cuda_file, extra_nvcc_options=[]):
    cuda_file_base = os.path.basename(cuda_file)
    cuda_file_name, cuda_file_ext = os.path.splitext(cuda_file_base)

    # Already in cache?
    if cuda_file in _plugin_cache:
        return _plugin_cache[cuda_file]

    # Setup plugin.
    if verbose:
        print('Setting up TensorFlow plugin "%s": ' % cuda_file_base,
              end='',
              flush=True)
    try:
        # Hash CUDA source.
        md5 = hashlib.md5()
        with open(cuda_file, 'rb') as f:
            md5.update(f.read())
        md5.update(b'\n')

        # Hash headers included by the CUDA code by running it through the preprocessor.
        if not do_not_hash_included_headers:
            if verbose:
                print('Preprocessing... ', end='', flush=True)
            with tempfile.TemporaryDirectory() as tmp_dir:
                tmp_file = os.path.join(
                    tmp_dir, cuda_file_name + '_tmp' + cuda_file_ext)
                _run_cmd(
                    _prepare_nvcc_cli(
                        '"%s" --preprocess -o "%s" --keep --keep-dir "%s"' %
                        (cuda_file, tmp_file, tmp_dir)))
                with open(tmp_file, 'rb') as f:
                    bad_file_str = (
                        '"' + cuda_file.replace('\\', '/') + '"').encode(
                            'utf-8')  # __FILE__ in error check macros
                    good_file_str = ('"' + cuda_file_base +
                                     '"').encode('utf-8')
                    for ln in f:
                        if not ln.startswith(b'# ') and not ln.startswith(
                                b'#line '):  # ignore line number pragmas
                            ln = ln.replace(bad_file_str, good_file_str)
                            md5.update(ln)
                    md5.update(b'\n')

        # Select compiler options.
        compile_opts = ''
        if os.name == 'nt':
            compile_opts += '"%s"' % os.path.join(
                tf.sysconfig.get_lib(), 'python',
                '_pywrap_tensorflow_internal.lib')
        elif os.name == 'posix':
            compile_opts += f' --compiler-options \'-fPIC\''
            compile_opts += f' --compiler-options \'{" ".join(tf.sysconfig.get_compile_flags())}\''
            compile_opts += f' --linker-options \'{" ".join(tf.sysconfig.get_link_flags())}\''
        else:
            assert False  # not Windows or Linux, w00t?
        compile_opts += f' --gpu-architecture={_get_cuda_gpu_arch_string()}'
        compile_opts += ' --use_fast_math'
        for opt in extra_nvcc_options:
            compile_opts += ' ' + opt
        nvcc_cmd = _prepare_nvcc_cli(compile_opts)

        # Hash build configuration.
        md5.update(('nvcc_cmd: ' + nvcc_cmd).encode('utf-8') + b'\n')
        md5.update(('tf.VERSION: ' + tf.version.VERSION).encode('utf-8') +
                   b'\n')
        md5.update(('cuda_cache_version_tag: ' +
                    cuda_cache_version_tag).encode('utf-8') + b'\n')

        # Compile if not already compiled.
        cache_dir = util.make_cache_dir_path(
            'tflib-cudacache') if cuda_cache_path is None else cuda_cache_path
        bin_file_ext = '.dll' if os.name == 'nt' else '.so'
        bin_file = os.path.join(
            cache_dir, cuda_file_name + '_' + md5.hexdigest() + bin_file_ext)
        if not os.path.isfile(bin_file):
            if verbose:
                print('Compiling... ', end='', flush=True)
            with tempfile.TemporaryDirectory() as tmp_dir:
                tmp_file = os.path.join(tmp_dir,
                                        cuda_file_name + '_tmp' + bin_file_ext)
                _run_cmd(nvcc_cmd +
                         ' "%s" --shared -o "%s" --keep --keep-dir "%s"' %
                         (cuda_file, tmp_file, tmp_dir))
                os.makedirs(cache_dir, exist_ok=True)
                intermediate_file = os.path.join(
                    cache_dir, cuda_file_name + '_' + uuid.uuid4().hex +
                    '_tmp' + bin_file_ext)
                shutil.copyfile(tmp_file, intermediate_file)
                os.rename(intermediate_file, bin_file)  # atomic

        # Load.
        if verbose:
            print('Loading... ', end='', flush=True)
        plugin = tf.load_op_library(bin_file)

        # Add to cache.
        _plugin_cache[cuda_file] = plugin
        if verbose:
            print('Done.', flush=True)
        return plugin

    except:
        if verbose:
            print('Failed!', flush=True)
        raise
Esempio n. 30
0
# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""ORQA ops."""
import os
import tensorflow.compat.v1 as tf

try:
  orqa_ops
except NameError:
  orqa_ops = tf.load_op_library(
      os.path.join(os.path.dirname(os.path.abspath(__file__)), "orqa_ops.so"))

has_answer = orqa_ops.has_answer
reader_inputs = orqa_ops.reader_inputs