def _count_ops_parameterized_by_layers(k): inlined_tuple_with_k_layers = _construct_inlined_tuple(k) tf_representing_block_with_k_layers, _ = compiler_transformations.remove_duplicate_called_graphs( inlined_tuple_with_k_layers) block_ops_with_k_layers = tree_analysis.count_tensorflow_ops_under( tf_representing_block_with_k_layers) parser_callable = transformations.TFParser() naively_generated_tf_with_k_layers, _ = transformation_utils.transform_postorder( inlined_tuple_with_k_layers, parser_callable) naive_ops_with_k_layers = tree_analysis.count_tensorflow_ops_under( naively_generated_tf_with_k_layers) return block_ops_with_k_layers, naive_ops_with_k_layers
def test_ops_not_duplicated_in_resulting_tensorflow(self): def _construct_block_and_inlined_tuple(k): concrete_int_type = computation_types.TensorType(tf.int32) concrete_int = building_block_factory.create_tensorflow_constant( concrete_int_type, 1) first_tf_id_type = computation_types.TensorType(tf.int32) first_tf_id = building_block_factory.create_compiled_identity( first_tf_id_type) called_tf_id = building_blocks.Call(first_tf_id, concrete_int) for _ in range(k): # Simulating large TF computation called_tf_id = building_blocks.Call(first_tf_id, called_tf_id) ref_to_call = building_blocks.Reference( 'call', called_tf_id.type_signature) block_locals = [('call', called_tf_id)] block = building_blocks.Block( block_locals, building_blocks.Tuple([ref_to_call, ref_to_call])) inlined_tuple = building_blocks.Tuple([called_tf_id, called_tf_id]) return block, inlined_tuple block_with_5_ids, inlined_tuple_with_5_ids = _construct_block_and_inlined_tuple( 5) block_with_10_ids, inlined_tuple_with_10_ids = _construct_block_and_inlined_tuple( 10) tf_representing_block_with_5_ids, _ = transformations.create_tensorflow_representing_block( block_with_5_ids) tf_representing_block_with_10_ids, _ = transformations.create_tensorflow_representing_block( block_with_10_ids) block_ops_with_5_ids = tree_analysis.count_tensorflow_ops_under( tf_representing_block_with_5_ids) block_ops_with_10_ids = tree_analysis.count_tensorflow_ops_under( tf_representing_block_with_10_ids) parser_callable = tree_to_cc_transformations.TFParser() naively_generated_tf_with_5_ids, _ = transformation_utils.transform_postorder( inlined_tuple_with_5_ids, parser_callable) naively_generated_tf_with_10_ids, _ = transformation_utils.transform_postorder( inlined_tuple_with_10_ids, parser_callable) tuple_ops_with_5_ids = tree_analysis.count_tensorflow_ops_under( naively_generated_tf_with_5_ids) tuple_ops_with_10_ids = tree_analysis.count_tensorflow_ops_under( naively_generated_tf_with_10_ids) # asserting that block ops are linear in k with slope 1. self.assertEqual((block_ops_with_10_ids - block_ops_with_5_ids) / 5, 1) # asserting that tuple ops are linear in k with slope 2. self.assertEqual((tuple_ops_with_10_ids - tuple_ops_with_5_ids) / 5, 2)
def test_tensorflow_op_count_doubles_number_of_ops_in_two_tuple(self): integer_identity = building_block_factory.create_compiled_identity(tf.int32) node_tf_op_count = building_block_analysis.count_tensorflow_ops_in( integer_identity) tf_tuple = building_blocks.Tuple([integer_identity, integer_identity]) tree_tf_op_count = tree_analysis.count_tensorflow_ops_under(tf_tuple) self.assertEqual(tree_tf_op_count, 2 * node_tf_op_count)
def test_single_tensorflow_node_count_agrees_with_node_count(self): integer_identity = building_block_factory.create_compiled_identity(tf.int32) node_tf_op_count = building_block_analysis.count_tensorflow_ops_in( integer_identity) tree_tf_op_count = tree_analysis.count_tensorflow_ops_under( integer_identity) self.assertEqual(node_tf_op_count, tree_tf_op_count)
def test_returns_zero_no_tensorflow(self): no_tensorflow_comp = building_block_test_utils.create_nested_syntax_tree( ) tf_count = tree_analysis.count_tensorflow_ops_under(no_tensorflow_comp) self.assertEqual(tf_count, 0)
def test_raises_on_none(self): with self.assertRaises(TypeError): tree_analysis.count_tensorflow_ops_under(None)