def make_cos_tests(options): """Make a set of tests to do cos.""" test_parameters = [{ "input_dtype": [tf.float32], "input_shape": [[], [3], [1, 100], [4, 2, 3], [5, 224, 224, 3]], }] def build_graph(parameters): """Build the cos op testing graph.""" input_tensor = tf.compat.v1.placeholder( dtype=parameters["input_dtype"], name="input", shape=parameters["input_shape"]) out = tf.cos(input_tensor) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): values = [ create_tensor_data(parameters["input_dtype"], parameters["input_shape"], min_value=-np.pi, max_value=np.pi) ] return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_resize_bilinear_tests(options): """Make a set of tests to do resize_bilinear.""" test_parameters = [{ "dtype": [tf.float32, tf.int32], "input_shape": [[1, 3, 4, 3], [1, 10, 2, 1]], "size": [[1, 1], [4, 3], [2, 2], [5, 6]], "align_corners": [None, True, False], }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=parameters["dtype"], name="input", shape=parameters["input_shape"]) out = tf.image.resize_bilinear( input_tensor, size=parameters["size"], align_corners=parameters["align_corners"]) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(parameters["dtype"], parameters["input_shape"]) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
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_convert_options = ExtraConvertOptions() make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_convert_options)
def make_sparse_to_dense_tests(options): """Make a set of tests to do sparse to dense.""" test_parameters = [{ "value_dtype": [tf.float32, tf.int32, tf.int64], "index_dtype": [tf.int32, tf.int64], "value_count": [1, 3, 6, 8], "dense_shape": [[15], [3, 10], [4, 4, 4, 4], [7, 10, 9]], "default_value": [0, -1], "value_is_scalar": [True, False], }] # Return a single value for 1-D dense shape, but a tuple for other shapes. def generate_index(dense_shape): if len(dense_shape) == 1: return np.random.randint(dense_shape[0]) else: index = [] for shape in dense_shape: index.append(np.random.randint(shape)) return tuple(index) def build_graph(parameters): """Build the sparse_to_dense op testing graph.""" dense_shape = parameters["dense_shape"] # Special handle for value_is_scalar case. # value_count must be 1. if parameters["value_is_scalar"] and parameters["value_count"] == 1: value = tf.placeholder( name="value", dtype=parameters["value_dtype"], shape=()) else: value = tf.placeholder( name="value", dtype=parameters["value_dtype"], shape=[parameters["value_count"]]) indices = set() while len(indices) < parameters["value_count"]: indices.add(generate_index(dense_shape)) indices = tf.constant(tuple(indices), dtype=parameters["index_dtype"]) # TODO(renjieliu): Add test for validate_indices case. out = tf.sparse_to_dense( indices, dense_shape, value, parameters["default_value"], validate_indices=False) return [value], [out] def build_inputs(parameters, sess, inputs, outputs): if parameters["value_is_scalar"] and parameters["value_count"] == 1: input_value = create_scalar_data(parameters["value_dtype"]) else: input_value = create_tensor_data(parameters["value_dtype"], [parameters["value_count"]]) return [input_value], sess.run( outputs, feed_dict=dict(zip(inputs, [input_value]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_relu1_tests(options): """Make a set of tests to do relu1.""" # Chose a set of parameters test_parameters = [{ "input_shape": [[], [1, 1, 1, 1], [1, 3, 4, 3], [3, 15, 14, 3], [3, 1, 2, 4, 6], [2, 2, 3, 4, 5, 6]], }] def build_graph(parameters): input_tensor = tf.placeholder(dtype=tf.float32, name="input", shape=parameters["input_shape"]) # Note that the following is not supported: # out = tf.maximum(-1.0, tf.minimum(input_tensor, 1.0)) out = tf.minimum(1.0, tf.maximum(input_tensor, -1.0)) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(np.float32, parameters["input_shape"], min_value=-3, max_value=10) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
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_zeros_like_tests(options): """Make a set of tests to do zeros_like.""" test_parameters = [{ "input_dtype": [tf.float32, tf.int32, tf.int64], "input_shape": [[], [1], [1, 2], [5, 6, 7, 8], [3, 4, 5, 6]], }] def build_graph(parameters): """Build the zeros_like op testing graph.""" input_tensor = tf.placeholder(dtype=parameters["input_dtype"], name="input", shape=parameters["input_shape"]) zeros = tf.zeros_like(input_tensor) # This maximum node is so that toco can perform the constants-propagation # through the above zeros_like, which it can't do if the output of the # zeros_like as an output of the whole graphs (graph outputs can't be # constants). If toco does not perform such constants-propagation then # the resulting tflite graph retains the zeros_like as a Fill op, which # is unsupported by TFLite, even as a custom op. out = tf.maximum(zeros, input_tensor) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): values = create_tensor_data(parameters["input_dtype"], parameters["input_shape"]) return [values], sess.run(outputs, feed_dict=dict(zip(inputs, [values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_embedding_lookup_tests(options): """Make a set of tests to do gather.""" test_parameters = [ { "params_dtype": [tf.float32], "params_shape": [[10], [10, 10]], "ids_dtype": [tf.int32], "ids_shape": [[3], [5]], }, ] def build_graph(parameters): """Build the gather op testing graph.""" params = tf.placeholder( dtype=parameters["params_dtype"], name="params", shape=parameters["params_shape"]) ids = tf.placeholder( dtype=parameters["ids_dtype"], name="ids", shape=parameters["ids_shape"]) out = tf.nn.embedding_lookup(params, ids) return [params, ids], [out] def build_inputs(parameters, sess, inputs, outputs): params = create_tensor_data(parameters["params_dtype"], parameters["params_shape"]) ids = create_tensor_data(parameters["ids_dtype"], parameters["ids_shape"], 0, parameters["params_shape"][0] - 1) return [params, ids], sess.run( outputs, feed_dict=dict(zip(inputs, [params, ids]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_complex_abs_tests(options): """Make a set of tests to do complex abs.""" # Chose a set of parameters test_parameters = [{ "dtype": [tf.complex64], "input_shape": [[], [1], [2, 3], [1, 3, 4, 3], [2, 2, 3, 4, 5, 6]], "Tout": [tf.float32] }, { "dtype": [tf.complex128], "input_shape": [[], [1], [2, 3], [1, 3, 4, 3], [2, 2, 3, 4, 5, 6]], "Tout": [tf.float64] }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=parameters["dtype"], name="input", shape=parameters["input_shape"]) out = tf.raw_ops.ComplexAbs(x=input_tensor, Tout=parameters["Tout"]) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(parameters["dtype"].as_numpy_dtype, parameters["input_shape"], min_value=-10, max_value=10) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_not_equal_tests(options): """Make a set of tests to do not equal.""" test_parameters = [{ "input_dtype": [tf.float32, tf.int32, tf.int64], "input_shape_pair": [([1, 1, 1, 3], [1, 1, 1, 3]), ([2, 3, 4, 5], [2, 3, 4, 5]), ([2, 3, 3], [2, 3]), ([5, 5], [1]), ([10], [2, 4, 10])], }] def build_graph(parameters): """Build the not euqal op testing graph.""" input_value1 = tf.placeholder(dtype=parameters["input_dtype"], name="input1", shape=parameters["input_shape_pair"][0]) input_value2 = tf.placeholder(dtype=parameters["input_dtype"], name="input2", shape=parameters["input_shape_pair"][1]) out = tf.not_equal(input_value1, input_value2) return [input_value1, input_value2], [out] def build_inputs(parameters, sess, inputs, outputs): input_value1 = create_tensor_data(parameters["input_dtype"], parameters["input_shape_pair"][0]) input_value2 = create_tensor_data(parameters["input_dtype"], parameters["input_shape_pair"][1]) return [input_value1, input_value2], sess.run( outputs, feed_dict=dict(zip(inputs, [input_value1, input_value2]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs, expected_tf_failures=3)
def make_space_to_depth_tests(options): """Make a set of tests to do space_to_depth.""" test_parameters = [{ "dtype": [tf.float32, tf.int32, tf.uint8, tf.int64], "input_shape": [[2, 12, 24, 1]], "block_size": [2, 3, 4], "fully_quantize": [False], }, { "dtype": [tf.float32], "input_shape": [[2, 12, 24, 1], [1, 12, 24, 1]], "block_size": [2, 3, 4], "fully_quantize": [True], }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=parameters["dtype"], name="input", shape=parameters["input_shape"]) out = tf.space_to_depth(input_tensor, block_size=parameters["block_size"]) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(parameters["dtype"], parameters["input_shape"], min_value=-1, max_value=1) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_abs_tests(options): """Make a set of tests to do abs.""" # Chose a set of parameters test_parameters = [{ "input_shape": [[], [1], [2, 3], [1, 1, 1, 1], [1, 3, 4, 3], [3, 15, 14, 3], [3, 1, 2, 4, 6], [2, 2, 3, 4, 5, 6]], }] def build_graph(parameters): input_tensor = tf.placeholder(dtype=tf.float32, name="input", shape=parameters["input_shape"]) out = tf.abs(input_tensor) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(np.float32, parameters["input_shape"], min_value=-10, max_value=10) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_leaky_relu_tests(options): """Make a set of tests to do LeakyRelu.""" test_parameters = [ { "input_shape": [[], [1], [5], [1, 10, 10, 3], [3, 3, 3, 3]], "alpha": [0.1, 1.0, 2.0, -0.1, -1.0, -2.0], }, ] def build_graph(parameters): """Build the graph for the test case.""" input_tensor = tf.compat.v1.placeholder( dtype=tf.float32, name="input", shape=parameters["input_shape"]) out = tf.nn.leaky_relu(input_tensor, alpha=parameters["alpha"]) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): """Build the inputs for the test case.""" input_values = create_tensor_data(np.float32, parameters["input_shape"], min_value=-3, max_value=10) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_real_tests(options): """Make a set of tests to do real op.""" # Chose a set of parameters test_parameters = [{ "dtype": [tf.complex64, tf.complex128], "input_shape": [[], [1], [2, 3], [1, 3, 4, 3], [2, 2, 3, 4, 5, 6]], }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=parameters["dtype"], name="input", shape=parameters["input_shape"]) out = tf.math.real(input_tensor) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(parameters["dtype"].as_numpy_dtype, parameters["input_shape"], min_value=-10, max_value=10) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_sigmoid_tests(options): """Make a set of tests to do sigmoid.""" test_parameters = [{ "dtype": [tf.float32], "input_shape": [[1, 3, 4, 3], [4], [], [1, 2, 3, 4, 5, 6]], "fully_quantize": [True, False], "input_range": [(-10, 10)], }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=parameters["dtype"], name="input", shape=parameters["input_shape"]) out = tf.sigmoid(input_tensor) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): min_value, max_value = parameters["input_range"] input_values = create_tensor_data(parameters["dtype"], parameters["input_shape"], min_value, max_value) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_reciprocal_tests(options): """Make a set of tests to do reciprocal.""" # Chose a set of parameters test_parameters = [{ "input_dtype": [tf.float32, tf.int32, tf.int64], "input_shape": [[1, 2], [1, 2, 3, 4], [10]], }] def build_graph(parameters): """Build the graph for cond tests.""" input_tensor = tf.compat.v1.placeholder( dtype=parameters["input_dtype"], name="input", shape=parameters["input_shape"]) out = tf.math.reciprocal(input_tensor) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = [ create_tensor_data(parameters["input_dtype"], parameters["input_shape"]) ] return input_values, sess.run(outputs, feed_dict=dict(zip(inputs, input_values))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs, expected_tf_failures=6)
def make_placeholder_with_default_tests(options): """Make a set of tests to test placeholder_with_default.""" test_parameters = [{ "dtype": [tf.float32, tf.int32, tf.int64], }] def build_graph(parameters): """Build the placeholder_with_default testing graph.""" const_node = tf.constant([1, 2, 2, 0], shape=[2, 2], dtype=parameters["dtype"]) input_tensor = tf.placeholder_with_default(const_node, shape=[2, 2], name="input") out = tf.equal(input_tensor, const_node, name="output") return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): numpy_type = TF_TYPE_INFO[parameters["dtype"]][0] input_value = np.array([[1, 0], [2, 1]], numpy_type) return [input_value ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_value]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_squeeze_transpose_tests(options): """Make a set of tests to do squeeze followed by transpose.""" test_parameters = [{ "dtype": [tf.int32, tf.float32, tf.int64], "input_shape": [[1, 4, 10, 1]], "axis": [[-1], [3]], }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=parameters["dtype"], name="input", shape=parameters["input_shape"]) out = tf.squeeze(input_tensor, axis=parameters["axis"]) out = tf.transpose(out, perm=[1, 2]) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(parameters["dtype"], parameters["input_shape"]) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs, expected_tf_failures=0)
def make_fill_tests(options): """Make a set of tests to do fill.""" test_parameters = [{ "dims_dtype": [tf.int32, tf.int64], "dims_shape": [[], [1], [3], [3, 3]], "value_dtype": [tf.int32, tf.int64, tf.float32], }] def build_graph(parameters): """Build the fill op testing graph.""" input1 = tf.placeholder(dtype=parameters["dims_dtype"], name="dims", shape=parameters["dims_shape"]) input2 = tf.placeholder(dtype=parameters["value_dtype"], name="value", shape=[]) out = tf.fill(input1, input2) return [input1, input2], [out] def build_inputs(parameters, sess, inputs, outputs): input1 = create_tensor_data(parameters["dims_dtype"], parameters["dims_shape"], 1) input2 = create_scalar_data(parameters["value_dtype"]) return [input1, input2], sess.run(outputs, feed_dict=dict(zip(inputs, [input1, input2]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs, expected_tf_failures=12)
def make_gather_with_constant_tests(options): """Make a set of test which feed a constant to gather toco.""" test_parameters = [{ "input_shape": [[3]], "reference_shape": [[2]], }, { "input_shape": [[2, 3]], "reference_shape": [[2, 3]], }] def build_graph(parameters): """Build a graph where the inputs to Gather are constants.""" reference = tf.compat.v1.placeholder( dtype=tf.int32, shape=parameters["reference_shape"]) gather_input = tf.constant( create_tensor_data(tf.int32, parameters["input_shape"])) gather_indices = tf.constant([0, 1], tf.int32) out = tf.equal(reference, tf.gather(gather_input, gather_indices)) return [reference], [out] def build_inputs(parameters, sess, inputs, outputs): reference_values = np.zeros(parameters["reference_shape"], dtype=np.int32) return [reference_values ], sess.run(outputs, feed_dict={inputs[0]: reference_values}) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_l2norm_shared_epsilon_tests(options): """Regression test for a bug (b/122651451).""" # Chose a set of parameters test_parameters = [{ "input_shape": [[5, 7]], "dim": [1], "epsilon": [1e-8], }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=tf.float32, name="input", shape=parameters["input_shape"]) epsilon = tf.constant(parameters["epsilon"]) out1 = tf.nn.l2_normalize(input_tensor, parameters["dim"], epsilon=epsilon) out2 = tf.nn.l2_normalize(input_tensor, parameters["dim"], epsilon=epsilon) out = out1 + out2 return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data( np.float32, parameters["input_shape"], min_value=-4, max_value=10) return [input_values], sess.run( outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_local_response_norm_tests(options): """Make a set of tests to do local_response_norm.""" # Chose a set of parameters test_parameters = [{ "input_shape": [[1, 1, 1, 1], [1, 3, 4, 3], [3, 15, 14, 3]], "depth_radius": [None, 0, 1, 3, 5], "bias": [None, 0.3, -0.1], "alpha": [None, 2, -3], "beta": [None, 0.25, 2], }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=tf.float32, name="input", shape=parameters["input_shape"]) out = tf.nn.local_response_normalization( input_tensor, depth_radius=parameters["depth_radius"], bias=parameters["bias"], alpha=parameters["alpha"], beta=parameters["beta"]) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(np.float32, parameters["input_shape"], min_value=-4, max_value=10) return [input_values ], sess.run(outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_tensor_list_get_item_tests(options): """Make a set of tests to do TensorListGetItem.""" test_parameters = [ { "element_dtype": [tf.float32, tf.int32], "num_elements": [4, 5, 6], "element_shape": [[], [5], [3, 3]], "index": [0, 1, 2, 3], }, ] def build_graph(parameters): """Build the TensorListGetItem 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"]) out = list_ops.tensor_list_get_item(tensor_list, parameters["index"], 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]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_relu_tests(options): """Make a set of tests to do relu.""" # Chose a set of parameters test_parameters = [{ "input_shape": [[], [1], [2, 3], [1, 1, 1, 1], [1, 3, 4, 3], [3, 15, 14, 3], [3, 1, 2, 4, 6], [2, 2, 3, 4, 5, 6]], "fully_quantize": [True, False], "input_range": [(-8, 8)] }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=tf.float32, name="input", shape=parameters["input_shape"]) out = tf.nn.relu(input_tensor) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): min_value, max_value = parameters["input_range"] input_values = create_tensor_data( np.float32, parameters["input_shape"], min_value, max_value) return [input_values], sess.run( outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_splitv_tests(options): """Make a set of tests to do tf.split_v.""" test_parameters = [{ "input_shape": [[1, 3, 4, 6], [2, 4, 1], [6, 4], [8]], "size_splits": [[2, 2], [1, 3], [4, 2], [5, 3], [-1, 1], [-1, 2], [-1, 4]], "axis": [0, 1, 2, 3, -4, -3, -2, -1], }] def build_graph(parameters): input_tensor = tf.placeholder(dtype=tf.float32, name="input", shape=parameters["input_shape"]) out = tf.split(input_tensor, parameters["size_splits"], parameters["axis"]) return [input_tensor], [out[0]] def build_inputs(parameters, sess, inputs, outputs): values = [create_tensor_data(np.float32, parameters["input_shape"])] return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs, expected_tf_failures=158)
def make_elu_tests(options): """Make a set of tests to do (float) tf.nn.elu.""" test_parameters = [ { "input_shape": [[], [1], [2, 3], [1, 1, 1, 1], [1, 3, 4, 3], [3, 15, 14, 3], [3, 1, 2, 4, 6], [2, 2, 3, 4, 5, 6]], }, ] def build_graph(parameters): """Build the graph for the test case.""" input_tensor = tf.compat.v1.placeholder( dtype=tf.float32, name="input", shape=parameters["input_shape"]) out = tf.nn.elu(input_tensor) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): """Build the inputs for the test case.""" input_values = create_tensor_data( np.float32, parameters["input_shape"], min_value=-4, max_value=10) return [input_values], sess.run( outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_conv_to_depthwiseconv_with_shared_weights_tests(options): """Make a test where 2 Conv ops shared the same constant weight tensor.""" test_parameters = [{ "input_shape": [[1, 10, 10, 1]], "filter_shape": [[3, 3]], "strides": [[1, 1, 1, 1]], "dilations": [[1, 1, 1, 1]], "padding": ["SAME"], "data_format": ["NHWC"], "channel_multiplier": [3], }] def get_tensor_shapes(parameters): input_shape = parameters["input_shape"] filter_size = parameters["filter_shape"] filter_shape = filter_size + [ input_shape[3], parameters["channel_multiplier"] ] return [input_shape, filter_shape] def build_graph(parameters): """Build a conv graph given `parameters`.""" input_shape, filter_shape = get_tensor_shapes(parameters) input_tensor = tf.compat.v1.placeholder(dtype=tf.float32, name="input", shape=input_shape) # Construct a constant weights tensor which will be used by both Conv2D. filter_tensor = tf.constant(create_tensor_data(np.float32, filter_shape), dtype=tf.float32) input_tensors = [input_tensor] # Construct 2 Conv2D operations which use exactly the same input and # weights. result1 = tf.nn.conv2d(input_tensor, filter_tensor, strides=parameters["strides"], dilations=parameters["dilations"], padding=parameters["padding"], data_format=parameters["data_format"]) result2 = tf.nn.conv2d(input_tensor, filter_tensor, strides=parameters["strides"], dilations=parameters["dilations"], padding=parameters["padding"], data_format=parameters["data_format"]) # Add the 2 results up. out = result1 + result2 return input_tensors, [out] def build_inputs(parameters, sess, inputs, outputs): # Build list of input values either containing 1 tensor (input) or 2 tensors # (input, filter) based on whether filter is constant or variable input. input_shape, unused_filter_shape = get_tensor_shapes(parameters) values = [create_tensor_data(np.float32, input_shape)] return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
def make_softmax_tests(options): """Make a set of tests to do softmax.""" test_parameters = [{ "dtype": [tf.float32], "input_shape": [[1, 3, 4, 3], [2, 3]], "dim": [-1, 0], }, { "dtype": [tf.float32], "input_shape": [[4, 7]], "dim": [-1, 1], }] def build_graph(parameters): input_tensor = tf.compat.v1.placeholder( dtype=parameters["dtype"], name="input", shape=parameters["input_shape"]) out = tf.nn.softmax(input_tensor, dim=parameters["dim"]) return [input_tensor], [out] def build_inputs(parameters, sess, inputs, outputs): input_values = create_tensor_data(parameters["dtype"], parameters["input_shape"]) return [input_values], sess.run( outputs, feed_dict=dict(zip(inputs, [input_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
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], [3, 1, 16]], "fft_length": [ None, [4, 4], [4, 8], [8, 4], [8, 8], [8, 16], [16, 8], [16, 16], [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.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() make_zip_of_tests(options, test_parameters, build_graph, build_inputs, extra_toco_options)
def make_matrix_set_diag_tests(options): """Make a set of tests for tf.linalg.set_diag op.""" test_parameters = [ { "input_diag_shapes": [([3, 3], [3]), ([2, 3], [2]), ([2, 4, 4], [2, 4]), ([3, 4, 5, 6], [3, 4, 5])], "input_dtype": [tf.int32, tf.float32, tf.uint8], }, ] def build_graph(parameters): input_shape = parameters["input_diag_shapes"][0] diag_shape = parameters["input_diag_shapes"][1] input_tensor = tf.compat.v1.placeholder( dtype=parameters["input_dtype"], name="input", shape=input_shape) diag_tensor = tf.compat.v1.placeholder(dtype=parameters["input_dtype"], name="diagonal", shape=diag_shape) outs = tf.linalg.set_diag(input_tensor, diag_tensor) return [input_tensor, diag_tensor], [outs] def build_inputs(parameters, sess, inputs, outputs): input_shape = parameters["input_diag_shapes"][0] diag_shape = parameters["input_diag_shapes"][1] input_values = create_tensor_data(parameters["input_dtype"], input_shape) diag_values = create_tensor_data(parameters["input_dtype"], diag_shape) return [input_values, diag_values], sess.run( outputs, feed_dict=dict(zip(inputs, [input_values, diag_values]))) make_zip_of_tests(options, test_parameters, build_graph, build_inputs)