def profile_graph(self, options): """Profile the statistics of graph nodes, organized by dataflow graph. Args: options: A dict of options. See core/profiler/g3doc/options.md. Returns: a GraphNodeProto that records the results. """ opts = _build_options(options) tfprof_node = tfprof_output_pb2.GraphNodeProto() tfprof_node.ParseFromString( print_mdl.Profile('graph'.encode('utf-8'), opts.SerializeToString())) return tfprof_node
def extract_data(self): # def parse(stats_str): # stats = tfprof_output_pb2.GraphNodeProto() # stats.ParseFromString( stats_str ) # return stats if hasattr(self, '_scope_all_stats_str'): self._scope_all_stats = tfprof_output_pb2.GraphNodeProto() self._scope_all_stats.ParseFromString( self._scope_all_stats_str ) if hasattr(self, '_op_all_stats_str'): self._op_all_stats = tfprof_output_pb2.MultiGraphNodeProto() self._op_all_stats.ParseFromString( self._op_all_stats_str )
def profile_graph(self, options): """Profile the statistics of graph nodes, organized by dataflow graph. Args: options: A dict of options. See core/profiler/g3doc/options.md. Returns: a GraphNodeProto that records the results. """ opts = _build_options(options) tfprof_node = tfprof_output_pb2.GraphNodeProto() try: tfprof_node.ParseFromString( print_mdl.Profile('graph'.encode('utf-8'), opts.SerializeToString())) except message.DecodeError as e: sys.stderr.write('Cannot parse returned proto: %s.\n' % e) return tfprof_node
def profile_name_scope(self, options): """Profile the statistics of graph nodes, organized by name scope. Args: options: A dict of options. See core/profiler/g3doc/options.md. Returns: a GraphNodeProto that records the results. """ opts = _build_options(options) tfprof_node = tfprof_output_pb2.GraphNodeProto() try: tfprof_node.ParseFromString( print_mdl.Profile('scope'.encode('utf-8'), opts.SerializeToString())) except message.DecodeError as _: pass return tfprof_node
def profile(graph=None, run_meta=None, op_log=None, cmd='scope', options=_DEFAULT_PROFILE_OPTIONS): """Profile model. Tutorials and examples can be found in: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/README.md Args: graph: tf.Graph. If None and eager execution is not enabled, use default graph. run_meta: optional tensorflow.RunMetadata proto. It is necessary to to support run time information profiling, such as time and memory. op_log: tensorflow.tfprof.OpLogProto proto. User can assign "types" to graph nodes with op_log. "types" allow user to flexibly group and account profiles using options['accounted_type_regexes']. cmd: string. Either 'op', 'scope', 'graph' or 'code'. 'op' view organizes profile using operation type. (e.g. MatMul) 'scope' view organizes profile using graph node name scope. 'graph' view organizes profile using graph node inputs/outputs. 'code' view organizes profile using Python call stack. options: A dict of options. See core/profiler/g3doc/options.md. Returns: If cmd is 'scope' or 'graph', returns GraphNodeProto proto. If cmd is 'op' or 'code', returns MultiGraphNodeProto proto. Side effect: stdout/file/timeline.json depending on options['output'] """ if not graph and not context.executing_eagerly(): graph = ops.get_default_graph() if options == _DEFAULT_PROFILE_OPTIONS: options = (option_builder.ProfileOptionBuilder. trainable_variables_parameter()) # pylint: disable=protected-access op_log = tfprof_logger.merge_default_with_oplog(graph, op_log, run_meta, add_trace=cmd == 'code') # pylint: enable=protected-access opts = _build_options(options) run_meta_str = run_meta.SerializeToString() if run_meta else b'' graph_str = _graph_string(graph) if cmd == 'code' or cmd == 'op': tfprof_node = tfprof_output_pb2.MultiGraphNodeProto() ret = print_mdl.PrintModelAnalysis(graph_str, run_meta_str, op_log.SerializeToString(), cmd.encode('utf-8'), opts.SerializeToString()) try: tfprof_node.ParseFromString(ret) except message.DecodeError as e: sys.stderr.write('Cannot parse returned proto: %s.\n' % e) elif cmd == 'graph' or cmd == 'scope': tfprof_node = tfprof_output_pb2.GraphNodeProto() ret = print_mdl.PrintModelAnalysis(graph_str, run_meta_str, op_log.SerializeToString(), cmd.encode('utf-8'), opts.SerializeToString()) try: tfprof_node.ParseFromString(ret) except message.DecodeError as e: sys.stderr.write('Cannot parse returned proto: %s.\n' % e) else: raise errors.InvalidArgumentError(None, None, 'unknown cmd: %s\n' % cmd) return tfprof_node
def testPrintModelAnalysis(self): opts = tfprof_options_pb2.OptionsProto() opts.max_depth = TEST_OPTIONS['max_depth'] opts.min_bytes = TEST_OPTIONS['min_bytes'] opts.min_micros = TEST_OPTIONS['min_micros'] opts.min_params = TEST_OPTIONS['min_params'] opts.min_float_ops = TEST_OPTIONS['min_float_ops'] opts.order_by = TEST_OPTIONS['order_by'] opts.step = -1 for p in TEST_OPTIONS['account_type_regexes']: opts.account_type_regexes.append(p) for p in TEST_OPTIONS['start_name_regexes']: opts.start_name_regexes.append(p) for p in TEST_OPTIONS['trim_name_regexes']: opts.trim_name_regexes.append(p) for p in TEST_OPTIONS['show_name_regexes']: opts.show_name_regexes.append(p) for p in TEST_OPTIONS['hide_name_regexes']: opts.hide_name_regexes.append(p) opts.account_displayed_op_only = TEST_OPTIONS[ 'account_displayed_op_only'] for p in TEST_OPTIONS['select']: opts.select.append(p) opts.output = TEST_OPTIONS['output'] with session.Session() as sess, ops.device('/cpu:0'): _ = self._BuildSmallModel() tfprof_pb = tfprof_output_pb2.GraphNodeProto() tfprof_pb.ParseFromString( print_mdl.PrintModelAnalysis( sess.graph.as_graph_def( add_shapes=True).SerializeToString(), b'', b'', b'scope', opts.SerializeToString())) expected_pb = tfprof_output_pb2.GraphNodeProto() text_format.Merge( r"""name: "_TFProfRoot" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 648 children { name: "Conv2D" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 2 } dim { size: 6 } dim { size: 6 } dim { size: 3 } } } input_shapes { key: 1 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW" exec_micros: 0 requested_bytes: 0 parameters: 648 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 648 children { name: "DW/Assign" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } input_shapes { key: 1 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 children { name: "DW/Initializer/random_normal" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 children { name: "DW/Initializer/random_normal/RandomStandardNormal" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 4 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer/random_normal/mean" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer/random_normal/mul" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } input_shapes { key: 1 value { dim { size: 1 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer/random_normal/shape" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer/random_normal/stddev" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } input_shapes { key: 1 value { dim { size: 1 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 6 } float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 7 } children { name: "DW/read" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 10 } children { name: "zeros" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 13""", expected_pb) self.assertEqual(expected_pb, tfprof_pb)
def profile(graph, run_meta=None, op_log=None, cmd='scope', options=_DEFAULT_PROFILE_OPTIONS): """Print model statistics. https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/README.md Args: graph: tf.Graph. run_meta: tensorflow::RunMetadata proto. When provided, also shows valid timing and memory information when 'select' option contains 'micros' and 'bytes'. op_log: tensorflow::tfprof::OpLogProto proto. users can use this proto to group together ops and use a op_type to select the group. cmd: string. Either 'op', 'scope', 'graph', 'code'. 'op' view organize outputs using operation type. (e.g. MatMul) 'scope' view organize outputs using graph node name scope. 'graph' view organize outputs using graph node inputs/outputs. 'code' view organize outputs using Python call stack. options: A dict of options. See core/profiler/g3doc/options.md. Returns: If cmd is 'scope' or 'graph', returns GraphNodeProto proto. If cmd is 'op' or 'code', returns MultiGraphNodeProto proto. Side effect: stdout/file/timeline.json depending on options['output'] """ if options == _DEFAULT_PROFILE_OPTIONS: options = (option_builder.ProfileOptionBuilder. trainable_variables_parameter()) # pylint: disable=protected-access op_log = tfprof_logger._merge_default_with_oplog(graph, op_log, run_meta, add_trace=cmd == 'code') # pylint: enable=protected-access opts = _build_options(options) run_meta_str = run_meta.SerializeToString() if run_meta else b'' if cmd == 'code' or cmd == 'op': tfprof_node = tfprof_output_pb2.MultiGraphNodeProto() tfprof_node.ParseFromString( print_mdl.PrintModelAnalysis( graph.as_graph_def(add_shapes=True).SerializeToString(), run_meta_str, op_log.SerializeToString(), cmd.encode('utf-8'), opts.SerializeToString())) elif cmd == 'graph' or cmd == 'scope': tfprof_node = tfprof_output_pb2.GraphNodeProto() tfprof_node.ParseFromString( print_mdl.PrintModelAnalysis( graph.as_graph_def(add_shapes=True).SerializeToString(), run_meta_str, op_log.SerializeToString(), cmd.encode('utf-8'), opts.SerializeToString())) else: raise errors.InvalidArgumentError(None, None, 'unknown cmd: %s\n' % cmd) return tfprof_node