def make_rfft2d_tests(options): """Make a set of tests to do rfft2d.""" test_parameters = [{ "input_dtype": [tf.float32], "input_shape": [[8, 8], [3, 8, 8]], "fft_length": [None, [4, 4], [4, 8], [8, 4], [8, 8], [8, 16], [16, 8], [16, 16]] }] def build_graph(parameters): input_value = tf.compat.v1.placeholder(dtype=parameters["input_dtype"], name="input", shape=parameters["input_shape"]) outs = tf.signal.rfft2d(input_value, fft_length=parameters["fft_length"]) return [input_value], [outs] def build_inputs(parameters, sess, inputs, outputs): input_value = create_tensor_data(parameters["input_dtype"], parameters["input_shape"]) return [input_value ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_value]))) extra_toco_options = ExtraTocoOptions() extra_toco_options.allow_custom_ops = True make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options)
def make_control_dep_tests(options): """Make a set of tests that use control dependencies.""" test_parameters = [{ "input_shape": [[], [1, 1, 1, 1], [1, 15, 14, 1], [3, 15, 14, 3]], }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=tf.float32, name="input", shape=parameters["input_shape"]) filter_value = tf.zeros((3, 3, TEST_INPUT_DEPTH, 8), tf.float32) assert_op = tf.assert_greater_equal(input_tensor, input_tensor - 1) with tf.control_dependencies([assert_op]): out = tf.nn.conv2d(input_tensor, filter_value, strides=(1, 1, 1, 1), padding="SAME") return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(tf.float32, parameters["input_shape"]) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) extra_toco_options = ExtraTocoOptions() extra_toco_options.drop_control_dependency = True make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options, expected_tf_failures=3)
def make_roll_tests(options): """Make a set of tests to do roll.""" ext_test_parameters = test_parameters + [ # Scalar axis. { "input_dtype": [tf.float32, tf.int32], "input_shape": [[None, 8, 4]], "shift": [-3, 5], "axis": [1, 2], } ] def set_dynamic_shape(shape): return [4 if x is None else x for x in shape] def get_shape(param): if np.isscalar(param): return [] return [len(param)] def get_value(param, dtype): if np.isscalar(param): return np.dtype(dtype).type(param) return np.array(param).astype(dtype) def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=parameters["input_dtype"], name="input", shape=parameters["input_shape"]) shift_tensor = tf.compat.v1.placeholder(dtype=tf.int64, name="shift", shape=get_shape( parameters["shift"])) axis_tensor = tf.compat.v1.placeholder(dtype=tf.int64, name="axis", shape=get_shape( parameters["axis"])) outs = tf.roll(input_tensor, shift_tensor, axis_tensor) return [input_tensor, shift_tensor, axis_tensor], [outs] def build_inputs(parameters, sess, inputs, outputs): input_value = create_tensor_data( parameters["input_dtype"], set_dynamic_shape(parameters["input_shape"])) shift_value = get_value(parameters["shift"], np.int64) axis_value = get_value(parameters["axis"], np.int64) return [input_value, shift_value, axis_value], sess.run( outputs, feed_dict=dict(zip(inputs, [input_value, shift_value, axis_value]))) extra_toco_options = ExtraTocoOptions() extra_toco_options.allow_custom_ops = True make_zip_of_tests(options, ext_test_parameters, build_graph, build_inputs, extra_toco_options)
def make_parse_example_tests(options): """Make a set of tests to use parse_example.""" # Chose a set of parameters test_parameters = [{ "feature_dtype": [tf.string, tf.float32, tf.int64], "is_dense": [True, False], "feature_shape": [[1], [2], [16]], }] def build_graph(parameters): """Build the graph for parse_example tests.""" feature_dtype = parameters["feature_dtype"] feature_shape = parameters["feature_shape"] is_dense = parameters["is_dense"] input_value = tf.compat.v1.placeholder(dtype=tf.string, name="input", shape=[1]) if is_dense: feature_default_value = np.zeros(shape=feature_shape) if feature_dtype == tf.string: feature_default_value = np.array(["missing"] * feature_shape[0]) features = { "x": tf.FixedLenFeature(shape=feature_shape, dtype=feature_dtype, default_value=feature_default_value) } else: # Sparse features = {"x": tf.VarLenFeature(dtype=feature_dtype)} out = tf.parse_example(input_value, features) output_tensor = out["x"] if not is_dense: output_tensor = out["x"].values return [input_value], [output_tensor] def build_inputs(parameters, sess, inputs, outputs): feature_dtype = parameters["feature_dtype"] feature_shape = parameters["feature_shape"] input_values = [create_example_data(feature_dtype, feature_shape)] return input_values, sess.run(outputs, feed_dict=dict(zip(inputs, input_values))) extra_toco_options = ExtraTocoOptions() extra_toco_options.allow_custom_ops = True make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options)
def make_tensor_list_resize_tests(options): """Make a set of tests to do TensorListResize.""" test_parameters = [ { "element_dtype": [tf.float32, tf.int32], "num_elements": [4, 5, 6], "element_shape": [[], [5], [3, 3]], "new_size": [1, 3, 5, 7], }, ] def build_graph(parameters): """Build the TensorListResize op testing graph.""" data = tf.placeholder(dtype=parameters["element_dtype"], shape=[parameters["num_elements"]] + parameters["element_shape"]) tensor_list = list_ops.tensor_list_from_tensor( data, parameters["element_shape"]) tensor_list = list_ops.tensor_list_resize(tensor_list, parameters["new_size"]) out = list_ops.tensor_list_stack( tensor_list, element_dtype=parameters["element_dtype"]) return [data], [out] def build_inputs(parameters, sess, inputs, outputs): data = create_tensor_data(parameters["element_dtype"], [parameters["num_elements"]] + parameters["element_shape"]) return [data], sess.run(outputs, feed_dict=dict(zip(inputs, [data]))) extra_toco_options = ExtraTocoOptions() make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options)
def f(options, expected_tf_failures=0): """Actual function that generates examples. Args: options: An Options instance. expected_tf_failures: number of expected tensorflow failures. """ # Chose a set of parameters test_parameters = [ { "ksize": [[1, 1, 1, 1, 1], [1, 2, 2, 2, 1], [1, 2, 3, 4, 1]], "strides": [[1, 1, 1, 1, 1], [1, 2, 1, 2, 1], [1, 2, 2, 4, 1]], "input_shape": [[1, 1, 1, 1, 1], [1, 16, 15, 14, 1], [3, 16, 15, 14, 3]], "padding": ["SAME", "VALID"], "data_format": ["NDHWC"], }, ] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=tf.float32, name="input", shape=parameters["input_shape"]) out = pool_op(input_tensor, ksize=parameters["ksize"], strides=parameters["strides"], data_format=parameters["data_format"], padding=parameters["padding"]) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(tf.float32, parameters["input_shape"]) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) extra_toco_options = ExtraTocoOptions() extra_toco_options.allow_custom_ops = True make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options, expected_tf_failures=expected_tf_failures)
def make_broadcast_gradient_args_tests(options): """Make a set of tests to do broadcast_gradient_args.""" test_parameters = [{ 'input_case': ['ALL_EQUAL', 'ONE_DIM', 'NON_BROADCASTABLE'], 'dtype': [tf.dtypes.int32, tf.dtypes.int64], }] def build_graph(parameters): """Build the op testing graph.""" input1 = tf.compat.v1.placeholder(dtype=parameters['dtype'], name='input1') input2 = tf.compat.v1.placeholder(dtype=parameters['dtype'], name='input2') output1, output2 = tf.raw_ops.BroadcastGradientArgs(s0=input1, s1=input2) return [input1, input2], [output1, output2] def build_inputs(parameters, sess, inputs, outputs): dtype = parameters['dtype'].as_numpy_dtype() if parameters['input_case'] == 'ALL_EQUAL': values = [ np.array([2, 4, 1, 3], dtype=dtype), np.array([2, 4, 1, 3], dtype=dtype) ] elif parameters['input_case'] == 'ONE_DIM': values = [ np.array([2, 4, 1, 3], dtype=dtype), np.array([2, 1, 1, 3], dtype=dtype) ] elif parameters['input_case'] == 'NON_BROADCASTABLE': values = [ np.array([2, 4, 1, 3], dtype=dtype), np.array([2, 5, 1, 3], dtype=dtype) ] return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) extra_toco_options = ExtraTocoOptions() extra_toco_options.allow_custom_ops = True make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options, expected_tf_failures=2)
def make_max_pool_with_argmax_tests(options): """Make a set of tests to do max_pool_with_argmax.""" test_parameters = [{ 'input_size': [[2, 4, 2, 2], [2, 4, 3, 2]], 'pool_size': [(2, 2), (2, 1)], 'strides': [(2, 2)], 'padding': ['SAME', 'VALID'], }, { 'input_size': [[2, 4, 10, 2], [2, 4, 11, 2], [2, 4, 12, 2]], 'pool_size': [(2, 2)], 'strides': [(2, 3)], 'padding': ['SAME', 'VALID'], }] def build_graph(parameters): """Build the exp op testing graph.""" input_tensor = tf.compat.v1.placeholder(dtype=tf.float32, name='input', shape=parameters['input_size']) updates, indices = tf.nn.max_pool_with_argmax( input_tensor, ksize=parameters['pool_size'], strides=parameters['strides'], padding=parameters['padding'], output_dtype=tf.dtypes.int32) return [input_tensor], [updates, indices] def build_inputs(parameters, sess, inputs, outputs): values = [ create_tensor_data(tf.float32, parameters['input_size'], min_value=-10, max_value=10) ] return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) extra_toco_options = ExtraTocoOptions() extra_toco_options.allow_custom_ops = True make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options)
def make_tensor_list_resize_tests(options): """Make a set of tests to do TensorListResize.""" test_parameters = [ { "element_dtype": [tf.float32, tf.int32], "num_elements": [4, 5, 6], "element_shape": [[], [5], [3, 3]], "new_size": [1, 3, 5, 7], }, ] def build_graph(parameters): """Build the TensorListResize op testing graph.""" data = tf.placeholder( dtype=parameters["element_dtype"], shape=[parameters["num_elements"]] + parameters["element_shape"]) tensor_list = list_ops.tensor_list_from_tensor(data, parameters["element_shape"]) tensor_list = list_ops.tensor_list_resize(tensor_list, parameters["new_size"]) out = list_ops.tensor_list_stack( tensor_list, element_dtype=parameters["element_dtype"]) return [data], [out] def build_inputs(parameters, sess, inputs, outputs): data = create_tensor_data(parameters["element_dtype"], [parameters["num_elements"]] + parameters["element_shape"]) return [data], sess.run(outputs, feed_dict=dict(zip(inputs, [data]))) # The cond_true and cond_false functions are not exported so they have to be # marked as private. Otherwise, an error will be thrown when importing the # saved model. Currently, there is no easy way to set the visibility of a # function so this test is fell back to convert from GraphDef. # TODO(b/203013020): Converts from SavedModel when the bug is fixed. extra_toco_options = ExtraTocoOptions() extra_toco_options.convert_from_graphdef = True make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options)
def make_irfft2d_tests(options): """Make a set of tests to do irfft2d.""" test_parameters = [{ "input_dtype": [tf.complex64], "input_shape": [[4, 3]], "fft_length": [[4, 4], [2, 2], [2, 4]] }, { "input_dtype": [tf.complex64], "input_shape": [[3, 8, 5]], "fft_length": [[2, 4], [2, 8], [8, 8]] }, { "input_dtype": [tf.complex64], "input_shape": [[3, 1, 9]], "fft_length": [[1, 8], [1, 16]] }] def build_graph(parameters): input_value = tf.compat.v1.placeholder(dtype=parameters["input_dtype"], name="input", shape=parameters["input_shape"]) outs = tf.signal.irfft2d(input_value, fft_length=parameters["fft_length"]) return [input_value], [outs] def build_inputs(parameters, sess, inputs, outputs): rfft_length = [] rfft_length.append(parameters["input_shape"][-2]) rfft_length.append((parameters["input_shape"][-1] - 1) * 2) rfft_input = create_tensor_data(np.float32, parameters["input_shape"]) rfft_result = np.fft.rfft2(rfft_input, rfft_length) return [rfft_result ], sess.run(outputs, feed_dict=dict(zip(inputs, [rfft_result]))) extra_toco_options = ExtraTocoOptions() extra_toco_options.allow_custom_ops = True make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options)
def make_dense_image_warp_tests(options): """Make a set of tests to do dense_image_warp.""" test_parameters = [{ 'input_size': [[2, 4, 4, 1], [2, 4, 3, 3], [3, 7, 9, 2]], 'flow_size': [[2, 4, 4, 2], [2, 4, 3, 2], [3, 7, 9, 2]], }] def build_graph(parameters): """Build the exp op testing graph.""" input_tensor = tf.compat.v1.placeholder( dtype=tf.float32, name='input', shape=parameters['input_size']) flow_tensor = tf.compat.v1.placeholder( dtype=tf.float32, name='flow', shape=parameters['flow_size']) output = dense_image_warp_annotated(input_tensor, flow_tensor) return [input_tensor, flow_tensor], [output] def build_inputs(parameters, sess, inputs, outputs): values = [ create_tensor_data( tf.float32, parameters['input_size'], min_value=-10, max_value=10), create_tensor_data( tf.float32, parameters['flow_size'], min_value=-10, max_value=10) ] return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) extra_toco_options = ExtraTocoOptions() extra_toco_options.allow_custom_ops = True options.expected_ops_in_converted_model = ['DenseImageWarp'] make_zip_of_tests( options, test_parameters, build_graph, build_inputs, extra_toco_options, expected_tf_failures=6)
def make_static_hashtable_tests(options): """Make a set of tests to use static hashtable.""" # Chose a set of parameters test_parameters = [{ "table": [(tf.string, tf.int64, ["1", "2", "3"], [4, 5, 6], -1), (tf.int64, tf.string, [1, 2, 3], ["4", "5", "6"], "-1")], "input_shape": [[], [3], [1], [10]], }] def build_graph(parameters): """Build the graph for static hashtable tests.""" (key_dtype, value_dtype, keys, values, default_value) = parameters["table"] key_tensor = tf.constant(keys, dtype=key_dtype) value_tensor = tf.constant(values, dtype=value_dtype) initializer = tf.lookup.KeyValueTensorInitializer( key_tensor, value_tensor) table = tf.lookup.StaticHashTable(initializer, default_value) with tf.control_dependencies([tf.initializers.tables_initializer()]): input_value = tf.compat.v1.placeholder( dtype=key_dtype, name="input", shape=parameters["input_shape"]) out = table.lookup(key_tensor) return [input_value], [out] def build_inputs(parameters, sess, inputs, outputs): (key_dtype, _, _, _, _) = parameters["table"] input_values = [ create_tensor_data(key_dtype, parameters["input_shape"]) ] return input_values, sess.run(outputs, feed_dict=dict(zip(inputs, input_values))) extra_toco_options = ExtraTocoOptions() make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options)
def make_lstm_tests(options): """Make a set of tests to do basic Lstm cell.""" test_parameters = [ { "dtype": [tf.float32], "num_batchs": [1], "time_step_size": [1], "input_vec_size": [3], "num_cells": [4], "split_tflite_lstm_inputs": [False], }, ] def build_graph(parameters): """Build a simple graph with BasicLSTMCell.""" num_batchs = parameters["num_batchs"] time_step_size = parameters["time_step_size"] input_vec_size = parameters["input_vec_size"] num_cells = parameters["num_cells"] inputs_after_split = [] for i in xrange(time_step_size): one_timestamp_input = tf.compat.v1.placeholder( dtype=parameters["dtype"], name="split_{}".format(i), shape=[num_batchs, input_vec_size]) inputs_after_split.append(one_timestamp_input) # Currently lstm identifier has a few limitations: only supports # forget_bias == 0, inner state activation == tanh. # TODO(zhixianyan): Add another test with forget_bias == 1. # TODO(zhixianyan): Add another test with relu as activation. lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_cells, forget_bias=0.0, state_is_tuple=True) cell_outputs, _ = rnn.static_rnn(lstm_cell, inputs_after_split, dtype=tf.float32) out = cell_outputs[-1] return inputs_after_split, [out] def build_inputs(parameters, sess, inputs, outputs): """Feed inputs, assign variables, and freeze graph.""" with tf.variable_scope("", reuse=True): kernel = tf.get_variable("rnn/basic_lstm_cell/kernel") bias = tf.get_variable("rnn/basic_lstm_cell/bias") kernel_values = create_tensor_data( parameters["dtype"], [kernel.shape[0], kernel.shape[1]], -1, 1) bias_values = create_tensor_data(parameters["dtype"], [bias.shape[0]], 0, 1) sess.run( tf.group(kernel.assign(kernel_values), bias.assign(bias_values))) num_batchs = parameters["num_batchs"] time_step_size = parameters["time_step_size"] input_vec_size = parameters["input_vec_size"] input_values = [] for _ in xrange(time_step_size): tensor_data = create_tensor_data(parameters["dtype"], [num_batchs, input_vec_size], 0, 1) input_values.append(tensor_data) out = sess.run(outputs, feed_dict=dict(zip(inputs, input_values))) return input_values, out # TODO(zhixianyan): Automatically generate rnn_states for lstm cell. extra_toco_options = ExtraTocoOptions() extra_toco_options.rnn_states = ( "{state_array:rnn/BasicLSTMCellZeroState/zeros," "back_edge_source_array:rnn/basic_lstm_cell/Add_1,size:4}," "{state_array:rnn/BasicLSTMCellZeroState/zeros_1," "back_edge_source_array:rnn/basic_lstm_cell/Mul_2,size:4}") make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options, use_frozen_graph=True)